-
-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathcodemirror.js
More file actions
45 lines (38 loc) · 1.41 KB
/
codemirror.js
File metadata and controls
45 lines (38 loc) · 1.41 KB
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
/* eslint-env browser */
// @ts-ignore
import CodeMirror from 'codemirror'
import * as Y from 'yjs'
import { WebsocketProvider } from 'y-websocket'
import { CodemirrorBinding } from 'y-codemirror'
import 'codemirror/mode/javascript/javascript.js'
const roomname = `codemirror-demo-${new Date().toLocaleDateString('en-CA')}`
window.addEventListener('load', () => {
const ydoc = new Y.Doc()
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)
roomname,
ydoc
)
const ytext = ydoc.getText('codemirror')
const editorContainer = document.createElement('div')
editorContainer.setAttribute('id', 'editor')
document.body.insertBefore(editorContainer, null)
const editor = CodeMirror(editorContainer, {
mode: 'javascript',
lineNumbers: true
})
const binding = new CodemirrorBinding(ytext, editor, provider.awareness)
const connectBtn = /** @type {HTMLElement} */ (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, ytext, binding, Y }
})