feat(monaco): reintroduce meta-json-schema (#6509)

* refactor(editor): make EditorViewer controlled and unify document state handling

* fix(monaco-yaml): add patchCreateWebWorker

* feat(monaco): reintroduce meta-json-schema

* fix(editor): reset document state on target change
This commit is contained in:
Slinetrac
2026-03-15 06:04:59 +00:00
committed by GitHub
parent 9989bff4e6
commit 837508c02c
14 changed files with 431 additions and 481 deletions
+17 -6
View File
@@ -9,14 +9,24 @@ import { getRuntimeYaml } from "@/services/cmds";
export const ConfigViewer = forwardRef<DialogRef>((_, ref) => {
const { t } = useTranslation();
const [open, setOpen] = useState(false);
const [loading, setLoading] = useState(false);
const [runtimeConfig, setRuntimeConfig] = useState("");
useImperativeHandle(ref, () => ({
open: () => {
getRuntimeYaml().then((data) => {
setRuntimeConfig(data ?? "# Error getting runtime yaml\n");
setOpen(true);
});
setRuntimeConfig("");
setLoading(true);
setOpen(true);
getRuntimeYaml()
.then((data) => {
setRuntimeConfig(data ?? "# Error getting runtime yaml\n");
})
.catch(() => {
setRuntimeConfig("# Error getting runtime yaml\n");
})
.finally(() => {
setLoading(false);
});
},
close: () => setOpen(false),
}));
@@ -31,10 +41,11 @@ export const ConfigViewer = forwardRef<DialogRef>((_, ref) => {
<Chip label={t("shared.labels.readOnly")} size="small" />
</Box>
}
initialData={() => Promise.resolve(runtimeConfig)}
dataKey="runtime-config"
value={runtimeConfig}
readOnly
language="yaml"
path="runtime-config.yaml"
loading={loading}
onClose={() => setOpen(false)}
/>
);
+23 -12
View File
@@ -102,6 +102,8 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
const [open, setOpen] = useState(false);
const [editorOpen, setEditorOpen] = useState(false);
const [pacEditorValue, setPacEditorValue] = useState(DEFAULT_PAC);
const [pacEditorSavedValue, setPacEditorSavedValue] = useState(DEFAULT_PAC);
const [saving, setSaving] = useState(false);
const { verge, patchVerge, mutateVerge } = useVerge();
const [hostOptions, setHostOptions] = useState<string[]>([]);
@@ -218,6 +220,21 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
? !validReg.test(value.bypass)
: false;
const openPacEditor = () => {
const nextPac = value.pac_content ?? DEFAULT_PAC;
setPacEditorValue(nextPac);
setPacEditorSavedValue(nextPac);
setEditorOpen(true);
};
const handleSavePac = useLockFn(async () => {
const nextPac =
pacEditorValue.trim().length > 0 ? pacEditorValue : DEFAULT_PAC;
setValue((current) => ({ ...current, pac_content: nextPac }));
setPacEditorSavedValue(nextPac);
});
useImperativeHandle(ref, () => ({
open: () => {
setOpen(true);
@@ -665,9 +682,7 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
<Button
startIcon={<EditRounded />}
variant="outlined"
onClick={() => {
setEditorOpen(true);
}}
onClick={openPacEditor}
>
{t("settings.modals.sysproxy.actions.editPac")}
</Button>
@@ -675,16 +690,12 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
<EditorViewer
open={true}
title={t("settings.modals.sysproxy.actions.editPac")}
initialData={() => Promise.resolve(value.pac_content ?? "")}
dataKey="sysproxy-pac"
value={pacEditorValue}
language="javascript"
onSave={(_prev, curr) => {
let pac = DEFAULT_PAC;
if (curr && curr.trim().length > 0) {
pac = curr;
}
setValue((v) => ({ ...v, pac_content: pac }));
}}
path="sysproxy-pac.js"
dirty={pacEditorValue !== pacEditorSavedValue}
onChange={setPacEditorValue}
onSave={handleSavePac}
onClose={() => setEditorOpen(false)}
/>
)}
+20 -19
View File
@@ -15,7 +15,6 @@ import {
useMemo,
useRef,
useState,
useCallback,
} from "react";
import { useTranslation } from "react-i18next";
@@ -31,6 +30,8 @@ export function ThemeViewer(props: { ref?: React.Ref<DialogRef> }) {
const [open, setOpen] = useState(false);
const [editorOpen, setEditorOpen] = useState(false);
const [cssEditorValue, setCssEditorValue] = useState("");
const [cssEditorSavedValue, setCssEditorSavedValue] = useState("");
const { verge, patchVerge } = useVerge();
const { theme_setting } = verge ?? {};
const [theme, setTheme] = useState(theme_setting || {});
@@ -111,12 +112,18 @@ export function ThemeViewer(props: { ref?: React.Ref<DialogRef> }) {
[],
);
// Stable loader that returns a fresh Promise each call so EditorViewer
// can retry/refresh and always read the latest staged CSS from state.
const loadCss = useCallback(
() => Promise.resolve(themeRef.current?.css_injection ?? ""),
[],
);
const openCssEditor = () => {
const nextCss = themeRef.current?.css_injection ?? "";
setCssEditorValue(nextCss);
setCssEditorSavedValue(nextCss);
setEditorOpen(true);
};
const handleSaveCss = useLockFn(async () => {
const prevTheme = themeRef.current || {};
setTheme({ ...prevTheme, css_injection: cssEditorValue });
setCssEditorSavedValue(cssEditorValue);
});
const renderItem = (labelKey: string, key: ThemeKey) => {
const label = t(labelKey);
@@ -167,9 +174,7 @@ export function ThemeViewer(props: { ref?: React.Ref<DialogRef> }) {
<Button
startIcon={<EditRounded />}
variant="outlined"
onClick={() => {
setEditorOpen(true);
}}
onClick={openCssEditor}
>
{t("settings.components.verge.theme.actions.editCss")}
</Button>
@@ -177,16 +182,12 @@ export function ThemeViewer(props: { ref?: React.Ref<DialogRef> }) {
<EditorViewer
open={true}
title={t("settings.components.verge.theme.dialogs.editCssTitle")}
initialData={loadCss}
dataKey="theme-css"
value={cssEditorValue}
language="css"
onSave={async (_prev, curr) => {
// Only stage the CSS change locally. Persistence happens
// when the outer Theme dialog's Save button is pressed.
const prevTheme = themeRef.current || {};
const nextCss = curr ?? "";
setTheme({ ...prevTheme, css_injection: nextCss });
}}
path="theme-css.css"
dirty={cssEditorValue !== cssEditorSavedValue}
onChange={setCssEditorValue}
onSave={handleSaveCss}
onClose={() => {
setEditorOpen(false);
}}