Skip to content

Commit

Permalink
easier lazy loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Equinox- committed Jun 4, 2024
1 parent 667c9bb commit 52a2a4e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
2 changes: 1 addition & 1 deletion SchemaFrontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
"dependencies": {
"ace-code": "^1.34.2"
}
}
}
31 changes: 31 additions & 0 deletions SchemaFrontend/src/editor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as ace from "ace-code";
import { AceTooltips } from "./tooltip";
import { XmlBuilder } from "./xml";
import { SchemaIr } from "./ir";
import { generateExample } from "./generate";

export function setupEditor(editorDom: HTMLElement, schema: string, path: string[]) {
const editor = ace.edit(editorDom);
editor.setReadOnly(true);
const tooltips = new AceTooltips(editor);

import('ace-code/src/mode/xml').then(xml => editor.session.setMode(new xml.Mode()));
import('ace-code/src/theme/monokai').then(theme => {
editor.setStyle(theme.cssClass);
const styleRef = document.getElementById(theme.cssClass);
// Move to end so the theme takes priority.
styleRef.parentElement.appendChild(styleRef);
});

const schemaPath = "https://storage.googleapis.com/unofficial-keen-schemas/latest/" + schema;
fetch(schemaPath + ".json")
.then(response => response.json())
.then(json => json as SchemaIr)
.then(ir => {
const builder = new XmlBuilder({ editor, tooltips, schema: schemaPath + ".xsd" });
generateExample(ir, builder, path);
})
.catch(err => {
console.warn("Failed to load schema IR for " + schema[0], err);
});
}
31 changes: 1 addition & 30 deletions SchemaFrontend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import "ace-code/src/theme/monokai";
import { SchemaIr } from "./ir";
import { locationParameters } from "./util";
import { XmlBuilder } from "./xml";
import { generateExample } from "./generate";
import { AceTooltips } from "./tooltip";

(function () {
const editorDom = document.createElement("editor");
Expand All @@ -23,29 +18,5 @@ import { AceTooltips } from "./tooltip";
const stripPrefix = "MyObjectBuilder_";
document.title = root.length == 2 ? root[1].startsWith(stripPrefix) ? root[1].substring(stripPrefix.length) : root[1] : root[0];

import('ace-code').then(ace => {
const editor = ace.edit(editorDom);
editor.setReadOnly(true);
const tooltips = new AceTooltips(editor);

import('ace-code/src/mode/xml').then(xml => editor.session.setMode(new xml.Mode()));
import('ace-code/src/theme/monokai').then(theme => {
editor.setStyle(theme.cssClass);
const styleRef = document.getElementById(theme.cssClass);
// Move to end so the theme takes priority.
styleRef.parentElement.appendChild(styleRef);
});

const schemaPath = "https://storage.googleapis.com/unofficial-keen-schemas/latest/" + schema[0];
fetch(schemaPath + ".json")
.then(response => response.json())
.then(json => json as SchemaIr)
.then(ir => {
const builder = new XmlBuilder({ editor, tooltips, schema: schemaPath + ".xsd" });
generateExample(ir, builder, path);
})
.catch(err => {
console.warn("Failed to load schema IR for " + schema[0], err);
});
});
import('./editor').then(editor => editor.setupEditor(editorDom, schema[0], path));
})();

0 comments on commit 52a2a4e

Please sign in to comment.