-
Notifications
You must be signed in to change notification settings - Fork 336
Expand file tree
/
Copy pathindex.tsx
More file actions
460 lines (424 loc) · 14.6 KB
/
index.tsx
File metadata and controls
460 lines (424 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import React, { useEffect, useMemo, useState } from "react";
import {
Button,
Collapse,
Empty,
Form,
Input,
InputNumber,
Message,
Popconfirm,
Space,
Switch,
} from "@arco-design/web-react";
import {
IconCaretDown,
IconCaretUp,
IconDelete,
IconEdit,
IconMenu,
IconMinus,
IconPlus,
IconSettings,
} from "@arco-design/web-react/icon";
import { SCRIPT_RUN_STATUS_RUNNING } from "@App/app/repo/scripts";
import { RiPlayFill, RiStopFill } from "react-icons/ri";
import { useTranslation } from "react-i18next";
import { ScriptIcons } from "@App/pages/options/routes/utils";
import type {
GroupScriptMenuItem,
ScriptMenu,
ScriptMenuItem,
ScriptMenuItemOption,
} from "@App/app/service/service_worker/types";
import { popupClient, runtimeClient, scriptClient } from "@App/pages/store/features/script";
import { i18nName } from "@App/locales/locales";
const CollapseItem = Collapse.Item;
const sendMenuAction = (
uuid: string,
options: ScriptMenuItemOption | undefined,
menus: ScriptMenuItem[],
inputValue?: any
) => {
popupClient.menuClick(uuid, menus, inputValue).then(() => {
options?.autoClose !== false && window.close();
});
};
const FormItem = Form.Item;
type MenuItemProps = {
menuItems: ScriptMenuItem[];
uuid: string;
};
type GroupScriptMenuItemsProp = { group: GroupScriptMenuItem[]; menuUpdated: number };
const MenuItem = React.memo(({ menuItems, uuid }: MenuItemProps) => {
const menuItem = menuItems[0];
const { name, options } = menuItem;
const initialValue = options?.inputDefaultValue;
const InputMenu = (() => {
const placeholder = options?.inputPlaceholder;
switch (options?.inputType) {
case "text":
return <Input type="text" placeholder={placeholder} />;
case "number":
return <InputNumber placeholder={placeholder} />;
case "boolean":
return <Switch defaultChecked={initialValue as boolean} />;
default:
return null;
}
})();
return (
<Form
initialValues={{ inputValue: initialValue }}
size="small"
labelCol={{ flex: "none" }}
wrapperCol={{ flex: "none" }}
autoComplete="off"
onSubmit={(v) => {
const inputValue = v.inputValue;
sendMenuAction(uuid, options, menuItems, inputValue);
}}
>
<Button
className="text-left"
type="secondary"
htmlType="submit"
icon={<IconMenu />}
title={options?.title}
style={{ display: "block", width: "100%" }}
>
{name}
{options?.accessKey && `(${options.accessKey.toUpperCase()})`}
</Button>
{InputMenu && (
<FormItem
label={options?.inputLabel}
field="inputValue"
style={{ marginTop: 5, marginBottom: 5, paddingLeft: 20 }}
>
{InputMenu}
</FormItem>
)}
</Form>
);
});
MenuItem.displayName = "MenuItem";
interface CollapseHeaderProps {
item: ScriptMenu;
onEnableChange: (item: ScriptMenu, checked: boolean) => void;
}
const CollapseHeader = React.memo(
({ item, onEnableChange }: CollapseHeaderProps) => {
const { t } = useTranslation();
return (
<div
onClick={(e) => {
e.stopPropagation();
}}
title={
item.enable
? item.runNumByIframe
? t("script_total_runs", {
runNum: item.runNum,
runNumByIframe: item.runNumByIframe,
})!
: t("script_total_runs_single", { runNum: item.runNum })!
: t("script_disabled")!
}
>
<Space>
<Switch size="small" checked={item.enable} onChange={(checked) => onEnableChange(item, checked)} />
<span
style={{
display: "block",
overflow: "hidden",
textOverflow: "ellipsis",
whiteSpace: "nowrap",
color: item.runNum === 0 ? "rgb(var(--gray-5))" : "",
lineHeight: "20px",
}}
>
<ScriptIcons script={item} size={20} />
{i18nName(item)}
</span>
</Space>
</div>
);
},
(prevProps, nextProps) => {
return prevProps.item === nextProps.item;
}
);
CollapseHeader.displayName = "CollapseHeader";
interface ListMenuItemProps {
item: ScriptMenu;
scriptMenus: GroupScriptMenuItemsProp;
menuExpandNum: number;
isBackscript: boolean;
url: URL | null;
onEnableChange: (item: ScriptMenu, checked: boolean) => void;
handleDeleteScript: (uuid: string) => void;
}
const ListMenuItem = React.memo(
({ item, scriptMenus, menuExpandNum, isBackscript, url, onEnableChange, handleDeleteScript }: ListMenuItemProps) => {
const { t } = useTranslation();
const [isEffective, setIsEffective] = useState<boolean | null>(item.isEffective);
const [isExpand, setIsExpand] = useState<boolean>(false);
const handleExpandMenu = () => {
setIsExpand((e) => !e);
};
const visibleMenus = useMemo(() => {
const m = scriptMenus?.group || [];
return m.length > menuExpandNum && !isExpand ? m.slice(0, menuExpandNum) : m;
}, [scriptMenus?.group, isExpand, menuExpandNum]);
const shouldShowMore = useMemo(
() => scriptMenus?.group?.length > menuExpandNum,
[scriptMenus?.group, menuExpandNum]
);
const handleExcludeUrl = (uuid: string, excludePattern: string, isExclude: boolean) => {
scriptClient.excludeUrl(uuid, excludePattern, isExclude).finally(() => {
setIsEffective(isExclude);
});
};
return (
<Collapse bordered={false} expandIconPosition="right" key={item.uuid}>
<CollapseItem
header={<CollapseHeader item={item} onEnableChange={onEnableChange} />}
name={item.uuid}
contentStyle={{ padding: "0 0 0 40px" }}
>
<div className="flex flex-col">
{isBackscript && (
<Button
className="text-left"
type="secondary"
icon={item.runStatus !== SCRIPT_RUN_STATUS_RUNNING ? <RiPlayFill /> : <RiStopFill />}
onClick={() => {
// 运行或停止脚本
if (item.runStatus !== SCRIPT_RUN_STATUS_RUNNING) {
runtimeClient.runScript(item.uuid);
} else {
runtimeClient.stopScript(item.uuid);
}
}}
>
{item.runStatus !== SCRIPT_RUN_STATUS_RUNNING ? t("run_once") : t("stop")}
</Button>
)}
<Button
className="text-left"
type="secondary"
icon={<IconEdit />}
onClick={() => {
window.open(`/src/options.html#/script/editor/${item.uuid}`, "_blank");
window.close();
}}
>
{t("edit")}
</Button>
{url && isEffective !== null && (
<Button
className="text-left"
status="warning"
type="secondary"
icon={!isEffective ? <IconPlus /> : <IconMinus />}
onClick={() => handleExcludeUrl(item.uuid, `*://${url.host}/*`, !isEffective)}
>
{(!isEffective ? t("exclude_on") : t("exclude_off")).replace("$0", `${url.host}`)}
</Button>
)}
<Popconfirm
title={t("confirm_delete_script")}
icon={<IconDelete />}
onOk={() => handleDeleteScript(item.uuid)}
>
<Button className="text-left" status="danger" type="secondary" icon={<IconDelete />}>
{t("delete")}
</Button>
</Popconfirm>
</div>
</CollapseItem>
<div className="arco-collapse-item-content-box flex flex-col" style={{ padding: "0 0 0 40px" }}>
{/* 依数量与展开状态决定要显示的分组项(收合时只显示前 menuExpandNum 笔) */}
{visibleMenus.map(({ uuid, groupKey, menus }) => {
// 不同脚本之间可能出现相同的 groupKey;为避免 React key 冲突,需加上 uuid 做区分。
return <MenuItem key={`${uuid}:${groupKey}`} menuItems={menus} uuid={uuid} />;
})}
{shouldShowMore && (
<Button
className="text-left"
key="expand"
type="secondary"
icon={isExpand ? <IconCaretUp /> : <IconCaretDown />}
onClick={handleExpandMenu}
>
{isExpand ? t("collapse") : t("expand")}
</Button>
)}
{item.hasUserConfig && (
<Button
className="text-left"
key="config"
type="secondary"
icon={<IconSettings />}
onClick={() => {
window.open(`/src/options.html#/?userConfig=${item.uuid}`, "_blank");
window.close();
}}
>
{t("user_config")}
</Button>
)}
</div>
</Collapse>
);
},
(prevProps, nextProps) => {
return (
prevProps.url?.href === nextProps.url?.href &&
prevProps.item === nextProps.item &&
prevProps.isBackscript === nextProps.isBackscript &&
prevProps.menuExpandNum === nextProps.menuExpandNum &&
prevProps.scriptMenus?.menuUpdated === nextProps.scriptMenus?.menuUpdated
);
}
);
ListMenuItem.displayName = "ListMenuItem";
type TGrouppedMenus = Record<string, GroupScriptMenuItemsProp> & { __length__?: number };
// Popup 页面使用的脚本/选单清单元件:只负责渲染与互动,状态与持久化交由外部 client 处理。
const ScriptMenuList = React.memo(
({
script,
isBackscript,
currentUrl,
menuExpandNum,
}: {
script: (ScriptMenu & {
menuUpdated?: number;
})[];
isBackscript: boolean;
currentUrl: string;
menuExpandNum: number;
}) => {
const [list, setList] = useState(
[] as (ScriptMenu & {
menuUpdated?: number;
})[]
);
const { t } = useTranslation();
const [grouppedMenus, setGrouppedMenus] = useState<TGrouppedMenus>({});
// 依 groupKey 进行聚合:将同语义(mainframe/subframe)命令合并为单一分组以供 UI 呈现。
useEffect(() => {
const list_ = list;
setGrouppedMenus((prev) => {
const ret = {} as TGrouppedMenus;
let changed = false;
let retLen = 0;
for (const { uuid, menus, menuUpdated: m } of list_) {
retLen++;
const menuUpdated = m || 0;
if (prev[uuid]?.menuUpdated === menuUpdated) {
ret[uuid] = prev[uuid];
continue; // Skip if unchanged
}
const resultMap = new Map<string, ScriptMenuItem[]>();
for (const menu of menus) {
if (menu.options?.mSeparator) continue; // popup 不显示分隔线
const groupKey = menu.groupKey.split(",")[0]; // popup 显示不区分二级菜单或三级菜单
let m = resultMap.get(groupKey);
if (!m) resultMap.set(groupKey, (m = []));
m.push(menu);
}
const result = [];
for (const [groupKey, arr] of resultMap) {
result.push({ uuid, groupKey, menus: arr } as GroupScriptMenuItem);
}
// 输出以 uuid 分组存放;不依赖 list 的迭代顺序以避免不稳定渲染。
ret[uuid] = { group: result, menuUpdated };
changed = true;
}
ret.__length__ = retLen;
if (!changed && ret.__length__ !== prev.__length__) changed = true;
// 若无引用变更则维持原物件以降低重渲染
return changed ? ret : prev;
});
}, [list]);
const url = useMemo(() => {
let url: URL;
try {
// 容错:当 currentUrl 为空或非法时改用预设 URL,避免 URL 解析抛错。
const urlToUse = currentUrl?.trim() || "https://example.com";
url = new URL(urlToUse);
} catch (e: any) {
console.error("Invalid URL:", e);
// 提供预设 URL 以确保后续依赖 url 的流程不会中断。
url = new URL("https://example.com");
}
return url;
}, [currentUrl]);
useEffect(() => {
setList(script);
// 注册菜单快速键(accessKey):以各分组第一个项目的 accessKey 作为触发条件。
const checkItems = new Map();
for (const [_uuid, menus] of Object.entries(grouppedMenus)) {
if (typeof menus !== "object") continue;
for (const menu of menus.group) {
const menuItem = menu.menus[0]; // 同一分组的语义一致,取首项即可读取 accessKey / name 等共用属性。
const { name, options } = menuItem;
const accessKey = options?.accessKey;
if (typeof accessKey === "string") {
checkItems.set(`${menu.uuid}:${menu.groupKey}`, [menu.uuid, accessKey.toUpperCase(), name, menu.menus]);
}
}
}
if (!checkItems.size) return;
const sharedKeyPressListner = (e: KeyboardEvent) => {
const keyUpper = e.key.toUpperCase();
checkItems.forEach(([uuid, accessKeyUpper, _name, menuItems]) => {
if (keyUpper === accessKeyUpper) {
// 快速键触发不需传递 options(autoClose 由 sendMenuAction 内部处理)。
sendMenuAction(uuid, {}, menuItems);
}
});
};
document.addEventListener("keypress", sharedKeyPressListner);
return () => {
checkItems.clear();
document.removeEventListener("keypress", sharedKeyPressListner);
};
}, [script, grouppedMenus]);
const handleDeleteScript = (uuid: string) => {
// 本地先行移除列表项(乐观更新);若删除失败会显示错误讯息。
scriptClient.deletes([uuid]).catch((e) => {
Message.error(`${t("delete_failed")}: ${e}`);
});
};
const onEnableChange = (item: ScriptMenu, checked: boolean) => {
scriptClient.enable(item.uuid, checked).catch((err) => {
Message.error(err);
});
};
return (
<>
{list.length === 0 ? (
<Empty description={t("no_data")} />
) : (
list.map((item, _index) => (
<ListMenuItem
key={`${item.uuid}`}
url={url}
item={item}
scriptMenus={grouppedMenus[item.uuid] || {}}
isBackscript={isBackscript}
onEnableChange={onEnableChange}
handleDeleteScript={handleDeleteScript}
menuExpandNum={menuExpandNum}
/>
))
)}
</>
);
}
);
ScriptMenuList.displayName = "ScriptMenuList";
export default ScriptMenuList;