@bitbindle/prosemirror-view 0.0.0-stage → 1.44.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.tern-project +8 -0
- package/CHANGELOG.md +2639 -0
- package/CONTRIBUTING.md +104 -0
- package/FORK.md +67 -0
- package/LICENSE +19 -0
- package/README.md +27 -2
- package/dist/index.cjs +5668 -0
- package/dist/index.d.cts +921 -0
- package/dist/index.d.ts +921 -0
- package/dist/index.js +6135 -0
- package/package.json +56 -4
- package/src/README.md +38 -0
- package/src/browser.ts +18 -0
- package/src/capturekeys.ts +317 -0
- package/src/clipboard.ts +282 -0
- package/src/decoration.ts +834 -0
- package/src/dom.ts +174 -0
- package/src/domchange.ts +363 -0
- package/src/domcoords.ts +557 -0
- package/src/domobserver.ts +279 -0
- package/src/index.ts +800 -0
- package/src/input.ts +1001 -0
- package/src/selection.ts +460 -0
- package/src/viewdesc.ts +1518 -0
- package/style/prosemirror.css +54 -0
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import {Selection} from "prosemirror-state"
|
|
2
|
+
import * as browser from "./browser"
|
|
3
|
+
import {domIndex, selectionCollapsed, parentNode,
|
|
4
|
+
DOMSelectionRange, DOMNode, isText, isElt} from "./dom"
|
|
5
|
+
import {hasFocusAndSelection, selectionFromDOM} from "./selection"
|
|
6
|
+
import {EditorView} from "./index"
|
|
7
|
+
import {readDOMChange} from "./domchange"
|
|
8
|
+
import {Damage, TextViewDesc, ViewDesc} from "./viewdesc"
|
|
9
|
+
|
|
10
|
+
const observeOptions = {
|
|
11
|
+
childList: true,
|
|
12
|
+
characterData: true,
|
|
13
|
+
characterDataOldValue: true,
|
|
14
|
+
attributes: true,
|
|
15
|
+
attributeOldValue: true,
|
|
16
|
+
subtree: true
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class DOMObserver {
|
|
20
|
+
queue: MutationRecord[] = []
|
|
21
|
+
flushingSoon = -1
|
|
22
|
+
observer: MutationObserver
|
|
23
|
+
suppressingSelectionUpdates = false
|
|
24
|
+
lastChangedTextNode: Text | null = null
|
|
25
|
+
started = false
|
|
26
|
+
|
|
27
|
+
constructor(readonly view: EditorView) {
|
|
28
|
+
this.observer = new window.MutationObserver(mutations => {
|
|
29
|
+
for (let i = 0; i < mutations.length; i++) this.queue.push(mutations[i])
|
|
30
|
+
if (browser.safari && view.composing && mutations.some(
|
|
31
|
+
m => m.type == "childList" && m.target.nodeName == "TR")) {
|
|
32
|
+
// Safari does weird stuff when finishing a composition in a
|
|
33
|
+
// table cell, which tends to involve inserting inappropriate
|
|
34
|
+
// nodes in the table row.
|
|
35
|
+
view.input.badSafariComposition = true
|
|
36
|
+
this.flushSoon()
|
|
37
|
+
} else {
|
|
38
|
+
this.flush()
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
flushSoon() {
|
|
44
|
+
if (this.flushingSoon < 0)
|
|
45
|
+
this.flushingSoon = window.setTimeout(() => { this.flushingSoon = -1; this.flush() }, 20)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
forceFlush(always = false) {
|
|
49
|
+
if (this.flushingSoon > -1) {
|
|
50
|
+
window.clearTimeout(this.flushingSoon)
|
|
51
|
+
this.flushingSoon = -1
|
|
52
|
+
this.flush(true)
|
|
53
|
+
} else if (always) {
|
|
54
|
+
this.flush(true)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
start() {
|
|
59
|
+
this.observer.takeRecords()
|
|
60
|
+
this.observer.observe(this.view.dom, observeOptions)
|
|
61
|
+
this.started = true
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
stop() {
|
|
65
|
+
let take = this.observer.takeRecords()
|
|
66
|
+
if (take.length) {
|
|
67
|
+
for (let i = 0; i < take.length; i++) this.queue.push(take[i])
|
|
68
|
+
window.setTimeout(() => this.flush(), 20)
|
|
69
|
+
}
|
|
70
|
+
this.observer.disconnect()
|
|
71
|
+
this.started = false
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
stopped<T>(f: () => T): T {
|
|
75
|
+
if (!this.started) return f()
|
|
76
|
+
this.stop()
|
|
77
|
+
try { return f() }
|
|
78
|
+
finally { this.start() }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
ignoreSelectionChange(sel: DOMSelectionRange) {
|
|
82
|
+
if (!sel.focusNode) return true
|
|
83
|
+
let ancestors: Set<Node> = new Set, container: DOMNode | undefined
|
|
84
|
+
for (let scan: DOMNode | null = sel.focusNode; scan; scan = parentNode(scan)) ancestors.add(scan)
|
|
85
|
+
for (let scan = sel.anchorNode; scan; scan = parentNode(scan)) if (ancestors.has(scan)) {
|
|
86
|
+
container = scan
|
|
87
|
+
break
|
|
88
|
+
}
|
|
89
|
+
let desc = container && this.view.docView.nearestDesc(container)
|
|
90
|
+
if (desc && desc.ignoreMutation({
|
|
91
|
+
type: "selection",
|
|
92
|
+
target: isText(container!) ? container!.parentNode! : container!
|
|
93
|
+
})) {
|
|
94
|
+
this.view.domSel.ignore(sel)
|
|
95
|
+
return true
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
pendingRecords() {
|
|
100
|
+
for (let mut of this.observer.takeRecords()) this.queue.push(mut)
|
|
101
|
+
return this.queue
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
flush(readSel = true) {
|
|
105
|
+
let {view} = this
|
|
106
|
+
if (!view.docView || this.flushingSoon > -1) return
|
|
107
|
+
let mutations = this.pendingRecords()
|
|
108
|
+
if (mutations.length) this.queue = []
|
|
109
|
+
if (readSel) view.domSel.read()
|
|
110
|
+
|
|
111
|
+
let selChange = !view.domSel.suppress && !view.domSel.synced && !!hasFocusAndSelection(view) &&
|
|
112
|
+
!this.ignoreSelectionChange(view.domSel.range)
|
|
113
|
+
|
|
114
|
+
let from = -1, to = -1, typeOver = false, added: Node[] = []
|
|
115
|
+
if (view.editable) {
|
|
116
|
+
for (let i = 0; i < mutations.length; i++) {
|
|
117
|
+
let result = this.registerMutation(mutations[i], added)
|
|
118
|
+
if (result) {
|
|
119
|
+
from = from < 0 ? result.from : Math.min(result.from, from)
|
|
120
|
+
to = to < 0 ? result.to : Math.max(result.to, to)
|
|
121
|
+
if (result.typeOver) typeOver = true
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (added.some(n => n.nodeName == "BR") &&
|
|
127
|
+
(view.input.lastKeyCode == 8 || view.input.lastKeyCode == 46 ||
|
|
128
|
+
browser.chrome && (view.composing || view.input.compositionEndedAt > Date.now() - 50) &&
|
|
129
|
+
mutations.some(m => m.type == "childList" && m.removedNodes.length))) {
|
|
130
|
+
// Browsers sometimes insert a bogus break node if you
|
|
131
|
+
// backspace out the last bit of text before an inline-flex node (#1552)
|
|
132
|
+
for (let node of added) if (node.nodeName == "BR" && node.parentNode) {
|
|
133
|
+
let after = node.nextSibling
|
|
134
|
+
while (after && isElt(after)) {
|
|
135
|
+
if (after.contentEditable == "false") {
|
|
136
|
+
node.parentNode.removeChild(node)
|
|
137
|
+
break
|
|
138
|
+
}
|
|
139
|
+
after = after.firstChild
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
} else if (browser.gecko && added.length) {
|
|
143
|
+
let brs = added.filter(n => n.nodeName == "BR") as HTMLElement[]
|
|
144
|
+
if (brs.length == 2) {
|
|
145
|
+
let [a, b] = brs
|
|
146
|
+
if (a.parentNode && a.parentNode.parentNode == b.parentNode) b.remove()
|
|
147
|
+
else a.remove()
|
|
148
|
+
} else {
|
|
149
|
+
let {focusNode} = view.domSel.range
|
|
150
|
+
for (let br of brs) {
|
|
151
|
+
let parent = br.parentNode
|
|
152
|
+
if (parent && parent.nodeName == "LI" && (!focusNode || blockParent(view, focusNode) != parent))
|
|
153
|
+
br.remove()
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
let newSel: Selection | null = null
|
|
159
|
+
// If it looks like the browser has reset the selection to the
|
|
160
|
+
// start of the document after focus, restore the selection from
|
|
161
|
+
// the state
|
|
162
|
+
if (from < 0 && selChange && view.domSel.active && view.input.lastFocus > Date.now() - 200 &&
|
|
163
|
+
view.input.lastTouch < Date.now() - 300 &&
|
|
164
|
+
selectionCollapsed(view.domSel.range) && (newSel = selectionFromDOM(view, view.domSel.range)) &&
|
|
165
|
+
newSel.eq(Selection.near(view.state.doc.resolve(0), 1))) {
|
|
166
|
+
view.input.lastFocus = 0
|
|
167
|
+
view.domSel.sync()
|
|
168
|
+
view.scrollToSelection()
|
|
169
|
+
} else if (from > -1 || selChange) {
|
|
170
|
+
if (from > -1) checkCSS(view)
|
|
171
|
+
if (view.input.badSafariComposition) {
|
|
172
|
+
view.input.badSafariComposition = false
|
|
173
|
+
fixUpBadSafariComposition(view, added)
|
|
174
|
+
}
|
|
175
|
+
readDOMChange(this.view, from, to, selChange, typeOver, added)
|
|
176
|
+
if (view.docView && view.docView.damage) view.updateState(view.state)
|
|
177
|
+
else if (selChange) view.domSel.sync()
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
registerMutation(mut: MutationRecord, added: Node[]) {
|
|
182
|
+
// Ignore mutations inside nodes that were already noted as inserted
|
|
183
|
+
if (added.indexOf(mut.target) > -1) return null
|
|
184
|
+
let desc: ViewDesc | undefined
|
|
185
|
+
for (let scan: DOMNode | null = mut.target; scan; scan = scan.parentNode) {
|
|
186
|
+
if (desc = this.view.docView.getDesc(scan)) break
|
|
187
|
+
}
|
|
188
|
+
if (mut.type == "attributes" &&
|
|
189
|
+
(desc == this.view.docView || mut.attributeName == "contenteditable" ||
|
|
190
|
+
// Firefox sometimes fires spurious events for null/empty styles
|
|
191
|
+
(mut.attributeName == "style" && !mut.oldValue && !(mut.target as HTMLElement).getAttribute("style"))))
|
|
192
|
+
return null
|
|
193
|
+
if (!desc || desc.ignoreMutation(mut)) return null
|
|
194
|
+
|
|
195
|
+
if (mut.type == "childList") {
|
|
196
|
+
for (let i = 0; i < mut.addedNodes.length; i++) {
|
|
197
|
+
let node = mut.addedNodes[i]
|
|
198
|
+
added.push(node)
|
|
199
|
+
if (isText(node)) this.lastChangedTextNode = node
|
|
200
|
+
}
|
|
201
|
+
if (desc.contentDOM && desc.contentDOM != desc.dom && !desc.contentDOM.contains(mut.target)) {
|
|
202
|
+
desc.markDamage(Damage.Structure)
|
|
203
|
+
return {from: desc.posBefore, to: desc.posAfter}
|
|
204
|
+
}
|
|
205
|
+
let prev = mut.previousSibling, next = mut.nextSibling
|
|
206
|
+
let fromOffset = prev && prev.parentNode == mut.target
|
|
207
|
+
? domIndex(prev) + 1 : 0
|
|
208
|
+
let from = desc.localPosFromDOM(mut.target, fromOffset, -1)
|
|
209
|
+
let toOffset = next && next.parentNode == mut.target
|
|
210
|
+
? domIndex(next) : mut.target.childNodes.length
|
|
211
|
+
let to = desc.localPosFromDOM(mut.target, toOffset, 1)
|
|
212
|
+
desc.markDamage(desc.contentDOM?.contains(mut.target) ? Damage.ChildList : Damage.Structure)
|
|
213
|
+
return {from, to}
|
|
214
|
+
} else if (mut.type == "attributes") {
|
|
215
|
+
desc.markDamage(Damage.Structure)
|
|
216
|
+
return {from: desc.posAtStart - desc.border, to: desc.posAtEnd + desc.border}
|
|
217
|
+
} else { // "characterData"
|
|
218
|
+
this.lastChangedTextNode = mut.target as Text
|
|
219
|
+
desc.markDamage(desc instanceof TextViewDesc && mut.target == desc.nodeDOM ? Damage.Text : Damage.Structure)
|
|
220
|
+
return {
|
|
221
|
+
from: desc.posAtStart,
|
|
222
|
+
to: desc.posAtEnd,
|
|
223
|
+
// An event was generated for a text change that didn't change
|
|
224
|
+
// any text. Mark the dom change to fall back to assuming the
|
|
225
|
+
// selection was typed over with an identical value if it can't
|
|
226
|
+
// find another change.
|
|
227
|
+
typeOver: mut.target.nodeValue == mut.oldValue
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
let cssChecked: WeakMap<EditorView, null> = new WeakMap()
|
|
234
|
+
let cssCheckWarned: boolean = false
|
|
235
|
+
|
|
236
|
+
function checkCSS(view: EditorView) {
|
|
237
|
+
if (cssChecked.has(view)) return
|
|
238
|
+
cssChecked.set(view, null)
|
|
239
|
+
if (['normal', 'nowrap', 'pre-line'].indexOf(getComputedStyle(view.dom).whiteSpace) !== -1) {
|
|
240
|
+
view.requiresGeckoHackNode = browser.gecko
|
|
241
|
+
if (cssCheckWarned) return
|
|
242
|
+
console["warn"]("ProseMirror expects the CSS white-space property to be set, preferably to 'pre-wrap'. It is recommended to load style/prosemirror.css from the prosemirror-view package.")
|
|
243
|
+
cssCheckWarned = true
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function blockParent(view: EditorView, node: DOMNode): Node | null {
|
|
248
|
+
for (let p = node.parentNode; p && p != view.dom; p = p.parentNode) {
|
|
249
|
+
let desc = view.docView.nearestDesc(p, true)
|
|
250
|
+
if (desc && desc.node.isBlock) return p
|
|
251
|
+
}
|
|
252
|
+
return null
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// Kludge for a Safari bug where, on ending a composition in an
|
|
256
|
+
// otherwise empty table cell, it randomly moves the composed text
|
|
257
|
+
// into the table row around that cell, greatly confusing everything
|
|
258
|
+
// (#188).
|
|
259
|
+
function fixUpBadSafariComposition(view: EditorView, addedNodes: readonly DOMNode[]) {
|
|
260
|
+
let {focusNode, focusOffset} = view.domSel.range
|
|
261
|
+
for (let node of addedNodes) {
|
|
262
|
+
if (node.parentNode?.nodeName == "TR") {
|
|
263
|
+
let nextCell = node.nextSibling
|
|
264
|
+
while (nextCell && (nextCell.nodeName != "TD" && nextCell.nodeName != "TH")) nextCell = nextCell.nextSibling
|
|
265
|
+
if (nextCell) {
|
|
266
|
+
let parent = nextCell
|
|
267
|
+
for (;;) {
|
|
268
|
+
let first = parent.firstChild
|
|
269
|
+
if (!first || !isElt(first) || first.contentEditable == "false" || /^(BR|IMG)$/.test(first.nodeName)) break
|
|
270
|
+
parent = first
|
|
271
|
+
}
|
|
272
|
+
parent.insertBefore(node, parent.firstChild)
|
|
273
|
+
if (focusNode == node) view.domSelection()!.collapse(node, focusOffset)
|
|
274
|
+
} else {
|
|
275
|
+
node.parentNode.removeChild(node)
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|