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 { OptionSwitch } from "../../ui/React/OptionSwitch";
import { defaultMonacoTheme } from "./themes"; import { defaultMonacoTheme } from "./themes";
import { dialogBoxCreate } from "../../ui/React/DialogBox";
type ColorEditorProps = { type ColorEditorProps = {
label: string; label: string;
@ -74,12 +75,19 @@ export function ThemeEditorModal(props: ThemeEditorProps): React.ReactElement {
function onThemeChange(event: React.ChangeEvent<HTMLInputElement>): void { function onThemeChange(event: React.ChangeEvent<HTMLInputElement>): void {
try { try {
const importedTheme = JSON.parse(event.target.value); const importedTheme = JSON.parse(event.target.value) as typeof Settings.EditorTheme;
if (typeof importedTheme !== "object") return; 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; Settings.EditorTheme = importedTheme;
props.onChange(); props.onChange();
} catch (err) { } catch (error) {
// ignore 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 { Router } from "../../ui/GameRoot";
import { Page } from "../../ui/Router"; import { Page } from "../../ui/Router";
import { ThemeCollaborate } from "./ThemeCollaborate"; import { ThemeCollaborate } from "./ThemeCollaborate";
import { dialogBoxCreate } from "../../ui/React/DialogBox";
interface IProps { interface IProps {
open: boolean; open: boolean;
@ -81,15 +82,22 @@ export function ThemeEditorModal(props: IProps): React.ReactElement {
function onThemeChange(event: React.ChangeEvent<HTMLInputElement>): void { function onThemeChange(event: React.ChangeEvent<HTMLInputElement>): void {
try { try {
const importedTheme = JSON.parse(event.target.value); const importedTheme = JSON.parse(event.target.value) as typeof Settings.theme;
if (typeof importedTheme !== "object") return; 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); setCustomTheme(importedTheme);
for (const key of Object.keys(importedTheme)) { for (const key of Object.keys(importedTheme)) {
Settings.theme[key] = importedTheme[key]; Settings.theme[key] = importedTheme[key];
} }
ThemeEvents.emit(); ThemeEvents.emit();
} catch (err) { } catch (error) {
// ignore console.error(`Theme data is invalid. Data: ${event.target.value}.`);
console.error(error);
dialogBoxCreate(`Invalid theme. ${error}`);
} }
} }