Skip to content

Commit f1f32d0

Browse files
载入样例配置功能 (#14)
* 载入样例配置功能 * 优化语义性
1 parent 363d7f6 commit f1f32d0

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

src/contestEditor/index.tsx

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { type FC, useEffect, useState, useMemo, use, Suspense } from "react";
22
import { useImmer } from "use-immer";
33
import type { ImmerContestData } from "@/types/contestData";
44
import exampleStatements from "./exampleStatements";
5-
import { App, Button, Tabs, type TabsProps, Space } from "antd";
5+
import { App, Button, Tabs, type TabsProps, Space, Dropdown } from "antd";
66
import Body from "./body";
77
import {
88
newProblem,
@@ -14,17 +14,17 @@ import {
1414
faFileArrowDown,
1515
faFileImport,
1616
faFileExport,
17-
faRotateLeft,
17+
faChevronDown,
1818
} from "@fortawesome/free-solid-svg-icons";
1919
import { debounce } from "lodash";
2020
import { compileToPdf, typstInitPromise, registerAssetUrls } from "@/compiler";
2121
import {
2222
saveConfigToDB,
2323
loadConfigFromDB,
24-
clearDB,
2524
exportConfig,
2625
importConfig,
2726
saveImageToDB,
27+
clearDB,
2828
} from "@/utils/indexedDBUtils";
2929

3030
import "./index.css";
@@ -130,35 +130,45 @@ const ContestEditorImpl: FC<{
130130
tabBarExtraContent={{
131131
right: (
132132
<Space>
133-
<Button
134-
type="default"
135-
icon={<FontAwesomeIcon icon={faRotateLeft} />}
136-
onClick={async () => {
137-
const confirmed = await modal.confirm({
138-
title: "确认重置配置",
139-
content:
140-
"这将清除所有当前的编辑,恢复为初始配置。此操作不可撤销。",
141-
});
142-
if (confirmed) {
143-
// Clear IndexedDB and revoke blob URLs
144-
await clearDB();
145-
contestData.images.forEach((img) =>
146-
URL.revokeObjectURL(img.url),
147-
);
148-
149-
const initialData = toImmerContestData({
150-
...exampleStatements["SupportedGrammer"],
151-
images: [],
133+
<Dropdown
134+
menu={{
135+
items: Object.keys(exampleStatements).map((x) => ({
136+
key: x,
137+
label: x,
138+
})),
139+
onClick: async ({ key }) => {
140+
const r = await modal.confirm({
141+
title: "载入示例配置",
142+
content: "载入示例配置将会覆盖当前的所有配置,是否继续?",
143+
okText: "继续",
144+
cancelText: "取消",
152145
});
153-
updateContestData(() => initialData);
146+
if (!r) return;
147+
for (const i of contestData.images)
148+
URL.revokeObjectURL(i.url);
149+
const example = exampleStatements[key];
154150
setPanel("config");
155-
message.success("配置已重置");
156-
}
151+
const conf = toImmerContestData({
152+
...example,
153+
images: [],
154+
});
155+
updateContestData(() => conf);
156+
await clearDB();
157+
await saveConfigToDB(conf);
158+
message.success("示例配置已经载入");
159+
},
157160
}}
158-
title="重置配置"
161+
trigger={["click", "hover"]}
159162
>
160-
重置
161-
</Button>
163+
<a
164+
role="button"
165+
tabIndex={0}
166+
href="#"
167+
onClick={(e) => e.preventDefault()}
168+
>
169+
载入示例配置 <FontAwesomeIcon icon={faChevronDown} />
170+
</a>
171+
</Dropdown>
162172
<Button
163173
type="default"
164174
icon={<FontAwesomeIcon icon={faFileImport} />}

0 commit comments

Comments
 (0)