11import * as iter from "./iter"
22import useMethods from "use-methods"
33import uuidv4 from "uuid/v4"
4- import { mergeRedundantSpans } from "./spans"
4+ import { mergeRepeatSpans } from "./spans"
55import { newCursor } from "./cursors"
66
77// Counts the number of bytes between the cursors.
@@ -74,13 +74,21 @@ const methods = state => ({
7474 */
7575 // Counts the number of bytes to a boundary.
7676 countBytesToBoundary ( state , iterator ) {
77- const dir = new Map ( Object . entries ( {
78- [ iter . rtl . rune ] : "rtl" ,
79- [ iter . rtl . word ] : "rtl" ,
80- [ iter . rtl . line ] : "rtl" ,
81- [ iter . ltr . rune ] : "ltr" ,
82- [ iter . ltr . word ] : "ltr" ,
83- } ) ) [ iterator ]
77+ let dir = ""
78+ switch ( iterator ) {
79+ case iter . rtl . rune :
80+ case iter . rtl . word :
81+ case iter . rtl . line :
82+ dir = "rtl"
83+ break
84+ case iter . ltr . rune :
85+ case iter . ltr . word :
86+ dir = "ltr"
87+ break
88+ default :
89+ // No-op
90+ break
91+ }
8492 if ( ! state . cursors . collapsed ) {
8593 return countBytesBetweenCursors ( state )
8694 }
@@ -96,6 +104,9 @@ const methods = state => ({
96104 removeByteCounts ( countL , countR ) {
97105 // NOTE: Uses state.cursors[1] because of
98106 // !state.cursors.collapsed case.
107+ //
108+ // TODO: Change uuidElement to x so removeByteCounts can
109+ // span one or more elements
99110 const uuidElement = state . elements . find ( each => each . uuid === state . cursors [ 1 ] . uuid )
100111 let offset = state . cursors [ 1 ] . offset
101112
@@ -134,7 +145,8 @@ const methods = state => ({
134145 }
135146 }
136147
137- mergeRedundantSpans ( uuidElement . spans )
148+ // TODO: We need to merge *many* uuidElement.spans
149+ mergeRepeatSpans ( uuidElement . spans )
138150
139151 if ( state . cursors . collapsed ) {
140152 state . cursors [ 0 ] . offset -= decremented
@@ -143,24 +155,24 @@ const methods = state => ({
143155
144156 } ,
145157 backspaceRune ( ) {
146- const count = this . countBytesToBoundary ( state , iter . rtl . rune )
147- this . removeByteCounts ( count , 0 )
158+ const countL = this . countBytesToBoundary ( state , iter . rtl . rune )
159+ this . removeByteCounts ( countL , 0 )
148160 } ,
149161 backspaceWord ( ) {
150- const count = this . countBytesToBoundary ( state , iter . rtl . word )
151- this . removeByteCounts ( count , 0 )
162+ const countL = this . countBytesToBoundary ( state , iter . rtl . word )
163+ this . removeByteCounts ( countL , 0 )
152164 } ,
153165 backspaceParagraph ( ) {
154- const count = this . countBytesToBoundary ( state , iter . rtl . line )
155- this . removeByteCounts ( count , 0 )
166+ const countL = this . countBytesToBoundary ( state , iter . rtl . line )
167+ this . removeByteCounts ( countL , 0 )
156168 } ,
157169 forwardBackspaceRune ( ) {
158- const count = this . countBytesToBoundary ( state , iter . ltr . rune )
159- this . removeByteCounts ( 0 , count )
170+ const countR = this . countBytesToBoundary ( state , iter . ltr . rune )
171+ this . removeByteCounts ( 0 , countR )
160172 } ,
161173 forwardBackspaceWord ( ) {
162- const count = this . countBytesToBoundary ( state , iter . ltr . word )
163- this . removeByteCounts ( 0 , count )
174+ const countR = this . countBytesToBoundary ( state , iter . ltr . word )
175+ this . removeByteCounts ( 0 , countR )
164176 } ,
165177 /*
166178 * Input
0 commit comments