Skip to content

Commit

Permalink
use local monaco by default instead of cdn, expose loaderParams prop …
Browse files Browse the repository at this point in the history
…to configure it
  • Loading branch information
alxnddr committed Sep 1, 2024
1 parent 849b025 commit fb8b91d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
17 changes: 10 additions & 7 deletions src/MonacoDiffEditor.tsx
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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) => {
Expand All @@ -45,13 +47,14 @@ export const MonacoDiffEditor = (inputProps: MonacoDiffEditorProps) => {
let containerRef: HTMLDivElement

const [monaco, setMonaco] = createSignal<Monaco>()
const [editor, setEditor] = createSignal<monacoEditor.IStandaloneDiffEditor>()
const [editor, setEditor] = createSignal<monacoEditor.editor.IStandaloneDiffEditor>()

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()
Expand Down
19 changes: 11 additions & 8 deletions src/MonacoEditor.tsx
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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) => {
Expand All @@ -39,13 +41,14 @@ export const MonacoEditor = (inputProps: MonacoEditorProps) => {
let containerRef: HTMLDivElement

const [monaco, setMonaco] = createSignal<Monaco>()
const [editor, setEditor] = createSignal<monacoEditor.IStandaloneCodeEditor>()
const [editor, setEditor] = createSignal<monacoEditor.editor.IStandaloneCodeEditor>()

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()
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import loader from '@monaco-editor/loader'

export type LoaderParams = Parameters<typeof loader.config>[0]

0 comments on commit fb8b91d

Please sign in to comment.