-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathprosemirror-versions.js
190 lines (171 loc) · 6.45 KB
/
prosemirror-versions.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
/* eslint-env browser */
import * as Y from 'yjs'
import { WebsocketProvider } from 'y-websocket'
import { ySyncPlugin, ySyncPluginKey, yCursorPlugin, yUndoPlugin, undo, redo } from 'y-prosemirror'
import { EditorState } from 'prosemirror-state'
import { EditorView } from 'prosemirror-view'
import { schema } from './schema.js'
import { exampleSetup } from 'prosemirror-example-setup'
import { keymap } from 'prosemirror-keymap'
import * as random from 'lib0/random.js'
import { html, render } from 'lit-html'
import * as dom from 'lib0/dom.js'
import * as pair from 'lib0/pair.js'
/**
* @typedef {Object} Version
* @property {number} date
* @property {Uint8Array} snapshot
* @property {number} clientID
*/
/**
* @param {Y.Doc} doc
*/
const addVersion = doc => {
const versions = doc.getArray('versions')
const prevVersion = versions.length === 0 ? null : versions.get(versions.length - 1)
const prevSnapshot = prevVersion === null ? Y.emptySnapshot : Y.decodeSnapshot(prevVersion.snapshot)
const snapshot = Y.snapshot(doc)
if (prevVersion != null) {
// account for the action of adding a version to ydoc
prevSnapshot.sv.set(prevVersion.clientID, /** @type {number} */ (prevSnapshot.sv.get(prevVersion.clientID)) + 1)
}
if (!Y.equalSnapshots(prevSnapshot, snapshot)) {
versions.push([{
date: new Date().getTime(),
snapshot: Y.encodeSnapshot(snapshot),
clientID: doc.clientID
}])
}
}
const liveTracking = /** @type {HTMLInputElement} */ (dom.element('input', [
pair.create('type', 'checkbox'),
pair.create('name', 'yjs-live-tracking'),
pair.create('value', 'Live Tracking ')
]))
const updateLiveTrackingState = editorstate => {
setTimeout(() => {
const syncState = ySyncPluginKey.getState(editorstate.state)
liveTracking.checked = syncState.prevSnapshot != null && syncState.snapshot == null
}, 500)
}
const renderVersion = (editorview, version, prevSnapshot) => {
editorview.dispatch(editorview.state.tr.setMeta(ySyncPluginKey, { snapshot: Y.decodeSnapshot(version.snapshot), prevSnapshot: prevSnapshot == null ? Y.emptySnapshot : Y.decodeSnapshot(prevSnapshot) }))
updateLiveTrackingState(editorview)
}
const unrenderVersion = editorview => {
const binding = ySyncPluginKey.getState(editorview.state).binding
if (binding != null) {
binding.unrenderSnapshot()
}
updateLiveTrackingState(editorview)
}
/**
* @param {EditorView} editorview
* @param {Version} version
* @param {Version|null} prevSnapshot
*/
const versionTemplate = (editorview, version, prevSnapshot) => html`<div class="version-list" @click=${() => renderVersion(editorview, version, prevSnapshot)}>${new Date(version.date).toLocaleString()}</div>`
const versionList = (editorview, doc) => {
const versions = doc.getArray('versions')
return html`<div>${versions.length > 0 ? versions.map((version, i) => versionTemplate(editorview, version, i > 0 ? versions.get(i - 1).snapshot : null)) : html`<div>No snapshots..</div>`}</div>`
}
const snapshotButton = doc => {
return html`<button @click=${() => addVersion(doc)}>Snapshot</button>`
}
/**
* @param {HTMLElement} parent
* @param {Y.Doc} doc
* @param {EditorView} editorview
*/
export const attachVersion = (parent, doc, editorview) => {
let open = false
const rerender = () => {
render(html`<div class="version-modal" ?hidden=${open}>${snapshotButton(doc)}${versionList(editorview, doc)}</div>`, vContainer)
}
updateLiveTrackingState(editorview)
liveTracking.addEventListener('click', () => {
if (liveTracking.checked) {
const versions = doc.getArray('versions')
const lastVersion = versions.length > 0 ? Y.decodeSnapshot(versions.get(versions.length - 1).snapshot) : Y.emptySnapshot
editorview.dispatch(editorview.state.tr.setMeta(ySyncPluginKey, { snapshot: null, prevSnapshot: lastVersion }))
} else {
unrenderVersion(editorview)
}
})
parent.insertBefore(liveTracking, null)
parent.insertBefore(dom.element('label', [
pair.create('for', 'yjs-live-tracking')
], [
dom.text('Live Tracking ')
]), null)
const btn = document.createElement('button')
btn.setAttribute('type', 'button')
btn.textContent = 'Versions'
btn.addEventListener('click', () => {
open = !open
unrenderVersion(editorview)
rerender()
})
const vContainer = document.createElement('div')
parent.insertBefore(btn, null)
parent.insertBefore(vContainer, null)
doc.getArray('versions').observe(rerender)
rerender()
}
const testUsers = [
{ username: 'Alice', color: '#ecd444', lightColor: '#ecd44433' },
{ username: 'Bob', color: '#ee6352', lightColor: '#ee635233' },
{ username: 'Max', color: '#6eeb83', lightColor: '#6eeb8333' }
]
const colors = [
{ light: '#ecd44433', dark: '#ecd444' },
{ light: '#ee635233', dark: '#ee6352' },
{ light: '#6eeb8333', dark: '#6eeb83' }
]
const user = random.oneOf(testUsers)
window.addEventListener('load', () => {
const ydoc = new Y.Doc()
const permanentUserData = new Y.PermanentUserData(ydoc)
permanentUserData.setUserMapping(ydoc, ydoc.clientID, user.username)
ydoc.gc = false
const provider = new WebsocketProvider(
'wss://demos.yjs.dev/ws', // use the public ws server
// `ws${location.protocol.slice(4)}//${location.host}/ws`, // alternatively: use the local ws server (run `npm start` in root directory)
'prosemirror-versions-demo-2024/06',
ydoc
)
const yXmlFragment = ydoc.get('prosemirror', Y.XmlFragment)
const editor = document.createElement('div')
editor.setAttribute('id', 'editor')
const editorContainer = document.createElement('div')
editorContainer.insertBefore(editor, null)
const prosemirrorView = new EditorView(editor, {
state: EditorState.create({
schema,
plugins: [
ySyncPlugin(yXmlFragment, { permanentUserData, colors }),
yCursorPlugin(provider.awareness),
yUndoPlugin(),
keymap({
'Mod-z': undo,
'Mod-y': redo,
'Mod-Shift-z': redo
})
].concat(exampleSetup({ schema }))
})
})
document.body.insertBefore(editorContainer, null)
attachVersion(document.getElementById('y-version'), ydoc, prosemirrorView)
const connectBtn = document.getElementById('y-connect-btn')
connectBtn.addEventListener('click', () => {
if (provider.shouldConnect) {
provider.disconnect()
connectBtn.textContent = 'Connect'
} else {
provider.connect()
connectBtn.textContent = 'Disconnect'
}
})
// @ts-ignore
window.example = { provider, ydoc, yXmlFragment, prosemirrorView }
})