Skip to content

Commit fe4b3a1

Browse files
committed
fix: inject the monaco loader from ms.Application
1 parent f70defa commit fe4b3a1

File tree

8 files changed

+41
-74
lines changed

8 files changed

+41
-74
lines changed

backend/modelscope_studio/components/pro/monaco_editor/diff_editor/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(
4545
before_mount: str | None = None,
4646
after_mount: str | None = None,
4747
override_services: dict | None = None,
48-
loading: str | None = None,
48+
loading: str | None = "Editor is loading...",
4949
read_only: bool | None = None,
5050
options: dict | None = None,
5151
line: int | None = None,

frontend/pro/monaco-editor/Index.svelte

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,32 +77,21 @@
7777
slots: $slots,
7878
themeMode: gradio.theme,
7979
};
80+
81+
$: awaitedLoader =
82+
_loader?.mode === 'local'
83+
? initLocalLoader()
84+
: _loader?.cdn_url
85+
? initCDNLoader(_loader.cdn_url)
86+
: undefined;
8087
</script>
8188

8289
{#if $mergedProps.visible}
83-
{#if _loader?.mode === 'local'}
84-
{#await initLocalLoader()}
85-
<!-- Loading local loader -->
86-
{:then}
87-
{#await AwaitedMonacoEditor then MonacoEditor}
88-
<MonacoEditor {...editorProps}>
89-
<slot></slot>
90-
</MonacoEditor>
91-
{/await}
92-
{/await}
93-
{:else if _loader?.cdn_url}
94-
{#await initCDNLoader(_loader.cdn_url)}
95-
{#await AwaitedMonacoEditor then MonacoEditor}
96-
<MonacoEditor {...editorProps}>
97-
<slot></slot>
98-
</MonacoEditor>
99-
{/await}
100-
{/await}
101-
{:else}
90+
{#await awaitedLoader then}
10291
{#await AwaitedMonacoEditor then MonacoEditor}
10392
<MonacoEditor {...editorProps}>
10493
<slot></slot>
10594
</MonacoEditor>
10695
{/await}
107-
{/if}
96+
{/await}
10897
{/if}

frontend/pro/monaco-editor/diff-editor/Index.svelte

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,32 +79,21 @@
7979
slots: $slots,
8080
themeMode: gradio.theme,
8181
};
82+
83+
$: awaitedLoader =
84+
_loader?.mode === 'local'
85+
? initLocalLoader()
86+
: _loader?.cdn_url
87+
? initCDNLoader(_loader.cdn_url)
88+
: undefined;
8289
</script>
8390

8491
{#if $mergedProps.visible}
85-
{#if _loader?.mode === 'local'}
86-
{#await initLocalLoader()}
87-
<!-- Loading local loader -->
88-
{:then}
89-
{#await AwaitedMonacoDiffEditor then MonacoDiffEditor}
90-
<MonacoDiffEditor {...editorProps}>
91-
<slot></slot>
92-
</MonacoDiffEditor>
93-
{/await}
94-
{/await}
95-
{:else if _loader?.cdn_url}
96-
{#await initCDNLoader(_loader.cdn_url)}
97-
{#await AwaitedMonacoDiffEditor then MonacoDiffEditor}
98-
<MonacoDiffEditor {...editorProps}>
99-
<slot></slot>
100-
</MonacoDiffEditor>
101-
{/await}
102-
{/await}
103-
{:else}
92+
{#await awaitedLoader then}
10493
{#await AwaitedMonacoDiffEditor then MonacoDiffEditor}
10594
<MonacoDiffEditor {...editorProps}>
10695
<slot></slot>
10796
</MonacoDiffEditor>
10897
{/await}
109-
{/if}
98+
{/await}
11099
{/if}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
import config from '../../../defineConfig.js';
22

3-
export default config({
4-
external: {
5-
excludes: ['@monaco-editor/loader'],
6-
},
7-
});
3+
export default config();
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
import config from '../../defineConfig.js';
22

3-
export default config({
4-
external: {
5-
excludes: ['@monaco-editor/loader'],
6-
},
7-
});
3+
export default config();

frontend/pro/monaco-editor/loader.ts

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
1-
function getMonacoLoader() {
1+
import { initialize } from '@svelte-preprocess-react/component';
2+
3+
async function getMonacoLoader() {
4+
await initialize();
25
return new Promise<{
36
loader: typeof window.ms_globals.monacoLoader;
47
done?: () => void;
58
}>((resolve) => {
6-
if (!window.ms_globals?.monacoLoader) {
7-
if (window.ms_globals?.monacoLoaderPromise) {
8-
window.ms_globals.monacoLoaderPromise.then((loader) => {
9-
resolve({
10-
loader,
11-
});
12-
});
13-
} else {
14-
window.ms_globals ??= {} as typeof window.ms_globals;
15-
window.ms_globals.monacoLoaderPromise = new Promise((resolve2) => {
16-
import('@monaco-editor/react').then((m) => {
17-
window.ms_globals.monacoLoader = m.loader;
18-
resolve({
19-
loader: m.loader,
20-
done: () => resolve2(m.loader),
21-
});
22-
});
9+
if (window.ms_globals?.monacoLoaderPromise) {
10+
window.ms_globals.monacoLoaderPromise.then(() => {
11+
resolve({
12+
loader: window.ms_globals.monacoLoader,
2313
});
24-
}
14+
});
2515
} else {
26-
resolve({
27-
loader: window.ms_globals.monacoLoader,
16+
window.ms_globals.monacoLoaderPromise = new Promise((resolve2) => {
17+
resolve({
18+
loader: window.ms_globals.monacoLoader,
19+
done: () => {
20+
resolve2();
21+
},
22+
});
2823
});
2924
}
3025
});

frontend/svelte-preprocess-react/inject.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { loader as monacoLoader } from '@monaco-editor/react';
2+
import { loader } from '@monaco-editor/react';
23
import React from 'react';
34
import ReactDOM from 'react-dom';
45
import ReactDOMClient from 'react-dom/client';
@@ -42,7 +43,7 @@ declare global {
4243
autokey: number;
4344
loadingKey: number;
4445
monacoLoader: typeof monacoLoader | null;
45-
monacoLoaderPromise: Promise<typeof monacoLoader> | null;
46+
monacoLoaderPromise: Promise<void> | null;
4647
rerender: (props: BridgeProps) => void;
4748
// render items
4849
createItemsContext: typeof createItemsContext;
@@ -103,7 +104,7 @@ window.ms_globals = {
103104
nodes: [],
104105
},
105106
rerender,
106-
monacoLoader: null,
107+
monacoLoader: loader,
107108
monacoLoaderPromise: null,
108109
// render items
109110
createItemsContext,

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ artifacts = [
231231
"/backend/modelscope_studio/components/pro/web_sandbox/templates",
232232
"/backend/modelscope_studio/components/antd/statistic/timer/templates",
233233
"/backend/modelscope_studio/components/pro/monaco_editor/templates",
234+
"/backend/modelscope_studio/components/pro/monaco_editor/diff_editor/templates",
234235
]
235236

236237
[tool.yapfignore]

0 commit comments

Comments
 (0)