@bitbindle/prosemirror-view 0.0.0-stage → 1.44.1
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/bitbindle-prosemirror-view-1.44.1.tgz +0 -0
- package/package.json +53 -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
package/src/dom.ts
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
export type DOMNode = InstanceType<typeof window.Node>
|
|
2
|
+
export type DOMSelection = InstanceType<typeof window.Selection>
|
|
3
|
+
export type DOMSelectionRange = {
|
|
4
|
+
focusNode: DOMNode | null, focusOffset: number,
|
|
5
|
+
anchorNode: DOMNode | null, anchorOffset: number
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const domIndex = function(node: Node) {
|
|
9
|
+
for (var index = 0;; index++) {
|
|
10
|
+
node = node.previousSibling!
|
|
11
|
+
if (!node) return index
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function parentNode(node: Node): Node | null {
|
|
16
|
+
let parent = (node as HTMLSlotElement).assignedSlot || node.parentNode
|
|
17
|
+
return parent && parent.nodeType == 11 ? (parent as ShadowRoot).host : parent
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let reusedRange: Range | null = null
|
|
21
|
+
|
|
22
|
+
// Note that this will always return the same range, because DOM range
|
|
23
|
+
// objects are every expensive, and keep slowing down subsequent DOM
|
|
24
|
+
// updates, for some reason.
|
|
25
|
+
export function textRange(node: Text, from?: number, to?: number) {
|
|
26
|
+
let range = reusedRange || (reusedRange = document.createRange())
|
|
27
|
+
range.setEnd(node, to == null ? node.nodeValue!.length : to)
|
|
28
|
+
range.setStart(node, from || 0)
|
|
29
|
+
return range
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function clearReusedRange() {
|
|
33
|
+
reusedRange = null
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Scans forward and backward through DOM positions equivalent to the
|
|
37
|
+
// given one to see if the two are in the same place (i.e. after a
|
|
38
|
+
// text node vs at the end of that text node)
|
|
39
|
+
export function isEquivalentPosition(node: Node, off: number, targetNode: Node | null, targetOff: number) {
|
|
40
|
+
return targetNode && (scanFor(node, off, targetNode, targetOff, -1) ||
|
|
41
|
+
scanFor(node, off, targetNode, targetOff, 1))
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const atomElements = /^(img|br|input|textarea|hr)$/i
|
|
45
|
+
|
|
46
|
+
function scanFor(node: Node, off: number, targetNode: Node, targetOff: number, dir: number) {
|
|
47
|
+
for (;;) {
|
|
48
|
+
if (node == targetNode && off == targetOff) return true
|
|
49
|
+
if (off == (dir < 0 ? 0 : nodeSize(node))) {
|
|
50
|
+
let parent = node.parentNode
|
|
51
|
+
if (!parent || !isElt(parent) || hasNodeDesc(node) || atomElements.test(node.nodeName) ||
|
|
52
|
+
(node as HTMLElement).contentEditable == "false")
|
|
53
|
+
return false
|
|
54
|
+
off = domIndex(node) + (dir < 0 ? 0 : 1)
|
|
55
|
+
node = parent
|
|
56
|
+
} else if (isElt(node)) {
|
|
57
|
+
let child = node.childNodes[off + (dir < 0 ? -1 : 0)]
|
|
58
|
+
if (isElt(child) && child.contentEditable == "false") {
|
|
59
|
+
if (child.pmViewDesc?.ignoreForSelection) off += dir
|
|
60
|
+
else return false
|
|
61
|
+
} else if (hasNodeDesc(child) || atomElements.test(child.nodeName)) {
|
|
62
|
+
return false
|
|
63
|
+
} else {
|
|
64
|
+
node = child
|
|
65
|
+
off = dir < 0 ? nodeSize(node) : 0
|
|
66
|
+
}
|
|
67
|
+
} else {
|
|
68
|
+
return false
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function nodeSize(node: Node) {
|
|
74
|
+
return node.nodeType == 3 ? node.nodeValue!.length : node.childNodes.length
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function isElt(node: Node): node is HTMLElement { return node.nodeType == 1 }
|
|
78
|
+
export function isText(node: Node): node is Text { return node.nodeType == 3 }
|
|
79
|
+
|
|
80
|
+
export function contains(parent: Node, child: Node | null) {
|
|
81
|
+
return child ? parent.contains(isElt(child) ? child : child.parentNode) : false
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function textNodeBefore(node: Node, offset: number) {
|
|
85
|
+
for (;;) {
|
|
86
|
+
if (isText(node) && offset) return node
|
|
87
|
+
if (isElt(node) && offset > 0) {
|
|
88
|
+
if ((node as HTMLElement).contentEditable == "false") return null
|
|
89
|
+
node = node.childNodes[offset - 1]
|
|
90
|
+
offset = nodeSize(node)
|
|
91
|
+
} else if (node.parentNode && !hasNodeDesc(node, true)) {
|
|
92
|
+
offset = domIndex(node)
|
|
93
|
+
node = node.parentNode
|
|
94
|
+
} else {
|
|
95
|
+
return null
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function textNodeAfter(node: Node, offset: number) {
|
|
101
|
+
for (;;) {
|
|
102
|
+
if (isText(node) && offset < node.nodeValue!.length) return node as Text
|
|
103
|
+
if (isElt(node) && offset < node.childNodes.length) {
|
|
104
|
+
if (node.contentEditable == "false") return null
|
|
105
|
+
node = node.childNodes[offset]
|
|
106
|
+
offset = 0
|
|
107
|
+
} else if (node.parentNode && !hasNodeDesc(node, true)) {
|
|
108
|
+
offset = domIndex(node) + 1
|
|
109
|
+
node = node.parentNode
|
|
110
|
+
} else {
|
|
111
|
+
return null
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function isOnEdge(node: Node, offset: number, parent: Node) {
|
|
117
|
+
for (let atStart = offset == 0, atEnd = offset == nodeSize(node); atStart || atEnd;) {
|
|
118
|
+
if (node == parent) return true
|
|
119
|
+
let index = domIndex(node)
|
|
120
|
+
node = node.parentNode!
|
|
121
|
+
if (!node) return false
|
|
122
|
+
atStart = atStart && index == 0
|
|
123
|
+
atEnd = atEnd && index == nodeSize(node)
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function hasNodeDesc(dom: Node, isBlock = false) {
|
|
128
|
+
let desc
|
|
129
|
+
for (let cur: Node | null = dom; cur; cur = cur.parentNode) if (desc = cur.pmViewDesc) break
|
|
130
|
+
return desc && desc.node && (!isBlock || desc.node.isBlock) && (desc.dom == dom || desc.contentDOM == dom)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Work around Chrome issue https://bugs.chromium.org/p/chromium/issues/detail?id=447523
|
|
134
|
+
// (isCollapsed inappropriately returns true in shadow dom)
|
|
135
|
+
export function selectionCollapsed(domSel: DOMSelectionRange) {
|
|
136
|
+
return domSel.focusNode && isEquivalentPosition(domSel.focusNode, domSel.focusOffset,
|
|
137
|
+
domSel.anchorNode!, domSel.anchorOffset)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function writeSelection(sel: DOMSelection, range: DOMSelectionRange) {
|
|
141
|
+
sel.collapse(range.anchorNode, range.anchorOffset)
|
|
142
|
+
if (range.focusNode != range.anchorNode || range.focusOffset != range.anchorOffset)
|
|
143
|
+
sel.extend(range.focusNode!, range.focusOffset)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function keyEvent(keyCode: number, key: string) {
|
|
147
|
+
let event = document.createEvent("Event") as KeyboardEvent
|
|
148
|
+
event.initEvent("keydown", true, true)
|
|
149
|
+
;(event as any).keyCode = keyCode
|
|
150
|
+
;(event as any).key = (event as any).code = key
|
|
151
|
+
return event
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function deepActiveElement(doc: Document) {
|
|
155
|
+
let elt = doc.activeElement
|
|
156
|
+
while (elt && elt.shadowRoot) elt = elt.shadowRoot.activeElement
|
|
157
|
+
return elt
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function caretFromPoint(doc: Document, x: number, y: number): {node: Node, offset: number} | undefined {
|
|
161
|
+
if ((doc as any).caretPositionFromPoint) {
|
|
162
|
+
try { // Firefox throws for this call in hard-to-predict circumstances (#994)
|
|
163
|
+
let pos = (doc as any).caretPositionFromPoint(x, y)
|
|
164
|
+
// Clip the offset, because Chrome will return a text offset
|
|
165
|
+
// into <input> nodes, which can't be treated as a regular DOM
|
|
166
|
+
// offset
|
|
167
|
+
if (pos) return {node: pos.offsetNode, offset: Math.min(nodeSize(pos.offsetNode), pos.offset)}
|
|
168
|
+
} catch (_) {}
|
|
169
|
+
}
|
|
170
|
+
if (doc.caretRangeFromPoint) {
|
|
171
|
+
let range = doc.caretRangeFromPoint(x, y)
|
|
172
|
+
if (range) return {node: range.startContainer, offset: Math.min(nodeSize(range.startContainer), range.startOffset)}
|
|
173
|
+
}
|
|
174
|
+
}
|
package/src/domchange.ts
ADDED
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import {Fragment, DOMParser, TagParseRule, Node, Mark, ResolvedPos} from "prosemirror-model"
|
|
2
|
+
import {TextSelection, Transaction} from "prosemirror-state"
|
|
3
|
+
|
|
4
|
+
import {selectionBetween, selectionFromDOM} from "./selection"
|
|
5
|
+
import {selectionCollapsed, DOMNode, isElt, contains} from "./dom"
|
|
6
|
+
import * as browser from "./browser"
|
|
7
|
+
import {EditorView} from "./index"
|
|
8
|
+
import {dispatchKey} from "./input"
|
|
9
|
+
|
|
10
|
+
// Note that all referencing and parsing is done with the
|
|
11
|
+
// start-of-operation selection and document, since that's the one
|
|
12
|
+
// that the DOM represents. If any changes came in in the meantime,
|
|
13
|
+
// the modification is mapped over those before it is applied, in
|
|
14
|
+
// readDOMChange.
|
|
15
|
+
|
|
16
|
+
function parseBetween(view: EditorView, from_: number, to_: number, addedNodes: readonly DOMNode[]) {
|
|
17
|
+
let {node: parent, fromOffset, toOffset, from, to} = view.docView.parseRange(from_, to_)
|
|
18
|
+
|
|
19
|
+
let domSel = view.domSel.range
|
|
20
|
+
let find: {node: DOMNode, offset: number, pos?: number}[] | undefined
|
|
21
|
+
let anchor = domSel.anchorNode
|
|
22
|
+
if (anchor && contains(view.dom, anchor)) {
|
|
23
|
+
find = [{node: anchor, offset: domSel.anchorOffset}]
|
|
24
|
+
if (!selectionCollapsed(domSel))
|
|
25
|
+
find.push({node: domSel.focusNode!, offset: domSel.focusOffset})
|
|
26
|
+
}
|
|
27
|
+
// Work around issue in Chrome where backspacing sometimes replaces
|
|
28
|
+
// the deleted content with a random BR node (issues #799, #831)
|
|
29
|
+
if (browser.chrome && view.input.lastKeyCode === 8) {
|
|
30
|
+
for (let off = toOffset; off > fromOffset; off--) {
|
|
31
|
+
let node = parent.childNodes[off - 1], desc = node.pmViewDesc
|
|
32
|
+
if (node.nodeName == "BR" && !desc) { toOffset = off; break }
|
|
33
|
+
if (!desc || desc.size) break
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
let startDoc = view.state.doc
|
|
37
|
+
let parser = view.someProp("domParser") || DOMParser.fromSchema(view.state.schema)
|
|
38
|
+
let $from = startDoc.resolve(from)
|
|
39
|
+
|
|
40
|
+
let sel = null, doc = parser.parse(parent, {
|
|
41
|
+
topNode: $from.parent,
|
|
42
|
+
topMatch: $from.parent.contentMatchAt($from.index()),
|
|
43
|
+
topOpen: true,
|
|
44
|
+
from: fromOffset,
|
|
45
|
+
to: toOffset,
|
|
46
|
+
preserveWhitespace: $from.parent.type.whitespace == "pre" ? "full" : true,
|
|
47
|
+
findPositions: find,
|
|
48
|
+
ruleFromNode: ruleFromNode(addedNodes),
|
|
49
|
+
context: $from
|
|
50
|
+
})
|
|
51
|
+
if (find && find[0].pos != null) {
|
|
52
|
+
let anchor = find[0].pos, head = find[1] && find[1].pos
|
|
53
|
+
if (head == null) head = anchor
|
|
54
|
+
sel = {anchor: anchor + from, head: head + from}
|
|
55
|
+
}
|
|
56
|
+
return {doc, sel, from, to}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const ruleFromNode = (added: readonly DOMNode[]) => (dom: DOMNode): Omit<TagParseRule, "tag"> | null => {
|
|
60
|
+
let desc = dom.pmViewDesc
|
|
61
|
+
if (desc) {
|
|
62
|
+
return desc.parseRule(added)
|
|
63
|
+
} else if (dom.nodeName == "BR" && dom.parentNode) {
|
|
64
|
+
// Safari replaces the list item or table cell with a BR
|
|
65
|
+
// directly in the list node (?!) if you delete the last
|
|
66
|
+
// character in a list item or table cell (#708, #862)
|
|
67
|
+
if (browser.safari && /^(ul|ol)$/i.test(dom.parentNode.nodeName)) {
|
|
68
|
+
let skip = document.createElement("div")
|
|
69
|
+
skip.appendChild(document.createElement("li"))
|
|
70
|
+
return {skip} as any
|
|
71
|
+
} else if (dom.parentNode.lastChild == dom || browser.safari && /^(tr|table)$/i.test(dom.parentNode.nodeName)) {
|
|
72
|
+
return {ignore: true}
|
|
73
|
+
}
|
|
74
|
+
} else if (dom.nodeName == "IMG" && (dom as HTMLElement).getAttribute("mark-placeholder")) {
|
|
75
|
+
return {ignore: true}
|
|
76
|
+
}
|
|
77
|
+
return null
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const isInline = /^(a|abbr|acronym|b|bd[io]|big|br|button|cite|code|data(list)?|del|dfn|em|i|img|ins|kbd|label|map|mark|meter|output|q|ruby|s|samp|small|span|strong|su[bp]|time|u|tt|var)$/i
|
|
81
|
+
|
|
82
|
+
export function readDOMChange(view: EditorView, from: number, to: number, selChange: boolean,
|
|
83
|
+
typeOver: boolean, addedNodes: readonly DOMNode[]) {
|
|
84
|
+
let compositionID = view.input.compositionPendingChanges || (view.composing ? view.input.compositionID : 0)
|
|
85
|
+
view.input.compositionPendingChanges = 0
|
|
86
|
+
|
|
87
|
+
if (from < 0) {
|
|
88
|
+
let origin = view.input.lastSelectionTime > Date.now() - 50 ? view.input.lastSelectionOrigin : null
|
|
89
|
+
let newSel = selectionFromDOM(view, view.domSel.range, origin)
|
|
90
|
+
if (newSel && !view.state.selection.eq(newSel) && view.domSel.active) {
|
|
91
|
+
if (browser.chrome && browser.android &&
|
|
92
|
+
view.input.lastKeyCode === 13 && Date.now() - 100 < view.input.lastKeyCodeTime &&
|
|
93
|
+
dispatchKey(view, "Enter"))
|
|
94
|
+
return
|
|
95
|
+
let tr = view.state.tr.setSelection(newSel)
|
|
96
|
+
if (origin == "pointer") tr.setMeta("pointer", true)
|
|
97
|
+
else if (origin == "key") tr.scrollIntoView()
|
|
98
|
+
if (compositionID) tr.setMeta("composition", compositionID)
|
|
99
|
+
view.dispatch(tr)
|
|
100
|
+
}
|
|
101
|
+
return
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
let $before = view.state.doc.resolve(from)
|
|
105
|
+
let shared = $before.sharedDepth(to)
|
|
106
|
+
from = $before.before(shared + 1)
|
|
107
|
+
to = view.state.doc.resolve(to).after(shared + 1)
|
|
108
|
+
|
|
109
|
+
let sel = view.state.selection
|
|
110
|
+
let parse = parseBetween(view, from, to, addedNodes)
|
|
111
|
+
|
|
112
|
+
let doc = view.state.doc, compare = doc.slice(parse.from, parse.to)
|
|
113
|
+
let preferredPos, preferredSide: "start" | "end"
|
|
114
|
+
// Prefer anchoring to end when Backspace is pressed
|
|
115
|
+
if (view.input.lastKeyCode === 8 && Date.now() - 100 < view.input.lastKeyCodeTime) {
|
|
116
|
+
preferredPos = view.state.selection.to
|
|
117
|
+
preferredSide = "end"
|
|
118
|
+
} else {
|
|
119
|
+
preferredPos = view.state.selection.from
|
|
120
|
+
preferredSide = "start"
|
|
121
|
+
}
|
|
122
|
+
view.input.lastKeyCode = null
|
|
123
|
+
|
|
124
|
+
let change = findDiff(compare.content, parse.doc.content, parse.from, preferredPos, preferredSide)
|
|
125
|
+
if (change) view.input.domChangeCount++
|
|
126
|
+
if ((browser.ios && view.input.lastIOSEnter > Date.now() - 225 || browser.android) &&
|
|
127
|
+
addedNodes.some(n => isElt(n) && !isInline.test(n.nodeName)) &&
|
|
128
|
+
(!change || change.endA >= change.endB) &&
|
|
129
|
+
dispatchKey(view, "Enter")) {
|
|
130
|
+
view.input.lastIOSEnter = 0
|
|
131
|
+
return
|
|
132
|
+
}
|
|
133
|
+
if (!change) {
|
|
134
|
+
if (typeOver && sel instanceof TextSelection && !sel.empty && sel.$head.sameParent(sel.$anchor) &&
|
|
135
|
+
!view.composing && !(parse.sel && parse.sel.anchor != parse.sel.head)) {
|
|
136
|
+
change = {start: sel.from, endA: sel.to, endB: sel.to}
|
|
137
|
+
} else {
|
|
138
|
+
if (parse.sel) {
|
|
139
|
+
let sel = resolveSelection(view, view.state.doc, parse.sel)
|
|
140
|
+
if (sel && !sel.eq(view.state.selection)) {
|
|
141
|
+
let tr = view.state.tr.setSelection(sel)
|
|
142
|
+
if (compositionID) tr.setMeta("composition", compositionID)
|
|
143
|
+
view.dispatch(tr)
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Handle the case where overwriting a selection by typing matches
|
|
151
|
+
// the start or end of the selected content, creating a change
|
|
152
|
+
// that's smaller than what was actually overwritten.
|
|
153
|
+
if (view.state.selection.from < view.state.selection.to &&
|
|
154
|
+
change.start == change.endB &&
|
|
155
|
+
view.state.selection instanceof TextSelection) {
|
|
156
|
+
if (change.start > view.state.selection.from && change.start <= view.state.selection.from + 2 &&
|
|
157
|
+
view.state.selection.from >= parse.from) {
|
|
158
|
+
change.start = view.state.selection.from
|
|
159
|
+
} else if (change.endA < view.state.selection.to && change.endA >= view.state.selection.to - 2 &&
|
|
160
|
+
view.state.selection.to <= parse.to) {
|
|
161
|
+
change.endB += (view.state.selection.to - change.endA)
|
|
162
|
+
change.endA = view.state.selection.to
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
let $from = parse.doc.resolveNoCache(change.start - parse.from)
|
|
167
|
+
let $to = parse.doc.resolveNoCache(change.endB - parse.from)
|
|
168
|
+
let $fromA = doc.resolve(change.start)
|
|
169
|
+
let inlineChange = $from.sameParent($to) && $from.parent.inlineContent && $fromA.end() >= change.endA
|
|
170
|
+
// If this looks like the effect of pressing Enter (or was recorded
|
|
171
|
+
// as being an iOS enter press), just dispatch an Enter key instead.
|
|
172
|
+
if (((browser.ios && view.input.lastIOSEnter > Date.now() - 225 &&
|
|
173
|
+
(!inlineChange || addedNodes.some(n => n.nodeName == "DIV" || n.nodeName == "P"))) ||
|
|
174
|
+
(!inlineChange && looksLikeEnter(parse.doc, $from.pos, $to.pos))) &&
|
|
175
|
+
dispatchKey(view, "Enter")) {
|
|
176
|
+
view.input.lastIOSEnter = 0
|
|
177
|
+
return
|
|
178
|
+
}
|
|
179
|
+
// Same for backspace
|
|
180
|
+
if (view.state.selection.anchor > change.start &&
|
|
181
|
+
(inlineChange ? change.endA > change.start && change.endB == change.start
|
|
182
|
+
: looksLikeBlockJoin(doc, change.start, change.endA, $from, $to)) &&
|
|
183
|
+
dispatchKey(view, "Backspace")) {
|
|
184
|
+
if (browser.android && browser.chrome) view.domSel.suppressChanges()
|
|
185
|
+
return
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Chrome will occasionally, during composition, delete the
|
|
189
|
+
// entire composition and then immediately insert it again. This is
|
|
190
|
+
// used to detect that situation.
|
|
191
|
+
if (browser.chrome && change.endB == change.start)
|
|
192
|
+
view.input.lastChromeDelete = Date.now()
|
|
193
|
+
|
|
194
|
+
// This tries to detect Android virtual keyboard
|
|
195
|
+
// enter-and-pick-suggestion action. That sometimes (see issue
|
|
196
|
+
// #1059) first fires a DOM mutation, before moving the selection to
|
|
197
|
+
// the newly created block. And then, because ProseMirror cleans up
|
|
198
|
+
// the DOM selection, it gives up moving the selection entirely,
|
|
199
|
+
// leaving the cursor in the wrong place. When that happens, we drop
|
|
200
|
+
// the new paragraph from the initial change, and fire a simulated
|
|
201
|
+
// enter key afterwards.
|
|
202
|
+
if (browser.android && !inlineChange && $from.start() != $to.start() && $to.parentOffset == 0 && $from.depth == $to.depth &&
|
|
203
|
+
parse.sel && parse.sel.anchor == parse.sel.head && parse.sel.head == change.endA) {
|
|
204
|
+
change.endB -= 2
|
|
205
|
+
$to = parse.doc.resolveNoCache(change.endB - parse.from)
|
|
206
|
+
setTimeout(() => dispatchKey(view, "Enter"), 20)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
let chFrom = change.start, chTo = change.endA
|
|
210
|
+
|
|
211
|
+
let mkTr = (base?: Transaction) => {
|
|
212
|
+
let tr = base || view.state.tr.replace(chFrom, chTo, parse.doc.slice(change!.start - parse.from,
|
|
213
|
+
change!.endB - parse.from))
|
|
214
|
+
if (parse.sel) {
|
|
215
|
+
let sel = resolveSelection(view, tr.doc, parse.sel)
|
|
216
|
+
// Chrome will sometimes, during composition, report the
|
|
217
|
+
// selection in the wrong place. If it looks like that is
|
|
218
|
+
// happening, don't update the selection.
|
|
219
|
+
if (sel && !(browser.chrome && view.composing && sel.empty &&
|
|
220
|
+
(change!.start != change!.endB || view.input.lastChromeDelete < Date.now() - 100) &&
|
|
221
|
+
(sel.head == chFrom || sel.head == tr.mapping.map(chTo) - 1)))
|
|
222
|
+
tr.setSelection(sel)
|
|
223
|
+
}
|
|
224
|
+
if (compositionID) tr.setMeta("composition", compositionID)
|
|
225
|
+
return tr.scrollIntoView()
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
let markChange
|
|
229
|
+
if (inlineChange) {
|
|
230
|
+
if ($from.pos == $to.pos) { // Deletion
|
|
231
|
+
let tr = mkTr(view.state.tr.delete(chFrom, chTo))
|
|
232
|
+
let marks = doc.resolve(change.start).marksAcross(doc.resolve(change.endA))
|
|
233
|
+
if (marks) tr.ensureMarks(marks)
|
|
234
|
+
view.dispatch(tr)
|
|
235
|
+
} else if ( // Adding or removing a mark
|
|
236
|
+
change.endA == change.endB &&
|
|
237
|
+
(markChange = isMarkChange($from.parent.content.cut($from.parentOffset, $to.parentOffset),
|
|
238
|
+
$fromA.parent.content.cut($fromA.parentOffset, change.endA - $fromA.start())))
|
|
239
|
+
) {
|
|
240
|
+
let tr = mkTr(view.state.tr)
|
|
241
|
+
if (markChange.type == "add") tr.addMark(chFrom, chTo, markChange.mark)
|
|
242
|
+
else tr.removeMark(chFrom, chTo, markChange.mark)
|
|
243
|
+
view.dispatch(tr)
|
|
244
|
+
} else if ($from.parent.child($from.index()).isText && $from.index() == $to.index() - ($to.textOffset ? 0 : 1)) {
|
|
245
|
+
// Both positions in the same text node -- simply insert text
|
|
246
|
+
let text = $from.parent.textBetween($from.parentOffset, $to.parentOffset)
|
|
247
|
+
let deflt = () => mkTr(view.state.tr.insertText(text, chFrom, chTo))
|
|
248
|
+
if (!view.someProp("handleTextInput", f => f(view, chFrom, chTo, text, deflt)))
|
|
249
|
+
view.dispatch(deflt())
|
|
250
|
+
} else {
|
|
251
|
+
view.dispatch(mkTr())
|
|
252
|
+
}
|
|
253
|
+
} else {
|
|
254
|
+
view.dispatch(mkTr())
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function resolveSelection(view: EditorView, doc: Node, parsedSel: {anchor: number, head: number}) {
|
|
259
|
+
if (Math.max(parsedSel.anchor, parsedSel.head) > doc.content.size) return null
|
|
260
|
+
return selectionBetween(view, doc.resolve(parsedSel.anchor), doc.resolve(parsedSel.head))
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Given two same-length, non-empty fragments of inline content,
|
|
264
|
+
// determine whether the first could be created from the second by
|
|
265
|
+
// removing or adding a single mark type.
|
|
266
|
+
function isMarkChange(cur: Fragment, prev: Fragment) {
|
|
267
|
+
let curMarks = cur.firstChild!.marks, prevMarks = prev.firstChild!.marks
|
|
268
|
+
let added = curMarks, removed = prevMarks, type, mark: Mark | undefined, update
|
|
269
|
+
for (let i = 0; i < prevMarks.length; i++) added = prevMarks[i].removeFromSet(added)
|
|
270
|
+
for (let i = 0; i < curMarks.length; i++) removed = curMarks[i].removeFromSet(removed)
|
|
271
|
+
if (added.length == 1 && removed.length == 0) {
|
|
272
|
+
mark = added[0]
|
|
273
|
+
type = "add"
|
|
274
|
+
update = (node: Node) => node.mark(mark!.addToSet(node.marks))
|
|
275
|
+
} else if (added.length == 0 && removed.length == 1) {
|
|
276
|
+
mark = removed[0]
|
|
277
|
+
type = "remove"
|
|
278
|
+
update = (node: Node) => node.mark(mark!.removeFromSet(node.marks))
|
|
279
|
+
} else {
|
|
280
|
+
return null
|
|
281
|
+
}
|
|
282
|
+
let updated = []
|
|
283
|
+
for (let i = 0; i < prev.childCount; i++) updated.push(update(prev.child(i)))
|
|
284
|
+
if (Fragment.from(updated).eq(cur)) return {mark, type}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function looksLikeBlockJoin(old: Node, start: number, end: number, $newStart: ResolvedPos, $newEnd: ResolvedPos) {
|
|
288
|
+
if (// The content must have shrunk
|
|
289
|
+
end - start <= $newEnd.pos - $newStart.pos ||
|
|
290
|
+
// newEnd must point directly at or after the end of the block that newStart points into
|
|
291
|
+
skipClosingAndOpening($newStart, true, false) < $newEnd.pos)
|
|
292
|
+
return false
|
|
293
|
+
|
|
294
|
+
let $start = old.resolve(start)
|
|
295
|
+
|
|
296
|
+
// Handle the case where, rather than joining blocks, the change just removed an entire block
|
|
297
|
+
if (!$newStart.parent.inlineContent) {
|
|
298
|
+
let after = $start.nodeAfter
|
|
299
|
+
return after != null && end == start + after.nodeSize
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Start must be at the end of a block
|
|
303
|
+
if ($start.parentOffset < $start.parent.content.size || !$start.parent.isTextblock)
|
|
304
|
+
return false
|
|
305
|
+
let $next = old.resolve(skipClosingAndOpening($start, true, true))
|
|
306
|
+
// The next textblock must start before end and end near it
|
|
307
|
+
if (!$next.parent.isTextblock || $next.pos > end ||
|
|
308
|
+
skipClosingAndOpening($next, true, false) < end)
|
|
309
|
+
return false
|
|
310
|
+
|
|
311
|
+
// The fragments after the join point must match
|
|
312
|
+
return $newStart.parent.content.cut($newStart.parentOffset).eq($next.parent.content)
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function looksLikeEnter(content: Node, from: number, to: number) {
|
|
316
|
+
let newTextlines = 0, newLeaves = 0
|
|
317
|
+
content.nodesBetween(from, to, (node, pos) => {
|
|
318
|
+
if (node.isTextblock && pos >= from && pos < to)
|
|
319
|
+
newTextlines++
|
|
320
|
+
else if (node.isText ? /\S/.test(node.text!.slice(Math.max(0, from - pos), Math.min(node.nodeSize, to - pos))) : node.isLeaf)
|
|
321
|
+
newLeaves++
|
|
322
|
+
})
|
|
323
|
+
return newLeaves == 0 && newTextlines == 1
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function skipClosingAndOpening($pos: ResolvedPos, fromEnd: boolean, mayOpen: boolean) {
|
|
327
|
+
let depth = $pos.depth, end = fromEnd ? $pos.end() : $pos.pos
|
|
328
|
+
while (depth > 0 && (fromEnd || $pos.indexAfter(depth) == $pos.node(depth).childCount)) {
|
|
329
|
+
depth--
|
|
330
|
+
end++
|
|
331
|
+
fromEnd = false
|
|
332
|
+
}
|
|
333
|
+
if (mayOpen) {
|
|
334
|
+
let next = $pos.node(depth).maybeChild($pos.indexAfter(depth))
|
|
335
|
+
while (next && !next.isLeaf) {
|
|
336
|
+
next = next.firstChild
|
|
337
|
+
end++
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return end
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function findDiff(a: Fragment, b: Fragment, pos: number, preferredPos: number, preferredSide: "start" | "end") {
|
|
344
|
+
let start = a.findDiffStart(b, pos), lenA = pos + a.size, lenB = pos + b.size
|
|
345
|
+
if (start == null) return null
|
|
346
|
+
let {a: endA, b: endB} = a.findDiffEnd(b, lenA, lenB)!
|
|
347
|
+
if (preferredSide == "end") {
|
|
348
|
+
let adjust = Math.max(0, start - Math.min(endA, endB))
|
|
349
|
+
preferredPos -= endA + adjust - start
|
|
350
|
+
}
|
|
351
|
+
if (endA < start && lenA < lenB) {
|
|
352
|
+
let move = preferredPos <= start && preferredPos >= endA ? start - preferredPos : 0
|
|
353
|
+
start -= move
|
|
354
|
+
endB = start + (endB - endA)
|
|
355
|
+
endA = start
|
|
356
|
+
} else if (endB < start) {
|
|
357
|
+
let move = preferredPos <= start && preferredPos >= endB ? start - preferredPos : 0
|
|
358
|
+
start -= move
|
|
359
|
+
endA = start + (endA - endB)
|
|
360
|
+
endB = start
|
|
361
|
+
}
|
|
362
|
+
return {start, endA, endB}
|
|
363
|
+
}
|