forked from ProseMirror/prosemirror-collab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-rebase.js
168 lines (143 loc) · 5.68 KB
/
test-rebase.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
const {Transform} = require("prosemirror-transform")
const ist = require("ist")
const {schema, eq, doc, blockquote, p, li, ul, em} = require("prosemirror-model/test/build")
const {rebaseSteps} = require("../dist/collab")
function runRebase(transforms, expected) {
let start = transforms[0].before, full = new Transform(start)
transforms.forEach(transform => {
let rebased = new Transform(transform.doc)
let start = rebaseSteps(rebased, transform.steps,
transform.steps.map((s, i) => s.invert(transform.docs[i])),
full.steps)
for (let i = start; i < rebased.steps.length; i++) full.step(rebased.steps[i])
})
ist(full.doc, expected, eq)
for (let tag in start.tag) {
let mapped = full.mapping.mapResult(start.tag[tag])
let exp = expected.tag[tag]
if (mapped.deleted) {
if (exp) throw new Error("Tag " + tag + " was unexpectedly deleted")
} else {
if (!exp) throw new Error("Tag " + tag + " is not actually deleted")
ist(mapped.pos, exp)
}
}
}
function permute(array) {
if (array.length < 2) return [array]
let result = []
for (let i = 0; i < array.length; i++) {
let others = permute(array.slice(0, i).concat(array.slice(i + 1)))
for (let j = 0; j < others.length; j++)
result.push([array[i]].concat(others[j]))
}
return result
}
function rebase(doc, ...clients) {
let expected = clients.pop()
runRebase(clients.map(cl => cl(new Transform(doc))), expected)
}
function rebase$(doc, ...clients) {
let expected = clients.pop()
permute(clients.map(cl => cl(new Transform(doc)))).forEach(transforms => runRebase(transforms, expected))
}
function type(tr, pos, text) {
return tr.replaceWith(pos, pos, schema.text(text))
}
function wrap(tr, pos, type) {
let $pos = tr.doc.resolve(pos)
return tr.wrap($pos.blockRange($pos), [{type: schema.nodes[type]}])
}
describe("rebaseSteps", () => {
it("supports concurrent typing", () => {
rebase$(doc(p("h<1>ell<2>o")),
tr => type(tr, 2, "X"),
tr => type(tr, 5, "Y"),
doc(p("hX<1>ellY<2>o")))
})
it("support multiple concurrently typed chars", () => {
rebase$(doc(p("h<1>ell<2>o")),
tr => type(type(type(tr, 2, "X"), 3, "Y"), 4, "Z"),
tr => type(type(tr, 5, "U"), 6, "V"),
doc(p("hXYZ<1>ellUV<2>o")))
})
it("supports three concurrent typers", () => {
rebase$(doc(p("h<1>ell<2>o th<3>ere")),
tr => type(tr, 2, "X"),
tr => type(tr, 5, "Y"),
tr => type(tr, 9, "Z"),
doc(p("hX<1>ellY<2>o thZ<3>ere")))
})
it("handles wrapping of changed blocks", () => {
rebase$(doc(p("<1>hell<2>o<3>")),
tr => type(tr, 5, "X"),
tr => wrap(tr, 1, "blockquote"),
doc(blockquote(p("<1>hellX<2>o<3>"))))
})
it("handles insertions in deleted content", () => {
rebase$(doc(p("hello<1> wo<2>rld<3>!")),
tr => tr.delete(6, 12),
tr => type(tr, 9, "X"),
doc(p("hello<3>!")))
})
it("allows deleting the same content twice", () => {
rebase(doc(p("hello<1> wo<2>rld<3>!")),
tr => tr.delete(6, 12),
tr => tr.delete(6, 12),
doc(p("hello<3>!")))
})
it("isn't confused by joining a block that's being edited", () => {
rebase$(doc(ul(li(p("one")), "<1>", li(p("tw<2>o")))),
tr => type(tr, 12, "A"),
tr => tr.join(8),
doc(ul(li(p("one"), p("twA<2>o")))))
})
it("supports typing concurrently with marking", () => {
rebase(doc(p("hello <1>wo<2>rld<3>")),
tr => tr.addMark(7, 12, schema.mark("em")),
tr => type(tr, 9, "_"),
doc(p("hello <1>", em("wo"), "_<2>", em("rld<3>"))))
})
it("doesn't unmark marks added concurrently", () => {
rebase(doc(p(em("<1>hello"), " world<2>")),
tr => tr.addMark(1, 12, schema.mark("em")),
tr => tr.removeMark(1, 12, schema.mark("em")),
doc(p("<1>hello", em(" world<2>"))))
})
it("doesn't mark concurrently unmarked text", () => {
rebase(doc(p("<1>hello ", em("world<2>"))),
tr => tr.removeMark(1, 12, schema.mark("em")),
tr => tr.addMark(1, 12, schema.mark("em")),
doc(p(em("<1>hello "), "world<2>")))
})
it("deletes inserts in replaced context", () => {
rebase(doc(p("b<before>efore"), blockquote(ul(li(p("o<1>ne")), li(p("t<2>wo")), li(p("thr<3>ee")))), p("a<after>fter")),
tr => tr.replace(tr.doc.tag[1], tr.doc.tag[3], doc(p("a"), blockquote(p("b")), p("c")).slice(2, 9)),
tr => type(tr, tr.doc.tag[2], "ayay"),
doc(p("b<before>efore"), blockquote(ul(li(p("o"), blockquote(p("b")), p("<3>ee")))), p("a<after>fter")))
})
it("maps through inserts", () => {
rebase$(doc(p("X<1>X<2>X")),
tr => type(tr, 2, "hello"),
tr => type(tr, 3, "goodbye").delete(4, 7),
doc(p("Xhello<1>Xgbye<2>X")))
})
it("handle concurrent removal of blocks", () => {
rebase(doc(p("a"), "<1>", p("b"), "<2>", p("c")),
tr => tr.delete(tr.doc.tag[1], tr.doc.tag[2]),
tr => tr.delete(tr.doc.tag[1], tr.doc.tag[2]),
doc(p("a"), "<2>", p("c")))
})
it("discards edits in removed blocks", () => {
rebase$(doc(p("a"), "<1>", p("b<2>"), "<3>", p("c")),
tr => tr.delete(tr.doc.tag[1], tr.doc.tag[3]),
tr => type(tr, tr.doc.tag[2], "ay"),
doc(p("a"), "<3>", p("c")))
})
it("preserves double block inserts", () => {
rebase(doc(p("a"), "<1>", p("b")),
tr => tr.replaceWith(3, 3, schema.node("paragraph")),
tr => tr.replaceWith(3, 3, schema.node("paragraph")),
doc(p("a"), p(), p(), "<1>", p("b")))
})
})