Skip to content

Commit bf77199

Browse files
author
Zaydek Michels-Gualtieri
committed
Small changes (mostly comments and API names)
1 parent 68c2a48 commit bf77199

File tree

5 files changed

+99
-105
lines changed

5 files changed

+99
-105
lines changed

src/Editor/Editor.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import ReactDOM from "react-dom"
77
import ReactRenderer from "./ReactRenderer"
88
import useEditor from "./useEditor"
99
import uuidv4 from "uuid/v4"
10-
import { computeCursors } from "./cursors"
11-
import { computeRange } from "./ranges"
12-
import { readSpans } from "./spans"
10+
import { computeDOMRange } from "./ranges"
11+
import { computeVDOMCursors } from "./cursors"
12+
import { readVDOMSpans } from "./spans"
1313

1414
import {
1515
Anchor,
@@ -168,7 +168,7 @@ const Editor = () => {
168168
// No-op
169169
return
170170
}
171-
const range = computeRange(state.cursors[0])
171+
const range = computeDOMRange(state.cursors[0])
172172
try {
173173
const domRange = document.createRange()
174174
domRange.setStart(range.container, range.offset)
@@ -194,7 +194,7 @@ const Editor = () => {
194194
onFocus={dispatch.focus}
195195
onBlur={dispatch.blur}
196196
onSelect={() => {
197-
const cursors = computeCursors()
197+
const cursors = computeVDOMCursors()
198198
dispatch.select(cursors)
199199
}}
200200
onPointerDown={() => {
@@ -205,7 +205,7 @@ const Editor = () => {
205205
pointerIsDownRef.current = false
206206
return
207207
}
208-
const cursors = computeCursors()
208+
const cursors = computeVDOMCursors()
209209
dispatch.select(cursors)
210210
}}
211211
onPointerUp={() => {
@@ -283,12 +283,12 @@ const Editor = () => {
283283
}}
284284

285285
onInput={() => {
286-
const [cursor] = computeCursors()
286+
const [cursor] = computeVDOMCursors()
287287
const uuidElement = document.getElementById(cursor.uuid)
288288
if (!uuidElement) {
289289
throw new Error("onInput: no such uuid element")
290290
}
291-
const spans = readSpans(uuidElement)
291+
const spans = readVDOMSpans(uuidElement)
292292
dispatch.input(uuidElement.id, spans, cursor)
293293
}}
294294

src/Editor/cursors.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
import ascendToUUIDElement from "./ascendToUUIDElement"
22

3-
// Creates a new cursor.
4-
export function newCursor() {
3+
// Creates a new VDOM cursor.
4+
export function newVDOMCursor() {
55
const cursor = {
66
uuid: "",
77
offset: 0,
88
}
99
return cursor
1010
}
1111

12-
// Computes a cursor from a UUID element and a range
12+
// Computes a VDOM cursor from a UUID element and a range
1313
// container and offset.
14-
function computeCursor(uuidElement, { container, offset }) {
14+
function computeVDOMCursor(uuidElement, { container, offset }) {
1515
while (container.nodeType === Node.ELEMENT_NODE && container.childNodes.length) {
1616
if (offset === container.childNodes.length) {
1717
offset = Math.min(0, container.childNodes.length - 1)
1818
}
1919
container = container.childNodes[offset]
2020
offset = 0
2121
}
22-
23-
const cursor = newCursor()
22+
const cursor = newVDOMCursor()
2423
if (!uuidElement.id) {
25-
throw new Error("computeCursor: no such uuid")
24+
throw new Error("computeVDOMCursor: no such uuid")
2625
}
2726
// Recurses on a DOM node, mutates cursor.
2827
const recurse = startDOMNode => {
@@ -50,8 +49,8 @@ function computeCursor(uuidElement, { container, offset }) {
5049
return cursor
5150
}
5251

53-
// Computes cursors from the current range.
54-
export function computeCursors() {
52+
// Computes VDOM cursors from the current range.
53+
export function computeVDOMCursors() {
5554
// Get the current range:
5655
const selection = document.getSelection()
5756
if (!selection || !selection.rangeCount) {
@@ -60,11 +59,17 @@ export function computeCursors() {
6059
const range = selection.getRangeAt(0)
6160
// Compute cursors:
6261
const cursors = []
63-
cursors.push(computeCursor(ascendToUUIDElement(range.startContainer), { container: range.startContainer, offset: range.startOffset }))
62+
cursors.push(computeVDOMCursor(ascendToUUIDElement(range.startContainer), {
63+
container: range.startContainer,
64+
offset: range.startOffset,
65+
}))
6466
if (range.collapsed) {
6567
cursors.push(cursors[0])
6668
} else {
67-
cursors.push(computeCursor(ascendToUUIDElement(range.endContainer), { container: range.endContainer, offset: range.endOffset }))
69+
cursors.push(computeVDOMCursor(ascendToUUIDElement(range.endContainer), {
70+
container: range.endContainer,
71+
offset: range.endOffset,
72+
}))
6873
}
6974
return cursors
7075
}

src/Editor/ranges.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Creates a new range.
2-
export function newRange() {
1+
// Creates a new DOM range.
2+
export function newDOMRange() {
33
const range = {
44
container: null,
55
offset: 0,
@@ -17,12 +17,12 @@ function isTextNodeOrBreakElement(domNode) {
1717
return ok
1818
}
1919

20-
// Computes a range from a cursor.
21-
export function computeRange({ uuid, offset }) { // NOTE: Copy offset -- do not mutate reference
22-
const range = newRange()
20+
// Computes a DOM range from a VDOM cursor.
21+
export function computeDOMRange({ uuid, offset }) { // NOTE: Copy offset -- do not mutate reference
22+
const range = newDOMRange()
2323
const uuidElement = document.getElementById(uuid)
2424
if (!uuidElement) {
25-
throw new Error("computeRange: no such uuid element")
25+
throw new Error("computeDOMRange: no such uuid element")
2626
}
2727
// Recurses on a DOM node, mutates range.
2828
const recurse = startDOMNode => {

src/Editor/spans.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import formatsEnum from "./formatsEnum"
66
// continue
77
// }
88

9-
// Reads a span from a DOM node.
10-
function readSpan(domNode) {
9+
// Reads a VDOM span from a DOM node.
10+
function readVDOMSpan(domNode) {
1111
const span = {
1212
content: domNode.textContent,
1313
formats: [],
@@ -36,23 +36,24 @@ function readSpan(domNode) {
3636
return span
3737
}
3838

39-
// Returns whether two spans’ formats and props are equal.
40-
function formatsAndPropsAreEqual(spanA, spanB) {
41-
if (spanA.formats.length !== spanB.formats.length) {
39+
// Returns whether two VDOM spans’ formats and props are
40+
// equal.
41+
function formatsAndPropsAreEqual(s1, s2) {
42+
if (s1.formats.length !== s2.formats.length) {
4243
return false
4344
}
44-
for (let x = 0; x < spanA.formats.length; x++) {
45-
if (spanA.formats[x] !== spanB.formats[x]) {
45+
for (let x = 0; x < s1.formats.length; x++) {
46+
if (s1.formats[x] !== s2.formats[x]) {
4647
return false
47-
} else if (JSON.stringify(spanA[spanA.formats[x]]) !== JSON.stringify(spanB[spanB.formats[x]])) {
48+
} else if (JSON.stringify(s1[s1.formats[x]]) !== JSON.stringify(s2[s2.formats[x]])) {
4849
return false
4950
}
5051
}
5152
return true
5253
}
5354

54-
// Merges repeat spans.
55-
export function mergeRepeatSpans(spans) {
55+
// Concatenates VDOM spans that share formats and props.
56+
export function concatenateVDOMSpans(spans) {
5657
for (let x = 0; x < spans.length; x++) {
5758
if (x && formatsAndPropsAreEqual(spans[x - 1], spans[x])) {
5859
spans.splice(x - 1, 2, {
@@ -65,12 +66,11 @@ export function mergeRepeatSpans(spans) {
6566
}
6667

6768
// Reads spans from a UUID element.
68-
export function readSpans(uuidElement) {
69+
export function readVDOMSpans(uuidElement) {
6970
const spans = []
7071
for (let x = 0; x < uuidElement.childNodes.length; x++) {
71-
spans.push(readSpan(uuidElement.childNodes[x]))
72+
spans.push(readVDOMSpan(uuidElement.childNodes[x]))
7273
}
73-
mergeRepeatSpans(spans)
74-
console.log(spans)
74+
concatenateVDOMSpans(spans)
7575
return spans
7676
}

0 commit comments

Comments
 (0)