Skip to content

Commit 5b2e236

Browse files
author
Zaydek Michels-Gualtieri
committed
More housekeeping
1 parent bf77199 commit 5b2e236

File tree

7 files changed

+35
-32
lines changed

7 files changed

+35
-32
lines changed

src/Editor/Editor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ const Editor = () => {
286286
const [cursor] = computeVDOMCursors()
287287
const uuidElement = document.getElementById(cursor.uuid)
288288
if (!uuidElement) {
289-
throw new Error("onInput: no such uuid element")
289+
throw new Error("onInput: no such uuid dom element")
290290
}
291291
const spans = readVDOMSpans(uuidElement)
292292
dispatch.input(uuidElement.id, spans, cursor)

src/Editor/ReactRenderer.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react"
22
import toReact from "./toReact"
33

4-
// Computes an array of types and type map for a pseudo-
4+
// Computes an array of types and a type map for a pseudo-
55
// React element.
66
function computeTypeInfo(element) {
77
const types = []
@@ -22,7 +22,7 @@ function computeTypeInfo(element) {
2222

2323
// Decorates pseudo-React elements; sets element.pos to
2424
// "at-start", "at-center", or "at-end".
25-
function decoratePos(elements) {
25+
function decorate(elements) {
2626
for (let x = 0; x < elements.length; x++) {
2727
if (!x || typeof elements[x - 1] === "string" || typeof elements[x] === "string") {
2828
// No-op
@@ -39,7 +39,7 @@ function decoratePos(elements) {
3939
}
4040

4141
// Parses spans to pseudo-React elements.
42-
function parseSpans(spans) {
42+
function parse(spans) {
4343
const elements = []
4444
for (const span of spans) {
4545
if (!span.formats.length) {
@@ -63,16 +63,17 @@ function parseSpans(spans) {
6363
lastRef.props.children = span.content
6464
elements.push(element)
6565
}
66-
decoratePos(elements)
66+
decorate(elements)
6767
return elements
6868
}
6969

70+
// React renderer for the VDOM state (e.g. state.elements).
7071
const ReactRenderer = ({ state, dispatch, renderableMap }) => (
7172
state.elements.map(({ type: T, spans, ...props }) => (
7273
React.createElement(T, {
7374
key: props.uuid,
7475
...props,
75-
}, toReact(parseSpans(spans), renderableMap))
76+
}, toReact(parse(spans), renderableMap))
7677
))
7778
)
7879

src/Editor/ascend.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Ascends to the nearest DOM element.
2+
export function ascendToElement(domNode) {
3+
if (domNode.nodeType !== Node.ELEMENT_NODE) {
4+
return domNode.parentElement // TODO: Check domNode.parentElement?
5+
}
6+
return domNode
7+
}
8+
9+
// Ascends to the nearest UUID DOM element.
10+
export function ascendToUUIDElement(domNode) {
11+
let element = ascendToElement(domNode)
12+
while (element && !element.id) {
13+
element = element.parentElement
14+
}
15+
return element
16+
}

src/Editor/ascendToUUIDElement.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Editor/cursors.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ascendToUUIDElement from "./ascendToUUIDElement"
1+
import { ascendToUUIDElement } from "./ascend"
22

33
// Creates a new VDOM cursor.
44
export function newVDOMCursor() {
@@ -9,8 +9,8 @@ export function newVDOMCursor() {
99
return cursor
1010
}
1111

12-
// Computes a VDOM cursor from a UUID element and a range
13-
// container and offset.
12+
// Computes a VDOM cursor from a UUID DOM element and a DOM
13+
// range container and offset.
1414
function computeVDOMCursor(uuidElement, { container, offset }) {
1515
while (container.nodeType === Node.ELEMENT_NODE && container.childNodes.length) {
1616
if (offset === container.childNodes.length) {
@@ -23,7 +23,7 @@ function computeVDOMCursor(uuidElement, { container, offset }) {
2323
if (!uuidElement.id) {
2424
throw new Error("computeVDOMCursor: no such uuid")
2525
}
26-
// Recurses on a DOM node, mutates cursor.
26+
// Recurses on a DOM node; mutates cursor.
2727
const recurse = startDOMNode => {
2828
// TODO: Guard non-text nodes?
2929
if (startDOMNode === container) {

src/Editor/ranges.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ export function computeDOMRange({ uuid, offset }) { // NOTE: Copy offset -- do n
2222
const range = newDOMRange()
2323
const uuidElement = document.getElementById(uuid)
2424
if (!uuidElement) {
25-
throw new Error("computeDOMRange: no such uuid element")
25+
throw new Error("computeDOMRange: no such uuid dom element")
2626
}
27-
// Recurses on a DOM node, mutates range.
27+
// Recurses on a DOM node; mutates range.
2828
const recurse = startDOMNode => {
2929
if (isTextNodeOrBreakElement(startDOMNode) && offset - startDOMNode.textContent.length <= 0) {
3030
Object.assign(range, {

src/Editor/spans.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import formatsEnum from "./formatsEnum"
22

3-
// // TODO
4-
// if (domNode.nodeType === Node.ELEMENT_NODE && domNode.getAttribute("contenteditable") === "false") {
5-
// // No-op
6-
// continue
7-
// }
8-
93
// Reads a VDOM span from a DOM node.
104
function readVDOMSpan(domNode) {
115
const span = {
@@ -65,10 +59,15 @@ export function concatenateVDOMSpans(spans) {
6559
}
6660
}
6761

68-
// Reads spans from a UUID element.
62+
// Reads spans from a UUID DOM element.
6963
export function readVDOMSpans(uuidElement) {
7064
const spans = []
7165
for (let x = 0; x < uuidElement.childNodes.length; x++) {
66+
// // TODO
67+
// if (domNode.nodeType === Node.ELEMENT_NODE && domNode.getAttribute("contenteditable") === "false") {
68+
// // No-op
69+
// continue
70+
// }
7271
spans.push(readVDOMSpan(uuidElement.childNodes[x]))
7372
}
7473
concatenateVDOMSpans(spans)

0 commit comments

Comments
 (0)