BUGFIX: Crash in theme editor modal (#1735)

This commit is contained in:
catloversg 2024-11-04 13:38:01 +07:00 committed by GitHub
parent f6502dd490
commit c8183e55db
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 8 deletions

@ -11,6 +11,7 @@ import { Modal } from "../../ui/React/Modal";
import { OptionSwitch } from "../../ui/React/OptionSwitch";
import { defaultMonacoTheme } from "./themes";
import { dialogBoxCreate } from "../../ui/React/DialogBox";
type ColorEditorProps = {
label: string;
@ -74,12 +75,19 @@ export function ThemeEditorModal(props: ThemeEditorProps): React.ReactElement {
function onThemeChange(event: React.ChangeEvent<HTMLInputElement>): void {
try {
const importedTheme = JSON.parse(event.target.value);
if (typeof importedTheme !== "object") return;
const importedTheme = JSON.parse(event.target.value) as typeof Settings.EditorTheme;
if (importedTheme == null) {
throw new Error("Theme data must not be null or undefined.");
}
if (typeof importedTheme !== "object") {
throw new Error(`Theme data is invalid.`);
}
Settings.EditorTheme = importedTheme;
props.onChange();
} catch (err) {
// ignore
} catch (error) {
console.error(`Theme data is invalid. Data: ${event.target.value}.`);
console.error(error);
dialogBoxCreate(`Invalid theme. ${error}`);
}
}

@ -18,6 +18,7 @@ import { UserInterfaceTheme } from "@nsdefs";
import { Router } from "../../ui/GameRoot";
import { Page } from "../../ui/Router";
import { ThemeCollaborate } from "./ThemeCollaborate";
import { dialogBoxCreate } from "../../ui/React/DialogBox";
interface IProps {
open: boolean;
@ -81,15 +82,22 @@ export function ThemeEditorModal(props: IProps): React.ReactElement {
function onThemeChange(event: React.ChangeEvent<HTMLInputElement>): void {
try {
const importedTheme = JSON.parse(event.target.value);
if (typeof importedTheme !== "object") return;
const importedTheme = JSON.parse(event.target.value) as typeof Settings.theme;
if (importedTheme == null) {
throw new Error("Theme data must not be null or undefined.");
}
if (typeof importedTheme !== "object") {
throw new Error(`Theme data is invalid.`);
}
setCustomTheme(importedTheme);
for (const key of Object.keys(importedTheme)) {
Settings.theme[key] = importedTheme[key];
}
ThemeEvents.emit();
} catch (err) {
// ignore
} catch (error) {
console.error(`Theme data is invalid. Data: ${event.target.value}.`);
console.error(error);
dialogBoxCreate(`Invalid theme. ${error}`);
}
}