From fb8b91d3c96ea10496fd87fdcae0fad6e935f3c8 Mon Sep 17 00:00:00 2001 From: Aleksandr Lesnenko Date: Sun, 1 Sep 2024 16:21:00 -0400 Subject: [PATCH] use local monaco by default instead of cdn, expose loaderParams prop to configure it --- src/MonacoDiffEditor.tsx | 17 ++++++++++------- src/MonacoEditor.tsx | 19 +++++++++++-------- src/types.ts | 3 +++ 3 files changed, 24 insertions(+), 15 deletions(-) create mode 100644 src/types.ts diff --git a/src/MonacoDiffEditor.tsx b/src/MonacoDiffEditor.tsx index 31c481f..8967f36 100644 --- a/src/MonacoDiffEditor.tsx +++ b/src/MonacoDiffEditor.tsx @@ -1,9 +1,10 @@ import { createSignal, createEffect, onCleanup, JSX, onMount, mergeProps, on } from 'solid-js' -import { editor as monacoEditor } from 'monaco-editor' +import * as monacoEditor from 'monaco-editor' import loader, { Monaco } from '@monaco-editor/loader' import { Loader } from './Loader' import { MonacoContainer } from './MonacoContainer' import { getOrCreateModel } from './utils' +import { LoaderParams } from './types' const viewStates = new Map() @@ -19,15 +20,16 @@ export interface MonacoDiffEditorProps { loadingState?: JSX.Element class?: string - theme?: monacoEditor.BuiltinTheme | string - overrideServices?: monacoEditor.IEditorOverrideServices + theme?: monacoEditor.editor.BuiltinTheme | string + overrideServices?: monacoEditor.editor.IEditorOverrideServices width?: string height?: string - options?: monacoEditor.IStandaloneEditorConstructionOptions + options?: monacoEditor.editor.IStandaloneEditorConstructionOptions saveViewState?: boolean + loaderParams?: LoaderParams onChange?: (value: string) => void - onMount?: (monaco: Monaco, editor: monacoEditor.IStandaloneDiffEditor) => void - onBeforeUnmount?: (monaco: Monaco, editor: monacoEditor.IStandaloneDiffEditor) => void + onMount?: (monaco: Monaco, editor: monacoEditor.editor.IStandaloneDiffEditor) => void + onBeforeUnmount?: (monaco: Monaco, editor: monacoEditor.editor.IStandaloneDiffEditor) => void } export const MonacoDiffEditor = (inputProps: MonacoDiffEditorProps) => { @@ -45,13 +47,14 @@ export const MonacoDiffEditor = (inputProps: MonacoDiffEditorProps) => { let containerRef: HTMLDivElement const [monaco, setMonaco] = createSignal() - const [editor, setEditor] = createSignal() + const [editor, setEditor] = createSignal() let abortInitialization: (() => void) | undefined let monacoOnChangeSubscription: any let isOnChangeSuppressed = false onMount(async () => { + loader.config(inputProps.loaderParams ?? { monaco: monacoEditor }) const loadMonaco = loader.init() abortInitialization = () => loadMonaco.cancel() diff --git a/src/MonacoEditor.tsx b/src/MonacoEditor.tsx index 5f829f1..dc75b4e 100644 --- a/src/MonacoEditor.tsx +++ b/src/MonacoEditor.tsx @@ -1,9 +1,10 @@ import { createSignal, createEffect, onCleanup, JSX, onMount, mergeProps, on } from 'solid-js' -import { editor as monacoEditor } from 'monaco-editor' +import * as monacoEditor from 'monaco-editor' import loader, { Monaco } from '@monaco-editor/loader' import { Loader } from './Loader' import { MonacoContainer } from './MonacoContainer' import { getOrCreateModel } from './utils' +import { LoaderParams } from './types' const viewStates = new Map() @@ -12,16 +13,17 @@ export interface MonacoEditorProps { value?: string loadingState?: JSX.Element class?: string - theme?: monacoEditor.BuiltinTheme | string + theme?: monacoEditor.editor.BuiltinTheme | string path?: string - overrideServices?: monacoEditor.IEditorOverrideServices + overrideServices?: monacoEditor.editor.IEditorOverrideServices width?: string height?: string - options?: monacoEditor.IStandaloneEditorConstructionOptions + options?: monacoEditor.editor.IStandaloneEditorConstructionOptions saveViewState?: boolean - onChange?: (value: string, event: monacoEditor.IModelContentChangedEvent) => void - onMount?: (monaco: Monaco, editor: monacoEditor.IStandaloneCodeEditor) => void - onBeforeUnmount?: (monaco: Monaco, editor: monacoEditor.IStandaloneCodeEditor) => void + loaderParams?: LoaderParams + onChange?: (value: string, event: monacoEditor.editor.IModelContentChangedEvent) => void + onMount?: (monaco: Monaco, editor: monacoEditor.editor.IStandaloneCodeEditor) => void + onBeforeUnmount?: (monaco: Monaco, editor: monacoEditor.editor.IStandaloneCodeEditor) => void } export const MonacoEditor = (inputProps: MonacoEditorProps) => { @@ -39,13 +41,14 @@ export const MonacoEditor = (inputProps: MonacoEditorProps) => { let containerRef: HTMLDivElement const [monaco, setMonaco] = createSignal() - const [editor, setEditor] = createSignal() + const [editor, setEditor] = createSignal() let abortInitialization: (() => void) | undefined let monacoOnChangeSubscription: any let isOnChangeSuppressed = false onMount(async () => { + loader.config(inputProps.loaderParams ?? { monaco: monacoEditor }) const loadMonaco = loader.init() abortInitialization = () => loadMonaco.cancel() diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..8d0e074 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,3 @@ +import loader from '@monaco-editor/loader' + +export type LoaderParams = Parameters[0]