Skip to content

Commit 4e3ecd8

Browse files
author
Zaydek Michels-Gualtieri
committed
Finished extracting backspace
1 parent 4bae799 commit 4e3ecd8

File tree

4 files changed

+34
-59
lines changed

4 files changed

+34
-59
lines changed

src/Editor/Iterators/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as emojiTrie from "emoji-trie"
22
import * as UTF8 from "lib/encoding/UTF8"
33

4-
// Right-to-left (RTL) iterator; returns a substring.
4+
// Right-to-left iterator; returns a substring.
55
export const RTL = {
66
rune(str) {
77
// return emojiTrie.atEnd(str)?.emoji || UTF8.atEnd(str)
@@ -70,7 +70,7 @@ export const RTL = {
7070
},
7171
}
7272

73-
// Left-to-right (LTR) iterator; returns a substring.
73+
// Left-to-right iterator; returns a substring.
7474
export const LTR = {
7575
rune(str) {
7676
// return emojiTrie.atStart(str)?.emoji || UTF8.atStart(str)

src/Editor/backspace/backspace.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,32 @@ import must from "lib/must"
77
// Returns the next right-to-left cursor for a boundary.
88
function nextRTLCursor(elements, { ...rtl }, boundary) {
99
const y = must(elements.findIndex(each => each.key === rtl.key))
10-
const substr = Spans.textContent(elements[y].props.children).slice(0, rtl.offset)
10+
const textContent = Spans.textContent(elements[y].props.children)
1111
if (!rtl.offset && y) {
1212
Object.assign(rtl, {
1313
key: elements[y - 1].key,
1414
offset: Spans.textContent(elements[y - 1].props.children).length,
1515
})
1616
return rtl
1717
}
18-
// const runes = Iterators.RTL[boundary](substr) // DEBUG
19-
// rtl.offset -= runes.length // DEBUG
20-
rtl.offset -= Iterators.RTL[boundary](substr).length
18+
const substr = Iterators.RTL[boundary](textContent.slice(0, rtl.offset))
19+
rtl.offset -= substr.length
2120
return rtl
2221
}
2322

2423
// Returns the next left-to-right cursor for a boundary.
2524
function nextLTRCursor(elements, { ...ltr }, boundary) {
2625
const y = must(elements.findIndex(each => each.key === ltr.key))
27-
const substr = Spans.textContent(elements[y].props.children).slice(ltr.offset)
28-
if (ltr.offset === substr.length && y + 1 < elements.length) {
26+
const textContent = Spans.textContent(elements[y].props.children)
27+
if (ltr.offset === textContent.length && y + 1 < elements.length) {
2928
Object.assign(ltr, {
3029
key: elements[y + 1].key,
31-
offset: Spans.textContent(elements[y + 1].props.children).length,
30+
offset: 0,
3231
})
3332
return ltr
3433
}
35-
// const runes = Iterators.LTR[boundary](substr) // DEBUG
36-
// ltr.offset += runes.length // DEBUG
37-
ltr.offset += Iterators.LTR[boundary](substr).length
34+
const substr = Iterators.LTR[boundary](textContent.slice(ltr.offset))
35+
ltr.offset += substr.length
3836
return ltr
3937
}
4038

src/Editor/backspace/dropCursors.js

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,14 @@ import * as Spans from "../Spans"
33
import JSONClone from "lib/JSONClone"
44
import must from "lib/must"
55

6-
// // Returns the span and character offsets for an array of
7-
// // spans at an offset.
8-
// function computeSpanOffsets(spans, offset) {
9-
// const offsets = {
10-
// span: 0,
11-
// char: 0,
12-
// }
13-
//
14-
// let x = 0
15-
// for (; x < spans.length; x++) {
16-
// const children = spans[x].props.children
17-
// if (offset - children.length <= 0) {
18-
// Object.assign(offsets, {
19-
// span: x,
20-
// char: offset,
21-
// })
22-
// return offsets
23-
// }
24-
// offset -= children.length
25-
// }
26-
// return null
27-
// }
28-
296
// Returns the span and character offsets for an array of
307
// spans at an offset.
31-
function computeSpanOffsets(spans, offset) {
8+
function Span_offsets(spans, offset) { // TODO
329
let x = 0
3310
for (; x < spans.length; x++) {
3411
const children = spans[x].props.children
3512
if (offset - children.length <= 0) {
36-
return { span: x, char: offset }
13+
return [x, offset]
3714
}
3815
offset -= children.length
3916
}
@@ -43,16 +20,16 @@ function computeSpanOffsets(spans, offset) {
4320
// Drops a number of characters from an array of spans at an
4421
// offset. Returns the number of characters dropped.
4522
function dropChars(spans, offset, nchars) {
46-
const offsets = must(computeSpanOffsets(spans, offset))
47-
if (nchars > offsets.char) {
48-
nchars = offsets.char
23+
const offsets = must(Span_offsets(spans, offset))
24+
if (nchars > offsets[1]) {
25+
nchars = offsets[1]
4926
}
50-
spans[offsets.span].props.children = (
51-
spans[offsets.span].props.children.slice(0, offsets.char - nchars) +
52-
spans[offsets.span].props.children.slice(offsets.char)
27+
spans[offsets[0]].props.children = (
28+
spans[offsets[0]].props.children.slice(0, offsets[1] - nchars) +
29+
spans[offsets[0]].props.children.slice(offsets[1])
5330
)
54-
if (!spans[offsets.span].props.children) {
55-
spans.splice(offsets.span, 1)
31+
if (!spans[offsets[0]].props.children) {
32+
spans.splice(offsets[0], 1)
5633
}
5734
Spans.defer(spans)
5835
return nchars
@@ -61,11 +38,16 @@ function dropChars(spans, offset, nchars) {
6138
// Counts the number of characters between the offsets of a
6239
// set of cursors.
6340
function nchars(cursors) {
64-
const nchars = 0
41+
let nchars = 0
6542
if (cursors[0].key === cursors[1].key) {
66-
return cursors[1].offset - cursors[0].offset
43+
// Hello, [world]!
44+
nchars = cursors[1].offset - cursors[0].offset
45+
} else {
46+
// Hello, world!
47+
// [Hello, world]!
48+
nchars = cursors[1].offset
6749
}
68-
return cursors[1].offset
50+
return nchars
6951
}
7052

7153
// Drops the characters between a set of cursors.

src/Editor/useEditor.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as Cursors from "./Cursors"
22
import * as Elements from "./Elements"
3-
import backspaceHandler from "./backspace"
3+
import backspace from "./backspace"
44
import must from "lib/must"
55
import newHashID from "lib/newHashID"
66
import React from "react"
@@ -106,32 +106,27 @@ const methods = state => ({
106106
// },
107107

108108
backspaceRTLRune() {
109-
const { elements, cursors } = state
110-
const collapsed = backspaceHandler(elements, cursors, "rtl", "rune")
109+
const collapsed = backspace(state.elements, state.cursors, "rtl", "rune")
111110
this.select(collapsed)
112111
this.render()
113112
},
114113
backspaceRTLWord() {
115-
const { elements, cursors } = state
116-
const collapsed = backspaceHandler(elements, cursors, "rtl", "word")
114+
const collapsed = backspace(state.elements, state.cursors, "rtl", "word")
117115
this.select(collapsed)
118116
this.render()
119117
},
120118
backspaceRTLLine() {
121-
const { elements, cursors } = state
122-
const collapsed = backspaceHandler(elements, cursors, "rtl", "line")
119+
const collapsed = backspace(state.elements, state.cursors, "rtl", "line")
123120
this.select(collapsed)
124121
this.render()
125122
},
126123
backspaceLTRRune() {
127-
const { elements, cursors } = state
128-
const collapsed = backspaceHandler(elements, cursors, "ltr", "rune")
124+
const collapsed = backspace(state.elements, state.cursors, "ltr", "rune")
129125
this.select(collapsed)
130126
this.render()
131127
},
132128
backspaceLTRWord() {
133-
const { elements, cursors } = state
134-
const collapsed = backspaceHandler(elements, cursors, "ltr", "word")
129+
const collapsed = backspace(state.elements, state.cursors, "ltr", "word")
135130
this.select(collapsed)
136131
this.render()
137132
},

0 commit comments

Comments
 (0)