Skip to content

Commit 0621f8c

Browse files
author
Zaydek Michels-Gualtieri
committed
Simplified countBytesToBoundary
1 parent 78f0ca0 commit 0621f8c

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

src/Editor/useEditor.js

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@ import uuidv4 from "uuid/v4"
44
import { mergeRedundantSpans } from "./spans"
55
import { newCursor } from "./cursors"
66

7+
// Counts the number of bytes between the cursors.
8+
function countBytesBetweenCursors(state) {
9+
let x1 = -1
10+
let x2 = -1
11+
for (let x = 0; x < state.elements.length; x++) {
12+
if (state.elements[x].uuid === state.cursors[0].uuid) {
13+
x1 = x
14+
}
15+
if (state.elements[x].uuid === state.cursors[1].uuid) {
16+
x2 = x
17+
}
18+
if (x1 !== -1 && x2 !== -1) {
19+
// No-op
20+
break
21+
}
22+
}
23+
let count = state.cursors[1].offset - state.cursors[0].offset
24+
while (x1 !== x2) {
25+
count += readSyntheticUUIDElement(state.element[x1])
26+
}
27+
return count
28+
}
29+
730
// Reads a synthetic UUID element.
831
function readSyntheticUUIDElement(uuidElement) {
932
const reducer = (acc, each) => {
@@ -49,16 +72,22 @@ const methods = state => ({
4972
/*
5073
* Backspace
5174
*/
52-
// Counts the number of bytes needed to iterate a boundary
53-
// such as "rune", "word", etc.
54-
countBytes(iterator, boundary, state) {
75+
// Counts the number of bytes to a boundary.
76+
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]
5584
if (!state.cursors.collapsed) {
5685
return 0
5786
}
5887
const { uuid, offset } = state.cursors[0]
5988
const x = state.elements.findIndex(each => each.uuid === uuid)
60-
let count = iterator[boundary](readSyntheticUUIDElement(state.elements[x]), offset)
61-
if (!count && ((iterator === iter.rtl && x) || (iterator === iter.ltr && x + 1 < state.elements.length))) {
89+
let count = iterator(readSyntheticUUIDElement(state.elements[x]), offset)
90+
if (!count && ((dir === "rtl" && x) || (dir === "ltr" && x + 1 < state.elements.length))) {
6291
count++
6392
}
6493
return count
@@ -67,7 +96,7 @@ const methods = state => ({
6796
removeByteCounts(countL, countR) {
6897

6998
// Get the current UUID element:
70-
let { uuid, offset } = state.cursors[0]
99+
let { uuid, offset } = state.cursors[0] // TODO: Change to state.cursors[1]?
71100
const uuidElement = state.elements.find(each => each.uuid === uuid)
72101

73102
// Get the span (x) and character offset (offset):
@@ -113,42 +142,42 @@ const methods = state => ({
113142
},
114143
backspaceRune() {
115144
if (!state.cursors.collapsed) {
116-
// TODO
145+
console.log(countBytesBetweenCursors(state))
117146
return
118147
}
119-
const count = this.countBytes(iter.rtl, "rune", state)
148+
const count = this.countBytesToBoundary(state, iter.rtl.rune)
120149
this.removeByteCounts(count, 0)
121150
},
122151
backspaceWord() {
123152
if (!state.cursors.collapsed) {
124-
// TODO
153+
// TODO: Compute the number of bytes needed
125154
return
126155
}
127-
const count = this.countBytes(iter.rtl, "word", state)
156+
const count = this.countBytesToBoundary(state, iter.rtl.word)
128157
this.removeByteCounts(count, 0)
129158
},
130159
backspaceParagraph() {
131160
if (!state.cursors.collapsed) {
132-
// TODO
161+
// TODO: Compute the number of bytes needed
133162
return
134163
}
135-
const count = this.countBytes(iter.rtl, "line", state)
164+
const count = this.countBytesToBoundary(state, iter.rtl.line)
136165
this.removeByteCounts(count, 0)
137166
},
138167
forwardBackspaceRune() {
139168
if (!state.cursors.collapsed) {
140-
// TODO
169+
// TODO: Compute the number of bytes needed
141170
return
142171
}
143-
const count = this.countBytes(iter.ltr, "rune", state)
172+
const count = this.countBytesToBoundary(state, iter.ltr.rune)
144173
this.removeByteCounts(0, count)
145174
},
146175
forwardBackspaceWord() {
147176
if (!state.cursors.collapsed) {
148-
// TODO
177+
// TODO: Compute the number of bytes needed
149178
return
150179
}
151-
const count = this.countBytes(iter.ltr, "word", state)
180+
const count = this.countBytesToBoundary(state, iter.ltr.word)
152181
this.removeByteCounts(0, count)
153182
},
154183
/*

0 commit comments

Comments
 (0)