mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-29 09:00:03 +08:00
perf: lazy load monaco editor
This commit is contained in:
@@ -12,6 +12,7 @@ export {
|
||||
} from './base-split-chip-editor'
|
||||
export { BaseStyledSelect } from './base-styled-select'
|
||||
export { BaseStyledTextField } from './base-styled-text-field'
|
||||
export { MonacoEditor } from './monaco-editor'
|
||||
export { Switch } from './base-switch'
|
||||
export { TooltipIcon } from './base-tooltip-icon'
|
||||
export { VirtualList, type VirtualListHandle } from './virtual-list'
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import { Suspense, lazy } from 'react'
|
||||
|
||||
import { loadMonacoEditor } from '@/services/monaco'
|
||||
|
||||
type MonacoEditorProps = import('@monaco-editor/react').EditorProps
|
||||
|
||||
let monacoEditorBundle: Awaited<ReturnType<typeof loadMonacoEditor>>
|
||||
|
||||
const MonacoEditorView = ({ beforeMount, ...props }: MonacoEditorProps) => {
|
||||
const { Editor, beforeEditorMount } = monacoEditorBundle
|
||||
|
||||
return (
|
||||
<Editor
|
||||
{...props}
|
||||
beforeMount={(monaco) => {
|
||||
beforeEditorMount()
|
||||
beforeMount?.(monaco)
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
const MonacoEditorContent = lazy(async () => {
|
||||
monacoEditorBundle = await loadMonacoEditor()
|
||||
return { default: MonacoEditorView }
|
||||
})
|
||||
|
||||
export const MonacoEditor = (props: MonacoEditorProps) => (
|
||||
<Suspense fallback={null}>
|
||||
<MonacoEditorContent {...props} />
|
||||
</Suspense>
|
||||
)
|
||||
@@ -1,4 +1,3 @@
|
||||
import MonacoEditor from '@monaco-editor/react'
|
||||
import {
|
||||
CloseFullscreenRounded,
|
||||
ContentPasteRounded,
|
||||
@@ -16,14 +15,13 @@ import {
|
||||
} from '@mui/material'
|
||||
import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow'
|
||||
import { useLockFn } from 'ahooks'
|
||||
import type { editor } from 'monaco-editor'
|
||||
import { type ReactNode, useCallback, useEffect, useRef, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
import { BaseLoadingOverlay } from '@/components/base'
|
||||
import { beforeEditorMount } from '@/services/monaco'
|
||||
import { BaseLoadingOverlay, MonacoEditor } from '@/components/base'
|
||||
import { showNotice } from '@/services/notice-service'
|
||||
import { useThemeMode } from '@/services/states'
|
||||
import type { MonacoEditorInstance, MonacoMarker } from '@/types/monaco'
|
||||
import debounce from '@/utils/debounce'
|
||||
import getSystem from '@/utils/get-system'
|
||||
|
||||
@@ -44,7 +42,7 @@ export interface EditorViewerProps {
|
||||
onChange?: (value: string) => void
|
||||
onSave?: () => void | Promise<void>
|
||||
onClose: () => void
|
||||
onValidate?: (markers: editor.IMarker[]) => void
|
||||
onValidate?: (markers: MonacoMarker[]) => void
|
||||
}
|
||||
|
||||
export const EditorViewer = ({
|
||||
@@ -65,7 +63,7 @@ export const EditorViewer = ({
|
||||
const { t } = useTranslation()
|
||||
const themeMode = useThemeMode()
|
||||
const [isMaximized, setIsMaximized] = useState(false)
|
||||
const editorRef = useRef<editor.IStandaloneCodeEditor | null>(null)
|
||||
const editorRef = useRef<MonacoEditorInstance | null>(null)
|
||||
|
||||
const resolvedTitle = title ?? t('profiles.components.menu.editFile')
|
||||
const disableSave = loading || saveDisabled || dirty === false
|
||||
@@ -218,7 +216,6 @@ export const EditorViewer = ({
|
||||
loading={null}
|
||||
saveViewState
|
||||
keepCurrentModel={false}
|
||||
beforeMount={beforeEditorMount}
|
||||
onMount={(editorInstance) => {
|
||||
editorRef.current = editorInstance
|
||||
syncEditorValue()
|
||||
|
||||
@@ -8,7 +8,6 @@ import {
|
||||
useSensors,
|
||||
} from '@dnd-kit/core'
|
||||
import { SortableContext, sortableKeyboardCoordinates } from '@dnd-kit/sortable'
|
||||
import MonacoEditor from '@monaco-editor/react'
|
||||
import {
|
||||
VerticalAlignBottomRounded,
|
||||
VerticalAlignTopRounded,
|
||||
@@ -34,7 +33,6 @@ import {
|
||||
requestIdleCallback,
|
||||
} from 'foxact/request-idle-callback'
|
||||
import yaml from 'js-yaml'
|
||||
import type { editor } from 'monaco-editor'
|
||||
import {
|
||||
startTransition,
|
||||
useCallback,
|
||||
@@ -46,7 +44,12 @@ import {
|
||||
import { Controller, useForm } from 'react-hook-form'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
import { BaseSearchBox, Switch, VirtualList } from '@/components/base'
|
||||
import {
|
||||
BaseSearchBox,
|
||||
MonacoEditor,
|
||||
Switch,
|
||||
VirtualList,
|
||||
} from '@/components/base'
|
||||
import { GroupItem } from '@/components/profile/group-item'
|
||||
import {
|
||||
getNetworkInterfaces,
|
||||
@@ -56,6 +59,7 @@ import {
|
||||
import { showNotice } from '@/services/notice-service'
|
||||
import { useThemeMode } from '@/services/states'
|
||||
import type { TranslationKey } from '@/types/generated/i18n-keys'
|
||||
import type { MonacoEditorInstance } from '@/types/monaco'
|
||||
import getSystem from '@/utils/get-system'
|
||||
|
||||
interface Props {
|
||||
@@ -151,7 +155,7 @@ export const GroupsEditorViewer = (props: Props) => {
|
||||
[t],
|
||||
)
|
||||
const themeMode = useThemeMode()
|
||||
const editorRef = useRef<editor.IStandaloneCodeEditor | null>(null)
|
||||
const editorRef = useRef<MonacoEditorInstance | null>(null)
|
||||
const [prevData, setPrevData] = useState('')
|
||||
const [currData, setCurrData] = useState('')
|
||||
const [visualization, setVisualization] = useState(true)
|
||||
|
||||
@@ -8,7 +8,6 @@ import {
|
||||
useSensors,
|
||||
} from '@dnd-kit/core'
|
||||
import { SortableContext, sortableKeyboardCoordinates } from '@dnd-kit/sortable'
|
||||
import MonacoEditor from '@monaco-editor/react'
|
||||
import {
|
||||
VerticalAlignBottomRounded,
|
||||
VerticalAlignTopRounded,
|
||||
@@ -27,7 +26,6 @@ import {
|
||||
} from '@mui/material'
|
||||
import { useLockFn } from 'ahooks'
|
||||
import yaml from 'js-yaml'
|
||||
import type { editor } from 'monaco-editor'
|
||||
import {
|
||||
startTransition,
|
||||
useCallback,
|
||||
@@ -38,11 +36,12 @@ import {
|
||||
} from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
import { BaseSearchBox, VirtualList } from '@/components/base'
|
||||
import { BaseSearchBox, MonacoEditor, VirtualList } from '@/components/base'
|
||||
import { ProxyItem } from '@/components/profile/proxy-item'
|
||||
import { readProfileFile, saveProfileFile } from '@/services/cmds'
|
||||
import { showNotice } from '@/services/notice-service'
|
||||
import { useThemeMode } from '@/services/states'
|
||||
import type { MonacoEditorInstance } from '@/types/monaco'
|
||||
import getSystem from '@/utils/get-system'
|
||||
import parseUri from '@/utils/uri-parser'
|
||||
|
||||
@@ -58,7 +57,7 @@ export const ProxiesEditorViewer = (props: Props) => {
|
||||
const { profileUid, property, open, onClose, onSave } = props
|
||||
const { t } = useTranslation()
|
||||
const themeMode = useThemeMode()
|
||||
const editorRef = useRef<editor.IStandaloneCodeEditor | null>(null)
|
||||
const editorRef = useRef<MonacoEditorInstance | null>(null)
|
||||
const [prevData, setPrevData] = useState('')
|
||||
const [currData, setCurrData] = useState('')
|
||||
const [visualization, setVisualization] = useState(true)
|
||||
|
||||
@@ -8,7 +8,6 @@ import {
|
||||
useSensors,
|
||||
} from '@dnd-kit/core'
|
||||
import { SortableContext, sortableKeyboardCoordinates } from '@dnd-kit/sortable'
|
||||
import MonacoEditor from '@monaco-editor/react'
|
||||
import {
|
||||
VerticalAlignBottomRounded,
|
||||
VerticalAlignTopRounded,
|
||||
@@ -29,7 +28,6 @@ import {
|
||||
} from '@mui/material'
|
||||
import { useLockFn } from 'ahooks'
|
||||
import yaml from 'js-yaml'
|
||||
import type { editor } from 'monaco-editor'
|
||||
import {
|
||||
startTransition,
|
||||
useCallback,
|
||||
@@ -40,12 +38,18 @@ import {
|
||||
} from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
import { BaseSearchBox, Switch, VirtualList } from '@/components/base'
|
||||
import {
|
||||
BaseSearchBox,
|
||||
MonacoEditor,
|
||||
Switch,
|
||||
VirtualList,
|
||||
} from '@/components/base'
|
||||
import { RuleItem } from '@/components/profile/rule-item'
|
||||
import { readProfileFile, saveProfileFile } from '@/services/cmds'
|
||||
import { showNotice } from '@/services/notice-service'
|
||||
import { useThemeMode } from '@/services/states'
|
||||
import type { TranslationKey } from '@/types/generated/i18n-keys'
|
||||
import type { MonacoEditorInstance } from '@/types/monaco'
|
||||
import getSystem from '@/utils/get-system'
|
||||
import { isValidIpCidr } from '@/utils/network'
|
||||
|
||||
@@ -253,7 +257,7 @@ export const RulesEditorViewer = (props: Props) => {
|
||||
const { t } = useTranslation()
|
||||
const themeMode = useThemeMode()
|
||||
|
||||
const editorRef = useRef<editor.IStandaloneCodeEditor | null>(null)
|
||||
const editorRef = useRef<MonacoEditorInstance | null>(null)
|
||||
|
||||
const [prevData, setPrevData] = useState('')
|
||||
const [currData, setCurrData] = useState('')
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import MonacoEditor from '@monaco-editor/react'
|
||||
import { RestartAltRounded } from '@mui/icons-material'
|
||||
import {
|
||||
Box,
|
||||
@@ -16,7 +15,6 @@ import {
|
||||
import { invoke } from '@tauri-apps/api/core'
|
||||
import { useLockFn } from 'ahooks'
|
||||
import yaml from 'js-yaml'
|
||||
import type { editor } from 'monaco-editor'
|
||||
import type { Ref } from 'react'
|
||||
import {
|
||||
useCallback,
|
||||
@@ -28,10 +26,11 @@ import {
|
||||
} from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
import { BaseDialog, DialogRef, Switch } from '@/components/base'
|
||||
import { BaseDialog, DialogRef, MonacoEditor, Switch } from '@/components/base'
|
||||
import { useClash } from '@/hooks/use-clash'
|
||||
import { showNotice } from '@/services/notice-service'
|
||||
import { useThemeMode } from '@/services/states'
|
||||
import type { MonacoEditorInstance } from '@/types/monaco'
|
||||
import { debugLog } from '@/utils/debug'
|
||||
import getSystem from '@/utils/get-system'
|
||||
|
||||
@@ -190,7 +189,7 @@ export function DnsViewer({ ref }: { ref?: Ref<DialogRef> }) {
|
||||
const [open, setOpen] = useState(false)
|
||||
const [visualization, setVisualization] = useState(true)
|
||||
const skipYamlSyncRef = useRef(false)
|
||||
const editorRef = useRef<editor.IStandaloneCodeEditor | null>(null)
|
||||
const editorRef = useRef<MonacoEditorInstance | null>(null)
|
||||
const [values, setValues] = useState<{
|
||||
enable: boolean
|
||||
listen: string
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import './assets/styles/index.scss'
|
||||
import './services/monaco'
|
||||
|
||||
import { ResizeObserver } from '@juggle/resize-observer'
|
||||
import { QueryClientProvider } from '@tanstack/react-query'
|
||||
|
||||
+79
-56
@@ -1,70 +1,93 @@
|
||||
import { loader } from '@monaco-editor/react'
|
||||
import metaSchema from 'meta-json-schema/schemas/meta-json-schema.json'
|
||||
import * as monaco from 'monaco-editor'
|
||||
import editorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker'
|
||||
import cssWorker from 'monaco-editor/esm/vs/language/css/css.worker?worker'
|
||||
import tsWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker'
|
||||
import { configureMonacoYaml, JSONSchema } from 'monaco-yaml'
|
||||
import pac from 'types-pac/pac.d.ts?raw'
|
||||
type MonacoModule = typeof import('monaco-editor')
|
||||
type MonacoEditorPackage = typeof import('@monaco-editor/react')
|
||||
|
||||
import yamlWorker from '@/utils/yaml.worker?worker'
|
||||
|
||||
self.MonacoEnvironment = {
|
||||
getWorker(_, label) {
|
||||
switch (label) {
|
||||
case 'css':
|
||||
case 'less':
|
||||
case 'scss':
|
||||
return new cssWorker()
|
||||
case 'typescript':
|
||||
case 'javascript':
|
||||
return new tsWorker()
|
||||
case 'yaml':
|
||||
return new yamlWorker()
|
||||
default:
|
||||
return new editorWorker()
|
||||
}
|
||||
},
|
||||
type MonacoEditorBundle = {
|
||||
Editor: MonacoEditorPackage['default']
|
||||
beforeEditorMount: () => void
|
||||
}
|
||||
|
||||
loader.config({ monaco })
|
||||
let loadPromise: Promise<MonacoEditorBundle> | undefined
|
||||
let monacoConfigured = false
|
||||
|
||||
// Work around https://github.com/remcohaszing/monaco-yaml/issues/272.
|
||||
const patchCreateWebWorker = () => {
|
||||
const oldCreateWebWorker = monaco.editor.createWebWorker
|
||||
const patchCreateWebWorker = (monaco: MonacoModule) => {
|
||||
const createWebWorker = monaco.editor.createWebWorker
|
||||
|
||||
monaco.editor.createWebWorker = (
|
||||
options: monaco.IWebWorkerOptions | monaco.editor.IInternalWebWorkerOptions,
|
||||
) => {
|
||||
if ('worker' in options) {
|
||||
return oldCreateWebWorker(options)
|
||||
}
|
||||
type CreateWebWorker = typeof createWebWorker
|
||||
type WorkerOptions = Parameters<CreateWebWorker>[0] & { worker?: unknown }
|
||||
|
||||
return monaco.createWebWorker(options)
|
||||
}
|
||||
monaco.editor.createWebWorker = ((options: WorkerOptions) =>
|
||||
'worker' in options
|
||||
? createWebWorker(options)
|
||||
: monaco.createWebWorker(options)) as CreateWebWorker
|
||||
}
|
||||
|
||||
let mounted = false
|
||||
export const loadMonacoEditor = () => {
|
||||
loadPromise ??= Promise.all([
|
||||
import('@monaco-editor/react'),
|
||||
import('monaco-editor'),
|
||||
import('monaco-editor/esm/vs/editor/editor.worker?worker'),
|
||||
import('monaco-editor/esm/vs/language/css/css.worker?worker'),
|
||||
import('monaco-editor/esm/vs/language/typescript/ts.worker?worker'),
|
||||
import('monaco-yaml'),
|
||||
import('meta-json-schema/schemas/meta-json-schema.json'),
|
||||
import('types-pac/pac.d.ts?raw'),
|
||||
import('@/utils/yaml.worker?worker'),
|
||||
]).then(
|
||||
([
|
||||
editorModule,
|
||||
monaco,
|
||||
{ default: EditorWorker },
|
||||
{ default: CssWorker },
|
||||
{ default: TsWorker },
|
||||
{ configureMonacoYaml },
|
||||
{ default: metaSchema },
|
||||
{ default: pac },
|
||||
{ default: YamlWorker },
|
||||
]) => {
|
||||
const workers = {
|
||||
css: CssWorker,
|
||||
less: CssWorker,
|
||||
scss: CssWorker,
|
||||
typescript: TsWorker,
|
||||
javascript: TsWorker,
|
||||
yaml: YamlWorker,
|
||||
}
|
||||
|
||||
export const beforeEditorMount = () => {
|
||||
if (mounted) return
|
||||
self.MonacoEnvironment = {
|
||||
getWorker(_, label) {
|
||||
return new (workers[label as keyof typeof workers] ?? EditorWorker)()
|
||||
},
|
||||
}
|
||||
|
||||
patchCreateWebWorker()
|
||||
editorModule.loader.config({ monaco })
|
||||
|
||||
monaco.typescript.javascriptDefaults.addExtraLib(pac, 'pac.d.ts')
|
||||
return {
|
||||
Editor: editorModule.default,
|
||||
beforeEditorMount: () => {
|
||||
if (monacoConfigured) return
|
||||
|
||||
configureMonacoYaml(monaco, {
|
||||
validate: true,
|
||||
enableSchemaRequest: true,
|
||||
completion: true,
|
||||
schemas: [
|
||||
{
|
||||
uri: 'http://example.com/meta-json-schema.json',
|
||||
fileMatch: ['**/*.yaml', '**/*.yml'],
|
||||
schema: metaSchema as unknown as JSONSchema, // JSON import is inferred as a literal type
|
||||
},
|
||||
],
|
||||
})
|
||||
patchCreateWebWorker(monaco)
|
||||
monaco.typescript.javascriptDefaults.addExtraLib(pac, 'pac.d.ts')
|
||||
|
||||
mounted = true
|
||||
configureMonacoYaml(monaco, {
|
||||
validate: true,
|
||||
enableSchemaRequest: true,
|
||||
completion: true,
|
||||
schemas: [
|
||||
{
|
||||
uri: 'http://example.com/meta-json-schema.json',
|
||||
fileMatch: ['**/*.yaml', '**/*.yml'],
|
||||
schema:
|
||||
metaSchema as unknown as import('monaco-yaml').JSONSchema,
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
monacoConfigured = true
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
return loadPromise
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
export type MonacoEditorInstance =
|
||||
import('monaco-editor').editor.IStandaloneCodeEditor
|
||||
|
||||
export type MonacoMarker = import('monaco-editor').editor.IMarker
|
||||
Reference in New Issue
Block a user