Skip to content
This repository was archived by the owner on Aug 23, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/codemirror/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"@codemirror/view": "^6.28.4",
"codemirror": "^6.0.1",
"@lezer/highlight": "^1.2.0",
"@kabelsalat/transpiler": "workspace:*"
"@kabelsalat/transpiler": "workspace:*",
"@replit/codemirror-emacs": "^6.1.0",
"@replit/codemirror-vim": "^6.2.1",
"@replit/codemirror-vscode-keymap": "^6.0.2"
}
}
29 changes: 23 additions & 6 deletions packages/codemirror/src/codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@ import { javascript } from "@codemirror/lang-javascript";
import { insertNewline } from "@codemirror/commands";
import { widgetPlugin } from "./widgets.js";
import { flashField } from "./flash.js";
import { keybindings } from "./keybindings.js";
import {
lineNumbers,
highlightActiveLineGutter,
highlightActiveLine,
highlightActiveLineGutter,
keymap,
lineNumbers,
} from "@codemirror/view";
import { bracketMatching } from "@codemirror/language";
import { Prec } from "@codemirror/state";
import { Compartment, Prec } from "@codemirror/state";

export function initEditor({ root, code, onChange, onEvaluate, onStop }) {
export function initEditor({
root,
code,
settings,
onChange,
onEvaluate,
onStop,
}) {
const keybindingsCompartment = new Compartment();
let editor = new EditorView({
extensions: [
//basicSetup,
Expand All @@ -26,6 +35,7 @@ export function initEditor({ root, code, onChange, onEvaluate, onStop }) {
widgetPlugin,
EditorView.lineWrapping,
javascript(),
keybindingsCompartment.of(keybindings(settings.keybindings)),
EditorView.updateListener.of((v) => {
if (v.docChanged) {
onChange?.(v.state.doc.toString());
Expand Down Expand Up @@ -64,6 +74,7 @@ export function initEditor({ root, code, onChange, onEvaluate, onStop }) {
],
parent: root,
});

const setCode = (code) => {
const changes = {
from: 0,
Expand All @@ -72,9 +83,15 @@ export function initEditor({ root, code, onChange, onEvaluate, onStop }) {
};
editor.dispatch({ changes });
};
setCode(code);

setCode(code);
const getCode = () => editor.state.doc.toString();

return { setCode, getCode, editor };
const setKeybindings = (bindings) => {
editor.dispatch({
effects: keybindingsCompartment.reconfigure(keybindings(bindings)),
});
};

return { setCode, getCode, editor, setKeybindings };
}
33 changes: 33 additions & 0 deletions packages/codemirror/src/keybindings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Prec } from "@codemirror/state";
import { keymap, ViewPlugin } from "@codemirror/view";
import { emacs } from "@replit/codemirror-emacs";
import { vim } from "@replit/codemirror-vim";
import { vscodeKeymap } from "@replit/codemirror-vscode-keymap";
import { defaultKeymap, historyKeymap } from "@codemirror/commands";

const vscodePlugin = ViewPlugin.fromClass(
class {
constructor() {}
},
{
provide: () => {
return Prec.highest(keymap.of([...vscodeKeymap]));
},
}
);
const vscodeExtension = (options) => [vscodePlugin].concat(options ?? []);

export const keymaps = {
vim,
emacs,
vscode: vscodeExtension,
};

export function keybindings(name) {
const active = keymaps[name];
return [
keymap.of(defaultKeymap),
keymap.of(historyKeymap),
active ? active() : [],
];
}
64 changes: 64 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions website/src/components/Codemirror.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ export function Codemirror(props) {
cm.setCode(props.code);
}
});

createEffect(() => {
cm.setKeybindings(props.settings.keybindings);
});

return (
<div class="resize-none bg-stone-900 shrink-0 focus:ring-0 outline-0 border-0 h-full overflow-hidden">
<div
class="w-full max-w-full overflow-hidden h-full"
ref={(el) => {
cm = initEditor({
root: el,
...props,
});
cm = initEditor({ root: el, ...props });
setView(cm.editor);
}}
></div>
Expand Down
8 changes: 3 additions & 5 deletions website/src/components/Reference.jsx

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry prettier gone rogue in this one...

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { For, createSignal } from "solid-js";
import { createSignal, For } from "solid-js";
import { nodeRegistry } from "@kabelsalat/core";
import { MiniRepl } from "./MiniRepl.jsx";

Expand Down Expand Up @@ -63,8 +63,7 @@ export function Reference() {
? "bg-teal-700"
: "bg-stone-700")
}
onClick={() => toggleTag(tag)}
>
onClick={() => toggleTag(tag)}>
{tag} ({tagRefs[tag]})
</a>{" "}
</>
Expand All @@ -84,8 +83,7 @@ export function Reference() {
const container = document.getElementById("scroll-container");
const pos = el.offsetTop - container.offsetTop - 10;
container.scrollTo(0, pos);
}}
>
}}>
{name}
</a>{" "}
</>
Expand Down
Loading