-
Notifications
You must be signed in to change notification settings - Fork 225
/
test-draw-decoration.js
433 lines (389 loc) · 16.4 KB
/
test-draw-decoration.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
const {doc, p, hr, em, strong, img, blockquote, schema} = require("prosemirror-test-builder")
const {Plugin, TextSelection} = require("prosemirror-state")
const {tempEditor} = require("./view")
const {DecorationSet, Decoration} = require("..")
const ist = require("ist")
function make(str) {
if (typeof str != "string") return str
let match = /^(\d+)(?:-(\d+))?-(.+)$/.exec(str)
if (match[3] == "widget") {
let widget = document.createElement("button")
widget.textContent = "ω"
return Decoration.widget(+match[1], widget)
}
return Decoration.inline(+match[1], +match[2], {class: match[3]})
}
function decoPlugin(decos) {
return new Plugin({
state: {
init(config) { return DecorationSet.create(config.doc, decos.map(make)) },
apply(tr, set, state) {
if (tr.docChanged) set = set.map(tr.mapping, tr.doc)
let change = tr.getMeta("updateDecorations")
if (change) {
if (change.remove) set = set.remove(change.remove)
if (change.add) set = set.add(state.doc, change.add)
}
return set
}
},
props: {
decorations(state) { return this.getState(state) }
}
})
}
function updateDeco(view, add, remove) {
view.dispatch(view.state.tr.setMeta("updateDecorations", {add, remove}))
}
describe("Decoration drawing", () => {
it("draws inline decorations", () => {
let view = tempEditor({doc: doc(p("foobar")),
plugins: [decoPlugin(["2-5-foo"])]})
let found = view.dom.querySelector(".foo")
ist(found)
ist(found.textContent, "oob")
})
it("draws wrapping decorations", () => {
let view = tempEditor({doc: doc(p("foo")),
plugins: [decoPlugin([Decoration.inline(1, 5, {nodeName: "i"})])]})
let found = view.dom.querySelector("i")
ist(found && found.innerHTML, "foo")
})
it("draws node decorations", () => {
let view = tempEditor({doc: doc(p("foo"), p("bar")),
plugins: [decoPlugin([Decoration.node(5, 10, {class: "cls"})])]})
let found = view.dom.querySelectorAll(".cls")
ist(found.length, 1)
ist(found[0].nodeName, "P")
ist(found[0].previousSibling.nodeName, "P")
})
it("can update multi-level wrapping decorations", () => {
let d2 = Decoration.inline(1, 5, {nodeName: "i", class: "b"})
let view = tempEditor({doc: doc(p("hello")),
plugins: [decoPlugin([Decoration.inline(1, 5, {nodeName: "i", class: "a"}), d2])]})
ist(view.dom.querySelectorAll("i").length, 2)
updateDeco(view, [Decoration.inline(1, 5, {nodeName: "i", class: "c"})], [d2])
let iNodes = view.dom.querySelectorAll("i")
ist(iNodes.length, 2)
ist(Array.prototype.map.call(iNodes, n => n.className).sort().join(), "a,c")
})
it("draws overlapping inline decorations", () => {
let view = tempEditor({doc: doc(p("abcdef")),
plugins: [decoPlugin(["3-5-foo", "4-6-bar", "1-7-baz"])]})
let baz = view.dom.querySelectorAll(".baz")
ist(baz.length, 5)
ist(Array.prototype.map.call(baz, x => x.textContent).join("-"), "ab-c-d-e-f")
function classes(n) { return n.className.split(" ").sort().join(" ") }
ist(classes(baz[1]), "baz foo")
ist(classes(baz[2]), "bar baz foo")
ist(classes(baz[3]), "bar baz")
})
it("draws multiple widgets", () => {
let view = tempEditor({doc: doc(p("foobar")),
plugins: [decoPlugin(["1-widget", "4-widget", "7-widget"])]})
let found = view.dom.querySelectorAll("button")
ist(found.length, 3)
ist(found[0].nextSibling.textContent, "foo")
ist(found[1].nextSibling.textContent, "bar")
ist(found[2].previousSibling.textContent, "bar")
})
it("orders widgets by their side option", () => {
let view = tempEditor({doc: doc(p("foobar")),
plugins: [decoPlugin([Decoration.widget(4, document.createTextNode("B")),
Decoration.widget(4, document.createTextNode("A"), {side: -100}),
Decoration.widget(4, document.createTextNode("C"), {side: 2})])]})
ist(view.dom.textContent, "fooABCbar")
})
it("draws a widget in an empty node", () => {
let view = tempEditor({doc: doc(p()),
plugins: [decoPlugin(["1-widget"])]})
ist(view.dom.querySelectorAll("button").length, 1)
})
it("draws widgets on node boundaries", () => {
let view = tempEditor({doc: doc(p("foo", em("bar"))),
plugins: [decoPlugin(["4-widget"])]})
ist(view.dom.querySelectorAll("button").length, 1)
})
it("draws decorations from multiple plugins", () => {
let view = tempEditor({doc: doc(p("foo", em("bar"))),
plugins: [decoPlugin(["2-widget"]), decoPlugin(["6-widget"])]})
ist(view.dom.querySelectorAll("button").length, 2)
})
it("draws inline decorations spanning multiple parents", () => {
let view = tempEditor({doc: doc(p("long first ", em("p"), "aragraph"), p("two")),
plugins: [decoPlugin(["7-25-foo"])]})
let foos = view.dom.querySelectorAll(".foo")
ist(foos.length, 4)
ist(foos[0].textContent, "irst ")
ist(foos[1].textContent, "p")
ist(foos[2].textContent, "aragraph")
ist(foos[3].textContent, "tw")
})
it("draws inline decorations across empty paragraphs", () => {
let view = tempEditor({doc: doc(p("first"), p(), p("second")),
plugins: [decoPlugin(["3-12-foo"])]})
let foos = view.dom.querySelectorAll(".foo")
ist(foos.length, 2)
ist(foos[0].textContent, "rst")
ist(foos[1].textContent, "se")
})
it("can handle inline decorations ending at the start or end of a node", () => {
let view = tempEditor({doc: doc(p(), p()),
plugins: [decoPlugin(["1-3-foo"])]})
ist(!view.dom.querySelector(".foo"))
})
it("can draw decorations with multiple classes", () => {
let view = tempEditor({doc: doc(p("foo")),
plugins: [decoPlugin(["1-4-foo bar"])]})
ist(view.dom.querySelectorAll(".foo").length, 1)
ist(view.dom.querySelectorAll(".bar").length, 1)
})
it("supports overlapping inline decorations", () => {
let view = tempEditor({doc: doc(p("foobar")),
plugins: [decoPlugin(["1-3-foo", "2-5-bar"])]})
let foos = view.dom.querySelectorAll(".foo")
let bars = view.dom.querySelectorAll(".bar")
ist(foos.length, 2)
ist(bars.length, 2)
ist(foos[0].textContent, "f")
ist(foos[1].textContent, "o")
ist(bars[0].textContent, "o")
ist(bars[1].textContent, "ob")
})
it("doesn't redraw when irrelevant decorations change", () => {
let view = tempEditor({doc: doc(p("foo"), p("baz")),
plugins: [decoPlugin(["7-8-foo"])]})
let para2 = view.dom.lastChild
updateDeco(view, [make("2-3-bar")])
ist(view.dom.lastChild, para2)
ist(view.dom.querySelector(".bar"))
})
it("doesn't redraw when irrelevant content changes", () => {
let view = tempEditor({doc: doc(p("foo"), p("baz")),
plugins: [decoPlugin(["7-8-foo"])]})
let para2 = view.dom.lastChild
view.dispatch(view.state.tr.delete(2, 3))
view.dispatch(view.state.tr.delete(2, 3))
ist(view.dom.lastChild, para2)
})
it("can add a widget on a node boundary", () => {
let view = tempEditor({doc: doc(p("foo", em("bar"))),
plugins: [decoPlugin([])]})
updateDeco(view, [make("4-widget")])
ist(view.dom.querySelectorAll("button").length, 1)
})
it("can remove a widget on a node boundary", () => {
let dec = make("4-widget")
let view = tempEditor({doc: doc(p("foo", em("bar"))),
plugins: [decoPlugin([dec])]})
updateDeco(view, null, [dec])
ist(view.dom.querySelector("button"), null)
})
it("can remove the class from a text node", () => {
let dec = make("1-4-foo")
let view = tempEditor({doc: doc(p("abc")),
plugins: [decoPlugin([dec])]})
ist(view.dom.querySelector(".foo"))
updateDeco(view, null, [dec])
ist(view.dom.querySelector(".foo"), null)
})
it("can remove the class from part of a text node", () => {
let dec = make("2-4-foo")
let view = tempEditor({doc: doc(p("abcd")),
plugins: [decoPlugin([dec])]})
ist(view.dom.querySelector(".foo"))
updateDeco(view, null, [dec])
ist(view.dom.querySelector(".foo"), null)
ist(view.dom.firstChild.innerHTML, "abcd")
})
it("can remove the class for part of a text node", () => {
let dec = make("2-4-foo")
let view = tempEditor({doc: doc(p("abcd")),
plugins: [decoPlugin([dec])]})
ist(view.dom.querySelector(".foo"))
updateDeco(view, [make("2-4-bar")], [dec])
ist(view.dom.querySelector(".foo"), null)
ist(view.dom.querySelector(".bar"))
})
it("draws a widget added in the middle of a text node", () => {
let view = tempEditor({doc: doc(p("foo")), plugins: [decoPlugin([])]})
updateDeco(view, [make("3-widget")])
ist(view.dom.firstChild.textContent, "foωo")
})
it("can update a text node around a widget", () => {
let view = tempEditor({doc: doc(p("bar")), plugins: [decoPlugin(["3-widget"])]})
view.dispatch(view.state.tr.delete(1, 2))
ist(view.dom.querySelectorAll("button").length, 1)
ist(view.dom.firstChild.textContent, "aωr")
})
it("can update a text node with an inline decoration", () => {
let view = tempEditor({doc: doc(p("bar")), plugins: [decoPlugin(["1-3-foo"])]})
view.dispatch(view.state.tr.delete(1, 2))
let foo = view.dom.querySelector(".foo")
ist(foo)
ist(foo.textContent, "a")
ist(foo.nextSibling.textContent, "r")
})
it("correctly redraws a partially decorated node when a widget is added", () => {
let view = tempEditor({doc: doc(p("one", em("two"))),
plugins: [decoPlugin(["1-6-foo"])]})
updateDeco(view, [make("6-widget")])
let foos = view.dom.querySelectorAll(".foo")
ist(foos.length, 2)
ist(foos[0].textContent, "one")
ist(foos[1].textContent, "tw")
})
it("correctly redraws when skipping split text node", () => {
let view = tempEditor({doc: doc(p("foo")),
plugins: [decoPlugin(["3-widget", "3-4-foo"])]})
updateDeco(view, [make("4-widget")])
ist(view.dom.querySelectorAll("button").length, 2)
})
it("drops removed node decorations from the view", () => {
let deco = Decoration.node(1, 6, {class: "cls"})
let view = tempEditor({doc: doc(blockquote(p("foo"), p("bar"))),
plugins: [decoPlugin([deco])]})
updateDeco(view, null, [deco])
ist(!view.dom.querySelector(".cls"))
})
it("can update a node's attributes without replacing the node", () => {
let deco = Decoration.node(0, 5, {title: "title", class: "foo"})
let view = tempEditor({doc: doc(p("foo")),
plugins: [decoPlugin([deco])]})
let para = view.dom.querySelector("p")
updateDeco(view, [Decoration.node(0, 5, {class: "foo bar"})], [deco])
ist(view.dom.querySelector("p"), para)
ist(para.className, "foo bar")
ist(!para.title)
})
it("can add and remove CSS custom properties from a node", () => {
let deco = Decoration.node(0, 5, {style: '--my-custom-property:36px'})
let view = tempEditor({doc: doc(p("foo")),
plugins: [decoPlugin([deco])]})
ist(view.dom.querySelector("p").style.getPropertyValue('--my-custom-property'), "36px")
updateDeco(view, null, [deco])
ist(view.dom.querySelector("p").style.getPropertyValue('--my-custom-property'), "")
})
it("updates decorated nodes even if a widget is added before them", () => {
let view = tempEditor({doc: doc(p("a"), p("b")), plugins: [decoPlugin([])]})
let lastP = view.dom.querySelectorAll("p")[1]
updateDeco(view, [make("3-widget"), Decoration.node(3, 6, {style: "color: red"})])
ist(lastP.style.color, "red")
})
it("can add and remove inline style", () => {
let deco = Decoration.inline(1, 6, {style: "color: rgba(0,10,200,.4); text-decoration: underline"})
let view = tempEditor({doc: doc(p("al", img, "lo")),
plugins: [decoPlugin([deco])]})
ist(/rgba/.test(view.dom.querySelector("img").style.color))
ist(view.dom.querySelector("img").previousSibling.style.textDecoration, "underline")
updateDeco(view, null, [deco])
ist(view.dom.querySelector("img").style.color, "")
ist(view.dom.querySelector("img").style.textDecoration, "")
})
it("passes decorations to a node view", () => {
let current = ""
let view = tempEditor({
doc: doc(p("foo"), hr),
plugins: [decoPlugin([])],
nodeViews: {horizontal_rule: () => ({
update(_, decos) {
current = decos.map(d => d.spec.name).join()
}
})}
})
let a = Decoration.node(5, 6, {}, {name: "a"})
updateDeco(view, [a], [])
ist(current, "a")
updateDeco(view, [Decoration.node(5, 6, {}, {name: "b"}),
Decoration.node(5, 6, {}, {name: "c"})], [a])
ist(current, "b,c")
})
it("draws the specified marks around a widget", () => {
let view = tempEditor({
doc: doc(p("foobar")),
plugins: [decoPlugin([Decoration.widget(4, document.createElement("img"), {marks: [schema.mark("em")]})])]
})
ist(view.dom.querySelector("em img"))
})
it("draws widgets inside the marks for their side", () => {
let view = tempEditor({
doc: doc(p(em("foo"), strong("bar"))),
plugins: [decoPlugin([Decoration.widget(4, document.createElement("img"), {side: -1})]),
decoPlugin([Decoration.widget(4, document.createElement("br"))]),
decoPlugin([Decoration.widget(7, document.createElement("span"))], {side: 1})]
})
ist(view.dom.querySelector("em img"))
ist(!view.dom.querySelector("strong img"))
ist(view.dom.querySelector("strong br"))
ist(!view.dom.querySelector("em br"))
ist(!view.dom.querySelector("strong span"))
})
it("draws decorations inside node views", () => {
let view = tempEditor({
doc: doc(p("foo")),
nodeViews: {paragraph() { let p = document.createElement("p"); return {dom: p, contentDOM: p} }},
plugins: [decoPlugin([Decoration.widget(2, document.createElement("img"))])]
})
ist(view.dom.querySelector("img"))
})
it("can delay widget drawing to render time", () => {
let view = tempEditor({
doc: doc(p("hi")),
decorations(state) {
return DecorationSet.create(state.doc, [Decoration.widget(3, view => {
ist(view.state, state)
let elt = document.createElement("span")
elt.textContent = "!"
return elt
})])
}
})
ist(view.dom.textContent, "hi!")
})
it("supports widgets querying their own position", () => {
let get
tempEditor({
doc: doc(p("hi")),
decorations(state) {
return DecorationSet.create(state.doc, [Decoration.widget(3, (_view, getPos) => {
ist(getPos(), 3)
get = getPos
return document.createElement("button")
})])
}
})
ist(get(), 3)
})
it("doesn't redraw widgets with matching keys", () => {
let view = tempEditor({
doc: doc(p("hi")),
decorations(state) {
return DecorationSet.create(state.doc, [Decoration.widget(2, document.createElement("button"), {key: "myButton"})])
}
})
let widgetDOM = view.dom.querySelector("button")
view.dispatch(view.state.tr.insertText("!", 2, 2))
ist(view.dom.querySelector("button"), widgetDOM)
})
it("doesn't redraw widgets with identical specs", () => {
let toDOM = () => document.createElement("button")
let view = tempEditor({
doc: doc(p("hi")),
decorations(state) {
return DecorationSet.create(state.doc, [Decoration.widget(2, toDOM, {side: 1})])
}
})
let widgetDOM = view.dom.querySelector("button")
view.dispatch(view.state.tr.insertText("!", 2, 2))
ist(view.dom.querySelector("button"), widgetDOM)
})
it("doesn't get confused by split text nodes", () => {
let view = tempEditor({doc: doc(p("abab")), decorations(state) {
return state.selection.from <= 1 ? null :
DecorationSet.create(view.state.doc, [Decoration.inline(1, 2, {class: "foo"}),
Decoration.inline(3, 4, {class: "foo"})])
}})
view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, 5)))
ist(view.dom.textContent, "abab")
})
})