Skip to content

Commit 849585d

Browse files
author
Zaydek Michels-Gualtieri
committed
Added a handler to disable read-only mode when DOMContentLoaded fires
1 parent c81c072 commit 849585d

File tree

4 files changed

+43
-227
lines changed

4 files changed

+43
-227
lines changed

src/Editor/Editor.js

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ const Editor = ({ markup, children }) => {
2828

2929
const [state, dispatch] = useEditor({ markup, children })
3030

31+
// Disables read-only mode on DOMContentLoaded.
32+
React.useEffect(() => {
33+
const handler = e => {
34+
dispatch.disableReadOnlyMode()
35+
}
36+
document.addEventListener("DOMContentLoaded", handler)
37+
return () => {
38+
document.removeEventListener("DOMContentLoaded", handler)
39+
}
40+
}, [dispatch])
41+
42+
// Renders on state.shouldRender.
3143
React.useLayoutEffect(
3244
React.useCallback(() => {
3345
// https://bugs.chromium.org/p/chromium/issues/detail?id=138439#c10
@@ -44,17 +56,19 @@ const Editor = ({ markup, children }) => {
4456
const domRange = Range.toDOMRange(state.range)
4557
domSelection.addRange(domRange)
4658
} catch (error) {
47-
console.error(error)
59+
console.error({
60+
range: state.range,
61+
error,
62+
})
4863
}
4964
})
5065
}, [state, dispatch]),
5166
[state.shouldRender],
5267
)
5368

54-
// Exclusively returns a handler when the editor is
55-
// unlocked; returns undefined when the editor is locked.
56-
const newUnlockedHandler = handler => {
57-
if (state.locked) {
69+
// Exclusively returns a handler when state.readOnly=true.
70+
const newReadWriteHandler = handler => {
71+
if (state.readOnly) {
5872
return undefined
5973
}
6074
return handler
@@ -78,19 +92,19 @@ const Editor = ({ markup, children }) => {
7892
tabSize: 4,
7993
}}
8094

81-
onFocus={newUnlockedHandler(e => {
95+
onFocus={newReadWriteHandler(e => {
8296
dispatch.focus()
8397
})}
8498

85-
onBlur={newUnlockedHandler(e => {
99+
onBlur={newReadWriteHandler(e => {
86100
dispatch.blur()
87101
})}
88102

89-
onPointerDown={newUnlockedHandler(e => {
103+
onPointerDown={newReadWriteHandler(e => {
90104
pointerdownRef.current = true
91105
})}
92106

93-
onPointerMove={newUnlockedHandler(e => {
107+
onPointerMove={newReadWriteHandler(e => {
94108
if (!state.focused) {
95109
pointerdownRef.current = false
96110
return
@@ -107,13 +121,13 @@ const Editor = ({ markup, children }) => {
107121
dispatch.select(range)
108122
})}
109123

110-
onPointerUp={newUnlockedHandler(e => {
124+
onPointerUp={newReadWriteHandler(e => {
111125
pointerdownRef.current = false
112126
})}
113127

114128
// TODO: Add COMPAT guard for select-all or prevent
115129
// default?
116-
onSelect={newUnlockedHandler(e => {
130+
onSelect={newReadWriteHandler(e => {
117131
const range = Range.compute(ref.current)
118132
if (!range) {
119133
// No-op
@@ -122,7 +136,7 @@ const Editor = ({ markup, children }) => {
122136
dispatch.select(range)
123137
})}
124138

125-
onKeyDown={newUnlockedHandler(e => {
139+
onKeyDown={newReadWriteHandler(e => {
126140
const keydownT = keydown.detectType(e)
127141
if (keydownT) {
128142
console.log(keydownT)
@@ -208,33 +222,33 @@ const Editor = ({ markup, children }) => {
208222
}
209223
})}
210224

211-
onInput={newUnlockedHandler(e => {
225+
onInput={newReadWriteHandler(e => {
212226
const collapsed = Range.collapse(Range.compute(ref.current)) // Takes precedence
213227
const spans = Readers.rendered.spans(document.getElementById(collapsed[0].key))
214228
dispatch.uncontrolledInputHandler(spans, collapsed)
215229
})}
216230

217-
onCut={newUnlockedHandler(e => {
231+
onCut={newReadWriteHandler(e => {
218232
e.preventDefault()
219233
// TODO: e.clipboardData.setData("text/plain", ...)
220234
})}
221235

222-
onCopy={newUnlockedHandler(e => {
236+
onCopy={newReadWriteHandler(e => {
223237
e.preventDefault()
224238
// TODO: e.clipboardData.setData("text/plain", ...)
225239
})}
226240

227-
onPaste={newUnlockedHandler(e => {
241+
onPaste={newReadWriteHandler(e => {
228242
e.preventDefault()
229243
// TODO: e.clipboardData.getData("text/plain")
230244
})}
231245

232-
onDragStart={newUnlockedHandler(e => {
246+
onDragStart={newReadWriteHandler(e => {
233247
e.preventDefault()
234248
})}
235249

236-
contentEditable={!state.locked}
237-
suppressContentEditableWarning={!state.locked}
250+
contentEditable={!state.readOnly}
251+
suppressContentEditableWarning={!state.readOnly}
238252
/>
239253

240254
{/* Debugger */}

src/Editor/notes.js

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

src/Editor/useEditor/index.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,11 @@ import useMethods from "use-methods"
2121
// this.select(collapsed)
2222
// render(state)()
2323
// },
24-
// input(spans, collapsed) {
25-
// const element = state.elements.find(each => each.key === collapsed[0].key)
26-
// element.props.spans = spans
27-
// this.select(collapsed)
28-
// render(state)()
29-
// },
3024

3125
const init = elements => ({
32-
locked: false,
26+
// NOTE: The DOM event "DOMContentLoaded" fires
27+
// dispatch.enableReadOnlyMode.
28+
readOnly: true,
3329
elements,
3430
focused: false,
3531
range: {

src/Editor/useEditor/methods.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
import $delete from "./delete"
22
import applyFormat from "./applyFormat"
33

4-
// TODO: Add findElementOrNode API or equivalent?
54
const methods = state => ({
6-
// Locks the editor; disables future edits. Unlike blur,
7-
// lock is expected to remove the DOM attribute
8-
// contenteditable.
9-
lock() {
10-
state.locked = true
11-
},
12-
// Unlocks the editor; enables future edits. Unlike focus,
13-
// unlock is expected to add the DOM attribute
14-
// contenteditable.
15-
unlock() {
16-
state.locked = false
5+
// Enables read-only mode; disables future edits.
6+
enableReadOnlyMode() {
7+
state.readOnly = true
8+
},
9+
// Disables read-only mode; enables future edits.
10+
disableReadOnlyMode() {
11+
state.readOnly = false
1712
},
1813
// Focuses the editor. When the editor is focused, editing
1914
// operations are expected to work.

0 commit comments

Comments
 (0)