@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/domcoords.ts
ADDED
|
@@ -0,0 +1,557 @@
|
|
|
1
|
+
import {EditorState} from "prosemirror-state"
|
|
2
|
+
import {nodeSize, textRange, parentNode, caretFromPoint, isElt, isText, contains,
|
|
3
|
+
domIndex, hasNodeDesc, writeSelection} from "./dom"
|
|
4
|
+
import * as browser from "./browser"
|
|
5
|
+
import {EditorView} from "./index"
|
|
6
|
+
|
|
7
|
+
export type Rect = {left: number, right: number, top: number, bottom: number}
|
|
8
|
+
|
|
9
|
+
function windowRect(doc: Document): Rect {
|
|
10
|
+
let vp = doc.defaultView && doc.defaultView.visualViewport
|
|
11
|
+
if (vp) return {
|
|
12
|
+
left: 0, right: vp.width,
|
|
13
|
+
top: 0, bottom: vp.height
|
|
14
|
+
}
|
|
15
|
+
return {left: 0, right: doc.documentElement.clientWidth,
|
|
16
|
+
top: 0, bottom: doc.documentElement.clientHeight}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function getSide(value: number | Rect, side: keyof Rect): number {
|
|
20
|
+
return typeof value == "number" ? value : value[side]
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function clientRect(node: HTMLElement): Rect {
|
|
24
|
+
let rect = node.getBoundingClientRect()
|
|
25
|
+
// Adjust for elements with style "transform: scale()"
|
|
26
|
+
let scaleX = (rect.width / node.offsetWidth) || 1
|
|
27
|
+
let scaleY = (rect.height / node.offsetHeight) || 1
|
|
28
|
+
// Make sure scrollbar width isn't included in the rectangle
|
|
29
|
+
return {left: rect.left, right: rect.left + node.clientWidth * scaleX,
|
|
30
|
+
top: rect.top, bottom: rect.top + node.clientHeight * scaleY}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function rectNextTo(node: Node, offset: number, dir: -1 | 1): Rect | null {
|
|
34
|
+
for (;;) {
|
|
35
|
+
if (dir < 0 ? offset : offset < nodeSize(node)) {
|
|
36
|
+
if (isText(node))
|
|
37
|
+
return singleRect(textRange(node, offset + (dir < 0 ? -1 : 0), offset + (dir > 0 ? 1 : 0)), -dir)
|
|
38
|
+
let next = node.childNodes[dir < 0 ? offset - 1 : offset]
|
|
39
|
+
if (isText(next)) { node = next; offset = dir < 0 ? next.nodeValue!.length : 0 }
|
|
40
|
+
else if (isElt(next)) return singleRect(next, -dir)
|
|
41
|
+
else return null
|
|
42
|
+
} else if (hasNodeDesc(node, true) || !node.parentNode) {
|
|
43
|
+
return null
|
|
44
|
+
} else {
|
|
45
|
+
offset = domIndex(node) + (dir < 0 ? 0 : 1)
|
|
46
|
+
node = node.parentNode
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function scrollRectIntoView(view: EditorView, rect: Rect, startDOM: Node) {
|
|
52
|
+
// Skip empty rects with all sides at 0, for example, when the element has no CSS box (display: none)
|
|
53
|
+
if (!nonZero(rect) && rect.left == 0) return
|
|
54
|
+
|
|
55
|
+
let scrollThreshold = view.someProp("scrollThreshold") || 0, scrollMargin = view.someProp("scrollMargin") || 5
|
|
56
|
+
let doc = view.dom.ownerDocument
|
|
57
|
+
for (let parent: Node | null = startDOM || view.dom;;) {
|
|
58
|
+
if (!parent) break
|
|
59
|
+
if (!isElt(parent)) { parent = parentNode(parent); continue }
|
|
60
|
+
let atTop = parent == doc.body
|
|
61
|
+
let bounding = atTop ? windowRect(doc) : clientRect(parent)
|
|
62
|
+
let moveX = 0, moveY = 0
|
|
63
|
+
if (rect.top < bounding.top + getSide(scrollThreshold, "top"))
|
|
64
|
+
moveY = -(bounding.top - rect.top + getSide(scrollMargin, "top"))
|
|
65
|
+
else if (rect.bottom > bounding.bottom - getSide(scrollThreshold, "bottom"))
|
|
66
|
+
moveY = rect.bottom - rect.top > bounding.bottom - bounding.top
|
|
67
|
+
? rect.top + getSide(scrollMargin, "top") - bounding.top
|
|
68
|
+
: rect.bottom - bounding.bottom + getSide(scrollMargin, "bottom")
|
|
69
|
+
if (rect.left < bounding.left + getSide(scrollThreshold, "left"))
|
|
70
|
+
moveX = -(bounding.left - rect.left + getSide(scrollMargin, "left"))
|
|
71
|
+
else if (rect.right > bounding.right - getSide(scrollThreshold, "right"))
|
|
72
|
+
moveX = rect.right - bounding.right + getSide(scrollMargin, "right")
|
|
73
|
+
if (moveX || moveY) {
|
|
74
|
+
if (atTop) {
|
|
75
|
+
doc.defaultView!.scrollBy(moveX, moveY)
|
|
76
|
+
} else {
|
|
77
|
+
let startX = parent.scrollLeft, startY = parent.scrollTop
|
|
78
|
+
if (moveY) parent.scrollTop += moveY
|
|
79
|
+
if (moveX) parent.scrollLeft += moveX
|
|
80
|
+
let dX = parent.scrollLeft - startX, dY = parent.scrollTop - startY
|
|
81
|
+
rect = {left: rect.left - dX, top: rect.top - dY, right: rect.right - dX, bottom: rect.bottom - dY}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
let pos: string = atTop ? "fixed" : getComputedStyle(parent).position
|
|
85
|
+
if (/^(fixed|sticky)$/.test(pos)) break
|
|
86
|
+
parent = pos == "absolute" ? parent.offsetParent : parentNode(parent)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Store the scroll position of the editor's parent nodes, along with
|
|
91
|
+
// the top position of an element near the top of the editor, which
|
|
92
|
+
// will be used to make sure the visible viewport remains stable even
|
|
93
|
+
// when the size of the content above changes.
|
|
94
|
+
export function storeScrollPos(view: EditorView): {
|
|
95
|
+
refDOM: HTMLElement,
|
|
96
|
+
refTop: number,
|
|
97
|
+
stack: {dom: HTMLElement, top: number, left: number}[]
|
|
98
|
+
} {
|
|
99
|
+
let rect = view.dom.getBoundingClientRect(), startY = Math.max(0, rect.top)
|
|
100
|
+
let refDOM: HTMLElement, refTop: number
|
|
101
|
+
for (let x = (rect.left + rect.right) / 2, y = startY + 1;
|
|
102
|
+
y < Math.min(innerHeight, rect.bottom); y += 5) {
|
|
103
|
+
let dom = view.root.elementFromPoint(x, y)
|
|
104
|
+
if (!dom || dom == view.dom || !view.dom.contains(dom)) continue
|
|
105
|
+
let localRect = dom.getBoundingClientRect()
|
|
106
|
+
if (localRect.top >= startY - 20) {
|
|
107
|
+
refDOM = dom as HTMLElement
|
|
108
|
+
refTop = localRect.top
|
|
109
|
+
break
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return {refDOM: refDOM!, refTop: refTop!, stack: scrollStack(view.dom)}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function scrollStack(dom: Node): {dom: HTMLElement, top: number, left: number}[] {
|
|
116
|
+
let stack = [], doc = dom.ownerDocument
|
|
117
|
+
for (let cur: Node | null = dom; cur; cur = parentNode(cur)) {
|
|
118
|
+
stack.push({dom: cur as HTMLElement, top: (cur as HTMLElement).scrollTop, left: (cur as HTMLElement).scrollLeft})
|
|
119
|
+
if (dom == doc) break
|
|
120
|
+
}
|
|
121
|
+
return stack
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Reset the scroll position of the editor's parent nodes to that what
|
|
125
|
+
// it was before, when storeScrollPos was called.
|
|
126
|
+
export function resetScrollPos({refDOM, refTop, stack}: {
|
|
127
|
+
refDOM: HTMLElement,
|
|
128
|
+
refTop: number,
|
|
129
|
+
stack: {dom: HTMLElement, top: number, left: number}[]
|
|
130
|
+
}) {
|
|
131
|
+
let newRefTop = refDOM ? refDOM.getBoundingClientRect().top : 0
|
|
132
|
+
restoreScrollStack(stack, newRefTop == 0 ? 0 : newRefTop - refTop)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function restoreScrollStack(stack: {dom: HTMLElement, top: number, left: number}[], dTop: number) {
|
|
136
|
+
for (let i = 0; i < stack.length; i++) {
|
|
137
|
+
let {dom, top, left} = stack[i]
|
|
138
|
+
if (dom.scrollTop != top + dTop) dom.scrollTop = top + dTop
|
|
139
|
+
if (dom.scrollLeft != left) dom.scrollLeft = left
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
let preventScrollSupported: false | null | {preventScroll: boolean} = null
|
|
144
|
+
// Feature-detects support for .focus({preventScroll: true}), and uses
|
|
145
|
+
// a fallback kludge when not supported.
|
|
146
|
+
export function focusPreventScroll(dom: HTMLElement) {
|
|
147
|
+
if ((dom as any).setActive) return (dom as any).setActive() // in IE
|
|
148
|
+
if (preventScrollSupported) return dom.focus(preventScrollSupported)
|
|
149
|
+
|
|
150
|
+
let stored = scrollStack(dom)
|
|
151
|
+
dom.focus(preventScrollSupported == null ? {
|
|
152
|
+
get preventScroll() {
|
|
153
|
+
preventScrollSupported = {preventScroll: true}
|
|
154
|
+
return true
|
|
155
|
+
}
|
|
156
|
+
} : undefined)
|
|
157
|
+
if (!preventScrollSupported) {
|
|
158
|
+
preventScrollSupported = false
|
|
159
|
+
restoreScrollStack(stored, 0)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function findOffsetInNode(node: HTMLElement, coords: {top: number, left: number}): {node: Node, offset: number} {
|
|
164
|
+
let closest: Text | HTMLElement | undefined
|
|
165
|
+
let dxClosest = 2e8, coordsClosest: {left: number, top: number} | undefined, offset = 0
|
|
166
|
+
let rowBot = coords.top, rowTop = coords.top
|
|
167
|
+
let firstBelow: Text | HTMLElement | undefined, coordsBelow: {left: number, top: number} | undefined
|
|
168
|
+
for (let child = node.firstChild, childIndex = 0; child; child = child.nextSibling, childIndex++) {
|
|
169
|
+
let rects
|
|
170
|
+
if (isElt(child)) rects = child.getClientRects()
|
|
171
|
+
else if (isText(child)) rects = textRange(child).getClientRects()
|
|
172
|
+
else continue
|
|
173
|
+
|
|
174
|
+
for (let i = 0; i < rects.length; i++) {
|
|
175
|
+
let rect = rects[i]
|
|
176
|
+
if (rect.top <= rowBot && rect.bottom >= rowTop) {
|
|
177
|
+
rowBot = Math.max(rect.bottom, rowBot)
|
|
178
|
+
rowTop = Math.min(rect.top, rowTop)
|
|
179
|
+
let dx = rect.left > coords.left ? rect.left - coords.left
|
|
180
|
+
: rect.right < coords.left ? coords.left - rect.right : 0
|
|
181
|
+
if (dx < dxClosest) {
|
|
182
|
+
closest = child
|
|
183
|
+
dxClosest = dx
|
|
184
|
+
coordsClosest = dx && isText(closest) ? {
|
|
185
|
+
left: rect.right < coords.left ? rect.right : rect.left,
|
|
186
|
+
top: coords.top
|
|
187
|
+
} : coords
|
|
188
|
+
if (isElt(child) && dx)
|
|
189
|
+
offset = childIndex + (coords.left >= (rect.left + rect.right) / 2 ? 1 : 0)
|
|
190
|
+
continue
|
|
191
|
+
}
|
|
192
|
+
} else if (rect.top > coords.top && !firstBelow && rect.left <= coords.left && rect.right >= coords.left) {
|
|
193
|
+
firstBelow = child
|
|
194
|
+
coordsBelow = {left: Math.max(rect.left, Math.min(rect.right, coords.left)), top: rect.top}
|
|
195
|
+
}
|
|
196
|
+
if (!closest && (coords.left >= rect.right && coords.top >= rect.top ||
|
|
197
|
+
coords.left >= rect.left && coords.top >= rect.bottom))
|
|
198
|
+
offset = childIndex + 1
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
if (!closest && firstBelow) { closest = firstBelow; coordsClosest = coordsBelow; dxClosest = 0 }
|
|
202
|
+
if (closest && isText(closest)) return findOffsetInText(closest, coordsClosest!)
|
|
203
|
+
if (!closest || dxClosest) return {node, offset}
|
|
204
|
+
return findOffsetInNode(closest, coordsClosest!)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function findOffsetInText(node: Text, coords: {top: number, left: number}) {
|
|
208
|
+
let len = node.nodeValue!.length
|
|
209
|
+
let range = document.createRange(), result: {node: Node, offset: number} | undefined
|
|
210
|
+
for (let i = 0; i < len; i++) {
|
|
211
|
+
range.setEnd(node, i + 1)
|
|
212
|
+
range.setStart(node, i)
|
|
213
|
+
let rect = singleRect(range, 1)
|
|
214
|
+
if (rect.top == rect.bottom) continue
|
|
215
|
+
if (inRect(coords, rect)) {
|
|
216
|
+
result = {node, offset: i + (coords.left >= (rect.left + rect.right) / 2 ? 1 : 0)}
|
|
217
|
+
break
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
range.detach()
|
|
221
|
+
return result || {node, offset: 0}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function inRect(coords: {top: number, left: number}, rect: Rect) {
|
|
225
|
+
return coords.left >= rect.left - 1 && coords.left <= rect.right + 1&&
|
|
226
|
+
coords.top >= rect.top - 1 && coords.top <= rect.bottom + 1
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function targetKludge(dom: HTMLElement, coords: {top: number, left: number}) {
|
|
230
|
+
let parent = dom.parentNode
|
|
231
|
+
if (parent && /^li$/i.test(parent.nodeName) && coords.left < dom.getBoundingClientRect().left)
|
|
232
|
+
return parent as HTMLElement
|
|
233
|
+
return dom
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function posFromElement(
|
|
237
|
+
view: EditorView,
|
|
238
|
+
elt: HTMLElement,
|
|
239
|
+
coords: {top: number, left: number}
|
|
240
|
+
): {pos: number, affinity: -1 | 0 | 1} {
|
|
241
|
+
let {node, offset} = findOffsetInNode(elt, coords), bias = -1
|
|
242
|
+
if (isElt(node) && !node.firstChild) {
|
|
243
|
+
let rect = node.getBoundingClientRect()
|
|
244
|
+
bias = rect.left != rect.right && coords.left > (rect.left + rect.right) / 2 ? 1 : -1
|
|
245
|
+
}
|
|
246
|
+
return {pos: view.docView.posFromDOM(node, offset, bias), affinity: affinityFromCoords(node, offset, coords)}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function horizDist(x: number, rect: Rect) {
|
|
250
|
+
return x < rect.left ? rect.left - x : x > rect.right ? x - rect.right : 0
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function affinityFromCoords(node: Node, offset: number, coords: {top: number, left: number}): -1 | 0 | 1 {
|
|
254
|
+
let before = rectNextTo(node, offset, -1), after = rectNextTo(node, offset, 1)
|
|
255
|
+
if (!before) return 1
|
|
256
|
+
if (!after) return -1
|
|
257
|
+
if (before.bottom <= after.top) // Different lines
|
|
258
|
+
return coords.top > (before.bottom + after.top) / 2 ? 1 : -1
|
|
259
|
+
// Same line
|
|
260
|
+
return horizDist(coords.left, before) < horizDist(coords.left, after) ? -1 : 1
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function posFromCaret(
|
|
264
|
+
view: EditorView,
|
|
265
|
+
node: Node,
|
|
266
|
+
offset: number,
|
|
267
|
+
coords: {top: number, left: number}
|
|
268
|
+
): {pos: number, affinity: -1 | 0 | 1} | null {
|
|
269
|
+
// Browser (in caretPosition/RangeFromPoint) will agressively
|
|
270
|
+
// normalize towards nearby inline nodes. Since we are interested in
|
|
271
|
+
// positions between block nodes too, we first walk up the hierarchy
|
|
272
|
+
// of nodes to see if there are parent block nodes that the
|
|
273
|
+
// coordinates fall vertically outside of.
|
|
274
|
+
let outsideBlock = -1, outsideAffinity: -1 | 0 | 1 = 0
|
|
275
|
+
for (let cur = node; cur != view.dom;) {
|
|
276
|
+
let desc = view.docView.nearestDesc(cur, true), rect
|
|
277
|
+
if (!desc) return null
|
|
278
|
+
// Ignore elements with zero-size bounding rectangles
|
|
279
|
+
if (isElt(desc.dom) && ((rect = desc.dom.getBoundingClientRect()).width || rect.height)) {
|
|
280
|
+
if (!desc.contentDOM && !desc.node.isText &&
|
|
281
|
+
coords.left >= rect.left && coords.left <= rect.right &&
|
|
282
|
+
coords.top >= rect.top && coords.top <= rect.bottom) {
|
|
283
|
+
// If we are inside a leaf, return the side of the leaf closer to the coords
|
|
284
|
+
let before = desc.node.isBlock ? coords.top < (rect.top + rect.bottom) / 2
|
|
285
|
+
: coords.left < (rect.left + rect.right) / 2
|
|
286
|
+
return {pos: before ? desc.posBefore : desc.posAfter, affinity: before ? 1 : -1}
|
|
287
|
+
}
|
|
288
|
+
if (desc.node.isBlock && desc.parent && !/^T(R|BODY|HEAD|FOOT)$/.test(desc.dom!.nodeName) &&
|
|
289
|
+
(coords.top < rect.top || coords.top > rect.bottom)) {
|
|
290
|
+
[outsideBlock, outsideAffinity] = coords.top < rect.top ? [desc.posBefore, 1] : [desc.posAfter, -1]
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
cur = desc.dom.parentNode!
|
|
294
|
+
}
|
|
295
|
+
if (outsideBlock > -1) return {pos: outsideBlock, affinity: outsideAffinity}
|
|
296
|
+
return {pos: view.docView.posFromDOM(node, offset, -1),
|
|
297
|
+
affinity: affinityFromCoords(node, offset, coords)}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function elementFromPoint(element: HTMLElement, coords: {top: number, left: number}, box: Rect): HTMLElement {
|
|
301
|
+
let len = element.childNodes.length
|
|
302
|
+
if (len && box.top < box.bottom) {
|
|
303
|
+
for (let startI = Math.max(0, Math.min(len - 1, Math.floor(len * (coords.top - box.top) / (box.bottom - box.top)) - 2)), i = startI;;) {
|
|
304
|
+
let child = element.childNodes[i]
|
|
305
|
+
if (isElt(child)) {
|
|
306
|
+
let rects = child.getClientRects()
|
|
307
|
+
for (let j = 0; j < rects.length; j++) {
|
|
308
|
+
let rect = rects[j]
|
|
309
|
+
if (inRect(coords, rect)) return elementFromPoint(child, coords, rect)
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
if ((i = (i + 1) % len) == startI) break
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return element
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// Given an x,y position on the editor, get the position in the document.
|
|
319
|
+
export function posAtCoords(view: EditorView, coords: {top: number, left: number}) {
|
|
320
|
+
let doc = view.dom.ownerDocument, node: Node | undefined, offset = 0
|
|
321
|
+
let caret = caretFromPoint(doc, coords.left, coords.top)
|
|
322
|
+
if (caret) ({node, offset} = caret)
|
|
323
|
+
|
|
324
|
+
let elt = ((view.root as any).elementFromPoint ? view.root : doc)
|
|
325
|
+
.elementFromPoint(coords.left, coords.top) as HTMLElement
|
|
326
|
+
let pos: number | undefined, affinity: -1 | 0 | 1 = 0
|
|
327
|
+
if (!elt || !view.dom.contains(elt)) {
|
|
328
|
+
let box = view.dom.getBoundingClientRect()
|
|
329
|
+
if (!inRect(coords, box)) return null
|
|
330
|
+
elt = elementFromPoint(view.dom, coords, box)
|
|
331
|
+
if (!elt) return null
|
|
332
|
+
}
|
|
333
|
+
// Safari's caretRangeFromPoint returns nonsense when on a draggable element
|
|
334
|
+
if (browser.safari) {
|
|
335
|
+
for (let p: Node | null = elt; node && p; p = parentNode(p))
|
|
336
|
+
if ((p as HTMLElement).draggable) node = undefined
|
|
337
|
+
}
|
|
338
|
+
elt = targetKludge(elt, coords)
|
|
339
|
+
if (node) {
|
|
340
|
+
if (browser.gecko && isElt(node)) {
|
|
341
|
+
// Firefox will sometimes return offsets into <input> nodes, which
|
|
342
|
+
// have no actual children, from caretPositionFromPoint (#953)
|
|
343
|
+
offset = Math.min(offset, node.childNodes.length)
|
|
344
|
+
// It'll also move the returned position before image nodes,
|
|
345
|
+
// even if those are behind it.
|
|
346
|
+
if (offset < node.childNodes.length) {
|
|
347
|
+
let next = node.childNodes[offset], box
|
|
348
|
+
if (next.nodeName == "IMG" && (box = (next as HTMLElement).getBoundingClientRect()).right <= coords.left &&
|
|
349
|
+
box.bottom > coords.top)
|
|
350
|
+
offset++
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
let prev
|
|
354
|
+
// When clicking above the right side of an uneditable node, Chrome will report a cursor position after that node.
|
|
355
|
+
if (browser.webkit && offset && isElt(node) && isElt(prev = node.childNodes[offset - 1]) &&
|
|
356
|
+
prev.contentEditable == "false" && prev.getBoundingClientRect().top >= coords.top)
|
|
357
|
+
offset--
|
|
358
|
+
// Suspiciously specific kludge to work around caret*FromPoint
|
|
359
|
+
// never returning a position at the end of the document
|
|
360
|
+
if (node == view.dom && offset == node.childNodes.length - 1 && isElt(node.lastChild!) &&
|
|
361
|
+
coords.top > node.lastChild.getBoundingClientRect().bottom) {
|
|
362
|
+
pos = view.state.doc.content.size
|
|
363
|
+
// Ignore positions directly after a BR, since caret*FromPoint
|
|
364
|
+
// 'round up' positions that would be more accurately placed
|
|
365
|
+
// before the BR node.
|
|
366
|
+
} else if (offset == 0 || !isElt(node) || node.childNodes[offset - 1].nodeName != "BR") {
|
|
367
|
+
let found = posFromCaret(view, node, offset, coords)
|
|
368
|
+
if (found) ({pos, affinity} = found)
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
if (pos == null) ({pos, affinity} = posFromElement(view, elt, coords))
|
|
372
|
+
|
|
373
|
+
let desc = view.docView.nearestDesc(elt, true)
|
|
374
|
+
return {pos, inside: desc ? desc.posAtStart - desc.border : -1, affinity}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function nonZero(rect: Rect) {
|
|
378
|
+
return rect.top < rect.bottom || rect.left < rect.right
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
function singleRect(target: HTMLElement | Range, bias: number): DOMRect {
|
|
382
|
+
let rects = target.getClientRects()
|
|
383
|
+
if (rects.length) {
|
|
384
|
+
let first = rects[bias < 0 ? 0 : rects.length - 1]
|
|
385
|
+
if (nonZero(first)) return first
|
|
386
|
+
}
|
|
387
|
+
return Array.prototype.find.call(rects, nonZero) || target.getBoundingClientRect()
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
const BIDI = /[\u0590-\u05f4\u0600-\u06ff\u0700-\u08ac]/
|
|
391
|
+
|
|
392
|
+
// Given a position in the document model, get a bounding box of the
|
|
393
|
+
// character at that position, relative to the window.
|
|
394
|
+
export function coordsAtPos(view: EditorView, pos: number, side: number): Rect {
|
|
395
|
+
let {node, offset, atom} = view.docView.domFromPos(pos, side < 0 ? -1 : 1)
|
|
396
|
+
|
|
397
|
+
let supportEmptyRange = browser.webkit || browser.gecko
|
|
398
|
+
if (isText(node)) {
|
|
399
|
+
// These browsers support querying empty text ranges. Prefer that in
|
|
400
|
+
// bidi context or when at the end of a node.
|
|
401
|
+
if (supportEmptyRange && (BIDI.test(node.nodeValue!) || (side < 0 ? !offset : offset == node.nodeValue!.length))) {
|
|
402
|
+
let rect = singleRect(textRange(node, offset, offset), side)
|
|
403
|
+
// Firefox returns bad results (the position before the space)
|
|
404
|
+
// when querying a position directly after line-broken
|
|
405
|
+
// whitespace. Detect this situation and and kludge around it
|
|
406
|
+
if (browser.gecko && offset && /\s/.test(node.nodeValue![offset - 1]) && offset < node.nodeValue!.length) {
|
|
407
|
+
let rectBefore = singleRect(textRange(node, offset - 1, offset - 1), -1)
|
|
408
|
+
if (rectBefore.top == rect.top) {
|
|
409
|
+
let rectAfter = singleRect(textRange(node, offset, offset + 1), -1)
|
|
410
|
+
if (rectAfter.top != rect.top)
|
|
411
|
+
return flattenV(rectAfter, rectAfter.left < rectBefore.left)
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
return rect
|
|
415
|
+
} else {
|
|
416
|
+
let from = offset, to = offset, takeSide = side < 0 ? 1 : -1
|
|
417
|
+
if (side < 0 && !offset) { to++; takeSide = -1 }
|
|
418
|
+
else if (side >= 0 && offset == node.nodeValue!.length) { from--; takeSide = 1 }
|
|
419
|
+
else if (side < 0) { from-- }
|
|
420
|
+
else { to ++ }
|
|
421
|
+
return flattenV(singleRect(textRange(node, from, to), takeSide), takeSide < 0)
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
let $dom = view.state.doc.resolve(pos - (atom || 0))
|
|
426
|
+
// Return a horizontal line in block context
|
|
427
|
+
if (!$dom.parent.inlineContent) {
|
|
428
|
+
if (atom == null && offset && (side < 0 || offset == nodeSize(node))) {
|
|
429
|
+
let before = node.childNodes[offset - 1]
|
|
430
|
+
if (isElt(before)) return flattenH(before.getBoundingClientRect(), false)
|
|
431
|
+
}
|
|
432
|
+
if (atom == null && offset < nodeSize(node)) {
|
|
433
|
+
let after = node.childNodes[offset]
|
|
434
|
+
if (isElt(after)) return flattenH(after.getBoundingClientRect(), true)
|
|
435
|
+
}
|
|
436
|
+
return flattenH((node as HTMLElement).getBoundingClientRect(), side >= 0)
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
// Inline, not in text node (this is not Bidi-safe)
|
|
440
|
+
if (atom == null && offset && (side < 0 || offset == nodeSize(node))) {
|
|
441
|
+
let before = node.childNodes[offset - 1]
|
|
442
|
+
let target = isText(before) ? textRange(before, nodeSize(before) - (supportEmptyRange ? 0 : 1))
|
|
443
|
+
// BR nodes tend to only return the rectangle before them.
|
|
444
|
+
// Only use them if they are the last element in their parent
|
|
445
|
+
: isElt(before) && (before.nodeName != "BR" || !before.nextSibling) ? before : null
|
|
446
|
+
if (target) return flattenV(singleRect(target, 1), false)
|
|
447
|
+
}
|
|
448
|
+
if (atom == null && offset < nodeSize(node)) {
|
|
449
|
+
let after = node.childNodes[offset]
|
|
450
|
+
while (after.pmViewDesc && after.pmViewDesc.ignoreForCoords) after = after.nextSibling!
|
|
451
|
+
let target = !after ? null : isText(after) ? textRange(after, 0, (supportEmptyRange ? 0 : 1))
|
|
452
|
+
: isElt(after) ? after : null
|
|
453
|
+
if (target) return flattenV(singleRect(target, -1), true)
|
|
454
|
+
}
|
|
455
|
+
// All else failed, just try to get a rectangle for the target node
|
|
456
|
+
return flattenV(singleRect(isText(node) ? textRange(node) : node as HTMLElement, -side), side >= 0)
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
function flattenV(rect: DOMRect, left: boolean) {
|
|
460
|
+
if (rect.width == 0) return rect
|
|
461
|
+
let x = left ? rect.left : rect.right
|
|
462
|
+
return {top: rect.top, bottom: rect.bottom, left: x, right: x}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function flattenH(rect: DOMRect, top: boolean) {
|
|
466
|
+
if (rect.height == 0) return rect
|
|
467
|
+
let y = top ? rect.top : rect.bottom
|
|
468
|
+
return {top: y, bottom: y, left: rect.left, right: rect.right}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
function withFlushedState<T>(view: EditorView, state: EditorState, f: () => T): T {
|
|
472
|
+
let viewState = view.state, active = view.root.activeElement as HTMLElement
|
|
473
|
+
if (viewState != state) view.updateState(state)
|
|
474
|
+
if (active != view.dom) view.focus()
|
|
475
|
+
try {
|
|
476
|
+
return f()
|
|
477
|
+
} finally {
|
|
478
|
+
if (viewState != state) view.updateState(viewState)
|
|
479
|
+
if (active != view.dom && active) active.focus()
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// Whether vertical position motion in a given direction
|
|
484
|
+
// from a position would leave a text block.
|
|
485
|
+
function endOfTextblockVertical(view: EditorView, state: EditorState, dir: "up" | "down") {
|
|
486
|
+
let sel = state.selection
|
|
487
|
+
let $pos = dir == "up" ? sel.$from : sel.$to
|
|
488
|
+
return withFlushedState(view, state, () => {
|
|
489
|
+
let {node: dom} = view.docView.domFromPos($pos.pos, dir == "up" ? -1 : 1)
|
|
490
|
+
for (;;) {
|
|
491
|
+
let nearest = view.docView.nearestDesc(dom, true)
|
|
492
|
+
if (!nearest) break
|
|
493
|
+
if (nearest.node.isBlock) { dom = nearest.contentDOM || nearest.dom; break }
|
|
494
|
+
dom = nearest.dom.parentNode!
|
|
495
|
+
}
|
|
496
|
+
let coords = coordsAtPos(view, $pos.pos, 1)
|
|
497
|
+
for (let child = dom.firstChild; child; child = child.nextSibling) {
|
|
498
|
+
let boxes
|
|
499
|
+
if (isElt(child)) boxes = child.getClientRects()
|
|
500
|
+
else if (isText(child)) boxes = textRange(child, 0, child.nodeValue!.length).getClientRects()
|
|
501
|
+
else continue
|
|
502
|
+
for (let i = 0; i < boxes.length; i++) {
|
|
503
|
+
let box = boxes[i]
|
|
504
|
+
if (box.bottom > box.top + 1 &&
|
|
505
|
+
(dir == "up" ? coords.top - box.top > (box.bottom - coords.top) * 2
|
|
506
|
+
: box.bottom - coords.bottom > (coords.bottom - box.top) * 2))
|
|
507
|
+
return false
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
return true
|
|
511
|
+
})
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
const maybeRTL = /[\u0590-\u08ac]/
|
|
515
|
+
|
|
516
|
+
function endOfTextblockHorizontal(view: EditorView, state: EditorState, dir: "left" | "right" | "forward" | "backward") {
|
|
517
|
+
let {$head} = state.selection
|
|
518
|
+
if (!$head.parent.isTextblock) return false
|
|
519
|
+
let offset = $head.parentOffset, atStart = !offset, atEnd = offset == $head.parent.content.size
|
|
520
|
+
let sel: Selection = view.domSelection()!
|
|
521
|
+
if (!sel) return $head.pos == $head.start() || $head.pos == $head.end()
|
|
522
|
+
// If the textblock is all LTR, or the browser doesn't support
|
|
523
|
+
// Selection.modify (Edge), fall back to a primitive approach
|
|
524
|
+
if (!maybeRTL.test($head.parent.textContent) || !(sel as any).modify)
|
|
525
|
+
return dir == "left" || dir == "backward" ? atStart : atEnd
|
|
526
|
+
|
|
527
|
+
return withFlushedState(view, state, () => {
|
|
528
|
+
// This is a huge hack, but appears to be the best we can
|
|
529
|
+
// currently do: use `Selection.modify` to move the selection by
|
|
530
|
+
// one character, and see if that moves the cursor out of the
|
|
531
|
+
// textblock (or doesn't move it at all, when at the start/end of
|
|
532
|
+
// the document).
|
|
533
|
+
let range = view.domSel.readRange()
|
|
534
|
+
let oldBidiLevel = (sel as any).caretBidiLevel // Only for Firefox
|
|
535
|
+
;(sel as any).modify("move", dir, "character")
|
|
536
|
+
let parentDOM = $head.depth ? view.docView.domAfterPos($head.before()) : view.dom
|
|
537
|
+
let {focusNode: newNode, focusOffset: newOff} = view.domSel.readRange()
|
|
538
|
+
let result = newNode && !contains(parentDOM, newNode) || (range.focusNode == newNode && range.focusOffset == newOff)
|
|
539
|
+
// Restore the previous selection
|
|
540
|
+
writeSelection(sel, range)
|
|
541
|
+
if (oldBidiLevel != null) (sel as any).caretBidiLevel = oldBidiLevel
|
|
542
|
+
return result
|
|
543
|
+
})
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
export type TextblockDir = "up" | "down" | "left" | "right" | "forward" | "backward"
|
|
547
|
+
|
|
548
|
+
let cachedState: EditorState | null = null
|
|
549
|
+
let cachedDir: TextblockDir | null = null
|
|
550
|
+
let cachedResult: boolean = false
|
|
551
|
+
export function endOfTextblock(view: EditorView, state: EditorState, dir: TextblockDir) {
|
|
552
|
+
if (cachedState == state && cachedDir == dir) return cachedResult
|
|
553
|
+
cachedState = state; cachedDir = dir
|
|
554
|
+
return cachedResult = dir == "up" || dir == "down"
|
|
555
|
+
? endOfTextblockVertical(view, state, dir)
|
|
556
|
+
: endOfTextblockHorizontal(view, state, dir)
|
|
557
|
+
}
|