@@ -7,7 +7,7 @@ import queryCollection from "./queryCollection"
77// Reducer for an array of spans; concatenates text.
88const reducer = ( acc , each ) => acc += each . text
99
10- // Creates a right-to-left range component based on the
10+ // Computes a right-to-left range component based on the
1111// current collapsed range and a boundary.
1212const nextRTL = state => boundary => {
1313 const x = state . elements . findIndex ( each => each . key === state . range [ 0 ] . key )
@@ -27,7 +27,7 @@ const nextRTL = state => boundary => {
2727 return component
2828}
2929
30- // Creates a left-to-right range component based on the
30+ // Computes a left-to-right range component based on the
3131// current collapsed range and a boundary.
3232const nextLTR = state => boundary => {
3333 const x = state . elements . findIndex ( each => each . key === state . range [ 0 ] . key )
@@ -47,28 +47,34 @@ const nextLTR = state => boundary => {
4747 return component
4848}
4949
50- // Deletes on the current range. If the cursor is collapsed,
51- // a synthetic range is computed based on dir and boundary.
52- //
53- // TODO: Remove empty spans
54- const $delete = state => ( dir , boundary ) => {
55-
56- // Compute the next range component:
57- if ( state . range . collapsed && dir === "rtl" ) {
50+ // Computes the next range based on the current collapsed
51+ // range and a boundary.
52+ const next = state => ( dir , boundary ) => {
53+ const next = { }
54+ if ( dir === "rtl" && dir !== "ltr" ) {
5855 const component = nextRTL ( state ) ( boundary )
59- Object . assign ( state . range , {
56+ Object . assign ( next , {
6057 ...[ component , state . range [ 0 ] ] ,
6158 collapsed : Range . areEqualComponents ( component , state . range [ 0 ] ) ,
6259 } )
63- } else if ( state . range . collapsed && dir === "ltr" ) {
60+ } else {
6461 const component = nextLTR ( state ) ( boundary )
65- Object . assign ( state . range , {
62+ Object . assign ( next , {
6663 ...[ state . range [ 0 ] , component ] ,
6764 collapsed : Range . areEqualComponents ( state . range [ 0 ] , component ) ,
6865 } )
6966 }
67+ return next
68+ }
7069
71- console . log ( JSONClone ( state . range ) )
70+ // Deletes on the current range. If the cursor is collapsed,
71+ // a synthetic range is computed based on dir and boundary.
72+ //
73+ // TODO: Remove empty spans
74+ const $delete = state => ( dir , boundary ) => {
75+ if ( state . range . collapsed && ( dir === "rtl" || dir === "ltr" ) ) {
76+ Object . assign ( state . range , next ( state ) ( dir , boundary ) )
77+ }
7278
7379 const collection = queryCollection ( state )
7480
0 commit comments