Skip to content

Commit

Permalink
✨ allow for client side opting out of using monaco
Browse files Browse the repository at this point in the history
  • Loading branch information
laquasicinque committed Jul 11, 2021
1 parent 3f68be7 commit eaf873b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 30 deletions.
1 change: 1 addition & 0 deletions dist/6640.module.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/module.js

Large diffs are not rendered by default.

46 changes: 28 additions & 18 deletions src/editor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as monaco from "monaco-editor";
import { debounce } from "./utils/debounce";
import { settings } from "./settings";
import { NewOptionsType } from "./types";
import type * as MonacoTypings from "monaco-editor";

const activeEditors = new Set<monaco.editor.IStandaloneCodeEditor>();
const activeEditors = new Set<MonacoTypings.editor.IStandaloneCodeEditor>();
export let monaco: typeof import("monaco-editor");

export function updateActiveEditors<T extends keyof NewOptionsType>(
str: T,
Expand All @@ -16,24 +17,33 @@ export function updateActiveEditors<T extends keyof NewOptionsType>(
}
}

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
);
export let registerTypes = (_: string, __: string): void => {};

monaco.editor.createModel(code, "typescript", monaco.Uri.parse(modifiedPath));
}
export async function setupMonaco() {
monaco = await import("monaco-editor");

// redefine registerTypes
registerTypes = async 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,
Expand Down
18 changes: 11 additions & 7 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { attachMonacoEditor, registerTypes, setupMonaco } from "./editor";
import { furnaceFix } from "./fixes/furnace";
import { bailOnMacroEditor } from "./fixes/macroeditor";
import { registerSettings } from "./settings";
import { registerSettings, settings } from "./settings";


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

Hooks.callAll("monaco-editor.ready", registerTypes);
if (settings.enableMonacoEditor) {
await setupMonaco()
Hooks.callAll("monaco-editor.ready", registerTypes);
}
});

Hooks.on("monaco-editor.ready", async (register: typeof registerTypes) => {
console.log("Monaco Editor | Load types")
// 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(
Expand All @@ -30,7 +32,9 @@ Hooks.on("monaco-editor.ready", async (register: typeof registerTypes) => {
});

Hooks.on("renderMacroConfig", ({ form }: { form: HTMLFormElement }) => {
bailOnMacroEditor()
furnaceFix(form);
attachMonacoEditor(form)
if (settings.enableMonacoEditor) {
bailOnMacroEditor()
furnaceFix(form);
attachMonacoEditor(form)
}
});
13 changes: 9 additions & 4 deletions src/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Settings = {
theme: string;
fontSize: number;
wordWrap: boolean;
enableMonacoEditor: boolean;
};

/**
Expand Down Expand Up @@ -39,26 +40,30 @@ export function registerSettings() {

defineSetting("fontFamily", {
default: `Jetbrains Mono, Fira Code, san-serif`,
type: String,
});

defineSetting("fontLigatures", {
type: Boolean,
default: true,
});

defineSetting("fontSize", {
type: Number,
default: 12,
});

defineSetting("wordWrap", {
type: Boolean,
default: true,
onChange(value) {
updateActiveEditors("wordWrap", value ? "on" : "off")
}
});

defineSetting("enableMonacoEditor", {
default: true,
onChange: () => {
// force a reload
window.location.reload()
}
})
}

function defineSetting<T>(
Expand Down

0 comments on commit eaf873b

Please sign in to comment.