Skip to content

Commit df215bc

Browse files
author
Zaydek Michels-Gualtieri
committed
Deprecated plaintext string spans (makes mutating spans harder, because sometimes 2x code is needed to guard all cases). We still need to make sure we combine spans that can be combined so long as their props (ignoring typePos) are not in conflict with each other. An example of this would be href; links are discrete if their hrefs are also discrete.
1 parent a7795f6 commit df215bc

4 files changed

Lines changed: 31 additions & 50 deletions

File tree

src/Editor/Editor.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,11 @@ function decorateTypePos(components) {
6767
function parseSpans(spans) {
6868
const components = []
6969
for (const span of spans) {
70-
if (typeof span === "string") {
71-
components.push(span)
70+
if (!span.formats.length) {
71+
components.push(span.content)
7272
continue
7373
}
74-
const component = {
75-
props: {
76-
children: {},
77-
},
78-
}
74+
const component = {}
7975
let lastRef = component
8076
let ref = lastRef
8177
for (const format of span.formats.sort()) {
@@ -146,15 +142,21 @@ const Editor = () => {
146142
content: "progress ",
147143
formats: [formatsEnum.code],
148144
},
149-
" on making a ",
145+
{
146+
content: " on making a ",
147+
formats: [],
148+
},
150149
{
151150
content: "WYSIWYG",
152151
formats: [formatsEnum.anchor],
153152
[formatsEnum.anchor]: {
154153
href: "https://heroicons.dev",
155154
},
156155
},
157-
" editor.",
156+
{
157+
content: " editor.",
158+
formats: [],
159+
},
158160
],
159161
},
160162
])
@@ -186,7 +188,6 @@ const Editor = () => {
186188
return
187189
}
188190
const range = computeRange(state.cursors[0])
189-
console.log(range)
190191
try {
191192
const domRange = document.createRange()
192193
domRange.setStart(range.container, range.offset)

src/Editor/spans.js

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

33
// Reads a span from a DOM node.
4-
//
5-
// TODO: Move props to JSON? E.g. data-codex-props="{ ... }"
64
function readSpan(domNode) {
7-
if (domNode.nodeType === Node.TEXT_NODE) {
8-
return domNode.textContent
9-
}
105
const span = {
116
content: domNode.textContent,
127
formats: [],
138
}
9+
if (domNode.nodeType === Node.TEXT_NODE) {
10+
span.content = domNode.textContent
11+
return span
12+
}
1413
let ref = domNode
1514
while (ref) {
16-
const type = Number(ref.getAttribute("data-codex-type"))
17-
if (!isNaN(type)) { // NOTE: "type" can be 0; !isNaN(Number(undefined))
15+
const attr = ref.getAttribute("data-codex-type")
16+
if (attr) {
17+
const type = Number(attr)
1818
span.formats.push(type)
1919
if (type === formatsEnum.anchor) {
2020
span[formatsEnum.anchor] = JSON.parse(ref.getAttribute("data-codex-props"))
@@ -30,15 +30,16 @@ function readSpan(domNode) {
3030
return span
3131
}
3232

33+
// // TODO
34+
// if (domNode.nodeType === Node.ELEMENT_NODE && domNode.getAttribute("contenteditable") === "false") {
35+
// // No-op
36+
// continue
37+
// }
38+
3339
// Reads spans from a UUID element.
3440
export function readSpans(uuidElement) {
3541
const spans = []
3642
for (const domNode of uuidElement.childNodes) {
37-
// // TODO
38-
// if (domNode.nodeType === Node.ELEMENT_NODE && domNode.getAttribute("contenteditable") === "false") {
39-
// // No-op
40-
// continue
41-
// }
4243
const span = readSpan(domNode)
4344
if (typeof span === "string") {
4445
if (spans.length && typeof spans[spans.length - 1] === "string") {

src/Editor/toReact.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
import React from "react"
22

3-
// Returns an array.
4-
function toArray(value) {
5-
if (!Array.isArray(value)) {
6-
return [value]
7-
}
8-
return value
9-
}
10-
113
// Converts components to renderable React elements.
124
function toReact(components, renderableMap) {
135
const renderable = []
14-
for (const component of toArray(components)) {
6+
for (const component of [components].flat()) {
157
if (typeof component === "string") {
168
renderable.push(component)
179
continue
@@ -22,9 +14,6 @@ function toReact(components, renderableMap) {
2214
...props,
2315
}, toReact(props.children, renderableMap)))
2416
}
25-
if (!renderable.length) {
26-
return null
27-
}
2817
return renderable
2918
}
3019

src/Editor/useEditor.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,12 @@ const methods = state => ({
8585
if (count > offset) {
8686
count = offset
8787
}
88-
if (typeof uuidElement.spans[x] === "string") {
89-
uuidElement.spans[x] = (
90-
uuidElement.spans[x].slice(0, offset - count) +
91-
uuidElement.spans[x].slice(offset)
92-
)
93-
if (!uuidElement.spans[x]) {
94-
uuidElement.spans.splice(x, 1)
95-
}
96-
} else {
97-
uuidElement.spans[x].content = (
98-
uuidElement.spans[x].content.slice(0, offset - count) +
99-
uuidElement.spans[x].content.slice(offset)
100-
)
101-
if (!uuidElement.spans[x].content) {
102-
uuidElement.spans.splice(x, 1)
103-
}
88+
uuidElement.spans[x].content = (
89+
uuidElement.spans[x].content.slice(0, offset - count) +
90+
uuidElement.spans[x].content.slice(offset)
91+
)
92+
if (!uuidElement.spans[x].content) {
93+
uuidElement.spans.splice(x, 1)
10494
}
10595
return count
10696
}

0 commit comments

Comments
 (0)