Skip to content

Commit a7795f6

Browse files
author
Zaydek Michels-Gualtieri
committed
Fixed a bug preventing selecting and or inserting on a lone break node e.g. <p id="..."><br></p>. Note this may need to be compatibility patched for Firefox because we can’t have nice things.
1 parent 8e0b829 commit a7795f6

2 files changed

Lines changed: 20 additions & 27 deletions

File tree

src/Editor/Editor.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import toReact from "./toReact"
88
import useEditor from "./useEditor"
99
import uuidv4 from "uuid/v4"
1010
import { computeCursors } from "./cursors"
11-
import { computeRanges } from "./ranges"
11+
import { computeRange } from "./ranges"
1212
import { readSpans } from "./spans"
1313

1414
import {
@@ -185,17 +185,13 @@ const Editor = () => {
185185
// No-op
186186
return
187187
}
188-
const ranges = computeRanges(state.cursors)
188+
const range = computeRange(state.cursors[0])
189+
console.log(range)
189190
try {
190-
const range = document.createRange()
191-
range.setStart(ranges[0].container, ranges[0].offset)
192-
if (state.cursors.collapsed) {
193-
range.collapse()
194-
} else {
195-
range.setEnd(ranges[1].container, ranges[1].offset)
196-
}
197-
// selection.removeAllRanges()
198-
selection.addRange(range)
191+
const domRange = document.createRange()
192+
domRange.setStart(range.container, range.offset)
193+
domRange.collapse()
194+
selection.addRange(domRange)
199195
} catch (error) {
200196
console.error(error)
201197
}

src/Editor/ranges.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
// Creates a new range.
22
export function newRange() {
33
const range = {
4-
container: 0,
4+
container: null,
55
offset: 0,
66
}
77
return range
88
}
99

10+
// Returns whether a DOM node is a text node or a break
11+
// element.
12+
function isTextNodeOrBreakElement(domNode) {
13+
const ok = (
14+
domNode.nodeType === Node.TEXT_NODE ||
15+
(domNode.nodeType === Node.ELEMENT_NODE && domNode.nodeName === "BR")
16+
)
17+
return ok
18+
}
19+
1020
// Computes a range from a cursor.
11-
function computeRange({ uuid, offset }) { // NOTE: Copy offset -- do not mutate reference
21+
export function computeRange({ uuid, offset }) { // NOTE: Copy offset -- do not mutate reference
1222
const range = newRange()
1323
const uuidElement = document.getElementById(uuid)
1424
if (!uuidElement) {
1525
throw new Error("computeRange: no such uuid element")
1626
}
1727
// Recurses on a DOM node, mutates range.
1828
const recurse = startDOMNode => {
19-
// TODO: if (pos - (on.nodeValue || "").length <= 0) {
20-
if (startDOMNode.nodeType === Node.TEXT_NODE && offset - startDOMNode.textContent.length <= 0) {
29+
if (isTextNodeOrBreakElement(startDOMNode) && offset - startDOMNode.textContent.length <= 0) {
2130
Object.assign(range, {
2231
container: startDOMNode,
2332
offset,
@@ -39,15 +48,3 @@ function computeRange({ uuid, offset }) { // NOTE: Copy offset -- do not mutate
3948
recurse(uuidElement)
4049
return range
4150
}
42-
43-
// Computes ranges from cursors.
44-
export function computeRanges(cursors) {
45-
const ranges = []
46-
ranges.push(computeRange(cursors[0]))
47-
if (cursors.collapsed) {
48-
ranges.push(ranges[0])
49-
} else {
50-
ranges.push(computeRange(cursors[1]))
51-
}
52-
return ranges
53-
}

0 commit comments

Comments
 (0)