Skip to content

Commit 59f108d

Browse files
author
Zaydek Michels-Gualtieri
committed
Comments
1 parent c2e89d3 commit 59f108d

6 files changed

Lines changed: 50 additions & 13 deletions

File tree

src/Editor/Editor.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const Editor = ({ html }) => {
2323
const pointerdownRef = React.useRef(false)
2424

2525
const [state, dispatch] = useEditor(html)
26+
console.log(state)
2627

2728
// // Disables read-only mode on DOMContentLoaded.
2829
// const DOMContentLoaded = useDOMContentLoaded()

src/Editor/components/toReact.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@ import toArray from "lib/toArray"
33
import toTree from "./toTree"
44
import types from "../types"
55

6-
// // Describes an array of inline elements.
7-
// class InlineElements extends Array {
8-
// // Converts to intermediary React elements.
9-
// toIntermediaryReact() {
10-
// // ...
11-
// }
12-
//
136
// // Converts to React elements. Uses an intermediary step
147
// // because React elements are read-only.
158
// toReact() {
@@ -22,8 +15,6 @@ import types from "../types"
2215
// // ...
2316
// }
2417
// }
25-
//
26-
// export default InlineElements
2718

2819
// Converts intermediary tree data structure to renderable
2920
// React elements.

src/Editor/model/Editor/VirtualElement.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class VirtualElement {
1414
constructor({ type, key, props } = {}) {
1515
Object.assign(this, {
1616
type: type || "",
17-
key: key || hash(),
17+
key: key || hash(6),
1818
props: props || {
1919
children: [],
2020
},

src/Editor/model/Editor/VirtualMultilineElement.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class VirtualMultilineElement {
1414
constructor({ type, key, props } = {}) {
1515
Object.assign(this, {
1616
type: type || "",
17-
key: key || hash(),
17+
key: key || hash(6),
1818
props: props || {
1919
elements: [],
2020
},

src/Editor/model/Scanners/AbstractScanner.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import VirtualElement from "../Editor/VirtualElement"
44
import VirtualInlineElement from "../Editor/VirtualInlineElement"
55

66
// Describes an abstract scanner; a scanner implements scan
7-
// to scan virtual elements and inline elements.
7+
// to scan virtual elements and children.
88
class AbstractScanner {
99
// Scans types and props.
1010
scanner = null
1111

12-
// Scans virtual inline elements from an element.
12+
// Scans virtual children from an element.
1313
scanChildren(element) {
1414
const children = []
1515
const recurse = (on, types = [], props = {}) => {

src/Editor/utils/defer.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import areEqualJSON from "lib/areEqualJSON"
2+
import must from "lib/must"
3+
import omitKeys from "lib/omitKeys"
4+
import { sortedTypeMap } from "../components/typeMaps"
5+
6+
// Compares whether spans are equal (omits props.children).
7+
function areEqualTypesAndProps(span1, span2) {
8+
const ok = (
9+
areEqualJSON(span1.types, span2.types) &&
10+
areEqualJSON(omitKeys(span1.props, "children"), omitKeys(span2.props, "children"))
11+
)
12+
return ok
13+
}
14+
15+
// Merges fragmented spans; spans must share equal types and
16+
// props to be merged.
17+
function merge(spans) {
18+
for (let x = 0; x < spans.length; x++) {
19+
if (x && areEqualTypesAndProps(spans[x - 1], spans[x])) {
20+
spans.splice(x - 1, 2, {
21+
...spans[x - 1],
22+
props: {
23+
...spans[x - 1].props,
24+
children: spans[x - 1].props.children + spans[x].props.children,
25+
},
26+
})
27+
continue
28+
}
29+
}
30+
return spans
31+
}
32+
33+
// Compares span types based on render precedence.
34+
function compareTypes(T1, T2) {
35+
const x1 = must(sortedTypeMap[T1])
36+
const x2 = must(sortedTypeMap[T2])
37+
return x1 - x2
38+
}
39+
40+
function defer(spans) {
41+
merge(spans)
42+
spans.map(each => each.types.sort(compareTypes))
43+
}
44+
45+
export default defer

0 commit comments

Comments
 (0)