forked from yjs/y-prosemirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathy-prosemirror.test.js
276 lines (251 loc) · 9.99 KB
/
y-prosemirror.test.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
import * as t from 'lib0/testing'
import * as prng from 'lib0/prng'
import * as math from 'lib0/math'
import * as Y from 'yjs'
// @ts-ignore
import { applyRandomTests } from 'yjs/testHelper'
import { ySyncPlugin, yUndoPlugin, undo, redo, prosemirrorJSONToYDoc, yDocToProsemirrorJSON, prosemirrorJSONToYXmlFragment, yXmlFragmentToProsemirrorJSON } from '../src/y-prosemirror.js'
import { EditorState, TextSelection } from 'prosemirror-state'
import { EditorView } from 'prosemirror-view'
import * as basicSchema from 'prosemirror-schema-basic'
import { findWrapping } from 'prosemirror-transform'
import { schema as complexSchema } from './complexSchema.js'
const schema = /** @type {any} */ (basicSchema.schema)
/**
* @param {t.TestCase} tc
*/
export const testDocTransformation = tc => {
const view = createNewProsemirrorView(new Y.Doc())
view.dispatch(view.state.tr.insert(0, /** @type {any} */ (schema.node('paragraph', undefined, schema.text('hello world')))))
const stateJSON = view.state.doc.toJSON()
// test if transforming back and forth from Yjs doc works
const backandforth = yDocToProsemirrorJSON(prosemirrorJSONToYDoc(/** @type {any} */ (schema), stateJSON))
t.compare(stateJSON, backandforth)
}
export const testXmlFragmentTransformation = tc => {
const view = createNewProsemirrorView(new Y.Doc())
view.dispatch(view.state.tr.insert(0, /** @type {any} */ (schema.node('paragraph', undefined, schema.text('hello world')))))
const stateJSON = view.state.doc.toJSON()
console.log(JSON.stringify(stateJSON))
// test if transforming back and forth from yXmlFragment works
const xml = new Y.XmlFragment()
prosemirrorJSONToYXmlFragment(/** @type {any} */ (schema), stateJSON, xml)
const doc = new Y.Doc()
doc.getMap('root').set('firstDoc', xml)
const backandforth = yXmlFragmentToProsemirrorJSON(xml)
console.log(JSON.stringify(backandforth))
t.compare(stateJSON, backandforth)
}
/**
* @param {t.TestCase} tc
*/
export const testEmptyNotSync = tc => {
const ydoc = new Y.Doc()
const type = ydoc.getXmlFragment('prosemirror')
const view = createNewComplexProsemirrorView(ydoc)
t.assert(type.toString() === '', 'should only sync after first change')
view.dispatch(
view.state.tr.setNodeMarkup(0, undefined, {
checked: true
})
)
t.compareStrings(type.toString(), '<custom checked="true"></custom><paragraph></paragraph>')
}
/**
* @param {t.TestCase} tc
*/
export const testEmptyParagraph = tc => {
const ydoc = new Y.Doc()
const view = createNewProsemirrorView(ydoc)
view.dispatch(view.state.tr.insert(0, /** @type {any} */ (schema.node('paragraph', undefined, schema.text('123')))))
const yxml = ydoc.get('prosemirror')
t.assert(yxml.length === 2 && yxml.get(0).length === 1, 'contains one paragraph containing a ytext')
view.dispatch(view.state.tr.delete(1, 4)) // delete characters 123
t.assert(yxml.length === 2 && yxml.get(0).length === 1, 'doesn\'t delete the ytext')
}
export const testAddToHistory = tc => {
const ydoc = new Y.Doc()
const view = createNewProsemirrorViewWithUndoManager(ydoc)
view.dispatch(view.state.tr.insert(0, /** @type {any} */ (schema.node('paragraph', undefined, schema.text('123')))))
const yxml = ydoc.get('prosemirror')
t.assert(yxml.length === 2 && yxml.get(0).length === 1, 'contains inserted content')
undo(view.state)
t.assert(yxml.length === 0, 'insertion was undone')
redo(view.state)
t.assert(yxml.length === 2 && yxml.get(0).length === 1, 'contains inserted content')
undo(view.state)
t.assert(yxml.length === 0, 'insertion was undone')
// now insert content again, but with `'addToHistory': false`
view.dispatch(view.state.tr.insert(0, /** @type {any} */ (schema.node('paragraph', undefined, schema.text('123')))).setMeta('addToHistory', false))
t.assert(yxml.length === 2 && yxml.get(0).length === 1, 'contains inserted content')
undo(view.state)
t.assert(yxml.length === 2 && yxml.get(0).length === 1, 'insertion was *not* undone')
}
export const testAddToHistoryIgnore = tc => {
const ydoc = new Y.Doc()
const view = createNewProsemirrorViewWithUndoManager(ydoc)
// perform two changes that are tracked by um - supposed to be merged into a single undo-manager item
view.dispatch(view.state.tr.insert(0, /** @type {any} */ (schema.node('paragraph', undefined, schema.text('123')))))
view.dispatch(view.state.tr.insert(0, /** @type {any} */ (schema.node('paragraph', undefined, schema.text('456')))))
const yxml = ydoc.get('prosemirror')
t.assert(yxml.length === 3 && yxml.get(0).length === 1, 'contains inserted content (1)')
view.dispatch(view.state.tr.insert(0, /** @type {any} */ (schema.node('paragraph', undefined, schema.text('abc')))).setMeta('addToHistory', false))
t.assert(yxml.length === 4 && yxml.get(0).length === 1, 'contains inserted content (2)')
view.dispatch(view.state.tr.insert(0, /** @type {any} */ (schema.node('paragraph', undefined, schema.text('xyz')))))
t.assert(yxml.length === 5 && yxml.get(0).length === 1, 'contains inserted content (3)')
undo(view.state)
t.assert(yxml.length === 4, 'insertion (3) was undone')
undo(view.state)
console.log(yxml.toString())
t.assert(yxml.length === 1 && yxml.get(0).toString() === '<paragraph>abc</paragraph>', 'insertion (1) was undone')
}
const createNewProsemirrorViewWithSchema = (y, schema, undoManager = false) => {
const view = new EditorView(null, {
// @ts-ignore
state: EditorState.create({
schema,
plugins: [ySyncPlugin(y.get('prosemirror', Y.XmlFragment))].concat(undoManager ? [yUndoPlugin()] : [])
})
})
return view
}
const createNewComplexProsemirrorView = (y, undoManager = false) => createNewProsemirrorViewWithSchema(y, complexSchema, undoManager)
const createNewProsemirrorView = y => createNewProsemirrorViewWithSchema(y, schema, false)
const createNewProsemirrorViewWithUndoManager = y => createNewProsemirrorViewWithSchema(y, schema, true)
let charCounter = 0
const marksChoices = [
[schema.mark('strong')],
[schema.mark('em')],
[schema.mark('em'), schema.mark('strong')],
[],
[]
]
const pmChanges = [
/**
* @param {Y.Doc} y
* @param {prng.PRNG} gen
* @param {EditorView} p
*/
(y, gen, p) => { // insert text
const insertPos = prng.int32(gen, 0, p.state.doc.content.size)
const marks = prng.oneOf(gen, marksChoices)
const tr = p.state.tr
const text = charCounter++ + prng.word(gen)
p.dispatch(tr.insert(insertPos, schema.text(text, marks)))
},
/**
* @param {Y.Doc} y
* @param {prng.PRNG} gen
* @param {EditorView} p
*/
(y, gen, p) => { // delete text
const insertPos = prng.int32(gen, 0, p.state.doc.content.size)
const overwrite = math.min(prng.int32(gen, 0, p.state.doc.content.size - insertPos), 2)
p.dispatch(p.state.tr.insertText('', insertPos, insertPos + overwrite))
},
/**
* @param {Y.Doc} y
* @param {prng.PRNG} gen
* @param {EditorView} p
*/
(y, gen, p) => { // replace text
const insertPos = prng.int32(gen, 0, p.state.doc.content.size)
const overwrite = math.min(prng.int32(gen, 0, p.state.doc.content.size - insertPos), 2)
const text = charCounter++ + prng.word(gen)
p.dispatch(p.state.tr.insertText(text, insertPos, insertPos + overwrite))
},
/**
* @param {Y.Doc} y
* @param {prng.PRNG} gen
* @param {EditorView} p
*/
(y, gen, p) => { // insert paragraph
const insertPos = prng.int32(gen, 0, p.state.doc.content.size)
const marks = prng.oneOf(gen, marksChoices)
const tr = p.state.tr
const text = charCounter++ + prng.word(gen)
p.dispatch(tr.insert(insertPos, schema.node('paragraph', undefined, schema.text(text, marks))))
},
/**
* @param {Y.Doc} y
* @param {prng.PRNG} gen
* @param {EditorView} p
*/
(y, gen, p) => { // insert codeblock
const insertPos = prng.int32(gen, 0, p.state.doc.content.size)
const tr = p.state.tr
const text = charCounter++ + prng.word(gen)
p.dispatch(tr.insert(insertPos, schema.node('code_block', undefined, schema.text(text))))
},
/**
* @param {Y.Doc} y
* @param {prng.PRNG} gen
* @param {EditorView} p
*/
(y, gen, p) => { // wrap in blockquote
const insertPos = prng.int32(gen, 0, p.state.doc.content.size)
const overwrite = prng.int32(gen, 0, p.state.doc.content.size - insertPos)
const tr = p.state.tr
tr.setSelection(TextSelection.create(tr.doc, insertPos, insertPos + overwrite))
const $from = tr.selection.$from
const $to = tr.selection.$to
const range = $from.blockRange($to)
const wrapping = range && findWrapping(range, schema.nodes.blockquote)
if (wrapping) {
p.dispatch(tr.wrap(range, wrapping))
}
}
]
/**
* @param {any} result
*/
const checkResult = result => {
for (let i = 1; i < result.testObjects.length; i++) {
const p1 = result.testObjects[i - 1].state.doc.toJSON()
const p2 = result.testObjects[i].state.doc.toJSON()
t.compare(p1, p2)
}
}
/**
* @param {t.TestCase} tc
*/
export const testRepeatGenerateProsemirrorChanges2 = tc => {
checkResult(applyRandomTests(tc, pmChanges, 2, createNewProsemirrorView))
}
/**
* @param {t.TestCase} tc
*/
export const testRepeatGenerateProsemirrorChanges3 = tc => {
checkResult(applyRandomTests(tc, pmChanges, 3, createNewProsemirrorView))
}
/**
* @param {t.TestCase} tc
*/
export const testRepeatGenerateProsemirrorChanges30 = tc => {
checkResult(applyRandomTests(tc, pmChanges, 30, createNewProsemirrorView))
}
/**
* @param {t.TestCase} tc
*/
export const testRepeatGenerateProsemirrorChanges40 = tc => {
checkResult(applyRandomTests(tc, pmChanges, 40, createNewProsemirrorView))
}
/**
* @param {t.TestCase} tc
*/
export const testRepeatGenerateProsemirrorChanges70 = tc => {
checkResult(applyRandomTests(tc, pmChanges, 70, createNewProsemirrorView))
}
/**
* @param {t.TestCase} tc
*
export const testRepeatGenerateProsemirrorChanges100 = tc => {
checkResult(applyRandomTests(tc, pmChanges, 100, createNewProsemirrorView))
}
/**
* @param {t.TestCase} tc
*
export const testRepeatGenerateProsemirrorChanges300 = tc => {
checkResult(applyRandomTests(tc, pmChanges, 300, createNewProsemirrorView))
}
*/