-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathcodemirror.next.js
60 lines (50 loc) · 1.81 KB
/
codemirror.next.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
/* eslint-env browser */
import * as Y from 'yjs'
// @ts-ignore
import { yCollab, yUndoManagerKeymap } from 'y-codemirror.next'
import { WebsocketProvider } from 'y-websocket'
import { EditorState, EditorView, basicSetup } from '@codemirror/basic-setup'
import { keymap } from '@codemirror/view'
import { javascript } from '@codemirror/lang-javascript'
import * as random from 'lib0/random'
export const usercolors = [
{ color: '#30bced', light: '#30bced33' },
{ color: '#6eeb83', light: '#6eeb8333' },
{ color: '#ffbc42', light: '#ffbc4233' },
{ color: '#ecd444', light: '#ecd44433' },
{ color: '#ee6352', light: '#ee635233' },
{ color: '#9ac2c9', light: '#9ac2c933' },
{ color: '#8acb88', light: '#8acb8833' },
{ color: '#1be7ff', light: '#1be7ff33' }
]
export const userColor = usercolors[random.uint32() % usercolors.length]
const ydoc = new Y.Doc()
// const provider = new WebrtcProvider('codemirror6-demo-room', ydoc)
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)
// 'ws://localhost:3334',
'codemirror.next-demo-2024/06',
ydoc
)
const ytext = ydoc.getText('codemirror')
provider.awareness.setLocalStateField('user', {
name: 'Anonymous ' + Math.floor(Math.random() * 100),
color: userColor.color,
colorLight: userColor.light
})
const state = EditorState.create({
doc: ytext.toString(),
extensions: [
keymap.of([
...yUndoManagerKeymap
]),
basicSetup,
javascript(),
yCollab(ytext, provider.awareness)
// oneDark
]
})
const view = new EditorView({ state, parent: /** @type {HTMLElement} */ (document.querySelector('#editor')) })
// @ts-ignore
window.example = { provider, ydoc, ytext, view }