Skip to content

Commit 68c2a48

Browse files
author
Zaydek Michels-Gualtieri
committed
Updated countBytesToBoundary implementation to not use an ES6 map to compute direction (dir) because Object.entries just maps strings to strings (we want functions to strings)
1 parent 8299247 commit 68c2a48

File tree

3 files changed

+82
-22
lines changed

3 files changed

+82
-22
lines changed

src/Editor/Editor.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,54 @@ const Editor = () => {
9292
},
9393
],
9494
},
95+
{
96+
type: Paragraph,
97+
uuid: uuidv4(),
98+
spans: [
99+
{
100+
content: "Hey, ",
101+
formats: [formatsEnum.strong],
102+
},
103+
{
104+
content: "Russ",
105+
formats: [formatsEnum.strong, formatsEnum.emphasis],
106+
},
107+
{
108+
content: "!",
109+
formats: [formatsEnum.strong],
110+
},
111+
{
112+
content: " I’m making some ",
113+
formats: [formatsEnum.strong],
114+
},
115+
{
116+
content: "progress ",
117+
formats: [formatsEnum.code],
118+
},
119+
{
120+
content: " on making a ",
121+
formats: [],
122+
},
123+
{
124+
content: "WYSIWYG",
125+
formats: [formatsEnum.anchor],
126+
[formatsEnum.anchor]: {
127+
href: "https://heroicons.dev",
128+
},
129+
},
130+
{
131+
content: " editor.",
132+
formats: [],
133+
},
134+
{
135+
content: "WYSIWYG",
136+
formats: [formatsEnum.anchor],
137+
[formatsEnum.anchor]: {
138+
href: "https://heroicons.dev",
139+
},
140+
},
141+
],
142+
},
95143
])
96144

97145
// TODO (1): Add backspace handlers

src/Editor/spans.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ function formatsAndPropsAreEqual(spanA, spanB) {
5151
return true
5252
}
5353

54-
// Merges redundant spans (e.g. fragmented).
55-
export function mergeRedundantSpans(spans) {
54+
// Merges repeat spans.
55+
export function mergeRepeatSpans(spans) {
5656
for (let x = 0; x < spans.length; x++) {
5757
if (x && formatsAndPropsAreEqual(spans[x - 1], spans[x])) {
5858
spans.splice(x - 1, 2, {
@@ -70,7 +70,7 @@ export function readSpans(uuidElement) {
7070
for (let x = 0; x < uuidElement.childNodes.length; x++) {
7171
spans.push(readSpan(uuidElement.childNodes[x]))
7272
}
73-
mergeRedundantSpans(spans)
73+
mergeRepeatSpans(spans)
7474
console.log(spans)
7575
return spans
7676
}

src/Editor/useEditor.js

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as iter from "./iter"
22
import useMethods from "use-methods"
33
import uuidv4 from "uuid/v4"
4-
import { mergeRedundantSpans } from "./spans"
4+
import { mergeRepeatSpans } from "./spans"
55
import { 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

Comments
 (0)