Skip to content

Commit

Permalink
add src folder + support strudel label statements
Browse files Browse the repository at this point in the history
  • Loading branch information
felixroos committed Dec 30, 2024
1 parent 5f6a8ed commit da89e5c
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 209 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
</div>
</div>

<script type="module" src="/main.js"></script>
<script type="module" src="./src/main.js"></script>
</body>
</html>
208 changes: 0 additions & 208 deletions main.js

This file was deleted.

101 changes: 101 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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 { StrudelSession } from "./strudel";

import "./style.css";

const onError = (err) => {
console.error(err);
};

const strudel = new StrudelSession({ onError });

const flokBasicSetup = (doc) => {
const text = doc.getText();
const undoManager = new UndoManager(text);
const web = true;

return [
flashField(),
remoteEvalFlash(doc),
Prec.high(evalKeymap(doc, { web, defaultMode: "document" })),
yCollab(text, doc.session.awareness, { undoManager }),
];
};

const createEditor = (doc) => {
console.log("createEditor", doc);
if (!["slot1", "slot2"].includes(doc.id)) {
console.warn(
`ignoring doc with id "${doc.id}". only slot1 and slot2 is allowed rn..`
);
return;
}
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 handleEvalHydra = (msg) => {
console.log("eval:hydra", msg);
// evaluate hydra code here...
};

const session = new Session("default", {
// changed this part to what flok.cc uses
hostname: "flok.cc",
port: "", //parseInt(port),
isSecure: true,
});
window.session = session;

session.on("change", (...args) => console.log("change", ...args));
session.on("message", (msg) => console.log("message", msg));
session.on("eval:hydra", handleEvalHydra);
session.on("eval:strudel", (msg) => strudel.eval(msg));

session.on("sync", () => {
// If session is empty, create two documents
if (session.getDocuments().length === 0) {
session.setActiveDocuments([
{ id: "slot1", target: "strudel" },
{ id: "slot2", target: "hydra" },
]);
}

// Create editors for each document
session.getDocuments().map((doc) => createEditor(doc));
});

session.initialize();
Loading

0 comments on commit da89e5c

Please sign in to comment.