|
|
- import * as monaco from 'monaco-editor';
- import { CubicalT, Environment, Value } from '../typings/cubical';
- import * as haskell from '../typings/cubical';
-
- let editorNum: number = 0;
-
- let cubical: CubicalT;
-
- export const MODEL_ENVS: Record<string, Environment> = {}
-
- async function reloadModelEnv(model: monaco.editor.ITextModel, environ: Environment): Promise<void> {
- console.log(`reloading environment for model ${model.id}`)
- let code, typed;
- try {
- code = await cubical.exports.parseFromStringJs(model.getValue());
- typed = await cubical.exports.typeCheckProgram(environ, code);
- MODEL_ENVS[model.id] = typed;
- } catch (e) {
- if (typeof (e) === 'string')
- console.log(e);
- }
- }
-
- export class CubicalEditor {
- private model: monaco.editor.ITextModel;
- private editor: monaco.editor.IStandaloneCodeEditor;
- private environment: Environment | undefined;
- private uri: monaco.Uri;
-
- private element: HTMLElement;
- private didChangeLocked: boolean;
-
- constructor(el: HTMLElement, code: string, uri: monaco.Uri) {
- this.element = el;
- this.uri = uri;
- this.model = monaco.editor.createModel(code, 'cubical', this.uri);
-
- this.editor = monaco.editor.create(el, {
- model: this.model,
- tabSize: 2,
- insertSpaces: true,
- })
-
- this.didChangeLocked = false;
- }
-
- async load() {
- cubical = await haskell.waitForLoad;
- this.environment = await cubical.exports.newEnvironment();
-
- this.model.onDidChangeContent(async (e) => {
- if (this.didChangeLocked) {
- console.log("going too fast");
- return;
- }
-
- this.didChangeLocked = true;
- console.log('doing the work')
- // reloadModelEnv(this.model!, this.environment!);
- setTimeout(() => {
- this.didChangeLocked = false;
- }, 1000);
- });
-
- this.editor.layout();
-
- reloadModelEnv(this.model, this.environment);
-
- window.addEventListener('resize', () => this.editor.layout());
- }
- }
|