@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
|
@@ -0,0 +1,834 @@
|
|
|
1
|
+
import {Node, Mark, Fragment} from "prosemirror-model"
|
|
2
|
+
import {Mappable, Mapping} from "prosemirror-transform"
|
|
3
|
+
import {EditorView} from "./index"
|
|
4
|
+
import {DOMNode} from "./dom"
|
|
5
|
+
|
|
6
|
+
function compareObjs(a: {[prop: string]: any}, b: {[prop: string]: any}) {
|
|
7
|
+
if (a == b) return true
|
|
8
|
+
for (let p in a) if (a[p] !== b[p]) return false
|
|
9
|
+
for (let p in b) if (!(p in a)) return false
|
|
10
|
+
return true
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
class FragmentCursor {
|
|
14
|
+
index = 0
|
|
15
|
+
off = 0
|
|
16
|
+
constructor(readonly frag: Fragment) {}
|
|
17
|
+
nodeAt(off: number) {
|
|
18
|
+
while (this.off <= off && this.index < this.frag.childCount) {
|
|
19
|
+
let next = this.frag.child(this.index)
|
|
20
|
+
if (this.off == off) return next
|
|
21
|
+
this.off += next.nodeSize
|
|
22
|
+
this.index++
|
|
23
|
+
}
|
|
24
|
+
return null
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface DecorationType {
|
|
29
|
+
spec: any
|
|
30
|
+
map(mapping: Mappable, span: Decoration, offset: number, oldOffset: number): Decoration | null
|
|
31
|
+
valid(node: Node, span: Decoration, cur: FragmentCursor): boolean
|
|
32
|
+
eq(other: DecorationType): boolean
|
|
33
|
+
destroy(dom: DOMNode): void
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export type WidgetConstructor = ((view: EditorView, getPos: () => number | undefined) => DOMNode) | DOMNode
|
|
37
|
+
|
|
38
|
+
export class WidgetType implements DecorationType {
|
|
39
|
+
spec: any
|
|
40
|
+
side: number
|
|
41
|
+
|
|
42
|
+
constructor(readonly toDOM: WidgetConstructor, spec: any) {
|
|
43
|
+
this.spec = spec || noSpec
|
|
44
|
+
this.side = this.spec.side || 0
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
map(mapping: Mappable, span: Decoration, offset: number, oldOffset: number): Decoration | null {
|
|
48
|
+
let {pos, deleted} = mapping.mapResult(span.from + oldOffset, this.side < 0 ? -1 : 1)
|
|
49
|
+
return deleted ? null : new Decoration(pos - offset, pos - offset, this)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
valid() { return true }
|
|
53
|
+
|
|
54
|
+
eq(other: WidgetType) {
|
|
55
|
+
return this == other ||
|
|
56
|
+
(other instanceof WidgetType &&
|
|
57
|
+
(this.spec.key && this.spec.key == other.spec.key ||
|
|
58
|
+
this.toDOM == other.toDOM && compareObjs(this.spec, other.spec)))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
destroy(node: DOMNode) {
|
|
62
|
+
if (this.spec.destroy) this.spec.destroy(node)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export class InlineType implements DecorationType {
|
|
67
|
+
spec: any
|
|
68
|
+
|
|
69
|
+
constructor(readonly attrs: DecorationAttrs, spec: any) {
|
|
70
|
+
this.spec = spec || noSpec
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
map(mapping: Mappable, span: Decoration, offset: number, oldOffset: number): Decoration | null {
|
|
74
|
+
let from = mapping.map(span.from + oldOffset, this.spec.inclusiveStart ? -1 : 1) - offset
|
|
75
|
+
let to = mapping.map(span.to + oldOffset, this.spec.inclusiveEnd ? 1 : -1) - offset
|
|
76
|
+
return from >= to ? null : new Decoration(from, to, this)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
valid(_: Node, span: Decoration) { return span.from < span.to }
|
|
80
|
+
|
|
81
|
+
eq(other: DecorationType): boolean {
|
|
82
|
+
return this == other ||
|
|
83
|
+
(other instanceof InlineType && compareObjs(this.attrs, other.attrs) &&
|
|
84
|
+
compareObjs(this.spec, other.spec))
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
static is(span: Decoration) { return span.type instanceof InlineType }
|
|
88
|
+
|
|
89
|
+
destroy() {}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export class NodeType implements DecorationType {
|
|
93
|
+
spec: any
|
|
94
|
+
constructor(readonly attrs: DecorationAttrs, spec: any) {
|
|
95
|
+
this.spec = spec || noSpec
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
map(mapping: Mappable, span: Decoration, offset: number, oldOffset: number): Decoration | null {
|
|
99
|
+
let from = mapping.mapResult(span.from + oldOffset, 1)
|
|
100
|
+
if (from.deleted) return null
|
|
101
|
+
let to = mapping.mapResult(span.to + oldOffset, -1)
|
|
102
|
+
if (to.deleted || to.pos <= from.pos) return null
|
|
103
|
+
return new Decoration(from.pos - offset, to.pos - offset, this)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
valid(node: Node, span: Decoration, cursor: FragmentCursor): boolean {
|
|
107
|
+
let found = cursor.nodeAt(span.from)
|
|
108
|
+
return !!found && !found.isText && span.from + found.nodeSize == span.to
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
eq(other: DecorationType): boolean {
|
|
112
|
+
return this == other ||
|
|
113
|
+
(other instanceof NodeType && compareObjs(this.attrs, other.attrs) &&
|
|
114
|
+
compareObjs(this.spec, other.spec))
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
destroy() {}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/// Decoration objects can be provided to the view through the
|
|
121
|
+
/// [`decorations` prop](#view.EditorProps.decorations). They come in
|
|
122
|
+
/// several variants—see the static members of this class for details.
|
|
123
|
+
export class Decoration {
|
|
124
|
+
/// @internal
|
|
125
|
+
constructor(
|
|
126
|
+
/// The start position of the decoration.
|
|
127
|
+
readonly from: number,
|
|
128
|
+
/// The end position. Will be the same as `from` for [widget
|
|
129
|
+
/// decorations](#view.Decoration^widget).
|
|
130
|
+
readonly to: number,
|
|
131
|
+
/// @internal
|
|
132
|
+
readonly type: DecorationType
|
|
133
|
+
) {}
|
|
134
|
+
|
|
135
|
+
/// @internal
|
|
136
|
+
copy(from: number, to: number) {
|
|
137
|
+
return new Decoration(from, to, this.type)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/// @internal
|
|
141
|
+
eq(other: Decoration, offset = 0) {
|
|
142
|
+
return this.type.eq(other.type) && this.from + offset == other.from && this.to + offset == other.to
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/// @internal
|
|
146
|
+
map(mapping: Mappable, offset: number, oldOffset: number) {
|
|
147
|
+
return this.type.map(mapping, this, offset, oldOffset)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/// Creates a widget decoration, which is a DOM node that's shown in
|
|
151
|
+
/// the document at the given position. It is recommended that you
|
|
152
|
+
/// delay rendering the widget by passing a function that will be
|
|
153
|
+
/// called when the widget is actually drawn in a view, but you can
|
|
154
|
+
/// also directly pass a DOM node. `getPos` can be used to find the
|
|
155
|
+
/// widget's current document position.
|
|
156
|
+
static widget(pos: number, toDOM: WidgetConstructor, spec?: {
|
|
157
|
+
/// Controls which side of the document position this widget is
|
|
158
|
+
/// associated with. When negative, it is drawn before a cursor
|
|
159
|
+
/// at its position, and content inserted at that position ends
|
|
160
|
+
/// up after the widget. When zero (the default) or positive, the
|
|
161
|
+
/// widget is drawn after the cursor and content inserted there
|
|
162
|
+
/// ends up before the widget.
|
|
163
|
+
///
|
|
164
|
+
/// When there are multiple widgets at a given position, their
|
|
165
|
+
/// `side` values determine the order in which they appear. Those
|
|
166
|
+
/// with lower values appear first. The ordering of widgets with
|
|
167
|
+
/// the same `side` value is unspecified.
|
|
168
|
+
///
|
|
169
|
+
/// When `marks` is null, `side` also determines the marks that
|
|
170
|
+
/// the widget is wrapped in—those of the node before when
|
|
171
|
+
/// negative, those of the node after when positive.
|
|
172
|
+
side?: number
|
|
173
|
+
|
|
174
|
+
/// By default, the cursor, when at the position of the widget,
|
|
175
|
+
/// will be strictly kept on the side indicated by
|
|
176
|
+
/// [`side`](#view.Decoration^widget^spec.side). Set this to true
|
|
177
|
+
/// to allow the DOM selection to stay on the other side if the
|
|
178
|
+
/// client sets it there.
|
|
179
|
+
///
|
|
180
|
+
/// **Note**: Mapping of this decoration, which decides on which
|
|
181
|
+
/// side insertions at its position appear, will still happen
|
|
182
|
+
/// according to `side`, and keyboard cursor motion will not,
|
|
183
|
+
/// without further custom handling, visit both sides of the
|
|
184
|
+
/// widget.
|
|
185
|
+
relaxedSide?: boolean
|
|
186
|
+
|
|
187
|
+
/// The precise set of marks to draw around the widget.
|
|
188
|
+
marks?: readonly Mark[]
|
|
189
|
+
|
|
190
|
+
/// Can be used to control which DOM events, when they bubble out
|
|
191
|
+
/// of this widget, the editor view should ignore.
|
|
192
|
+
stopEvent?: (event: Event) => boolean
|
|
193
|
+
|
|
194
|
+
/// When set (defaults to false), selection changes inside the
|
|
195
|
+
/// widget are ignored, and don't cause ProseMirror to try and
|
|
196
|
+
/// re-sync the selection with its selection state.
|
|
197
|
+
ignoreSelection?: boolean
|
|
198
|
+
|
|
199
|
+
/// When comparing decorations of this type (in order to decide
|
|
200
|
+
/// whether it needs to be redrawn), ProseMirror will by default
|
|
201
|
+
/// compare the widget DOM node by identity. If you pass a key,
|
|
202
|
+
/// that key will be compared instead, which can be useful when
|
|
203
|
+
/// you generate decorations on the fly and don't want to store
|
|
204
|
+
/// and reuse DOM nodes. Make sure that any widgets with the same
|
|
205
|
+
/// key are interchangeable—if widgets differ in, for example,
|
|
206
|
+
/// the behavior of some event handler, they should get
|
|
207
|
+
/// different keys.
|
|
208
|
+
key?: string
|
|
209
|
+
|
|
210
|
+
/// Called when the widget decoration is removed or the editor is
|
|
211
|
+
/// destroyed.
|
|
212
|
+
destroy?: (node: DOMNode) => void
|
|
213
|
+
|
|
214
|
+
/// Specs allow arbitrary additional properties.
|
|
215
|
+
[key: string]: any
|
|
216
|
+
}): Decoration {
|
|
217
|
+
return new Decoration(pos, pos, new WidgetType(toDOM, spec))
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/// Creates an inline decoration, which adds the given attributes to
|
|
221
|
+
/// each inline node between `from` and `to`.
|
|
222
|
+
static inline(from: number, to: number, attrs: DecorationAttrs, spec?: {
|
|
223
|
+
/// Determines how the left side of the decoration is
|
|
224
|
+
/// [mapped](#transform.Position_Mapping) when content is
|
|
225
|
+
/// inserted directly at that position. By default, the decoration
|
|
226
|
+
/// won't include the new content, but you can set this to `true`
|
|
227
|
+
/// to make it inclusive.
|
|
228
|
+
inclusiveStart?: boolean
|
|
229
|
+
|
|
230
|
+
/// Determines how the right side of the decoration is mapped.
|
|
231
|
+
/// See
|
|
232
|
+
/// [`inclusiveStart`](#view.Decoration^inline^spec.inclusiveStart).
|
|
233
|
+
inclusiveEnd?: boolean
|
|
234
|
+
|
|
235
|
+
/// Specs may have arbitrary additional properties.
|
|
236
|
+
[key: string]: any
|
|
237
|
+
}) {
|
|
238
|
+
return new Decoration(from, to, new InlineType(attrs, spec))
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/// Creates a node decoration. `from` and `to` should point precisely
|
|
242
|
+
/// before and after a node in the document. That node, and only that
|
|
243
|
+
/// node, will receive the given attributes.
|
|
244
|
+
static node(from: number, to: number, attrs: DecorationAttrs, spec?: any) {
|
|
245
|
+
return new Decoration(from, to, new NodeType(attrs, spec))
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/// The spec provided when creating this decoration. Can be useful
|
|
249
|
+
/// if you've stored extra information in that object.
|
|
250
|
+
get spec() { return this.type.spec }
|
|
251
|
+
|
|
252
|
+
/// @internal
|
|
253
|
+
get inline() { return this.type instanceof InlineType }
|
|
254
|
+
|
|
255
|
+
/// @internal
|
|
256
|
+
get widget() { return this.type instanceof WidgetType }
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/// A set of attributes to add to a decorated node. Most properties
|
|
260
|
+
/// simply directly correspond to DOM attributes of the same name,
|
|
261
|
+
/// which will be set to the property's value. These are exceptions:
|
|
262
|
+
export type DecorationAttrs = {
|
|
263
|
+
/// When non-null, the target node is wrapped in a DOM element of
|
|
264
|
+
/// this type (and the other attributes are applied to this element).
|
|
265
|
+
nodeName?: string
|
|
266
|
+
|
|
267
|
+
/// A CSS class name or a space-separated set of class names to be
|
|
268
|
+
/// _added_ to the classes that the node already had.
|
|
269
|
+
class?: string
|
|
270
|
+
|
|
271
|
+
/// A string of CSS to be _added_ to the node's existing `style` property.
|
|
272
|
+
style?: string
|
|
273
|
+
|
|
274
|
+
/// Any other properties are treated as regular DOM attributes.
|
|
275
|
+
[attribute: string]: string | undefined
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const none: readonly any[] = [], noSpec = {}
|
|
279
|
+
|
|
280
|
+
/// An object that can [provide](#view.EditorProps.decorations)
|
|
281
|
+
/// decorations. Implemented by [`DecorationSet`](#view.DecorationSet),
|
|
282
|
+
/// and passed to [node views](#view.EditorProps.nodeViews).
|
|
283
|
+
export interface DecorationSource {
|
|
284
|
+
/// Map the set of decorations in response to a change in the
|
|
285
|
+
/// document.
|
|
286
|
+
map: (mapping: Mapping, node: Node) => DecorationSource
|
|
287
|
+
/// @internal
|
|
288
|
+
locals(node: Node): readonly Decoration[]
|
|
289
|
+
/// Extract a DecorationSource containing decorations for the given child node at the given offset.
|
|
290
|
+
forChild(offset: number, child: Node): DecorationSource
|
|
291
|
+
/// @internal
|
|
292
|
+
eq(other: DecorationSource): boolean
|
|
293
|
+
/// Call the given function for each decoration set in the group.
|
|
294
|
+
forEachSet(f: (set: DecorationSet) => void): void
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/// A collection of [decorations](#view.Decoration), organized in such
|
|
298
|
+
/// a way that the drawing algorithm can efficiently use and compare
|
|
299
|
+
/// them. This is a persistent data structure—it is not modified,
|
|
300
|
+
/// updates create a new value.
|
|
301
|
+
export class DecorationSet implements DecorationSource {
|
|
302
|
+
/// @internal
|
|
303
|
+
local: readonly Decoration[]
|
|
304
|
+
/// @internal
|
|
305
|
+
children: readonly (number | DecorationSet)[]
|
|
306
|
+
// The inline decorations in `local`, computed on the first call to
|
|
307
|
+
// `forChild`. Only those can apply to a child node.
|
|
308
|
+
private inlineLocal: readonly Decoration[] | null = null
|
|
309
|
+
|
|
310
|
+
/// @internal
|
|
311
|
+
constructor(local: readonly Decoration[], children: readonly (number | DecorationSet)[]) {
|
|
312
|
+
this.local = local.length ? local : none
|
|
313
|
+
this.children = children.length ? children : none
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/// Create a set of decorations, using the structure of the given
|
|
317
|
+
/// document. This will consume (modify) the `decorations` array, so
|
|
318
|
+
/// you must make a copy if you want need to preserve that.
|
|
319
|
+
static create(doc: Node, decorations: Decoration[]) {
|
|
320
|
+
return decorations.length ? buildTree(decorations, doc, 0, noSpec) : empty
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/// Find all decorations in this set which touch the given range
|
|
324
|
+
/// (including decorations that start or end directly at the
|
|
325
|
+
/// boundaries) and match the given predicate on their spec. When
|
|
326
|
+
/// `start` and `end` are omitted, all decorations in the set are
|
|
327
|
+
/// considered. When `predicate` isn't given, all decorations are
|
|
328
|
+
/// assumed to match.
|
|
329
|
+
find(start?: number, end?: number, predicate?: (spec: any) => boolean): Decoration[] {
|
|
330
|
+
let result: Decoration[] = []
|
|
331
|
+
this.findInner(start == null ? 0 : start, end == null ? 1e9 : end, result, 0, predicate)
|
|
332
|
+
return result
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
private findInner(start: number, end: number, result: Decoration[], offset: number, predicate?: (spec: any) => boolean) {
|
|
336
|
+
for (let i = 0; i < this.local.length; i++) {
|
|
337
|
+
let span = this.local[i]
|
|
338
|
+
// Spans are sorted by start position.
|
|
339
|
+
if (span.from > end) break
|
|
340
|
+
if (span.to >= start && (!predicate || predicate(span.spec)))
|
|
341
|
+
result.push(span.copy(span.from + offset, span.to + offset))
|
|
342
|
+
}
|
|
343
|
+
for (let i = 0; i < this.children.length; i += 3) {
|
|
344
|
+
// Children are sorted by start position.
|
|
345
|
+
if ((this.children[i] as number) >= end) break
|
|
346
|
+
if ((this.children[i + 1] as number) > start) {
|
|
347
|
+
let childOff = (this.children[i] as number) + 1
|
|
348
|
+
;(this.children[i + 2] as DecorationSet).findInner(start - childOff, end - childOff,
|
|
349
|
+
result, offset + childOff, predicate)
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/// Map the set of decorations in response to a change in the
|
|
355
|
+
/// document.
|
|
356
|
+
map(mapping: Mapping, doc: Node, options?: {
|
|
357
|
+
/// When given, this function will be called for each decoration
|
|
358
|
+
/// that gets dropped as a result of the mapping, passing the
|
|
359
|
+
/// spec of that decoration.
|
|
360
|
+
onRemove?: (decorationSpec: any) => void
|
|
361
|
+
}) {
|
|
362
|
+
if (this == empty || mapping.maps.length == 0) return this
|
|
363
|
+
return this.mapInner(mapping, doc, 0, 0, options || noSpec)
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/// @internal
|
|
367
|
+
mapInner(mapping: Mapping, node: Node, offset: number, oldOffset: number, options: {
|
|
368
|
+
onRemove?: (decorationSpec: any) => void
|
|
369
|
+
}) {
|
|
370
|
+
let newLocal: Decoration[] | undefined
|
|
371
|
+
if (this.local.length) {
|
|
372
|
+
let cur = new FragmentCursor(node.content)
|
|
373
|
+
for (let i = 0; i < this.local.length; i++) {
|
|
374
|
+
let mapped = this.local[i].map(mapping, offset, oldOffset)
|
|
375
|
+
if (mapped && mapped.type.valid(node, mapped, cur)) (newLocal || (newLocal = [])).push(mapped)
|
|
376
|
+
else if (options.onRemove) options.onRemove(this.local[i].spec)
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
if (this.children.length)
|
|
381
|
+
return mapChildren(this.children, newLocal || [], mapping, node, offset, oldOffset, options)
|
|
382
|
+
else
|
|
383
|
+
return newLocal ? new DecorationSet(newLocal.sort(byPos), none) : empty
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/// Add the given array of decorations to the ones in the set,
|
|
387
|
+
/// producing a new set. Consumes the `decorations` array. Needs
|
|
388
|
+
/// access to the current document to create the appropriate tree
|
|
389
|
+
/// structure.
|
|
390
|
+
add(doc: Node, decorations: Decoration[]) {
|
|
391
|
+
if (!decorations.length) return this
|
|
392
|
+
if (this == empty) return DecorationSet.create(doc, decorations)
|
|
393
|
+
return this.addInner(doc, decorations, 0)
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
private addInner(doc: Node, decorations: Decoration[], offset: number) {
|
|
397
|
+
let children: (number | DecorationSet)[] | undefined, childIndex = 0
|
|
398
|
+
doc.forEach((childNode, childOffset) => {
|
|
399
|
+
let baseOffset = childOffset + offset, found
|
|
400
|
+
if (!(found = takeSpansForNode(decorations, childNode, baseOffset))) return
|
|
401
|
+
|
|
402
|
+
if (!children) children = this.children.slice()
|
|
403
|
+
while (childIndex < children.length && (children[childIndex] as number) < childOffset) childIndex += 3
|
|
404
|
+
if (children[childIndex] == childOffset)
|
|
405
|
+
children[childIndex + 2] = (children[childIndex + 2] as DecorationSet).addInner(childNode, found, baseOffset + 1)
|
|
406
|
+
else
|
|
407
|
+
children.splice(childIndex, 0, childOffset, childOffset + childNode.nodeSize, buildTree(found, childNode, baseOffset + 1, noSpec))
|
|
408
|
+
childIndex += 3
|
|
409
|
+
})
|
|
410
|
+
|
|
411
|
+
let local = moveSpans(childIndex ? withoutNulls(decorations) : decorations, -offset).sort(byPos)
|
|
412
|
+
if (local.length) {
|
|
413
|
+
let cur = new FragmentCursor(doc.content)
|
|
414
|
+
for (let i = 0; i < local.length; i++) if (!local[i].type.valid(doc, local[i], cur)) local.splice(i--, 1)
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
return new DecorationSet(local.length ? this.local.concat(local).sort(byPos) : this.local,
|
|
418
|
+
children || this.children)
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/// Create a new set that contains the decorations in this set, minus
|
|
422
|
+
/// the ones in the given array.
|
|
423
|
+
remove(decorations: Decoration[]) {
|
|
424
|
+
if (decorations.length == 0 || this == empty) return this
|
|
425
|
+
return this.removeInner(decorations, 0)
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
private removeInner(decorations: (Decoration | null)[], offset: number) {
|
|
429
|
+
let children = this.children as (number | DecorationSet)[], local = this.local as Decoration[]
|
|
430
|
+
for (let i = 0; i < children.length; i += 3) {
|
|
431
|
+
let found: Decoration[] | undefined
|
|
432
|
+
let from = (children[i] as number) + offset, to = (children[i + 1] as number) + offset
|
|
433
|
+
for (let j = 0, span; j < decorations.length; j++) if (span = decorations[j]) {
|
|
434
|
+
if (span.from > from && span.to < to) {
|
|
435
|
+
decorations[j] = null
|
|
436
|
+
;(found || (found = [])).push(span)
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
if (!found) continue
|
|
440
|
+
if (children == this.children) children = this.children.slice()
|
|
441
|
+
let removed = (children[i + 2] as DecorationSet).removeInner(found, from + 1)
|
|
442
|
+
if (removed != empty) {
|
|
443
|
+
children[i + 2] = removed
|
|
444
|
+
} else {
|
|
445
|
+
children.splice(i, 3)
|
|
446
|
+
i -= 3
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
if (local.length) for (let i = 0, span; i < decorations.length; i++) if (span = decorations[i]) {
|
|
450
|
+
for (let j = 0; j < local.length; j++) if (local[j].eq(span, offset)) {
|
|
451
|
+
if (local == this.local) local = this.local.slice()
|
|
452
|
+
local.splice(j--, 1)
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
if (children == this.children && local == this.local) return this
|
|
456
|
+
return local.length || children.length ? new DecorationSet(local, children) : empty
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
forChild(offset: number, node: Node): DecorationSet | DecorationGroup {
|
|
460
|
+
if (this == empty) return this
|
|
461
|
+
if (node.isLeaf) return DecorationSet.empty
|
|
462
|
+
|
|
463
|
+
let child, local: Decoration[] | undefined
|
|
464
|
+
// Children are sorted triples of start, end, and decoration set.
|
|
465
|
+
let low = 0, high = this.children.length / 3
|
|
466
|
+
while (low < high) {
|
|
467
|
+
let mid = (low + high) >>> 1, index = mid * 3, start = this.children[index] as number
|
|
468
|
+
if (start < offset) {
|
|
469
|
+
low = mid + 1
|
|
470
|
+
} else if (start > offset) {
|
|
471
|
+
high = mid
|
|
472
|
+
} else {
|
|
473
|
+
child = this.children[index + 2] as DecorationSet
|
|
474
|
+
break
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
let start = offset + 1, end = start + node.content.size
|
|
478
|
+
let inline = this.inlineLocal || (this.inlineLocal = this.local.filter(InlineType.is))
|
|
479
|
+
for (let i = 0; i < inline.length; i++) {
|
|
480
|
+
let dec = inline[i]
|
|
481
|
+
if (dec.from < end && dec.to > start) {
|
|
482
|
+
let from = Math.max(start, dec.from) - start, to = Math.min(end, dec.to) - start
|
|
483
|
+
if (from < to) (local || (local = [])).push(dec.copy(from, to))
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
if (local) {
|
|
487
|
+
let localSet = new DecorationSet(local.sort(byPos), none)
|
|
488
|
+
return child ? new DecorationGroup([localSet, child]) : localSet
|
|
489
|
+
}
|
|
490
|
+
return child || empty
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/// @internal
|
|
494
|
+
eq(other: DecorationSet) {
|
|
495
|
+
if (this == other) return true
|
|
496
|
+
if (!(other instanceof DecorationSet) ||
|
|
497
|
+
this.local.length != other.local.length ||
|
|
498
|
+
this.children.length != other.children.length) return false
|
|
499
|
+
for (let i = 0; i < this.local.length; i++)
|
|
500
|
+
if (!this.local[i].eq(other.local[i])) return false
|
|
501
|
+
for (let i = 0; i < this.children.length; i += 3)
|
|
502
|
+
if (this.children[i] != other.children[i] ||
|
|
503
|
+
this.children[i + 1] != other.children[i + 1] ||
|
|
504
|
+
!(this.children[i + 2] as DecorationSet).eq(other.children[i + 2] as DecorationSet))
|
|
505
|
+
return false
|
|
506
|
+
return true
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/// @internal
|
|
510
|
+
locals(node: Node) {
|
|
511
|
+
return removeOverlap(this.localsInner(node))
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/// @internal
|
|
515
|
+
localsInner(node: Node): readonly Decoration[] {
|
|
516
|
+
if (this == empty) return none
|
|
517
|
+
if (node.inlineContent || !this.local.some(InlineType.is)) return this.local
|
|
518
|
+
let result = []
|
|
519
|
+
for (let i = 0; i < this.local.length; i++) {
|
|
520
|
+
if (!(this.local[i].type instanceof InlineType))
|
|
521
|
+
result.push(this.local[i])
|
|
522
|
+
}
|
|
523
|
+
return result
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/// The empty set of decorations.
|
|
527
|
+
static empty: DecorationSet = new DecorationSet([], [])
|
|
528
|
+
|
|
529
|
+
/// @internal
|
|
530
|
+
static removeOverlap = removeOverlap
|
|
531
|
+
|
|
532
|
+
forEachSet(f: (set: DecorationSet) => void) { f(this) }
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
const empty = DecorationSet.empty
|
|
536
|
+
|
|
537
|
+
// An abstraction that allows the code dealing with decorations to
|
|
538
|
+
// treat multiple DecorationSet objects as if it were a single object
|
|
539
|
+
// with (a subset of) the same interface.
|
|
540
|
+
class DecorationGroup implements DecorationSource {
|
|
541
|
+
constructor(readonly members: readonly DecorationSet[]) {}
|
|
542
|
+
|
|
543
|
+
map(mapping: Mapping, doc: Node) {
|
|
544
|
+
const mappedDecos = this.members.map(
|
|
545
|
+
member => member.map(mapping, doc, noSpec)
|
|
546
|
+
)
|
|
547
|
+
return DecorationGroup.from(mappedDecos)
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
forChild(offset: number, child: Node) {
|
|
551
|
+
if (child.isLeaf) return DecorationSet.empty
|
|
552
|
+
let found: DecorationSet[] = []
|
|
553
|
+
for (let i = 0; i < this.members.length; i++) {
|
|
554
|
+
let result = this.members[i].forChild(offset, child)
|
|
555
|
+
if (result == empty) continue
|
|
556
|
+
if (result instanceof DecorationGroup) found = found.concat(result.members)
|
|
557
|
+
else found.push(result)
|
|
558
|
+
}
|
|
559
|
+
return DecorationGroup.from(found)
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
eq(other: DecorationGroup) {
|
|
563
|
+
if (!(other instanceof DecorationGroup) ||
|
|
564
|
+
other.members.length != this.members.length) return false
|
|
565
|
+
for (let i = 0; i < this.members.length; i++)
|
|
566
|
+
if (!this.members[i].eq(other.members[i])) return false
|
|
567
|
+
return true
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
locals(node: Node) {
|
|
571
|
+
let result: Decoration[] | undefined, sorted = true
|
|
572
|
+
for (let i = 0; i < this.members.length; i++) {
|
|
573
|
+
let locals = this.members[i].localsInner(node)
|
|
574
|
+
if (!locals.length) continue
|
|
575
|
+
if (!result) {
|
|
576
|
+
result = locals as Decoration[]
|
|
577
|
+
} else {
|
|
578
|
+
if (sorted) {
|
|
579
|
+
result = result.slice()
|
|
580
|
+
sorted = false
|
|
581
|
+
}
|
|
582
|
+
for (let j = 0; j < locals.length; j++) result.push(locals[j])
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
return result ? removeOverlap(sorted ? result : result.sort(byPos)) : none
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
// Create a group for the given array of decoration sets, or return
|
|
589
|
+
// a single set when possible.
|
|
590
|
+
static from(members: readonly DecorationSource[]): DecorationSource {
|
|
591
|
+
switch (members.length) {
|
|
592
|
+
case 0: return empty
|
|
593
|
+
case 1: return members[0]
|
|
594
|
+
default: return new DecorationGroup(
|
|
595
|
+
members.every(m => m instanceof DecorationSet) ? members as DecorationSet[] :
|
|
596
|
+
members.reduce((r, m) => r.concat(m instanceof DecorationSet ? m : (m as DecorationGroup).members),
|
|
597
|
+
[] as DecorationSet[]))
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
forEachSet(f: (set: DecorationSet) => void) {
|
|
602
|
+
for (let i = 0; i < this.members.length; i++) this.members[i].forEachSet(f)
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
function mapChildren(
|
|
607
|
+
oldChildren: readonly (number | DecorationSet)[],
|
|
608
|
+
newLocal: Decoration[],
|
|
609
|
+
mapping: Mapping,
|
|
610
|
+
node: Node,
|
|
611
|
+
offset: number,
|
|
612
|
+
oldOffset: number,
|
|
613
|
+
options: {onRemove?: (decorationSpec: any) => void}
|
|
614
|
+
) {
|
|
615
|
+
let children = oldChildren.slice() as (number | DecorationSet)[]
|
|
616
|
+
|
|
617
|
+
// Mark the children that are directly touched by changes, and
|
|
618
|
+
// move those that are after the changes.
|
|
619
|
+
for (let i = 0, baseOffset = oldOffset; i < mapping.maps.length; i++) {
|
|
620
|
+
let moved = 0
|
|
621
|
+
mapping.maps[i].forEach((oldStart: number, oldEnd: number, newStart: number, newEnd: number) => {
|
|
622
|
+
let dSize = (newEnd - newStart) - (oldEnd - oldStart)
|
|
623
|
+
for (let i = 0; i < children.length; i += 3) {
|
|
624
|
+
let end = children[i + 1] as number
|
|
625
|
+
if (end < 0 || oldStart > end + baseOffset - moved) continue
|
|
626
|
+
let start = (children[i] as number) + baseOffset - moved
|
|
627
|
+
if (oldEnd >= start) {
|
|
628
|
+
children[i + 1] = oldStart <= start ? -2 : -1
|
|
629
|
+
} else if (oldStart >= baseOffset && dSize) {
|
|
630
|
+
;(children[i] as number) += dSize
|
|
631
|
+
;(children[i + 1] as number) += dSize
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
moved += dSize
|
|
635
|
+
})
|
|
636
|
+
baseOffset = mapping.maps[i].map(baseOffset, -1)
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
// Find the child nodes that still correspond to a single node,
|
|
640
|
+
// recursively call mapInner on them and update their positions.
|
|
641
|
+
let mustRebuild = false
|
|
642
|
+
for (let i = 0; i < children.length; i += 3) if ((children[i + 1] as number) < 0) { // Touched nodes
|
|
643
|
+
if (children[i + 1] == -2) {
|
|
644
|
+
mustRebuild = true
|
|
645
|
+
children[i + 1] = -1
|
|
646
|
+
continue
|
|
647
|
+
}
|
|
648
|
+
let from = mapping.map((oldChildren[i] as number) + oldOffset), fromLocal = from - offset
|
|
649
|
+
if (fromLocal < 0 || fromLocal >= node.content.size) {
|
|
650
|
+
mustRebuild = true
|
|
651
|
+
continue
|
|
652
|
+
}
|
|
653
|
+
// Must read oldChildren because children was tagged with -1
|
|
654
|
+
let to = mapping.map((oldChildren[i + 1] as number) + oldOffset, -1), toLocal = to - offset
|
|
655
|
+
let {index, offset: childOffset} = node.content.findIndex(fromLocal)
|
|
656
|
+
let childNode = node.maybeChild(index)
|
|
657
|
+
if (childNode && childOffset == fromLocal && childOffset + childNode.nodeSize == toLocal) {
|
|
658
|
+
let mapped = (children[i + 2] as DecorationSet)
|
|
659
|
+
.mapInner(mapping, childNode, from + 1, (oldChildren[i] as number) + oldOffset + 1, options)
|
|
660
|
+
if (mapped != empty) {
|
|
661
|
+
children[i] = fromLocal
|
|
662
|
+
children[i + 1] = toLocal
|
|
663
|
+
children[i + 2] = mapped
|
|
664
|
+
} else {
|
|
665
|
+
children[i + 1] = -2
|
|
666
|
+
mustRebuild = true
|
|
667
|
+
}
|
|
668
|
+
} else {
|
|
669
|
+
mustRebuild = true
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
// Remaining children must be collected and rebuilt into the appropriate structure
|
|
674
|
+
if (mustRebuild) {
|
|
675
|
+
let decorations = mapAndGatherRemainingDecorations(children, oldChildren, newLocal, mapping,
|
|
676
|
+
offset, oldOffset, options)
|
|
677
|
+
let built = buildTree(decorations, node, 0, options)
|
|
678
|
+
newLocal = built.local as Decoration[]
|
|
679
|
+
for (let i = 0; i < children.length; i += 3) if ((children[i + 1] as number) < 0) {
|
|
680
|
+
children.splice(i, 3)
|
|
681
|
+
i -= 3
|
|
682
|
+
}
|
|
683
|
+
for (let i = 0, j = 0; i < built.children.length; i += 3) {
|
|
684
|
+
let from = built.children[i]
|
|
685
|
+
while (j < children.length && children[j] < from) j += 3
|
|
686
|
+
children.splice(j, 0, built.children[i], built.children[i + 1], built.children[i + 2])
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
return new DecorationSet(newLocal.sort(byPos), children)
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
function moveSpans(spans: Decoration[], offset: number) {
|
|
694
|
+
if (!offset || !spans.length) return spans
|
|
695
|
+
let result = []
|
|
696
|
+
for (let i = 0; i < spans.length; i++) {
|
|
697
|
+
let span = spans[i]
|
|
698
|
+
result.push(new Decoration(span.from + offset, span.to + offset, span.type))
|
|
699
|
+
}
|
|
700
|
+
return result
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
function mapAndGatherRemainingDecorations(
|
|
704
|
+
children: (number | DecorationSet)[],
|
|
705
|
+
oldChildren: readonly (number | DecorationSet)[],
|
|
706
|
+
decorations: Decoration[],
|
|
707
|
+
mapping: Mapping,
|
|
708
|
+
offset: number,
|
|
709
|
+
oldOffset: number,
|
|
710
|
+
options: {onRemove?: (decorationSpec: any) => void}
|
|
711
|
+
) {
|
|
712
|
+
// Gather all decorations from the remaining marked children
|
|
713
|
+
function gather(set: DecorationSet, oldOffset: number) {
|
|
714
|
+
for (let i = 0; i < set.local.length; i++) {
|
|
715
|
+
let mapped = set.local[i].map(mapping, offset, oldOffset)
|
|
716
|
+
if (mapped) decorations.push(mapped)
|
|
717
|
+
else if (options.onRemove) options.onRemove(set.local[i].spec)
|
|
718
|
+
}
|
|
719
|
+
for (let i = 0; i < set.children.length; i += 3)
|
|
720
|
+
gather(set.children[i + 2] as DecorationSet, set.children[i] as number + oldOffset + 1)
|
|
721
|
+
}
|
|
722
|
+
for (let i = 0; i < children.length; i += 3) if (children[i + 1] == -1)
|
|
723
|
+
gather(children[i + 2] as DecorationSet, oldChildren[i] as number + oldOffset + 1)
|
|
724
|
+
|
|
725
|
+
return decorations
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
function takeSpansForNode(spans: (Decoration | null)[], node: Node, offset: number): Decoration[] | null {
|
|
729
|
+
if (node.isLeaf) return null
|
|
730
|
+
let end = offset + node.nodeSize, found = null
|
|
731
|
+
for (let i = 0, span; i < spans.length; i++) {
|
|
732
|
+
if ((span = spans[i]) && span.from > offset && span.to < end) {
|
|
733
|
+
;(found || (found = [])).push(span)
|
|
734
|
+
spans[i] = null
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
return found
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
function withoutNulls<T>(array: readonly (T | null)[]): T[] {
|
|
741
|
+
let result: T[] = []
|
|
742
|
+
for (let i = 0; i < array.length; i++)
|
|
743
|
+
if (array[i] != null) result.push(array[i]!)
|
|
744
|
+
return result
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
// Build up a tree that corresponds to a set of decorations. `offset`
|
|
748
|
+
// is a base offset that should be subtracted from the `from` and `to`
|
|
749
|
+
// positions in the spans (so that we don't have to allocate new spans
|
|
750
|
+
// for recursive calls).
|
|
751
|
+
function buildTree(
|
|
752
|
+
spans: Decoration[],
|
|
753
|
+
node: Node,
|
|
754
|
+
offset: number,
|
|
755
|
+
options: {onRemove?: (decorationSpec: any) => void}
|
|
756
|
+
) {
|
|
757
|
+
let children: (DecorationSet | number)[] = [], hasNulls = false
|
|
758
|
+
node.forEach((childNode, localStart) => {
|
|
759
|
+
let found = takeSpansForNode(spans, childNode, localStart + offset)
|
|
760
|
+
if (found) {
|
|
761
|
+
hasNulls = true
|
|
762
|
+
let subtree = buildTree(found, childNode, offset + localStart + 1, options)
|
|
763
|
+
if (subtree != empty)
|
|
764
|
+
children.push(localStart, localStart + childNode.nodeSize, subtree)
|
|
765
|
+
}
|
|
766
|
+
})
|
|
767
|
+
let locals = moveSpans(hasNulls ? withoutNulls(spans) : spans, -offset).sort(byPos)
|
|
768
|
+
if (locals.length) {
|
|
769
|
+
let cur = new FragmentCursor(node.content)
|
|
770
|
+
for (let i = 0; i < locals.length; i++) if (!locals[i].type.valid(node, locals[i], cur)) {
|
|
771
|
+
if (options.onRemove) options.onRemove(locals[i].spec)
|
|
772
|
+
locals.splice(i--, 1)
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
return locals.length || children.length ? new DecorationSet(locals, children) : empty
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
// Used to sort decorations so that ones with a low start position
|
|
779
|
+
// come first, and within a set with the same start position, those
|
|
780
|
+
// with an smaller end position come first.
|
|
781
|
+
function byPos(a: Decoration, b: Decoration) {
|
|
782
|
+
return a.from - b.from || a.to - b.to
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
// Scan a sorted array of decorations for partially overlapping spans,
|
|
786
|
+
// and split those so that only fully overlapping spans are left (to
|
|
787
|
+
// make subsequent rendering easier). Will return the input array if
|
|
788
|
+
// no partially overlapping spans are found (the common case).
|
|
789
|
+
function removeOverlap(spans: readonly Decoration[]): Decoration[] {
|
|
790
|
+
let working: Decoration[] = spans as Decoration[]
|
|
791
|
+
for (let i = 0; i < working.length - 1; i++) {
|
|
792
|
+
let span = working[i]
|
|
793
|
+
if (span.from != span.to) for (let j = i + 1; j < working.length; j++) {
|
|
794
|
+
let next = working[j]
|
|
795
|
+
if (next.from == span.from) {
|
|
796
|
+
if (next.to != span.to) {
|
|
797
|
+
if (working == spans) working = spans.slice()
|
|
798
|
+
// Followed by a partially overlapping larger span. Split that
|
|
799
|
+
// span.
|
|
800
|
+
working[j] = next.copy(next.from, span.to)
|
|
801
|
+
insertAhead(working, j + 1, next.copy(span.to, next.to))
|
|
802
|
+
}
|
|
803
|
+
continue
|
|
804
|
+
} else {
|
|
805
|
+
if (next.from < span.to) {
|
|
806
|
+
if (working == spans) working = spans.slice()
|
|
807
|
+
// The end of this one overlaps with a subsequent span. Split
|
|
808
|
+
// this one.
|
|
809
|
+
working[i] = span.copy(span.from, next.from)
|
|
810
|
+
insertAhead(working, j, span.copy(next.from, span.to))
|
|
811
|
+
}
|
|
812
|
+
break
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
return working
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
function insertAhead(array: Decoration[], i: number, deco: Decoration) {
|
|
820
|
+
while (i < array.length && byPos(deco, array[i]) > 0) i++
|
|
821
|
+
array.splice(i, 0, deco)
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
// Get the decorations associated with the current props of a view.
|
|
825
|
+
export function viewDecorations(view: EditorView): DecorationSource {
|
|
826
|
+
let found: DecorationSource[] = []
|
|
827
|
+
view.someProp("decorations", f => {
|
|
828
|
+
let result = f(view.state)
|
|
829
|
+
if (result && result != empty) found.push(result)
|
|
830
|
+
})
|
|
831
|
+
if (view.cursorWrapper)
|
|
832
|
+
found.push(DecorationSet.create(view.state.doc, [view.cursorWrapper.deco]))
|
|
833
|
+
return DecorationGroup.from(found)
|
|
834
|
+
}
|