Skip to content

Commit

Permalink
🚚 reorganise files a little
Browse files Browse the repository at this point in the history
  • Loading branch information
laquasicinque committed Jul 10, 2021
1 parent fc2f2af commit 8c97823
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 106 deletions.
2 changes: 1 addition & 1 deletion dist/module.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "monaco-editor",
"name": "monaco-macro-editor",
"title": "Monaco Macro Editor",
"description": "testing monaco",
"version": "0.0.1",
Expand Down
107 changes: 107 additions & 0 deletions src/editor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import * as monaco from 'monaco-editor'
import { debounce } from "./utils/debounce";
import { settings } from "./utils/quickSettings";

export function registerTypes(filePath: string, code: string) {
const modifiedPath = filePath
// strip relative
.replace(/^(?:\.\.?\/)*/giu, "")
// replace starting point with `injected/`
.replace(/^\/?/gu, "injected/")
// add/replace ending with `.d.ts`
.replace(/(?:(:\.\d)?\.ts)?\$/giu, ".d.ts");

monaco.languages.typescript.javascriptDefaults.addExtraLib(
code,
modifiedPath
);

monaco.editor.createModel(code, "typescript", monaco.Uri.parse(modifiedPath));
}


export function setupMonaco(){
monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions({
noSemanticValidation: true,
noSyntaxValidation: false,
});

// compiler options
monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
target: monaco.languages.typescript.ScriptTarget.ESNext,
module: monaco.languages.typescript.ModuleKind.ESNext,
moduleResolution: monaco.languages.typescript.ModuleResolutionKind.NodeJs,
allowNonTsExtensions: true,
allowJs: true,
strict: false,
lib: ["DOM", "ESNext"],
types: ["injected/**/*.ts"],
allowSyntheticDefaultImports: true,
esModuleInterop: true,
});
}

export function attachMonacoEditor (form: HTMLFormElement) {

const oldTextArea = form.querySelector<HTMLTextAreaElement>(
'textarea[name="command"]'
);
const commandLabel = form.querySelector<HTMLLabelElement>(
".form-group.command"
);

if (!oldTextArea || !commandLabel) {
throw new Error("Monaco Macro Editor Error: Couldn't find old text area");
}

const div = document.createElement("div");
Object.assign(div.style, { width: "100%", height: "calc(100% - 24px)" });

const select: HTMLSelectElement = form.querySelector('select[name="type"]')!;

commandLabel.insertAdjacentElement("beforeend", div);

const editor = ((window as any).editor = monaco.editor.create(div, {
// editor specific
value: oldTextArea.value,
language: select.value === "script" ? "javascript" : "plaintext",

// permanent ones
minimap: {
enabled: false,
},
contextmenu: false,

// user changeable
wordWrap: settings.wordWrap ? "on" : "off",
fontFamily: settings.fontFamily,
fontLigatures: settings.fontLigatures,
theme: settings.theme,
fontSize: settings.fontSize,
}));

editor.onDidChangeModelContent(
debounce(() => {
oldTextArea.value = editor.getValue();
})
);

new ResizeObserver(() => {
editor.layout({ height: 0, width: 0 });
editor.layout();
}).observe(editor.getContainerDomNode());

form.addEventListener("submit", () => {
editor.dispose();
});

select.addEventListener("change", (e) => {
const model = editor.getModel();
if (!model) return;
if (select.value === "script") {
monaco.editor.setModelLanguage(model, "javascript");
} else if (select.value === "chat") {
monaco.editor.setModelLanguage(model, "plaintext");
}
});
}
109 changes: 5 additions & 104 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,18 @@
import * as monaco from "monaco-editor";
import { attachMonacoEditor, registerTypes, setupMonaco } from "./editor";
import { furnaceFix } from "./fixes/furnace";
import { registerSettings } from "./settings";
import { debounce } from "./utils/debounce";
import { settings } from "./utils/quickSettings";


Hooks.once("init", async function () {
registerSettings();

monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions({
noSemanticValidation: true,
noSyntaxValidation: false,
});

// compiler options
monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
target: monaco.languages.typescript.ScriptTarget.ESNext,
module: monaco.languages.typescript.ModuleKind.ESNext,
moduleResolution: monaco.languages.typescript.ModuleResolutionKind.NodeJs,
allowNonTsExtensions: true,
allowJs: true,
strict: false,
lib: ["DOM", "ESNext"],
types: ["injected/**/*.ts"],
allowSyntheticDefaultImports: true,
esModuleInterop: true,
});
setupMonaco()

Hooks.callAll("monaco-editor.ready", registerTypes);
});

Hooks.on("monaco-editor.ready", async (register: typeof registerTypes) => {
console.log("Monaco Ready");

// Load in definitions from @league-of-foundry-developers/foundry-vtt-types
const context = require.context("./typings", true, /\.ts$/i, "lazy-once");
const results = await Promise.allSettled(
context.keys().map(async (item) => {
Expand All @@ -49,86 +30,6 @@ Hooks.on("monaco-editor.ready", async (register: typeof registerTypes) => {
});

Hooks.on("renderMacroConfig", ({ form }: { form: HTMLFormElement }) => {
console.log("Monaco Render");

furnaceFix(form);

const oldTextArea = form.querySelector<HTMLTextAreaElement>(
'textarea[name="command"]'
);
const commandLabel = form.querySelector<HTMLLabelElement>(
".form-group.command"
);

if (!oldTextArea || !commandLabel) {
throw new Error("Monaco Editor Error: Couldn't find old text area");
}

const div = document.createElement("div");
Object.assign(div.style, { width: "100%", height: "calc(100% - 24px)" });

const select: HTMLSelectElement = form.querySelector('select[name="type"]')!;

commandLabel.insertAdjacentElement("beforeend", div);

const editor = ((window as any).editor = monaco.editor.create(div, {
// editor specific
value: oldTextArea.value,
language: select.value === "script" ? "javascript" : "plaintext",

// permanent ones
minimap: {
enabled: false,
},
contextmenu: false,

// user changeable
wordWrap: settings.wordWrap ? "on" : "off",
fontFamily: settings.fontFamily,
fontLigatures: settings.fontLigatures,
theme: settings.theme,
fontSize: settings.fontSize,
}));

editor.onDidChangeModelContent(
debounce(() => {
oldTextArea.value = editor.getValue();
})
);

new ResizeObserver(() => {
editor.layout({ height: 0, width: 0 });
editor.layout();
}).observe(editor.getContainerDomNode());

form.addEventListener("submit", () => {
editor.dispose();
});

select.addEventListener("change", (e) => {
const model = editor.getModel();
if (!model) return;
if (select.value === "script") {
monaco.editor.setModelLanguage(model, "javascript");
} else if (select.value === "chat") {
monaco.editor.setModelLanguage(model, "plaintext");
}
});
attachMonacoEditor(form)
});

function registerTypes(filePath: string, code: string) {
const modifiedPath = filePath
// strip relative
.replace(/^(?:\.\.?\/)*/giu, "")
// replace starting point with `injected/`
.replace(/^\/?/gu, "injected/")
// add/replace ending with `.d.ts`
.replace(/(?:(:\.\d)?\.ts)?\$/giu, ".d.ts");

monaco.languages.typescript.javascriptDefaults.addExtraLib(
code,
modifiedPath
);

monaco.editor.createModel(code, "typescript", monaco.Uri.parse(modifiedPath));
}

0 comments on commit 8c97823

Please sign in to comment.