bytebeat-composer/src/code-editor/mod.ts

30 lines
908 B
TypeScript
Raw Permalink Normal View History

import { initTextarea } from "./textarea.ts";
2024-03-13 21:20:53 +01:00
2024-03-14 06:49:35 +01:00
// must be constructed after dom is loaded
export class CodeEditor {
2024-03-14 06:49:35 +01:00
getCode: () => string;
setCode: (value?: string) => void;
2024-03-13 21:20:53 +01:00
2024-03-14 06:49:35 +01:00
constructor(editCallback: () => void) {
2024-03-14 23:32:54 +01:00
const textarea = document.getElementById("code-editor") as HTMLTextAreaElement;
2024-03-14 06:49:35 +01:00
initTextarea(editCallback, textarea);
this.getCode = () => textarea.value;
this.setCode = v => {
textarea.value = v as string;
};
2024-03-14 06:49:35 +01:00
// codemirror takes a long time to load, so import it late, then replace the textarea
import("./codemirror.ts").then(c => {
const codemirror = c.replace(editCallback, textarea);
this.getCode = () => codemirror.state.doc.toString();
this.setCode = v =>
2024-04-23 23:16:19 +02:00
// codemirror type is wrong
// TODO pull request fix
2024-08-03 05:55:27 +02:00
// @ts-expect-error
codemirror.dispatch({
changes: { from: 0, to: codemirror.state.doc.length, insert: v },
});
2024-03-13 21:20:53 +01:00
});
2024-03-14 06:49:35 +01:00
}
2024-03-13 21:20:53 +01:00
}