rules-editor-viewer.tsx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. import { ReactNode, useEffect, useMemo, useState } from "react";
  2. import { useLockFn } from "ahooks";
  3. import yaml from "js-yaml";
  4. import { useTranslation } from "react-i18next";
  5. import {
  6. DndContext,
  7. closestCenter,
  8. KeyboardSensor,
  9. PointerSensor,
  10. useSensor,
  11. useSensors,
  12. DragEndEvent,
  13. } from "@dnd-kit/core";
  14. import {
  15. SortableContext,
  16. sortableKeyboardCoordinates,
  17. } from "@dnd-kit/sortable";
  18. import {
  19. Autocomplete,
  20. Box,
  21. Button,
  22. Dialog,
  23. DialogActions,
  24. DialogContent,
  25. DialogTitle,
  26. List,
  27. ListItem,
  28. ListItemText,
  29. TextField,
  30. styled,
  31. } from "@mui/material";
  32. import { readProfileFile, saveProfileFile } from "@/services/cmds";
  33. import { Notice, Switch } from "@/components/base";
  34. import getSystem from "@/utils/get-system";
  35. import { RuleItem } from "@/components/profile/rule-item";
  36. import { BaseSearchBox } from "../base/base-search-box";
  37. import { Virtuoso } from "react-virtuoso";
  38. import MonacoEditor from "react-monaco-editor";
  39. import { useThemeMode } from "@/services/states";
  40. interface Props {
  41. groupsUid: string;
  42. mergeUid: string;
  43. profileUid: string;
  44. property: string;
  45. open: boolean;
  46. onClose: () => void;
  47. onSave?: (prev?: string, curr?: string) => void;
  48. }
  49. const portValidator = (value: string): boolean => {
  50. return new RegExp(
  51. "^(?:[1-9]\\d{0,3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])$"
  52. ).test(value);
  53. };
  54. const ipv4CIDRValidator = (value: string): boolean => {
  55. return new RegExp(
  56. "^(?:(?:[1-9]?[0-9]|1[0-9][0-9]|2(?:[0-4][0-9]|5[0-5]))\\.){3}(?:[1-9]?[0-9]|1[0-9][0-9]|2(?:[0-4][0-9]|5[0-5]))(?:\\/(?:[12]?[0-9]|3[0-2]))$"
  57. ).test(value);
  58. };
  59. const ipv6CIDRValidator = (value: string): boolean => {
  60. return new RegExp(
  61. "^([0-9a-fA-F]{1,4}(?::[0-9a-fA-F]{1,4}){7}|::|:(?::[0-9a-fA-F]{1,4}){1,6}|[0-9a-fA-F]{1,4}:(?::[0-9a-fA-F]{1,4}){1,5}|(?:[0-9a-fA-F]{1,4}:){2}(?::[0-9a-fA-F]{1,4}){1,4}|(?:[0-9a-fA-F]{1,4}:){3}(?::[0-9a-fA-F]{1,4}){1,3}|(?:[0-9a-fA-F]{1,4}:){4}(?::[0-9a-fA-F]{1,4}){1,2}|(?:[0-9a-fA-F]{1,4}:){5}:[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,6}:)\\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9])$"
  62. ).test(value);
  63. };
  64. const rules: {
  65. name: string;
  66. required?: boolean;
  67. example?: string;
  68. noResolve?: boolean;
  69. validator?: (value: string) => boolean;
  70. }[] = [
  71. {
  72. name: "DOMAIN",
  73. example: "example.com",
  74. },
  75. {
  76. name: "DOMAIN-SUFFIX",
  77. example: "example.com",
  78. },
  79. {
  80. name: "DOMAIN-KEYWORD",
  81. example: "example",
  82. },
  83. {
  84. name: "DOMAIN-REGEX",
  85. example: "example.*",
  86. },
  87. {
  88. name: "GEOSITE",
  89. example: "youtube",
  90. },
  91. {
  92. name: "GEOIP",
  93. example: "CN",
  94. noResolve: true,
  95. },
  96. {
  97. name: "SRC-GEOIP",
  98. example: "CN",
  99. },
  100. {
  101. name: "IP-ASN",
  102. example: "13335",
  103. noResolve: true,
  104. validator: (value) => (+value ? true : false),
  105. },
  106. {
  107. name: "SRC-IP-ASN",
  108. example: "9808",
  109. validator: (value) => (+value ? true : false),
  110. },
  111. {
  112. name: "IP-CIDR",
  113. example: "127.0.0.0/8",
  114. noResolve: true,
  115. validator: (value) => ipv4CIDRValidator(value) || ipv6CIDRValidator(value),
  116. },
  117. {
  118. name: "IP-CIDR6",
  119. example: "2620:0:2d0:200::7/32",
  120. noResolve: true,
  121. validator: (value) => ipv4CIDRValidator(value) || ipv6CIDRValidator(value),
  122. },
  123. {
  124. name: "SRC-IP-CIDR",
  125. example: "192.168.1.201/32",
  126. validator: (value) => ipv4CIDRValidator(value) || ipv6CIDRValidator(value),
  127. },
  128. {
  129. name: "IP-SUFFIX",
  130. example: "8.8.8.8/24",
  131. noResolve: true,
  132. validator: (value) => ipv4CIDRValidator(value) || ipv6CIDRValidator(value),
  133. },
  134. {
  135. name: "SRC-IP-SUFFIX",
  136. example: "192.168.1.201/8",
  137. validator: (value) => ipv4CIDRValidator(value) || ipv6CIDRValidator(value),
  138. },
  139. {
  140. name: "SRC-PORT",
  141. example: "7777",
  142. validator: (value) => portValidator(value),
  143. },
  144. {
  145. name: "DST-PORT",
  146. example: "80",
  147. validator: (value) => portValidator(value),
  148. },
  149. {
  150. name: "IN-PORT",
  151. example: "7890",
  152. validator: (value) => portValidator(value),
  153. },
  154. {
  155. name: "DSCP",
  156. example: "4",
  157. },
  158. {
  159. name: "PROCESS-NAME",
  160. example: getSystem() === "windows" ? "chrome.exe" : "curl",
  161. },
  162. {
  163. name: "PROCESS-PATH",
  164. example:
  165. getSystem() === "windows"
  166. ? "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
  167. : "/usr/bin/wget",
  168. },
  169. {
  170. name: "PROCESS-NAME-REGEX",
  171. example: ".*telegram.*",
  172. },
  173. {
  174. name: "PROCESS-PATH-REGEX",
  175. example:
  176. getSystem() === "windows" ? "(?i).*Application\\chrome.*" : ".*bin/wget",
  177. },
  178. {
  179. name: "NETWORK",
  180. example: "udp",
  181. validator: (value) => ["tcp", "udp"].includes(value),
  182. },
  183. {
  184. name: "UID",
  185. example: "1001",
  186. validator: (value) => (+value ? true : false),
  187. },
  188. {
  189. name: "IN-TYPE",
  190. example: "SOCKS/HTTP",
  191. },
  192. {
  193. name: "IN-USER",
  194. example: "mihomo",
  195. },
  196. {
  197. name: "IN-NAME",
  198. example: "ss",
  199. },
  200. {
  201. name: "SUB-RULE",
  202. example: "(NETWORK,tcp)",
  203. },
  204. {
  205. name: "RULE-SET",
  206. example: "providername",
  207. noResolve: true,
  208. },
  209. {
  210. name: "AND",
  211. example: "((DOMAIN,baidu.com),(NETWORK,UDP))",
  212. },
  213. {
  214. name: "OR",
  215. example: "((NETWORK,UDP),(DOMAIN,baidu.com))",
  216. },
  217. {
  218. name: "NOT",
  219. example: "((DOMAIN,baidu.com))",
  220. },
  221. {
  222. name: "MATCH",
  223. required: false,
  224. },
  225. ];
  226. const builtinProxyPolicies = ["DIRECT", "REJECT", "REJECT-DROP", "PASS"];
  227. export const RulesEditorViewer = (props: Props) => {
  228. const { groupsUid, mergeUid, profileUid, property, open, onClose, onSave } =
  229. props;
  230. const { t } = useTranslation();
  231. const themeMode = useThemeMode();
  232. const [prevData, setPrevData] = useState("");
  233. const [currData, setCurrData] = useState("");
  234. const [visualization, setVisualization] = useState(true);
  235. const [match, setMatch] = useState(() => (_: string) => true);
  236. const [ruleType, setRuleType] = useState<(typeof rules)[number]>(rules[0]);
  237. const [ruleContent, setRuleContent] = useState("");
  238. const [noResolve, setNoResolve] = useState(false);
  239. const [proxyPolicy, setProxyPolicy] = useState(builtinProxyPolicies[0]);
  240. const [proxyPolicyList, setProxyPolicyList] = useState<string[]>([]);
  241. const [ruleList, setRuleList] = useState<string[]>([]);
  242. const [ruleSetList, setRuleSetList] = useState<string[]>([]);
  243. const [subRuleList, setSubRuleList] = useState<string[]>([]);
  244. const [prependSeq, setPrependSeq] = useState<string[]>([]);
  245. const [appendSeq, setAppendSeq] = useState<string[]>([]);
  246. const [deleteSeq, setDeleteSeq] = useState<string[]>([]);
  247. const filteredRuleList = useMemo(
  248. () => ruleList.filter((rule) => match(rule)),
  249. [ruleList, match]
  250. );
  251. const sensors = useSensors(
  252. useSensor(PointerSensor),
  253. useSensor(KeyboardSensor, {
  254. coordinateGetter: sortableKeyboardCoordinates,
  255. })
  256. );
  257. const reorder = (list: string[], startIndex: number, endIndex: number) => {
  258. const result = Array.from(list);
  259. const [removed] = result.splice(startIndex, 1);
  260. result.splice(endIndex, 0, removed);
  261. return result;
  262. };
  263. const onPrependDragEnd = async (event: DragEndEvent) => {
  264. const { active, over } = event;
  265. if (over) {
  266. if (active.id !== over.id) {
  267. let activeIndex = prependSeq.indexOf(active.id.toString());
  268. let overIndex = prependSeq.indexOf(over.id.toString());
  269. setPrependSeq(reorder(prependSeq, activeIndex, overIndex));
  270. }
  271. }
  272. };
  273. const onAppendDragEnd = async (event: DragEndEvent) => {
  274. const { active, over } = event;
  275. if (over) {
  276. if (active.id !== over.id) {
  277. let activeIndex = appendSeq.indexOf(active.id.toString());
  278. let overIndex = appendSeq.indexOf(over.id.toString());
  279. setAppendSeq(reorder(appendSeq, activeIndex, overIndex));
  280. }
  281. }
  282. };
  283. const fetchContent = async () => {
  284. let data = await readProfileFile(property);
  285. let obj = yaml.load(data) as ISeqProfileConfig | null;
  286. setPrependSeq(obj?.prepend || []);
  287. setAppendSeq(obj?.append || []);
  288. setDeleteSeq(obj?.delete || []);
  289. setPrevData(data);
  290. setCurrData(data);
  291. };
  292. useEffect(() => {
  293. if (currData === "") return;
  294. if (visualization !== true) return;
  295. let obj = yaml.load(currData) as ISeqProfileConfig | null;
  296. setPrependSeq(obj?.prepend || []);
  297. setAppendSeq(obj?.append || []);
  298. setDeleteSeq(obj?.delete || []);
  299. }, [visualization]);
  300. useEffect(() => {
  301. if (prependSeq && appendSeq && deleteSeq)
  302. setCurrData(
  303. yaml.dump({ prepend: prependSeq, append: appendSeq, delete: deleteSeq })
  304. );
  305. }, [prependSeq, appendSeq, deleteSeq]);
  306. const fetchProfile = async () => {
  307. let data = await readProfileFile(profileUid); // 原配置文件
  308. let groupsData = await readProfileFile(groupsUid); // groups配置文件
  309. let mergeData = await readProfileFile(mergeUid); // merge配置文件
  310. let globalMergeData = await readProfileFile("Merge"); // global merge配置文件
  311. let rulesObj = yaml.load(data) as { rules: [] } | null;
  312. let originGroupsObj = yaml.load(data) as { "proxy-groups": [] } | null;
  313. let originGroups = originGroupsObj?.["proxy-groups"] || [];
  314. let moreGroupsObj = yaml.load(groupsData) as ISeqProfileConfig | null;
  315. let morePrependGroups = moreGroupsObj?.["prepend"] || [];
  316. let moreAppendGroups = moreGroupsObj?.["append"] || [];
  317. let moreDeleteGroups =
  318. moreGroupsObj?.["delete"] || ([] as string[] | { name: string }[]);
  319. let groups = morePrependGroups.concat(
  320. originGroups.filter((group: any) => {
  321. if (group.name) {
  322. return !moreDeleteGroups.includes(group.name);
  323. } else {
  324. return !moreDeleteGroups.includes(group);
  325. }
  326. }),
  327. moreAppendGroups
  328. );
  329. let originRuleSetObj = yaml.load(data) as { "rule-providers": {} } | null;
  330. let originRuleSet = originRuleSetObj?.["rule-providers"] || {};
  331. let moreRuleSetObj = yaml.load(mergeData) as {
  332. "rule-providers": {};
  333. } | null;
  334. let moreRuleSet = moreRuleSetObj?.["rule-providers"] || {};
  335. let globalRuleSetObj = yaml.load(globalMergeData) as {
  336. "rule-providers": {};
  337. } | null;
  338. let globalRuleSet = globalRuleSetObj?.["rule-providers"] || {};
  339. let ruleSet = Object.assign({}, originRuleSet, moreRuleSet, globalRuleSet);
  340. let originSubRuleObj = yaml.load(data) as { "sub-rules": {} } | null;
  341. let originSubRule = originSubRuleObj?.["sub-rules"] || {};
  342. let moreSubRuleObj = yaml.load(mergeData) as { "sub-rules": {} } | null;
  343. let moreSubRule = moreSubRuleObj?.["sub-rules"] || {};
  344. let globalSubRuleObj = yaml.load(globalMergeData) as {
  345. "sub-rules": {};
  346. } | null;
  347. let globalSubRule = globalSubRuleObj?.["sub-rules"] || {};
  348. let subRule = Object.assign({}, originSubRule, moreSubRule, globalSubRule);
  349. setProxyPolicyList(
  350. builtinProxyPolicies.concat(groups.map((group: any) => group.name))
  351. );
  352. setRuleSetList(Object.keys(ruleSet));
  353. setSubRuleList(Object.keys(subRule));
  354. setRuleList(rulesObj?.rules || []);
  355. };
  356. useEffect(() => {
  357. if (!open) return;
  358. fetchContent();
  359. fetchProfile();
  360. }, [open]);
  361. const validateRule = () => {
  362. if ((ruleType.required ?? true) && !ruleContent) {
  363. throw new Error(t("Rule Condition Required"));
  364. }
  365. if (ruleType.validator && !ruleType.validator(ruleContent)) {
  366. throw new Error(t("Invalid Rule"));
  367. }
  368. const condition = ruleType.required ?? true ? ruleContent : "";
  369. return `${ruleType.name}${condition ? "," + condition : ""},${proxyPolicy}${
  370. ruleType.noResolve && noResolve ? ",no-resolve" : ""
  371. }`;
  372. };
  373. const handleSave = useLockFn(async () => {
  374. try {
  375. await saveProfileFile(property, currData);
  376. onSave?.(prevData, currData);
  377. onClose();
  378. } catch (err: any) {
  379. Notice.error(err.message || err.toString());
  380. }
  381. });
  382. return (
  383. <Dialog open={open} onClose={onClose} maxWidth="xl" fullWidth>
  384. <DialogTitle>
  385. {
  386. <Box display="flex" justifyContent="space-between">
  387. {t("Edit Rules")}
  388. <Box>
  389. <Button
  390. variant="contained"
  391. size="small"
  392. onClick={() => {
  393. setVisualization((prev) => !prev);
  394. }}
  395. >
  396. {visualization ? t("Advanced") : t("Visualization")}
  397. </Button>
  398. </Box>
  399. </Box>
  400. }
  401. </DialogTitle>
  402. <DialogContent
  403. sx={{ display: "flex", width: "auto", height: "calc(100vh - 185px)" }}
  404. >
  405. {visualization ? (
  406. <>
  407. <List
  408. sx={{
  409. width: "50%",
  410. padding: "0 10px",
  411. }}
  412. >
  413. <Item>
  414. <ListItemText primary={t("Rule Type")} />
  415. <Autocomplete
  416. size="small"
  417. sx={{ minWidth: "240px" }}
  418. renderInput={(params) => <TextField {...params} />}
  419. options={rules}
  420. value={ruleType}
  421. getOptionLabel={(option) => option.name}
  422. renderOption={(props, option) => (
  423. <li {...props} title={t(option.name)}>
  424. {option.name}
  425. </li>
  426. )}
  427. onChange={(_, value) => value && setRuleType(value)}
  428. />
  429. </Item>
  430. <Item
  431. sx={{ display: !(ruleType.required ?? true) ? "none" : "" }}
  432. >
  433. <ListItemText primary={t("Rule Content")} />
  434. {ruleType.name === "RULE-SET" && (
  435. <Autocomplete
  436. size="small"
  437. sx={{ minWidth: "240px" }}
  438. renderInput={(params) => <TextField {...params} />}
  439. options={ruleSetList}
  440. value={ruleContent}
  441. onChange={(_, value) => value && setRuleContent(value)}
  442. />
  443. )}
  444. {ruleType.name === "SUB-RULE" && (
  445. <Autocomplete
  446. size="small"
  447. sx={{ minWidth: "240px" }}
  448. renderInput={(params) => <TextField {...params} />}
  449. options={subRuleList}
  450. value={ruleContent}
  451. onChange={(_, value) => value && setRuleContent(value)}
  452. />
  453. )}
  454. {ruleType.name !== "RULE-SET" &&
  455. ruleType.name !== "SUB-RULE" && (
  456. <TextField
  457. autoComplete="off"
  458. size="small"
  459. sx={{ minWidth: "240px" }}
  460. value={ruleContent}
  461. required={ruleType.required ?? true}
  462. error={(ruleType.required ?? true) && !ruleContent}
  463. placeholder={ruleType.example}
  464. onChange={(e) => setRuleContent(e.target.value)}
  465. />
  466. )}
  467. </Item>
  468. <Item>
  469. <ListItemText primary={t("Proxy Policy")} />
  470. <Autocomplete
  471. size="small"
  472. sx={{ minWidth: "240px" }}
  473. renderInput={(params) => <TextField {...params} />}
  474. options={proxyPolicyList}
  475. value={proxyPolicy}
  476. renderOption={(props, option) => (
  477. <li {...props} title={t(option)}>
  478. {option}
  479. </li>
  480. )}
  481. onChange={(_, value) => value && setProxyPolicy(value)}
  482. />
  483. </Item>
  484. {ruleType.noResolve && (
  485. <Item>
  486. <ListItemText primary={t("No Resolve")} />
  487. <Switch
  488. checked={noResolve}
  489. onChange={() => setNoResolve(!noResolve)}
  490. />
  491. </Item>
  492. )}
  493. <Item>
  494. <Button
  495. fullWidth
  496. variant="contained"
  497. onClick={() => {
  498. try {
  499. let raw = validateRule();
  500. if (prependSeq.includes(raw)) return;
  501. setPrependSeq([...prependSeq, raw]);
  502. } catch (err: any) {
  503. Notice.error(err.message || err.toString());
  504. }
  505. }}
  506. >
  507. {t("Prepend Rule")}
  508. </Button>
  509. </Item>
  510. <Item>
  511. <Button
  512. fullWidth
  513. variant="contained"
  514. onClick={() => {
  515. try {
  516. let raw = validateRule();
  517. if (appendSeq.includes(raw)) return;
  518. setAppendSeq([...appendSeq, raw]);
  519. } catch (err: any) {
  520. Notice.error(err.message || err.toString());
  521. }
  522. }}
  523. >
  524. {t("Append Rule")}
  525. </Button>
  526. </Item>
  527. </List>
  528. <List
  529. sx={{
  530. width: "50%",
  531. padding: "0 10px",
  532. }}
  533. >
  534. <BaseSearchBox
  535. matchCase={false}
  536. onSearch={(match) => setMatch(() => match)}
  537. />
  538. <Virtuoso
  539. style={{ height: "calc(100% - 24px)", marginTop: "8px" }}
  540. totalCount={
  541. filteredRuleList.length +
  542. (prependSeq.length > 0 ? 1 : 0) +
  543. (appendSeq.length > 0 ? 1 : 0)
  544. }
  545. increaseViewportBy={256}
  546. itemContent={(index) => {
  547. let shift = prependSeq.length > 0 ? 1 : 0;
  548. if (prependSeq.length > 0 && index === 0) {
  549. return (
  550. <DndContext
  551. sensors={sensors}
  552. collisionDetection={closestCenter}
  553. onDragEnd={onPrependDragEnd}
  554. >
  555. <SortableContext
  556. items={prependSeq.map((x) => {
  557. return x;
  558. })}
  559. >
  560. {prependSeq.map((item, index) => {
  561. return (
  562. <RuleItem
  563. key={`${item}-${index}`}
  564. type="prepend"
  565. ruleRaw={item}
  566. onDelete={() => {
  567. setPrependSeq(
  568. prependSeq.filter((v) => v !== item)
  569. );
  570. }}
  571. />
  572. );
  573. })}
  574. </SortableContext>
  575. </DndContext>
  576. );
  577. } else if (index < filteredRuleList.length + shift) {
  578. let newIndex = index - shift;
  579. return (
  580. <RuleItem
  581. key={`${filteredRuleList[newIndex]}-${index}`}
  582. type={
  583. deleteSeq.includes(filteredRuleList[newIndex])
  584. ? "delete"
  585. : "original"
  586. }
  587. ruleRaw={filteredRuleList[newIndex]}
  588. onDelete={() => {
  589. if (deleteSeq.includes(filteredRuleList[newIndex])) {
  590. setDeleteSeq(
  591. deleteSeq.filter(
  592. (v) => v !== filteredRuleList[newIndex]
  593. )
  594. );
  595. } else {
  596. setDeleteSeq((prev) => [
  597. ...prev,
  598. filteredRuleList[newIndex],
  599. ]);
  600. }
  601. }}
  602. />
  603. );
  604. } else {
  605. return (
  606. <DndContext
  607. sensors={sensors}
  608. collisionDetection={closestCenter}
  609. onDragEnd={onAppendDragEnd}
  610. >
  611. <SortableContext
  612. items={appendSeq.map((x) => {
  613. return x;
  614. })}
  615. >
  616. {appendSeq.map((item, index) => {
  617. return (
  618. <RuleItem
  619. key={`${item}-${index}`}
  620. type="append"
  621. ruleRaw={item}
  622. onDelete={() => {
  623. setAppendSeq(
  624. appendSeq.filter((v) => v !== item)
  625. );
  626. }}
  627. />
  628. );
  629. })}
  630. </SortableContext>
  631. </DndContext>
  632. );
  633. }
  634. }}
  635. />
  636. </List>
  637. </>
  638. ) : (
  639. <MonacoEditor
  640. height="100%"
  641. language="yaml"
  642. value={currData}
  643. theme={themeMode === "light" ? "vs" : "vs-dark"}
  644. options={{
  645. tabSize: 2, // 根据语言类型设置缩进大小
  646. minimap: {
  647. enabled: document.documentElement.clientWidth >= 1500, // 超过一定宽度显示minimap滚动条
  648. },
  649. mouseWheelZoom: true, // 按住Ctrl滚轮调节缩放比例
  650. quickSuggestions: {
  651. strings: true, // 字符串类型的建议
  652. comments: true, // 注释类型的建议
  653. other: true, // 其他类型的建议
  654. },
  655. padding: {
  656. top: 33, // 顶部padding防止遮挡snippets
  657. },
  658. fontFamily: `Fira Code, JetBrains Mono, Roboto Mono, "Source Code Pro", Consolas, Menlo, Monaco, monospace, "Courier New", "Apple Color Emoji"${
  659. getSystem() === "windows" ? ", twemoji mozilla" : ""
  660. }`,
  661. fontLigatures: true, // 连字符
  662. smoothScrolling: true, // 平滑滚动
  663. }}
  664. onChange={(value) => setCurrData(value)}
  665. />
  666. )}
  667. </DialogContent>
  668. <DialogActions>
  669. <Button onClick={onClose} variant="outlined">
  670. {t("Cancel")}
  671. </Button>
  672. <Button onClick={handleSave} variant="contained">
  673. {t("Save")}
  674. </Button>
  675. </DialogActions>
  676. </Dialog>
  677. );
  678. };
  679. const Item = styled(ListItem)(() => ({
  680. padding: "5px 2px",
  681. }));