-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (37 loc) · 1.18 KB
/
index.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
var { IndexeddbPersistence } = require('y-indexeddb')
var { WebrtcProvider } = require('y-webrtc')
var { keymap } = require('prosemirror-keymap')
var { ySyncPlugin, yCursorPlugin, yUndoPlugin, undo, redo } = require('y-prosemirror')
var Pamphlet = require('pamphlet')
var Y = require('yjs')
class Editorial {
constructor (element, room, user) {
this.room = room || this.rand()
this.doc = new Y.Doc()
this.network = new WebrtcProvider(this.room, this.doc, { password: this.room })
this.storage = new IndexeddbPersistence(this.room, this.doc)
this.editor = new Pamphlet(element, { plugins: this.plugins })
this.user = user || { name: this.rand() }
}
rand () {
return Math.round(Math.random() * Date.now()).toString(16).padStart(12, '0')
}
get plugins () {
return [
ySyncPlugin(this.doc.getXmlFragment(this.room)),
yCursorPlugin(this.network.awareness),
yUndoPlugin(),
keymap({
'Mod-z': undo,
'Mod-Shift-z': redo
})
]
}
get user () {
return this.network.awareness.getLocalState().user
}
set user (user) {
this.network.awareness.setLocalStateField('user', user)
}
}
module.exports = Editorial