diff --git a/SchemaFrontend/package.json b/SchemaFrontend/package.json index a056f46..e6a403a 100644 --- a/SchemaFrontend/package.json +++ b/SchemaFrontend/package.json @@ -17,4 +17,4 @@ "dependencies": { "ace-code": "^1.34.2" } -} +} \ No newline at end of file diff --git a/SchemaFrontend/src/editor.ts b/SchemaFrontend/src/editor.ts new file mode 100644 index 0000000..ac85cf5 --- /dev/null +++ b/SchemaFrontend/src/editor.ts @@ -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); + }); +} \ No newline at end of file diff --git a/SchemaFrontend/src/index.ts b/SchemaFrontend/src/index.ts index e486156..d36d475 100644 --- a/SchemaFrontend/src/index.ts +++ b/SchemaFrontend/src/index.ts @@ -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"); @@ -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)); })(); \ No newline at end of file