@nsnanocat/preference-panes 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -5
- package/dist/module/index.html +1 -1
- package/dist/module/{app.mjs → index.mjs} +773 -599
- package/dist/preference-panes.mjs +692 -565
- package/dist/web.js +1 -1
- package/package.json +1 -1
- package/src/browser/client.d.mts +5 -10
- package/src/browser/client.mjs +126 -48
- package/src/browser/index.d.ts +17 -2
- package/src/browser/index.mjs +94 -79
- package/src/browser/module.html +1 -1
- package/src/browser/mount.mjs +119 -0
- package/src/browser/panel.mjs +464 -444
- package/src/build.mjs +2 -2
- package/src/browser/app.mjs +0 -49
|
@@ -423,16 +423,111 @@ class ActionMenu {
|
|
|
423
423
|
}
|
|
424
424
|
|
|
425
425
|
/**
|
|
426
|
-
*
|
|
427
|
-
*
|
|
428
|
-
* @param {import("./client.mjs").PreferencesClientOptions} options API 模型、请求与通知 / API model, requests and notifications.
|
|
429
|
-
* @returns {import("./client.mjs").PreferencesClient} 页面客户端 / Page client.
|
|
426
|
+
* 管理单模块页面的 API 请求、值快照和会话终止。
|
|
427
|
+
* Manage API requests, value snapshots, and session termination for one module page.
|
|
430
428
|
*/
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
429
|
+
class PreferencesClient {
|
|
430
|
+
#module;
|
|
431
|
+
#configURL;
|
|
432
|
+
#definition;
|
|
433
|
+
#request;
|
|
434
|
+
#notify;
|
|
435
|
+
#timeout;
|
|
436
|
+
#session = new AbortController();
|
|
437
|
+
#values;
|
|
438
|
+
#saving = false;
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* 创建只调用模块 API、不读取或解析 BoxJS 的页面客户端。
|
|
442
|
+
* Create a page client that only calls the module API and never reads or parses BoxJS.
|
|
443
|
+
* @param {import("./client.mjs").PreferencesClientOptions} options API 模型、请求与通知 / API model, requests, and notifications.
|
|
444
|
+
*/
|
|
445
|
+
constructor({ model, definition, fetch: request = globalThis.fetch.bind(globalThis), notify = () => {}, timeout = 10000 }) {
|
|
446
|
+
this.#module = model.module;
|
|
447
|
+
this.#configURL = model.configURL;
|
|
448
|
+
this.#definition = definition;
|
|
449
|
+
this.#request = request;
|
|
450
|
+
this.#notify = notify;
|
|
451
|
+
this.#timeout = timeout;
|
|
452
|
+
this.#values = structuredClone(model.values);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* 获取当前字段定义和值的深拷贝,不发起网络请求。
|
|
457
|
+
* Return a deep copy of the current field definition and values without a network request.
|
|
458
|
+
* @returns {import("./client.mjs").ModuleSnapshot} 会话快照 / Session snapshot.
|
|
459
|
+
*/
|
|
460
|
+
snapshot() {
|
|
461
|
+
return structuredClone({ definition: this.#definition, values: this.#values });
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* 读取 Settings 子树。
|
|
466
|
+
* Read the Settings subtree.
|
|
467
|
+
* @returns {Promise<unknown>} Settings 内容或 undefined / Settings content or undefined.
|
|
468
|
+
*/
|
|
469
|
+
async readSettings() {
|
|
470
|
+
const response = await this.#send("get", { scope: "settings" });
|
|
471
|
+
return response.status === 404 ? undefined : response.json();
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* 读取 Caches 子树。
|
|
476
|
+
* Read the Caches subtree.
|
|
477
|
+
* @returns {Promise<unknown>} Caches 内容或 undefined / Caches content or undefined.
|
|
478
|
+
*/
|
|
479
|
+
async readCaches() {
|
|
480
|
+
const response = await this.#send("get", { scope: "caches" });
|
|
481
|
+
return response.status === 404 ? undefined : response.json();
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* 删除当前模块的 Caches 子树。
|
|
486
|
+
* Delete the current module Caches subtree.
|
|
487
|
+
* @returns {Promise<void>} 操作完成 / Operation completion.
|
|
488
|
+
*/
|
|
489
|
+
clearCaches() {
|
|
490
|
+
return this.#change("delete", { scope: "caches" }, "clearCaches");
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* 删除当前模块数据并恢复页面默认值。
|
|
495
|
+
* Delete current module data and restore page defaults.
|
|
496
|
+
* @returns {Promise<void>} 操作完成 / Operation completion.
|
|
497
|
+
*/
|
|
498
|
+
reset() {
|
|
499
|
+
return this.#change("delete", { scope: "module" }, "reset");
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* 终止当前页面仍在进行的请求。
|
|
504
|
+
* Abort requests still owned by the current page.
|
|
505
|
+
* @returns {void} 无返回值 / No return value.
|
|
506
|
+
*/
|
|
507
|
+
leave() {
|
|
508
|
+
this.#session.abort();
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* 写入单个字段。
|
|
513
|
+
* Write one field.
|
|
514
|
+
* @param {string} key 字段路径 / Field path.
|
|
515
|
+
* @param {unknown} value 已校验值 / Validated value.
|
|
516
|
+
* @returns {Promise<void>} 操作完成 / Operation completion.
|
|
517
|
+
*/
|
|
518
|
+
set(key, value) {
|
|
519
|
+
return this.#change("set", { key, value }, "write", key);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* 删除单个字段覆盖值。
|
|
524
|
+
* Delete one field override.
|
|
525
|
+
* @param {string} key 字段路径 / Field path.
|
|
526
|
+
* @returns {Promise<void>} 操作完成 / Operation completion.
|
|
527
|
+
*/
|
|
528
|
+
remove(key) {
|
|
529
|
+
return this.#change("delete", { key }, "delete", key);
|
|
530
|
+
}
|
|
436
531
|
|
|
437
532
|
/**
|
|
438
533
|
* 向模块 API 发送 JSON 动作。
|
|
@@ -441,26 +536,26 @@ function createPreferencesClient({ model, definition, fetch: request = globalThi
|
|
|
441
536
|
* @param {unknown} payload JSON 请求体 / JSON request body.
|
|
442
537
|
* @returns {Promise<Response>} 原始响应 / Raw response.
|
|
443
538
|
*/
|
|
444
|
-
async
|
|
539
|
+
async #send(action, payload) {
|
|
445
540
|
const controller = new AbortController();
|
|
446
541
|
const abort = () => controller.abort();
|
|
447
|
-
if (session.signal.aborted) abort();
|
|
448
|
-
session.signal.addEventListener("abort", abort, { once: true });
|
|
449
|
-
const timer = setTimeout(abort, timeout);
|
|
542
|
+
if (this.#session.signal.aborted) abort();
|
|
543
|
+
this.#session.signal.addEventListener("abort", abort, { once: true });
|
|
544
|
+
const timer = setTimeout(abort, this.#timeout);
|
|
450
545
|
try {
|
|
451
|
-
const response = await request(`/api/${encodeURIComponent(module)}/${action}`, {
|
|
546
|
+
const response = await this.#request(`/api/${encodeURIComponent(this.#module)}/${action}`, {
|
|
452
547
|
method: "POST",
|
|
453
548
|
credentials: "omit",
|
|
454
549
|
cache: "no-store",
|
|
455
550
|
signal: controller.signal,
|
|
456
|
-
headers: { "Content-Type": "application/json", "X-PreferencePanes-JSON": configURL },
|
|
551
|
+
headers: { "Content-Type": "application/json", "X-PreferencePanes-JSON": this.#configURL },
|
|
457
552
|
body: JSON.stringify(payload),
|
|
458
553
|
});
|
|
459
554
|
if (response.status !== 200 && !(action === "get" && response.status === 404)) throw new Error(`HTTP ${response.status}`);
|
|
460
555
|
return response;
|
|
461
556
|
} finally {
|
|
462
557
|
clearTimeout(timer);
|
|
463
|
-
session.signal.removeEventListener("abort", abort);
|
|
558
|
+
this.#session.signal.removeEventListener("abort", abort);
|
|
464
559
|
}
|
|
465
560
|
}
|
|
466
561
|
|
|
@@ -468,60 +563,43 @@ function createPreferencesClient({ model, definition, fetch: request = globalThi
|
|
|
468
563
|
* 执行写入动作;成功后只更新当前页面值。
|
|
469
564
|
* Execute a mutation and update only the current page values after success.
|
|
470
565
|
* @param {"set" | "delete"} action API 动作 / API action.
|
|
471
|
-
* @param {unknown} payload JSON 请求体 / JSON request body.
|
|
566
|
+
* @param {Record<string, unknown>} payload JSON 请求体 / JSON request body.
|
|
472
567
|
* @param {"write" | "delete" | "clearCaches" | "reset"} operation 通知操作 / Notification operation.
|
|
473
568
|
* @param {string} [key] 字段路径 / Field path.
|
|
474
569
|
* @returns {Promise<void>} 操作完成 / Operation completion.
|
|
475
570
|
*/
|
|
476
|
-
async
|
|
477
|
-
if (saving) throw new Error("A settings write is already in progress");
|
|
478
|
-
saving = true;
|
|
571
|
+
async #change(action, payload, operation, key) {
|
|
572
|
+
if (this.#saving) throw new Error("A settings write is already in progress");
|
|
573
|
+
this.#saving = true;
|
|
479
574
|
try {
|
|
480
|
-
await send(action, payload);
|
|
575
|
+
await this.#send(action, payload);
|
|
481
576
|
switch (operation) {
|
|
482
577
|
case "write":
|
|
483
|
-
values[key] = structuredClone(payload.value);
|
|
578
|
+
this.#values[key] = structuredClone(payload.value);
|
|
484
579
|
break;
|
|
485
580
|
case "delete": {
|
|
486
|
-
const field = definition.fields.find(candidate => candidate.key === key);
|
|
487
|
-
delete values[key];
|
|
488
|
-
if (field && Object.hasOwn(field, "defaultValue")) values[key] = structuredClone(field.defaultValue);
|
|
581
|
+
const field = this.#definition.fields.find(candidate => candidate.key === key);
|
|
582
|
+
delete this.#values[key];
|
|
583
|
+
if (field && Object.hasOwn(field, "defaultValue")) this.#values[key] = structuredClone(field.defaultValue);
|
|
489
584
|
break;
|
|
490
585
|
}
|
|
491
586
|
case "clearCaches":
|
|
492
587
|
break;
|
|
493
588
|
case "reset":
|
|
494
|
-
for (const field of definition.fields) {
|
|
495
|
-
delete values[field.key];
|
|
496
|
-
if (Object.hasOwn(field, "defaultValue")) values[field.key] = structuredClone(field.defaultValue);
|
|
589
|
+
for (const field of this.#definition.fields) {
|
|
590
|
+
delete this.#values[field.key];
|
|
591
|
+
if (Object.hasOwn(field, "defaultValue")) this.#values[field.key] = structuredClone(field.defaultValue);
|
|
497
592
|
}
|
|
498
593
|
break;
|
|
499
594
|
}
|
|
500
|
-
notify({ kind: "success", operation, module, key });
|
|
595
|
+
this.#notify({ kind: "success", operation, module: this.#module, key });
|
|
501
596
|
} catch (error) {
|
|
502
|
-
notify({ kind: "error", operation, module, key, message: error.message });
|
|
597
|
+
this.#notify({ kind: "error", operation, module: this.#module, key, message: error.message });
|
|
503
598
|
throw error;
|
|
504
599
|
} finally {
|
|
505
|
-
saving = false;
|
|
600
|
+
this.#saving = false;
|
|
506
601
|
}
|
|
507
602
|
}
|
|
508
|
-
|
|
509
|
-
return {
|
|
510
|
-
snapshot: () => structuredClone({ definition, values }),
|
|
511
|
-
async readSettings() {
|
|
512
|
-
const response = await send("get", { scope: "settings" });
|
|
513
|
-
return response.status === 404 ? undefined : response.json();
|
|
514
|
-
},
|
|
515
|
-
async readCaches() {
|
|
516
|
-
const response = await send("get", { scope: "caches" });
|
|
517
|
-
return response.status === 404 ? undefined : response.json();
|
|
518
|
-
},
|
|
519
|
-
clearCaches: () => change("delete", { scope: "caches" }, "clearCaches"),
|
|
520
|
-
reset: () => change("delete", { scope: "module" }, "reset"),
|
|
521
|
-
leave: () => session.abort(),
|
|
522
|
-
set: (key, value) => change("set", { key, value }, "write", key),
|
|
523
|
-
remove: key => change("delete", { key }, "delete", key),
|
|
524
|
-
};
|
|
525
603
|
}
|
|
526
604
|
|
|
527
605
|
/**
|
|
@@ -684,485 +762,496 @@ class Navigation extends EventTarget {
|
|
|
684
762
|
}
|
|
685
763
|
|
|
686
764
|
/**
|
|
687
|
-
*
|
|
688
|
-
*
|
|
689
|
-
* @param {HTMLElement} root 包内挂载元素 / Internal mount element.
|
|
690
|
-
* @param {import("../index.js").ModuleModel} model API 返回的模块模型 / Module model returned by the API.
|
|
691
|
-
* @returns {import("./index.js").MountedPreferences} 面板生命周期句柄 / Panel lifecycle handle.
|
|
765
|
+
* 管理模块表单、导航、操作队列和短暂通知。
|
|
766
|
+
* Manage the module form, navigation, operation queue, and transient notifications.
|
|
692
767
|
*/
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
const document = root.ownerDocument;
|
|
697
|
-
const window = document.defaultView;
|
|
698
|
-
const shell = element("div", "pp-panel");
|
|
699
|
-
shell.dataset.module = definition.module;
|
|
700
|
-
const header = element("header", "pp-header");
|
|
701
|
-
const back = element("button", "pp-back", "‹");
|
|
702
|
-
back.setAttribute("aria-label", "返回");
|
|
703
|
-
back.type = "button";
|
|
704
|
-
const heading = element("h1", "pp-title", title);
|
|
705
|
-
const handlers = new Map();
|
|
706
|
-
const menuItems = [
|
|
707
|
-
{ id: "viewSettings", label: "查看设置" },
|
|
708
|
-
{ id: "viewCaches", label: "查看缓存" },
|
|
709
|
-
{ id: "clearCaches", label: "清空缓存", destructive: true },
|
|
710
|
-
{ id: "reset", label: "重置设置", destructive: true },
|
|
711
|
-
];
|
|
712
|
-
const menu = new ActionMenu(id => runAction(id));
|
|
713
|
-
const trailing = element("span", "pp-nav-spacer");
|
|
714
|
-
trailing.append(menu.element);
|
|
715
|
-
const viewport = element("div", "pp-viewport");
|
|
716
|
-
let toast;
|
|
717
|
-
header.append(back, heading, trailing);
|
|
718
|
-
shell.append(header, viewport);
|
|
719
|
-
root.append(shell);
|
|
720
|
-
// 嵌入模式向宿主发布导航状态,宿主不读取或修改模块内部 DOM。
|
|
721
|
-
// Embedded mode publishes navigation state without host reads or mutations of the module DOM.
|
|
722
|
-
const publishNavigation = () => {
|
|
723
|
-
const actions = handlers.size ? menuItems : [];
|
|
724
|
-
menu.update(actions, saving);
|
|
725
|
-
const frame = window.frameElement;
|
|
726
|
-
if (!frame?.dataset.preferencePanes) return;
|
|
727
|
-
frame.dispatchEvent(
|
|
728
|
-
new frame.ownerDocument.defaultView.CustomEvent("preferencepanes:change", {
|
|
729
|
-
detail: { title: heading.textContent, module: definition.module, busy: saving, canGoBack: !back.disabled, actions },
|
|
730
|
-
}),
|
|
731
|
-
);
|
|
732
|
-
};
|
|
733
|
-
const onAction = event => {
|
|
734
|
-
if (!saving && handlers.has(event.detail)) runAction(event.detail);
|
|
735
|
-
};
|
|
736
|
-
window.frameElement?.addEventListener("preferencepanes:action", onAction);
|
|
737
|
-
let timer,
|
|
738
|
-
navigation,
|
|
739
|
-
generation = 0,
|
|
740
|
-
active = null,
|
|
741
|
-
saving = false,
|
|
742
|
-
destroyed = false;
|
|
743
|
-
/**
|
|
744
|
-
* 展示短暂通知,不刷新设置数据。
|
|
745
|
-
* Display a transient notification without refreshing settings.
|
|
746
|
-
* @param {{kind: "success" | "error", operation?: "write" | "delete" | "clearCaches" | "reset", message?: string}} event 操作结果 / Operation result.
|
|
747
|
-
* @returns {void} 无返回值 / No return value.
|
|
748
|
-
*/
|
|
749
|
-
const notify = event => {
|
|
750
|
-
if (destroyed) return;
|
|
751
|
-
let message;
|
|
752
|
-
switch (true) {
|
|
753
|
-
case event.kind === "error":
|
|
754
|
-
message = `操作失败:${event.message}`;
|
|
755
|
-
break;
|
|
756
|
-
case event.operation === "delete":
|
|
757
|
-
message = "删除成功";
|
|
758
|
-
break;
|
|
759
|
-
case event.operation === "clearCaches":
|
|
760
|
-
message = "Caches 已清空";
|
|
761
|
-
break;
|
|
762
|
-
case event.operation === "reset":
|
|
763
|
-
message = "设置已重置";
|
|
764
|
-
break;
|
|
765
|
-
default:
|
|
766
|
-
message = "修改成功";
|
|
767
|
-
break;
|
|
768
|
-
}
|
|
769
|
-
// 宿主接管时不创建网页 Toast,也不运行其计时器。
|
|
770
|
-
// A host-owned notice creates no web Toast and starts no local timer.
|
|
771
|
-
const frame = window.frameElement;
|
|
772
|
-
if (frame && !frame.dispatchEvent(new frame.ownerDocument.defaultView.CustomEvent("preferencepanes:notice", { cancelable: true, detail: { kind: event.kind, message } }))) return;
|
|
773
|
-
if (!toast) {
|
|
774
|
-
toast = element("div", "pp-toast");
|
|
775
|
-
toast.setAttribute("role", "status");
|
|
776
|
-
shell.append(toast);
|
|
777
|
-
}
|
|
778
|
-
toast.textContent = message;
|
|
779
|
-
toast.dataset.kind = event.kind;
|
|
780
|
-
toast.hidden = false;
|
|
781
|
-
clearTimeout(timer);
|
|
782
|
-
timer = setTimeout(() => {
|
|
783
|
-
toast.hidden = true;
|
|
784
|
-
}, 2400);
|
|
785
|
-
};
|
|
786
|
-
const client = createPreferencesClient({ model, definition, notify });
|
|
787
|
-
/**
|
|
788
|
-
* 两种菜单入口共用异步错误处理,包含宿主确认框错误。
|
|
789
|
-
* Share async error handling between both menus, including host-dialog errors.
|
|
790
|
-
* @param {string} id 操作标识 / Action identifier.
|
|
791
|
-
* @returns {Promise<void>} 操作已处理 / Action handled.
|
|
792
|
-
*/
|
|
793
|
-
async function runAction(id) {
|
|
794
|
-
try {
|
|
795
|
-
await handlers.get(id)();
|
|
796
|
-
} catch (error) {
|
|
797
|
-
notify({ kind: "error", message: error.message });
|
|
798
|
-
}
|
|
799
|
-
}
|
|
768
|
+
class PreferencesPanel {
|
|
769
|
+
#release;
|
|
770
|
+
|
|
800
771
|
/**
|
|
801
|
-
*
|
|
802
|
-
*
|
|
803
|
-
* @param {
|
|
804
|
-
* @
|
|
772
|
+
* 挂载 API 返回的模块模型表单。
|
|
773
|
+
* Mount the module form returned by the API.
|
|
774
|
+
* @param {HTMLElement} root 包内挂载元素 / Internal mount element.
|
|
775
|
+
* @param {import("../index.js").ModuleModel & {definition: import("../index.js").ModuleDefinition}} model 已规范化模块模型 / Normalized module model.
|
|
805
776
|
*/
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
active = module;
|
|
809
|
-
back.disabled = window.history.length <= 1;
|
|
810
|
-
heading.textContent = module;
|
|
811
|
-
publishNavigation();
|
|
812
|
-
viewport.replaceChildren(statusView("读取设置…"));
|
|
813
|
-
try {
|
|
814
|
-
if (version === generation) controls();
|
|
815
|
-
} catch (error) {
|
|
816
|
-
if (version !== generation) return;
|
|
817
|
-
viewport.replaceChildren(statusView(`加载失败:${error.message}`, () => open(module)));
|
|
818
|
-
publishNavigation();
|
|
819
|
-
}
|
|
777
|
+
constructor(root, model) {
|
|
778
|
+
this.#release = this.#mount(root, model);
|
|
820
779
|
}
|
|
780
|
+
|
|
821
781
|
/**
|
|
822
|
-
*
|
|
823
|
-
* Build
|
|
824
|
-
* @
|
|
782
|
+
* 建立面板 DOM、交互和会话,并返回其释放操作。
|
|
783
|
+
* Build panel DOM, interactions, and session, then return its release operation.
|
|
784
|
+
* @param {HTMLElement} root 包内挂载元素 / Internal mount element.
|
|
785
|
+
* @param {import("../index.js").ModuleModel & {definition: import("../index.js").ModuleDefinition}} model 已规范化模块模型 / Normalized module model.
|
|
786
|
+
* @returns {() => void} 释放操作 / Release operation.
|
|
825
787
|
*/
|
|
826
|
-
|
|
827
|
-
const { definition
|
|
828
|
-
|
|
829
|
-
const
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
const
|
|
838
|
-
const
|
|
839
|
-
|
|
840
|
-
|
|
788
|
+
#mount(root, model) {
|
|
789
|
+
const { definition } = model;
|
|
790
|
+
const title = definition.metadata?.name ?? definition.module;
|
|
791
|
+
const document = root.ownerDocument;
|
|
792
|
+
const window = document.defaultView;
|
|
793
|
+
const shell = element("div", "pp-panel");
|
|
794
|
+
shell.dataset.module = definition.module;
|
|
795
|
+
const header = element("header", "pp-header");
|
|
796
|
+
const back = element("button", "pp-back", "‹");
|
|
797
|
+
back.setAttribute("aria-label", "返回");
|
|
798
|
+
back.type = "button";
|
|
799
|
+
const heading = element("h1", "pp-title", title);
|
|
800
|
+
const handlers = new Map();
|
|
801
|
+
const menuItems = [
|
|
802
|
+
{ id: "viewSettings", label: "查看设置" },
|
|
803
|
+
{ id: "viewCaches", label: "查看缓存" },
|
|
804
|
+
{ id: "clearCaches", label: "清空缓存", destructive: true },
|
|
805
|
+
{ id: "reset", label: "重置设置", destructive: true },
|
|
806
|
+
];
|
|
807
|
+
const menu = new ActionMenu(id => runAction(id));
|
|
808
|
+
const trailing = element("span", "pp-nav-spacer");
|
|
809
|
+
trailing.append(menu.element);
|
|
810
|
+
const viewport = element("div", "pp-viewport");
|
|
811
|
+
let toast;
|
|
812
|
+
header.append(back, heading, trailing);
|
|
813
|
+
shell.append(header, viewport);
|
|
814
|
+
root.append(shell);
|
|
815
|
+
// 嵌入模式向宿主发布导航状态,宿主不读取或修改模块内部 DOM。
|
|
816
|
+
// Embedded mode publishes navigation state without host reads or mutations of the module DOM.
|
|
817
|
+
const publishNavigation = () => {
|
|
818
|
+
const actions = handlers.size ? menuItems : [];
|
|
819
|
+
menu.update(actions, saving);
|
|
820
|
+
const frame = window.frameElement;
|
|
821
|
+
if (!frame?.dataset.preferencePanes) return;
|
|
822
|
+
frame.dispatchEvent(
|
|
823
|
+
new frame.ownerDocument.defaultView.CustomEvent("preferencepanes:change", {
|
|
824
|
+
detail: { title: heading.textContent, module: definition.module, busy: saving, canGoBack: !back.disabled, actions },
|
|
825
|
+
}),
|
|
826
|
+
);
|
|
827
|
+
};
|
|
828
|
+
const onAction = event => {
|
|
829
|
+
if (!saving && handlers.has(event.detail)) runAction(event.detail);
|
|
830
|
+
};
|
|
831
|
+
window.frameElement?.addEventListener("preferencepanes:action", onAction);
|
|
832
|
+
let timer,
|
|
833
|
+
navigation,
|
|
834
|
+
generation = 0,
|
|
835
|
+
active = null,
|
|
836
|
+
saving = false,
|
|
837
|
+
destroyed = false;
|
|
841
838
|
/**
|
|
842
|
-
*
|
|
843
|
-
*
|
|
839
|
+
* 展示短暂通知,不刷新设置数据。
|
|
840
|
+
* Display a transient notification without refreshing settings.
|
|
841
|
+
* @param {{kind: "success" | "error", operation?: "write" | "delete" | "clearCaches" | "reset", message?: string}} event 操作结果 / Operation result.
|
|
844
842
|
* @returns {void} 无返回值 / No return value.
|
|
845
843
|
*/
|
|
846
|
-
const
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
844
|
+
const notify = event => {
|
|
845
|
+
if (destroyed) return;
|
|
846
|
+
let message;
|
|
847
|
+
switch (true) {
|
|
848
|
+
case event.kind === "error":
|
|
849
|
+
message = `操作失败:${event.message}`;
|
|
850
|
+
break;
|
|
851
|
+
case event.operation === "delete":
|
|
852
|
+
message = "删除成功";
|
|
853
|
+
break;
|
|
854
|
+
case event.operation === "clearCaches":
|
|
855
|
+
message = "Caches 已清空";
|
|
856
|
+
break;
|
|
857
|
+
case event.operation === "reset":
|
|
858
|
+
message = "设置已重置";
|
|
859
|
+
break;
|
|
860
|
+
default:
|
|
861
|
+
message = "修改成功";
|
|
862
|
+
break;
|
|
863
|
+
}
|
|
864
|
+
// 宿主接管时不创建网页 Toast,也不运行其计时器。
|
|
865
|
+
// A host-owned notice creates no web Toast and starts no local timer.
|
|
866
|
+
const frame = window.frameElement;
|
|
867
|
+
if (frame && !frame.dispatchEvent(new frame.ownerDocument.defaultView.CustomEvent("preferencepanes:notice", { cancelable: true, detail: { kind: event.kind, message } }))) return;
|
|
868
|
+
if (!toast) {
|
|
869
|
+
toast = element("div", "pp-toast");
|
|
870
|
+
toast.setAttribute("role", "status");
|
|
871
|
+
shell.append(toast);
|
|
872
|
+
}
|
|
873
|
+
toast.textContent = message;
|
|
874
|
+
toast.dataset.kind = event.kind;
|
|
875
|
+
toast.hidden = false;
|
|
876
|
+
clearTimeout(timer);
|
|
877
|
+
timer = setTimeout(() => {
|
|
878
|
+
toast.hidden = true;
|
|
879
|
+
}, 2400);
|
|
851
880
|
};
|
|
881
|
+
const client = new PreferencesClient({ model, definition, notify });
|
|
852
882
|
/**
|
|
853
|
-
*
|
|
854
|
-
*
|
|
855
|
-
* @param {
|
|
856
|
-
* @
|
|
857
|
-
* @param {() => void} [failure] 失败后恢复当前输入 / Restore the current input on failure.
|
|
858
|
-
* @returns {Promise<void>} 操作完成 / Operation completion.
|
|
883
|
+
* 两种菜单入口共用异步错误处理,包含宿主确认框错误。
|
|
884
|
+
* Share async error handling between both menus, including host-dialog errors.
|
|
885
|
+
* @param {string} id 操作标识 / Action identifier.
|
|
886
|
+
* @returns {Promise<void>} 操作已处理 / Action handled.
|
|
859
887
|
*/
|
|
860
|
-
function
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
queue = queue
|
|
866
|
-
.then(action)
|
|
867
|
-
.then(() => {
|
|
868
|
-
if (!destroyed) success();
|
|
869
|
-
})
|
|
870
|
-
.catch(() => {
|
|
871
|
-
/* 请求层已通知错误。
|
|
872
|
-
* The request layer has already reported the error. */
|
|
873
|
-
if (!destroyed) failure();
|
|
874
|
-
})
|
|
875
|
-
.finally(() => {
|
|
876
|
-
pendingWrites--;
|
|
877
|
-
saving = pendingWrites > 0;
|
|
878
|
-
if (destroyed && !saving) client.leave();
|
|
879
|
-
back.disabled = saving || !navigation.canGoBack;
|
|
880
|
-
publishNavigation();
|
|
881
|
-
});
|
|
882
|
-
return queue;
|
|
883
|
-
}
|
|
884
|
-
const metadata = definition.metadata;
|
|
885
|
-
if (metadata) {
|
|
886
|
-
const info = element("div", "pp-module-info");
|
|
887
|
-
const details = element("div", "pp-module-details");
|
|
888
|
-
for (const description of [metadata.author, metadata.desc ?? metadata.description, ...(metadata.descs ?? [])]) if (description) details.append(element("p", "pp-description", description));
|
|
889
|
-
if (metadata.repo) {
|
|
890
|
-
const link = element("a", "pp-module-source", "项目主页");
|
|
891
|
-
link.href = resourceURL(metadata.repo);
|
|
892
|
-
link.target = "_blank";
|
|
893
|
-
link.rel = "noopener noreferrer";
|
|
894
|
-
details.append(link);
|
|
888
|
+
async function runAction(id) {
|
|
889
|
+
try {
|
|
890
|
+
await handlers.get(id)();
|
|
891
|
+
} catch (error) {
|
|
892
|
+
notify({ kind: "error", message: error.message });
|
|
895
893
|
}
|
|
896
|
-
info.append(details);
|
|
897
|
-
view.append(info);
|
|
898
894
|
}
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
895
|
+
/**
|
|
896
|
+
* 打开模块并忽略已过期的异步结果。
|
|
897
|
+
* Open a module and ignore stale asynchronous results.
|
|
898
|
+
* @param {string} module 模块标识 / Module identifier.
|
|
899
|
+
* @returns {Promise<void>} 视图加载完成,失败显示错误视图 / View load completion; failures display an error view.
|
|
900
|
+
*/
|
|
901
|
+
async function open(module) {
|
|
902
|
+
const version = ++generation;
|
|
903
|
+
active = module;
|
|
904
|
+
back.disabled = window.history.length <= 1;
|
|
905
|
+
heading.textContent = module;
|
|
906
|
+
publishNavigation();
|
|
907
|
+
viewport.replaceChildren(statusView("读取设置…"));
|
|
908
|
+
try {
|
|
909
|
+
if (version === generation) controls();
|
|
910
|
+
} catch (error) {
|
|
911
|
+
if (version !== generation) return;
|
|
912
|
+
viewport.replaceChildren(statusView(`加载失败:${error.message}`, () => open(module)));
|
|
913
|
+
publishNavigation();
|
|
908
914
|
}
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
915
|
+
}
|
|
916
|
+
/**
|
|
917
|
+
* 从会话快照创建控件与操作按钮,不重新读取网络配置。
|
|
918
|
+
* Build controls and actions from the session snapshot without fetching config again.
|
|
919
|
+
* @returns {void} 无返回值 / No return value.
|
|
920
|
+
*/
|
|
921
|
+
function controls() {
|
|
922
|
+
const { definition, values } = client.snapshot();
|
|
923
|
+
heading.textContent = definition.metadata?.name || active;
|
|
924
|
+
const view = element("section", "pp-fields");
|
|
916
925
|
/**
|
|
917
|
-
*
|
|
918
|
-
*
|
|
919
|
-
* @type {() =>
|
|
926
|
+
* 挂载后执行的多行高度更新
|
|
927
|
+
* Textarea sizing callbacks run after mounting.
|
|
928
|
+
* @type {Array<() => void>}
|
|
920
929
|
*/
|
|
921
|
-
|
|
930
|
+
const growingInputs = [];
|
|
931
|
+
const editors = new Map();
|
|
932
|
+
const summaries = [];
|
|
933
|
+
const groups = new Map();
|
|
934
|
+
let queue = Promise.resolve(),
|
|
935
|
+
pendingWrites = 0;
|
|
922
936
|
/**
|
|
923
|
-
*
|
|
924
|
-
*
|
|
925
|
-
* @
|
|
937
|
+
* 导航组件处理页面切换,表单只更新当前标题与返回按钮。
|
|
938
|
+
* Let navigation own transitions; the form only updates the title and back button.
|
|
939
|
+
* @returns {void} 无返回值 / No return value.
|
|
926
940
|
*/
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
field.options
|
|
963
|
-
.filter(option => Array.isArray(value) && value.includes(option.key))
|
|
964
|
-
.map(option => option.label)
|
|
965
|
-
.join("、") || "未选择";
|
|
966
|
-
};
|
|
967
|
-
summaries.push(refresh);
|
|
968
|
-
refresh();
|
|
969
|
-
link.onclick = () => navigation.open(field.key);
|
|
970
|
-
row.addEventListener("click", event => {
|
|
971
|
-
if (!link.contains(event.target)) link.click();
|
|
972
|
-
});
|
|
973
|
-
const inputs = field.options.map(option => {
|
|
974
|
-
const label = settingRow("label");
|
|
975
|
-
label.classList.add("pp-choice");
|
|
976
|
-
label.textContent = option.label;
|
|
977
|
-
const input = element("input", "");
|
|
978
|
-
input.type = "checkbox";
|
|
979
|
-
input.setAttribute("aria-label", option.label);
|
|
980
|
-
label.append(input);
|
|
981
|
-
choices.append(label);
|
|
982
|
-
return { input, key: option.key };
|
|
941
|
+
const updateNavigation = () => {
|
|
942
|
+
const editor = editors.get(navigation.current);
|
|
943
|
+
heading.textContent = editor?.title ?? definition.metadata?.name ?? active;
|
|
944
|
+
back.disabled = saving || !navigation.canGoBack;
|
|
945
|
+
publishNavigation();
|
|
946
|
+
};
|
|
947
|
+
/**
|
|
948
|
+
* 串行执行模块操作,保持输入可编辑。
|
|
949
|
+
* Serialize module actions while keeping inputs editable.
|
|
950
|
+
* @param {() => Promise<void>} action 请求或写入 / Request or mutation.
|
|
951
|
+
* @param {() => void} success 成功后的局部更新 / Local update after success.
|
|
952
|
+
* @param {() => void} [failure] 失败后恢复当前输入 / Restore the current input on failure.
|
|
953
|
+
* @returns {Promise<void>} 操作完成 / Operation completion.
|
|
954
|
+
*/
|
|
955
|
+
function perform(action, success, failure = () => {}) {
|
|
956
|
+
pendingWrites++;
|
|
957
|
+
saving = true;
|
|
958
|
+
back.disabled = true;
|
|
959
|
+
publishNavigation();
|
|
960
|
+
queue = queue
|
|
961
|
+
.then(action)
|
|
962
|
+
.then(() => {
|
|
963
|
+
if (!destroyed) success();
|
|
964
|
+
})
|
|
965
|
+
.catch(() => {
|
|
966
|
+
/* 请求层已通知错误。
|
|
967
|
+
* The request layer has already reported the error. */
|
|
968
|
+
if (!destroyed) failure();
|
|
969
|
+
})
|
|
970
|
+
.finally(() => {
|
|
971
|
+
pendingWrites--;
|
|
972
|
+
saving = pendingWrites > 0;
|
|
973
|
+
if (destroyed && !saving) client.leave();
|
|
974
|
+
back.disabled = saving || !navigation.canGoBack;
|
|
975
|
+
publishNavigation();
|
|
983
976
|
});
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
977
|
+
return queue;
|
|
978
|
+
}
|
|
979
|
+
const metadata = definition.metadata;
|
|
980
|
+
if (metadata) {
|
|
981
|
+
const info = element("div", "pp-module-info");
|
|
982
|
+
const details = element("div", "pp-module-details");
|
|
983
|
+
for (const description of [metadata.author, metadata.desc ?? metadata.description, ...(metadata.descs ?? [])]) if (description) details.append(element("p", "pp-description", description));
|
|
984
|
+
if (metadata.repo) {
|
|
985
|
+
const link = element("a", "pp-module-source", "项目主页");
|
|
986
|
+
link.href = resourceURL(metadata.repo);
|
|
987
|
+
link.target = "_blank";
|
|
988
|
+
link.rel = "noopener noreferrer";
|
|
989
|
+
details.append(link);
|
|
989
990
|
}
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
991
|
+
info.append(details);
|
|
992
|
+
view.append(info);
|
|
993
|
+
}
|
|
994
|
+
for (const field of definition.fields) {
|
|
995
|
+
const match = /^\[([^\]]+)\]\s*(.*)$/.exec(field.name);
|
|
996
|
+
const group = match?.[1] ?? "通用";
|
|
997
|
+
if (!groups.has(group)) {
|
|
998
|
+
const section = element("section", "pp-group");
|
|
999
|
+
const rows = element("div", "pp-rows");
|
|
1000
|
+
section.append(element("h2", "pp-group-title", group), rows);
|
|
1001
|
+
groups.set(group, rows);
|
|
1002
|
+
view.append(section);
|
|
1002
1003
|
}
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1004
|
+
const row = settingRow("div");
|
|
1005
|
+
row.classList.add("pp-field");
|
|
1006
|
+
const label = element("div", "pp-label");
|
|
1007
|
+
label.append(element("span", "pp-field-name", match?.[2] ?? field.name));
|
|
1008
|
+
if (field.description) label.append(element("span", "pp-field-description", field.description));
|
|
1009
|
+
row.append(label);
|
|
1010
|
+
const value = values[field.key];
|
|
1011
|
+
/**
|
|
1012
|
+
* 读取尚未保存的输入
|
|
1013
|
+
* Read the unsaved input.
|
|
1014
|
+
* @type {() => unknown}
|
|
1015
|
+
*/
|
|
1016
|
+
let read;
|
|
1017
|
+
/**
|
|
1018
|
+
* 更新当前控件
|
|
1019
|
+
* Update the current control.
|
|
1020
|
+
* @type {(value: unknown) => void}
|
|
1021
|
+
*/
|
|
1022
|
+
let write;
|
|
1023
|
+
let inputContainer = row;
|
|
1024
|
+
let eventName = "change";
|
|
1025
|
+
switch (true) {
|
|
1026
|
+
case Boolean(field.options) && field.type !== "array": {
|
|
1027
|
+
const select = element("select", "");
|
|
1028
|
+
select.setAttribute("aria-label", field.name);
|
|
1029
|
+
field.options.forEach((option, index) => {
|
|
1030
|
+
const item = element("option", "", option.label);
|
|
1031
|
+
item.value = String(index);
|
|
1032
|
+
select.append(item);
|
|
1033
|
+
});
|
|
1034
|
+
write = value => {
|
|
1035
|
+
select.selectedIndex = field.options.findIndex(option => option.key === value);
|
|
1036
|
+
};
|
|
1037
|
+
row.append(fieldControl(select));
|
|
1038
|
+
read = () => field.options[select.selectedIndex]?.key;
|
|
1039
|
+
break;
|
|
1026
1040
|
}
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
+
case field.type === "array" && Boolean(field.options): {
|
|
1042
|
+
const page = element("section", "pp-choice-page");
|
|
1043
|
+
if (field.description) page.append(element("p", "pp-description", field.description));
|
|
1044
|
+
const choices = element("div", "pp-rows");
|
|
1045
|
+
page.append(choices);
|
|
1046
|
+
inputContainer = choices;
|
|
1047
|
+
editors.set(field.key, { node: page, title: match?.[2] ?? field.name });
|
|
1048
|
+
const summary = element("span", "pp-summary");
|
|
1049
|
+
const link = element("button", "pp-choice-link");
|
|
1050
|
+
link.type = "button";
|
|
1051
|
+
link.setAttribute("aria-label", field.name);
|
|
1052
|
+
link.append(summary, element("span", "pp-chevron", "›"));
|
|
1053
|
+
row.append(link);
|
|
1054
|
+
const refresh = () => {
|
|
1055
|
+
const value = client.snapshot().values[field.key];
|
|
1056
|
+
summary.textContent =
|
|
1057
|
+
field.options
|
|
1058
|
+
.filter(option => Array.isArray(value) && value.includes(option.key))
|
|
1059
|
+
.map(option => option.label)
|
|
1060
|
+
.join("、") || "未选择";
|
|
1061
|
+
};
|
|
1062
|
+
summaries.push(refresh);
|
|
1063
|
+
refresh();
|
|
1064
|
+
link.onclick = () => navigation.open(field.key);
|
|
1065
|
+
row.addEventListener("click", event => {
|
|
1066
|
+
if (!link.contains(event.target)) link.click();
|
|
1067
|
+
});
|
|
1068
|
+
const inputs = field.options.map(option => {
|
|
1069
|
+
const label = settingRow("label");
|
|
1070
|
+
label.classList.add("pp-choice");
|
|
1071
|
+
label.textContent = option.label;
|
|
1072
|
+
const input = element("input", "");
|
|
1073
|
+
input.type = "checkbox";
|
|
1074
|
+
input.setAttribute("aria-label", option.label);
|
|
1075
|
+
label.append(input);
|
|
1076
|
+
choices.append(label);
|
|
1077
|
+
return { input, key: option.key };
|
|
1078
|
+
});
|
|
1079
|
+
read = () => inputs.filter(option => option.input.checked).map(option => option.key);
|
|
1080
|
+
write = value => {
|
|
1081
|
+
for (const option of inputs) option.input.checked = Array.isArray(value) && value.includes(option.key);
|
|
1082
|
+
};
|
|
1083
|
+
break;
|
|
1084
|
+
}
|
|
1085
|
+
case field.type === "boolean": {
|
|
1086
|
+
const toggle = element("input", "pp-switch");
|
|
1087
|
+
toggle.type = "checkbox";
|
|
1088
|
+
toggle.setAttribute("switch", "");
|
|
1089
|
+
toggle.setAttribute("role", "switch");
|
|
1090
|
+
toggle.setAttribute("aria-label", field.name);
|
|
1091
|
+
write = value => {
|
|
1092
|
+
toggle.checked = value === true;
|
|
1093
|
+
};
|
|
1094
|
+
read = () => toggle.checked;
|
|
1095
|
+
row.append(toggle);
|
|
1096
|
+
break;
|
|
1097
|
+
}
|
|
1098
|
+
default: {
|
|
1099
|
+
const multiline = field.control === "textarea" || field.type === "array";
|
|
1100
|
+
const input = element(multiline ? "textarea" : "input", "");
|
|
1101
|
+
if (multiline) row.classList.add("pp-multiline");
|
|
1102
|
+
input.setAttribute("aria-label", field.name);
|
|
1103
|
+
if (field.placeholder) input.placeholder = field.placeholder;
|
|
1104
|
+
if (multiline && field.rows) input.rows = field.rows;
|
|
1105
|
+
/**
|
|
1106
|
+
* 在挂载后根据内容调整高度,同时保留基础行数。
|
|
1107
|
+
* Size mounted textareas to their contents while retaining baseline rows.
|
|
1108
|
+
* @returns {void} 无返回值 / No return value.
|
|
1109
|
+
*/
|
|
1110
|
+
const grow = () => {
|
|
1111
|
+
if (!multiline || !field.autoGrow || !input.isConnected) return;
|
|
1112
|
+
input.style.height = "auto";
|
|
1113
|
+
const baseline = input.getBoundingClientRect().height;
|
|
1114
|
+
const style = window.getComputedStyle(input);
|
|
1115
|
+
const borders = Number.parseFloat(style.borderTopWidth) + Number.parseFloat(style.borderBottomWidth);
|
|
1116
|
+
input.style.height = `${Math.max(baseline, input.scrollHeight + borders)}px`;
|
|
1117
|
+
};
|
|
1118
|
+
if (multiline && field.autoGrow) {
|
|
1119
|
+
input.addEventListener("input", grow);
|
|
1120
|
+
growingInputs.push(grow);
|
|
1041
1121
|
}
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1122
|
+
eventName = "input";
|
|
1123
|
+
if (!multiline) input.type = field.type === "number" ? "number" : "text";
|
|
1124
|
+
write = value => {
|
|
1125
|
+
input.value = field.type === "array" ? JSON.stringify(value ?? []) : (value ?? "");
|
|
1126
|
+
grow();
|
|
1127
|
+
};
|
|
1128
|
+
read = () => {
|
|
1129
|
+
switch (field.type) {
|
|
1130
|
+
case "array":
|
|
1131
|
+
return JSON.parse(input.value);
|
|
1132
|
+
case "number":
|
|
1133
|
+
return input.value === "" ? Number.NaN : Number(input.value);
|
|
1134
|
+
default:
|
|
1135
|
+
return input.value;
|
|
1136
|
+
}
|
|
1137
|
+
};
|
|
1138
|
+
row.append(fieldControl(input));
|
|
1139
|
+
break;
|
|
1140
|
+
}
|
|
1045
1141
|
}
|
|
1142
|
+
write(value);
|
|
1143
|
+
let inputVersion = 0;
|
|
1144
|
+
inputContainer.addEventListener(eventName, event => {
|
|
1145
|
+
if (event.isComposing) return;
|
|
1146
|
+
const version = ++inputVersion;
|
|
1147
|
+
let value;
|
|
1148
|
+
try {
|
|
1149
|
+
value = read();
|
|
1150
|
+
} catch (error) {
|
|
1151
|
+
notify({ kind: "error", message: error.message });
|
|
1152
|
+
return;
|
|
1153
|
+
}
|
|
1154
|
+
const restore = () => {
|
|
1155
|
+
if (version === inputVersion) write(client.snapshot().values[field.key]);
|
|
1156
|
+
};
|
|
1157
|
+
perform(
|
|
1158
|
+
() => {
|
|
1159
|
+
if (!validValue(field, value)) {
|
|
1160
|
+
const error = new TypeError("Invalid setting value");
|
|
1161
|
+
notify({ kind: "error", operation: "write", key: field.key, message: error.message });
|
|
1162
|
+
throw error;
|
|
1163
|
+
}
|
|
1164
|
+
return client.set(field.key, value);
|
|
1165
|
+
},
|
|
1166
|
+
() => {
|
|
1167
|
+
for (const refresh of summaries) refresh();
|
|
1168
|
+
},
|
|
1169
|
+
restore,
|
|
1170
|
+
);
|
|
1171
|
+
});
|
|
1172
|
+
if (eventName === "input") inputContainer.addEventListener("compositionend", event => event.target.dispatchEvent(new window.Event("input", { bubbles: true })));
|
|
1173
|
+
groups.get(group).append(row);
|
|
1046
1174
|
}
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1175
|
+
const settingsPage = element("section", "pp-settings-page");
|
|
1176
|
+
const settingsOutput = element("pre", "pp-cache");
|
|
1177
|
+
settingsOutput.setAttribute("aria-label", "Settings 内容");
|
|
1178
|
+
settingsPage.append(settingsOutput);
|
|
1179
|
+
editors.set("$settings", { node: settingsPage, title: "设置" });
|
|
1180
|
+
handlers.set("viewSettings", () => {
|
|
1181
|
+
if (saving) return;
|
|
1052
1182
|
let value;
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
perform(
|
|
1183
|
+
return perform(
|
|
1184
|
+
async () => {
|
|
1185
|
+
try {
|
|
1186
|
+
value = await client.readSettings();
|
|
1187
|
+
} catch (error) {
|
|
1188
|
+
notify({ kind: "error", message: error.message });
|
|
1189
|
+
throw error;
|
|
1190
|
+
}
|
|
1191
|
+
},
|
|
1063
1192
|
() => {
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1193
|
+
settingsOutput.textContent = value === undefined ? "暂无设置" : JSON.stringify(value, null, 2);
|
|
1194
|
+
navigation.open("$settings");
|
|
1195
|
+
},
|
|
1196
|
+
);
|
|
1197
|
+
});
|
|
1198
|
+
const cachePage = element("section", "pp-cache-page");
|
|
1199
|
+
const output = element("pre", "pp-cache");
|
|
1200
|
+
output.textContent = "暂无缓存";
|
|
1201
|
+
output.setAttribute("aria-label", "Caches 内容");
|
|
1202
|
+
cachePage.append(output);
|
|
1203
|
+
editors.set("$caches", { node: cachePage, title: "缓存" });
|
|
1204
|
+
handlers.set("viewCaches", () => {
|
|
1205
|
+
if (saving) return;
|
|
1206
|
+
let value;
|
|
1207
|
+
return perform(
|
|
1208
|
+
async () => {
|
|
1209
|
+
try {
|
|
1210
|
+
value = await client.readCaches();
|
|
1211
|
+
} catch (error) {
|
|
1212
|
+
notify({ kind: "error", message: error.message });
|
|
1067
1213
|
throw error;
|
|
1068
1214
|
}
|
|
1069
|
-
return client.set(field.key, value);
|
|
1070
1215
|
},
|
|
1071
1216
|
() => {
|
|
1072
|
-
|
|
1217
|
+
output.textContent = value === undefined ? "暂无缓存" : JSON.stringify(value, null, 2);
|
|
1218
|
+
navigation.open("$caches");
|
|
1073
1219
|
},
|
|
1074
|
-
restore,
|
|
1075
1220
|
);
|
|
1076
1221
|
});
|
|
1077
|
-
|
|
1078
|
-
|
|
1222
|
+
handlers.set("clearCaches", async () => {
|
|
1223
|
+
if (saving) return;
|
|
1224
|
+
if (!(await requestConfirmation(window, `清空 ${active} 的全部 Caches?`)) || destroyed || saving) return;
|
|
1225
|
+
return perform(
|
|
1226
|
+
() => client.clearCaches(),
|
|
1227
|
+
() => {
|
|
1228
|
+
output.textContent = "暂无缓存";
|
|
1229
|
+
},
|
|
1230
|
+
);
|
|
1231
|
+
});
|
|
1232
|
+
handlers.set("reset", async () => {
|
|
1233
|
+
if (saving) return;
|
|
1234
|
+
if (!(await requestConfirmation(window, `重置 ${active} 的设置?这将删除该模块的 Settings、Caches 和其它持久化数据。`)) || destroyed || saving) return;
|
|
1235
|
+
return perform(() => client.reset(), controls);
|
|
1236
|
+
});
|
|
1237
|
+
navigation?.destroy();
|
|
1238
|
+
navigation = new Navigation(viewport, view, key => editors.get(key)?.node);
|
|
1239
|
+
navigation.addEventListener("change", updateNavigation);
|
|
1240
|
+
for (const grow of growingInputs) grow();
|
|
1241
|
+
updateNavigation();
|
|
1079
1242
|
}
|
|
1080
|
-
const settingsPage = element("section", "pp-settings-page");
|
|
1081
|
-
const settingsOutput = element("pre", "pp-cache");
|
|
1082
|
-
settingsOutput.setAttribute("aria-label", "Settings 内容");
|
|
1083
|
-
settingsPage.append(settingsOutput);
|
|
1084
|
-
editors.set("$settings", { node: settingsPage, title: "设置" });
|
|
1085
|
-
handlers.set("viewSettings", () => {
|
|
1086
|
-
if (saving) return;
|
|
1087
|
-
let value;
|
|
1088
|
-
return perform(
|
|
1089
|
-
async () => {
|
|
1090
|
-
try {
|
|
1091
|
-
value = await client.readSettings();
|
|
1092
|
-
} catch (error) {
|
|
1093
|
-
notify({ kind: "error", message: error.message });
|
|
1094
|
-
throw error;
|
|
1095
|
-
}
|
|
1096
|
-
},
|
|
1097
|
-
() => {
|
|
1098
|
-
settingsOutput.textContent = value === undefined ? "暂无设置" : JSON.stringify(value, null, 2);
|
|
1099
|
-
navigation.open("$settings");
|
|
1100
|
-
},
|
|
1101
|
-
);
|
|
1102
|
-
});
|
|
1103
|
-
const cachePage = element("section", "pp-cache-page");
|
|
1104
|
-
const output = element("pre", "pp-cache");
|
|
1105
|
-
output.textContent = "暂无缓存";
|
|
1106
|
-
output.setAttribute("aria-label", "Caches 内容");
|
|
1107
|
-
cachePage.append(output);
|
|
1108
|
-
editors.set("$caches", { node: cachePage, title: "缓存" });
|
|
1109
|
-
handlers.set("viewCaches", () => {
|
|
1110
|
-
if (saving) return;
|
|
1111
|
-
let value;
|
|
1112
|
-
return perform(
|
|
1113
|
-
async () => {
|
|
1114
|
-
try {
|
|
1115
|
-
value = await client.readCaches();
|
|
1116
|
-
} catch (error) {
|
|
1117
|
-
notify({ kind: "error", message: error.message });
|
|
1118
|
-
throw error;
|
|
1119
|
-
}
|
|
1120
|
-
},
|
|
1121
|
-
() => {
|
|
1122
|
-
output.textContent = value === undefined ? "暂无缓存" : JSON.stringify(value, null, 2);
|
|
1123
|
-
navigation.open("$caches");
|
|
1124
|
-
},
|
|
1125
|
-
);
|
|
1126
|
-
});
|
|
1127
|
-
handlers.set("clearCaches", async () => {
|
|
1128
|
-
if (saving) return;
|
|
1129
|
-
if (!(await requestConfirmation(window, `清空 ${active} 的全部 Caches?`)) || destroyed || saving) return;
|
|
1130
|
-
return perform(
|
|
1131
|
-
() => client.clearCaches(),
|
|
1132
|
-
() => {
|
|
1133
|
-
output.textContent = "暂无缓存";
|
|
1134
|
-
},
|
|
1135
|
-
);
|
|
1136
|
-
});
|
|
1137
|
-
handlers.set("reset", async () => {
|
|
1138
|
-
if (saving) return;
|
|
1139
|
-
if (!(await requestConfirmation(window, `重置 ${active} 的设置?这将删除该模块的 Settings、Caches 和其它持久化数据。`)) || destroyed || saving) return;
|
|
1140
|
-
return perform(() => client.reset(), controls);
|
|
1141
|
-
});
|
|
1142
|
-
navigation?.destroy();
|
|
1143
|
-
navigation = new Navigation(viewport, view, key => editors.get(key)?.node);
|
|
1144
|
-
navigation.addEventListener("change", updateNavigation);
|
|
1145
|
-
for (const grow of growingInputs) grow();
|
|
1146
|
-
updateNavigation();
|
|
1147
|
-
}
|
|
1148
|
-
/**
|
|
1149
|
-
* 已加载的表单交由导航组件返回;加载阶段可以返回先前文档。
|
|
1150
|
-
* Loaded forms delegate back to navigation; loading views can return to the previous document.
|
|
1151
|
-
* @returns {void} 无返回值 / No return value.
|
|
1152
|
-
*/
|
|
1153
|
-
back.onclick = () => {
|
|
1154
|
-
if (saving) return;
|
|
1155
|
-
if (navigation) navigation.back();
|
|
1156
|
-
else window.history.back();
|
|
1157
|
-
};
|
|
1158
|
-
open(definition.module);
|
|
1159
|
-
return {
|
|
1160
1243
|
/**
|
|
1161
|
-
*
|
|
1162
|
-
*
|
|
1244
|
+
* 已加载的表单交由导航组件返回;加载阶段可以返回先前文档。
|
|
1245
|
+
* Loaded forms delegate back to navigation; loading views can return to the previous document.
|
|
1163
1246
|
* @returns {void} 无返回值 / No return value.
|
|
1164
1247
|
*/
|
|
1165
|
-
|
|
1248
|
+
back.onclick = () => {
|
|
1249
|
+
if (saving) return;
|
|
1250
|
+
if (navigation) navigation.back();
|
|
1251
|
+
else window.history.back();
|
|
1252
|
+
};
|
|
1253
|
+
open(definition.module);
|
|
1254
|
+
return () => {
|
|
1166
1255
|
destroyed = true;
|
|
1167
1256
|
menu.destroy();
|
|
1168
1257
|
window.frameElement?.removeEventListener("preferencepanes:action", onAction);
|
|
@@ -1171,8 +1260,17 @@ function mountPanel(root, model) {
|
|
|
1171
1260
|
if (active && !saving) client.leave();
|
|
1172
1261
|
clearTimeout(timer);
|
|
1173
1262
|
shell.remove();
|
|
1174
|
-
}
|
|
1175
|
-
}
|
|
1263
|
+
};
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1266
|
+
/**
|
|
1267
|
+
* 移除监听器、定时器、会话和挂载内容。
|
|
1268
|
+
* Remove listeners, timers, session, and mounted content.
|
|
1269
|
+
* @returns {void} 无返回值 / No return value.
|
|
1270
|
+
*/
|
|
1271
|
+
destroy() {
|
|
1272
|
+
this.#release();
|
|
1273
|
+
}
|
|
1176
1274
|
}
|
|
1177
1275
|
|
|
1178
1276
|
var defaults = "/* 通用默认样式只使用 pp 命名空间;项目可通过 CSS 输入覆盖变量和组件。\n * Generic defaults use only the pp namespace; projects may override variables and components through CSS input. */\n.pp-panel {\n --pp-text: #18191c;\n --pp-background: #f6f7f8;\n --pp-surface: #fff;\n --pp-field: #f1f2f3;\n --pp-border: #e3e5e7;\n --pp-muted: #797f87;\n --pp-accent: #1677ff;\n font:\n 15px / 1.5 -apple-system,\n BlinkMacSystemFont,\n \"Segoe UI\",\n sans-serif;\n color: var(--pp-text);\n background: var(--pp-background);\n position: relative;\n display: flex;\n flex-direction: column;\n width: 100%;\n max-width: 100vw;\n min-width: 0;\n height: 100vh;\n overflow: hidden;\n}\n\n:root[data-theme=\"dark\"] .pp-panel {\n --pp-text: #f1f2f3;\n --pp-background: #0d0e0f;\n --pp-surface: #18191c;\n --pp-field: #2f3238;\n --pp-border: #2f3238;\n --pp-muted: #9499a0;\n}\n.pp-panel * {\n box-sizing: border-box;\n letter-spacing: 0;\n}\n.pp-header {\n flex: none;\n height: calc(52px + env(safe-area-inset-top));\n padding: env(safe-area-inset-top) 12px 0;\n display: flex;\n align-items: center;\n background: var(--pp-surface);\n border-bottom: 1px solid var(--pp-border);\n position: relative;\n z-index: 2;\n}\n.pp-title {\n flex: 1;\n text-align: center;\n font-size: 17px;\n font-weight: 500;\n margin: 0;\n min-width: 0;\n overflow-wrap: anywhere;\n}\n.pp-nav-spacer {\n width: 44px;\n flex: none;\n}\n.pp-panel button {\n font: inherit;\n cursor: pointer;\n border: 0;\n background: none;\n color: inherit;\n}\n.pp-panel .pp-back {\n width: 44px;\n height: 44px;\n flex: none;\n font-size: 34px;\n line-height: 32px;\n padding: 0;\n}\n.pp-panel button:disabled {\n opacity: 0.5;\n cursor: wait;\n}\n.pp-viewport {\n position: relative;\n flex: 1;\n min-width: 0;\n min-height: 0;\n overflow: hidden;\n}\n:root[data-preference-panes-embedded] .pp-header {\n display: none;\n}\n@supports (height: 100dvh) {\n .pp-panel {\n height: 100dvh;\n }\n}\n.pp-fields,\n.pp-choice-page,\n.pp-settings-page,\n.pp-cache-page {\n position: absolute;\n inset: 0;\n min-width: 0;\n overflow-x: hidden;\n overflow-y: auto;\n padding: 12px max(16px, calc((100% - 688px) / 2)) calc(28px + env(safe-area-inset-bottom) + var(--pp-keyboard-height, 0px));\n scroll-padding-bottom: var(--pp-keyboard-height, 0px);\n background: var(--pp-background);\n}\n.pp-choice-link {\n display: flex;\n align-items: center;\n justify-content: flex-end;\n gap: 8px;\n max-width: 45%;\n min-width: 44px;\n min-height: 44px;\n padding: 0;\n text-align: right;\n flex: 1;\n}\n.pp-summary {\n color: var(--pp-muted);\n font-size: 13px;\n line-height: 18px;\n display: -webkit-box;\n -webkit-line-clamp: 2;\n -webkit-box-orient: vertical;\n overflow: hidden;\n overflow-wrap: anywhere;\n}\n.pp-chevron {\n color: var(--pp-muted);\n font-size: 22px;\n flex: none;\n}\n.pp-editor {\n flex: none;\n width: 45%;\n min-width: 0;\n min-height: 36px;\n padding: 8px 10px;\n font: inherit;\n color: var(--pp-text);\n background: var(--pp-field);\n border: 0;\n border-radius: 6px;\n}\n.pp-panel .pp-multiline {\n display: block;\n}\n.pp-multiline .pp-editor {\n width: 100%;\n margin-top: 10px;\n}\n.pp-panel [hidden] {\n display: none !important;\n}\n.pp-label {\n flex: 1;\n min-width: 0;\n display: flex;\n flex-direction: column;\n align-items: flex-start;\n margin-right: 16px;\n}\n.pp-field-name {\n color: var(--pp-text);\n font-size: 15px;\n}\n.pp-field-description {\n margin-top: 2px;\n color: var(--pp-muted);\n font-size: 12px;\n}\n.pp-group {\n margin-top: 16px;\n}\n.pp-group-title {\n margin: 0 0 8px;\n color: var(--pp-muted);\n font-size: 15px;\n font-weight: 400;\n}\n.pp-row {\n min-width: 0;\n min-height: 48px;\n padding: 16px;\n display: flex;\n align-items: center;\n justify-content: space-between;\n background: var(--pp-surface);\n border-bottom: 1px solid var(--pp-border);\n}\n.pp-rows > :last-child {\n border-bottom: 0 !important;\n}\n.pp-switch {\n flex: none;\n accent-color: var(--pp-accent);\n}\n.pp-choice {\n justify-content: space-between;\n cursor: pointer;\n}\n.pp-choice input {\n width: 20px;\n height: 20px;\n flex: none;\n accent-color: var(--pp-accent);\n margin: 0;\n}\n.pp-description {\n font-size: 12px;\n line-height: 1.6;\n color: var(--pp-muted);\n white-space: pre-wrap;\n overflow-wrap: anywhere;\n}\n.pp-module-info {\n display: flex;\n gap: 12px;\n margin: 12px 0;\n}\n.pp-module-details {\n min-width: 0;\n overflow-wrap: anywhere;\n}\n.pp-module-source {\n color: inherit;\n text-decoration: underline;\n}\n.pp-status {\n position: fixed;\n inset: 0;\n display: grid;\n place-content: center;\n justify-items: center;\n gap: 12px;\n min-width: 0;\n min-height: 0;\n margin: 0;\n padding: 24px;\n color: var(--pp-muted, GrayText);\n text-align: center;\n background: var(--pp-background, Canvas);\n}\n.pp-viewport > .pp-status {\n position: absolute;\n}\n.pp-status-spinner {\n box-sizing: border-box;\n width: 28px;\n height: 28px;\n border: 3px solid color-mix(in srgb, currentColor 25%, transparent);\n border-top-color: var(--pp-accent, AccentColor);\n border-radius: 50%;\n animation: pp-status-spin 0.8s linear infinite;\n}\n.pp-status-message {\n max-width: 100%;\n margin: 0;\n overflow-wrap: anywhere;\n}\n.pp-status-action {\n min-width: 96px;\n min-height: 44px;\n padding: 8px 16px;\n border: 0;\n border-radius: 6px;\n color: var(--pp-text, ButtonText);\n font: inherit;\n cursor: pointer;\n background: var(--pp-surface, ButtonFace);\n}\n@keyframes pp-status-spin {\n to {\n transform: rotate(1turn);\n }\n}\n.pp-cache {\n white-space: pre-wrap;\n overflow-wrap: anywhere;\n}\n.pp-toast {\n pointer-events: none;\n position: fixed;\n bottom: calc(30px + env(safe-area-inset-bottom));\n left: 50%;\n transform: translateX(-50%);\n max-width: 90vw;\n padding: 10px 16px;\n border-radius: 8px;\n background: #333e;\n color: white;\n font-size: 13px;\n z-index: 20;\n}\n.pp-toast[data-kind=\"error\"] {\n background: #8d2424;\n}\n.pp-panel :focus-visible {\n outline: 2px solid var(--pp-accent);\n outline-offset: -2px;\n}\n";
|
|
@@ -1196,89 +1294,118 @@ function installDefaultStyles(document) {
|
|
|
1196
1294
|
}
|
|
1197
1295
|
|
|
1198
1296
|
/**
|
|
1199
|
-
*
|
|
1200
|
-
*
|
|
1201
|
-
* @param {import("../index.js").ModuleModel} model API 返回的模块模型 / Module model returned by the API.
|
|
1202
|
-
* @param {string} [css] 可选 CSS 正文 / Optional CSS text.
|
|
1203
|
-
* @returns {import("./index.js").MountedPreferences} 模块生命周期句柄 / Module lifecycle handle.
|
|
1297
|
+
* 管理模块设置视图的模型规范化、样式、主题同步和面板生命周期。
|
|
1298
|
+
* Manage model normalization, styles, theme synchronization, and panel lifecycle for a module settings view.
|
|
1204
1299
|
*/
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1300
|
+
class PreferencesView {
|
|
1301
|
+
#existing;
|
|
1302
|
+
#root;
|
|
1303
|
+
#base;
|
|
1304
|
+
#ownsBase;
|
|
1305
|
+
#custom;
|
|
1306
|
+
#previousTitle;
|
|
1307
|
+
#previousTheme;
|
|
1308
|
+
#systemTheme;
|
|
1309
|
+
#previousKeyboard;
|
|
1310
|
+
#host;
|
|
1311
|
+
#observer;
|
|
1312
|
+
#panel;
|
|
1313
|
+
|
|
1314
|
+
/**
|
|
1315
|
+
* 使用模块 API 返回的模型挂载设置页。
|
|
1316
|
+
* Mount a settings page from the model returned by the module API.
|
|
1317
|
+
* @param {import("../index.js").ModuleModel} model API 返回的模块模型 / Module model returned by the API.
|
|
1318
|
+
* @param {string} [css] 可选 CSS 正文 / Optional module-scoped CSS text.
|
|
1319
|
+
*/
|
|
1320
|
+
constructor(model, css = "") {
|
|
1321
|
+
if (typeof css !== "string") throw new TypeError("CSS must be a string");
|
|
1322
|
+
const definition = normalizeBoxJs(model.boxjs, model.module);
|
|
1323
|
+
const values = { ...model.values };
|
|
1324
|
+
for (const field of definition.fields) {
|
|
1325
|
+
if (values[field.key] === undefined) continue;
|
|
1326
|
+
values[field.key] = normalizeStoredValue(field, values[field.key]);
|
|
1327
|
+
if (!validValue(field, values[field.key])) throw new TypeError(`Invalid stored value: ${field.key}`);
|
|
1328
|
+
}
|
|
1329
|
+
for (const field of definition.fields) if (values[field.key] === undefined && Object.hasOwn(field, "defaultValue")) values[field.key] = structuredClone(field.defaultValue);
|
|
1330
|
+
const rendered = { ...model, definition, values };
|
|
1331
|
+
const metadata = definition.metadata ?? {};
|
|
1332
|
+
const image = metadata.icon || metadata.icons?.[1] || metadata.icons?.[0];
|
|
1333
|
+
if (image) resourceURL(image);
|
|
1334
|
+
if (metadata.repo) resourceURL(metadata.repo);
|
|
1335
|
+
|
|
1336
|
+
this.#existing = document.querySelector("#preferences");
|
|
1337
|
+
this.#root = this.#existing ?? element("main", "");
|
|
1338
|
+
if (!this.#existing) {
|
|
1339
|
+
this.#root.id = "preferences";
|
|
1340
|
+
document.body.append(this.#root);
|
|
1341
|
+
}
|
|
1342
|
+
const styles = installDefaultStyles(document);
|
|
1343
|
+
this.#base = styles.element;
|
|
1344
|
+
this.#ownsBase = styles.owned;
|
|
1345
|
+
this.#custom = element("style", "");
|
|
1346
|
+
this.#custom.textContent = css;
|
|
1347
|
+
document.head.append(this.#custom);
|
|
1348
|
+
this.#previousTitle = document.title;
|
|
1349
|
+
this.#previousTheme = document.documentElement.dataset.theme;
|
|
1350
|
+
this.#systemTheme = window.matchMedia("(prefers-color-scheme: dark)");
|
|
1351
|
+
this.#previousKeyboard = document.documentElement.style.getPropertyValue("--pp-keyboard-height");
|
|
1352
|
+
this.#host = window.frameElement?.ownerDocument.documentElement;
|
|
1353
|
+
this.#syncAppearance();
|
|
1354
|
+
this.#systemTheme.addEventListener("change", this.#syncAppearance);
|
|
1355
|
+
if (this.#host) {
|
|
1356
|
+
this.#observer = new MutationObserver(this.#syncAppearance);
|
|
1357
|
+
this.#observer.observe(this.#host, { attributes: true, attributeFilter: ["data-theme", "style"] });
|
|
1358
|
+
}
|
|
1359
|
+
document.title = metadata.name ?? definition.module;
|
|
1360
|
+
try {
|
|
1361
|
+
this.#root.replaceChildren();
|
|
1362
|
+
this.#panel = new PreferencesPanel(this.#root, rendered);
|
|
1363
|
+
} catch (error) {
|
|
1364
|
+
this.destroy();
|
|
1365
|
+
throw error;
|
|
1366
|
+
}
|
|
1225
1367
|
}
|
|
1226
|
-
|
|
1227
|
-
const custom = element("style", "");
|
|
1228
|
-
custom.textContent = css;
|
|
1229
|
-
document.head.append(custom);
|
|
1230
|
-
const previousTitle = document.title;
|
|
1231
|
-
const previousTheme = document.documentElement.dataset.theme;
|
|
1232
|
-
const systemTheme = window.matchMedia("(prefers-color-scheme: dark)");
|
|
1233
|
-
const previousKeyboard = document.documentElement.style.getPropertyValue("--pp-keyboard-height");
|
|
1234
|
-
const host = window.frameElement?.ownerDocument.documentElement;
|
|
1368
|
+
|
|
1235
1369
|
/**
|
|
1236
1370
|
* 跟随嵌入宿主的通用环境状态,不识别业务 App 或解析其 UA。
|
|
1237
|
-
* Follow generic host appearance without detecting a business
|
|
1371
|
+
* Follow generic host appearance without detecting a business App or parsing its UA.
|
|
1238
1372
|
* @returns {void} 已同步主题与键盘避让 / Theme and keyboard clearance synchronized.
|
|
1239
1373
|
*/
|
|
1240
|
-
|
|
1241
|
-
const theme = host?.dataset.theme ?? previousTheme ?? (systemTheme.matches ? "dark" : "light");
|
|
1374
|
+
#syncAppearance = () => {
|
|
1375
|
+
const theme = this.#host?.dataset.theme ?? this.#previousTheme ?? (this.#systemTheme.matches ? "dark" : "light");
|
|
1242
1376
|
document.documentElement.dataset.theme = theme;
|
|
1243
|
-
if (host) document.documentElement.style.setProperty("--pp-keyboard-height", host.style.getPropertyValue("--pp-keyboard-height"));
|
|
1377
|
+
if (this.#host) document.documentElement.style.setProperty("--pp-keyboard-height", this.#host.style.getPropertyValue("--pp-keyboard-height"));
|
|
1244
1378
|
};
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
panel?.destroy();
|
|
1264
|
-
if (ownsBase) base.remove();
|
|
1265
|
-
custom.remove();
|
|
1266
|
-
if (existing) root.replaceChildren();
|
|
1267
|
-
else root.remove();
|
|
1268
|
-
document.title = previousTitle;
|
|
1269
|
-
if (previousTheme === undefined) delete document.documentElement.dataset.theme;
|
|
1270
|
-
else document.documentElement.dataset.theme = previousTheme;
|
|
1271
|
-
document.documentElement.style.setProperty("--pp-keyboard-height", previousKeyboard);
|
|
1272
|
-
},
|
|
1273
|
-
};
|
|
1274
|
-
try {
|
|
1275
|
-
root.replaceChildren();
|
|
1276
|
-
panel = mountPanel(root, rendered);
|
|
1277
|
-
return view;
|
|
1278
|
-
} catch (error) {
|
|
1279
|
-
view.destroy();
|
|
1280
|
-
throw error;
|
|
1379
|
+
|
|
1380
|
+
/**
|
|
1381
|
+
* 释放模块视图、样式与会话,不操作项目入口页。
|
|
1382
|
+
* Release the module view, styles, and session without operating a project landing page.
|
|
1383
|
+
* @returns {void} 无返回值 / No return value.
|
|
1384
|
+
*/
|
|
1385
|
+
destroy() {
|
|
1386
|
+
this.#observer?.disconnect();
|
|
1387
|
+
this.#systemTheme.removeEventListener("change", this.#syncAppearance);
|
|
1388
|
+
this.#panel?.destroy();
|
|
1389
|
+
if (this.#ownsBase) this.#base.remove();
|
|
1390
|
+
this.#custom.remove();
|
|
1391
|
+
if (this.#existing) this.#root.replaceChildren();
|
|
1392
|
+
else this.#root.remove();
|
|
1393
|
+
document.title = this.#previousTitle;
|
|
1394
|
+
if (this.#previousTheme === undefined) delete document.documentElement.dataset.theme;
|
|
1395
|
+
else document.documentElement.dataset.theme = this.#previousTheme;
|
|
1396
|
+
document.documentElement.style.setProperty("--pp-keyboard-height", this.#previousKeyboard);
|
|
1281
1397
|
}
|
|
1282
1398
|
}
|
|
1283
1399
|
|
|
1284
|
-
|
|
1400
|
+
/**
|
|
1401
|
+
* 使用模块 API 返回的模型挂载设置页;CSS 仅覆盖当前模块。
|
|
1402
|
+
* Mount a settings page from a module API model; CSS only overrides this module.
|
|
1403
|
+
* @param {import("../index.js").ModuleModel} model API 返回的模块模型 / Module model returned by the API.
|
|
1404
|
+
* @param {string} [css] 可选 CSS 正文 / Optional module-scoped CSS text.
|
|
1405
|
+
* @returns {PreferencesView} 模块视图 / Module view.
|
|
1406
|
+
*/
|
|
1407
|
+
function mount(model, css = "") {
|
|
1408
|
+
return new PreferencesView(model, css);
|
|
1409
|
+
}
|
|
1410
|
+
|
|
1411
|
+
export { PreferencesView, mount };
|