Skip to content

Commit

Permalink
More housekeeping
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaydek Michels-Gualtieri committed Jun 15, 2020
1 parent bf77199 commit b5190b4
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 30 deletions.
11 changes: 6 additions & 5 deletions src/Editor/ReactRenderer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react"
import toReact from "./toReact"

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

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

// Parses spans to pseudo-React elements.
function parseSpans(spans) {
function parse(spans) {
const elements = []
for (const span of spans) {
if (!span.formats.length) {
Expand All @@ -63,16 +63,17 @@ function parseSpans(spans) {
lastRef.props.children = span.content
elements.push(element)
}
decoratePos(elements)
decorate(elements)
return elements
}

// React renderer for the VDOM state (e.g. state.elements).
const ReactRenderer = ({ state, dispatch, renderableMap }) => (
state.elements.map(({ type: T, spans, ...props }) => (
React.createElement(T, {
key: props.uuid,
...props,
}, toReact(parseSpans(spans), renderableMap))
}, toReact(parse(spans), renderableMap))
))
)

Expand Down
16 changes: 16 additions & 0 deletions src/Editor/ascend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Ascends to the nearest DOM element.
export function ascendToElement(domNode) {
if (domNode.nodeType !== Node.ELEMENT_NODE) {
return domNode.parentElement // TODO: Check domNode.parentElement?
}
return domNode
}

// Ascends to the nearest UUID DOM element.
export function ascendToUUIDElement(domNode) {
let element = ascendToElement(domNode)
while (element && !element.id) {
element = element.parentElement
}
return element
}
13 changes: 0 additions & 13 deletions src/Editor/ascendToUUIDElement.js

This file was deleted.

8 changes: 4 additions & 4 deletions src/Editor/cursors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ascendToUUIDElement from "./ascendToUUIDElement"
import { ascendToUUIDElement } from "./ascend"

// Creates a new VDOM cursor.
export function newVDOMCursor() {
Expand All @@ -9,8 +9,8 @@ export function newVDOMCursor() {
return cursor
}

// Computes a VDOM cursor from a UUID element and a range
// container and offset.
// Computes a VDOM cursor from a UUID DOM element and a DOM
// range container and offset.
function computeVDOMCursor(uuidElement, { container, offset }) {
while (container.nodeType === Node.ELEMENT_NODE && container.childNodes.length) {
if (offset === container.childNodes.length) {
Expand All @@ -23,7 +23,7 @@ function computeVDOMCursor(uuidElement, { container, offset }) {
if (!uuidElement.id) {
throw new Error("computeVDOMCursor: no such uuid")
}
// Recurses on a DOM node, mutates cursor.
// Recurses on a DOM node; mutates cursor.
const recurse = startDOMNode => {
// TODO: Guard non-text nodes?
if (startDOMNode === container) {
Expand Down
2 changes: 1 addition & 1 deletion src/Editor/ranges.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function computeDOMRange({ uuid, offset }) { // NOTE: Copy offset -- do n
if (!uuidElement) {
throw new Error("computeDOMRange: no such uuid element")
}
// Recurses on a DOM node, mutates range.
// Recurses on a DOM node; mutates range.
const recurse = startDOMNode => {
if (isTextNodeOrBreakElement(startDOMNode) && offset - startDOMNode.textContent.length <= 0) {
Object.assign(range, {
Expand Down
13 changes: 6 additions & 7 deletions src/Editor/spans.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import formatsEnum from "./formatsEnum"

// // TODO
// if (domNode.nodeType === Node.ELEMENT_NODE && domNode.getAttribute("contenteditable") === "false") {
// // No-op
// continue
// }

// Reads a VDOM span from a DOM node.
function readVDOMSpan(domNode) {
const span = {
Expand Down Expand Up @@ -65,10 +59,15 @@ export function concatenateVDOMSpans(spans) {
}
}

// Reads spans from a UUID element.
// Reads spans from a UUID DOM element.
export function readVDOMSpans(uuidElement) {
const spans = []
for (let x = 0; x < uuidElement.childNodes.length; x++) {
// // TODO
// if (domNode.nodeType === Node.ELEMENT_NODE && domNode.getAttribute("contenteditable") === "false") {
// // No-op
// continue
// }
spans.push(readVDOMSpan(uuidElement.childNodes[x]))
}
concatenateVDOMSpans(spans)
Expand Down

0 comments on commit b5190b4

Please sign in to comment.