-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathmain.js
86 lines (71 loc) · 2.24 KB
/
main.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
import { EditorView, basicSetup } from "codemirror";
import { javascript } from "@codemirror/lang-javascript";
import { oneDark } from "@codemirror/theme-one-dark";
import { EditorState, Prec } from "@codemirror/state";
import { yCollab } from "y-codemirror.next";
import { Session } from "@flok-editor/session";
import { flashField, evalKeymap, remoteEvalFlash } from "@flok-editor/cm-eval";
import { UndoManager } from "yjs";
import "./style.css";
const flokBasicSetup = (doc) => {
const text = doc.getText();
const undoManager = new UndoManager(text);
// if target is hydra, use "web" mode to evaluate on browser only, not REPLs
const web = doc.target === "hydra";
return [
flashField(),
remoteEvalFlash(doc),
Prec.high(evalKeymap(doc, { web })),
yCollab(text, doc.session.awareness, { undoManager }),
];
};
const createEditor = (doc) => {
const state = EditorState.create({
doc: doc.content,
extensions: [
basicSetup,
flokBasicSetup(doc),
javascript(),
EditorView.lineWrapping,
oneDark,
],
});
const editorEl = document.querySelector(`#${doc.id} .editor`);
const view = new EditorView({
state,
parent: editorEl,
});
const targetEl = document.querySelector(`#${doc.id} .target`);
targetEl.value = doc.target;
targetEl.addEventListener("change", (e) => {
doc.target = e.target.value;
});
doc.session.on(`change-target:${doc.id}`, () => {
targetEl.value = doc.target;
});
return [state, view];
};
const handleMessage = (msg) => {
console.log("message", msg);
};
const handleEvalHydra = (msg) => {
console.log("eval:hydra", msg);
// evaluate hydra code here...
};
const session = new Session("default", { port: 3000 });
window.session = session;
session.on("change", (...args) => console.log("change", ...args));
session.on("message", handleMessage);
session.on("eval:hydra", handleEvalHydra);
session.on("sync", () => {
// If session is empty, create two documents
if (session.getDocuments().length === 0) {
session.setActiveDocuments([
{ id: "slot1", target: "tidal" },
{ id: "slot2", target: "hydra" },
]);
}
// Create editors for each document
session.getDocuments().map((doc) => createEditor(doc));
});
session.initialize();