dsh-workbench-tasks 5.2.47 → 5.3.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/lib/client.js CHANGED
@@ -2715,6 +2715,170 @@ var require_number_field = __commonJS({
2715
2715
  }
2716
2716
  });
2717
2717
 
2718
+ // packages/addons/lib/config-scheme-bar.cjs
2719
+ var require_config_scheme_bar = __commonJS({
2720
+ "packages/addons/lib/config-scheme-bar.cjs"(exports2, module2) {
2721
+ "use strict";
2722
+ var React = require("react");
2723
+ var h = React.createElement;
2724
+ var COPY = {
2725
+ zh: {
2726
+ title: "\u914D\u7F6E\u65B9\u6848",
2727
+ none: "\u4E0D\u4F7F\u7528\u65B9\u6848\uFF08\u63D2\u4EF6\u9ED8\u8BA4\u914D\u7F6E\uFF09",
2728
+ create: "\u65B0\u5EFA\u65B9\u6848",
2729
+ rename: "\u91CD\u547D\u540D",
2730
+ remove: "\u5220\u9664",
2731
+ namePlaceholder: "\u65B9\u6848\u540D\u79F0",
2732
+ ok: "\u786E\u5B9A",
2733
+ cancel: "\u53D6\u6D88",
2734
+ current: "\u5F53\u524D\u65B9\u6848",
2735
+ defaultHint: "\u672A\u9009\u62E9\u65B9\u6848\u65F6\u4F7F\u7528\u63D2\u4EF6\u9ED8\u8BA4\u914D\u7F6E\uFF1B\u4FEE\u6539\u4EFB\u610F\u8BBE\u7F6E\u4F1A\u81EA\u52A8\u65B0\u5EFA\u300C\u672C\u673A\u914D\u7F6E\u300D\u65B9\u6848\u3002",
2736
+ syncedHint: "\u65B9\u6848\u4F1A\u968F\u4E91\u7AEF\u540C\u6B65\u4E0E\u5907\u4EFD\u4E00\u8D77\u8D70\uFF1B\u5F53\u524D\u9009\u4E2D\u54EA\u4E2A\u65B9\u6848\u53EA\u4FDD\u5B58\u5728\u672C\u673A\u3002",
2737
+ confirmDelete: "\u5220\u9664\u5F53\u524D\u65B9\u6848\u540E\u4F1A\u56DE\u5230\u63D2\u4EF6\u9ED8\u8BA4\u914D\u7F6E\uFF0C\u786E\u5B9A\u5220\u9664\uFF1F",
2738
+ createTitle: "\u65B0\u5EFA\u914D\u7F6E\u65B9\u6848",
2739
+ renameTitle: "\u91CD\u547D\u540D\u914D\u7F6E\u65B9\u6848",
2740
+ fromCurrent: "\u4ECE\u5F53\u524D\u65B9\u6848\u590D\u5236",
2741
+ fromDefault: "\u4ECE\u63D2\u4EF6\u9ED8\u8BA4\u5F00\u59CB"
2742
+ },
2743
+ en: {
2744
+ title: "Config scheme",
2745
+ none: "No scheme (plugin defaults)",
2746
+ create: "New scheme",
2747
+ rename: "Rename",
2748
+ remove: "Delete",
2749
+ namePlaceholder: "Scheme name",
2750
+ ok: "OK",
2751
+ cancel: "Cancel",
2752
+ current: "Active scheme",
2753
+ defaultHint: 'With no scheme selected the plugin defaults apply. Editing any setting creates a local "My config" scheme automatically.',
2754
+ syncedHint: "Schemes sync to the cloud and travel with backups; which scheme this device uses stays local.",
2755
+ confirmDelete: "Deleting the active scheme returns to plugin defaults. Delete it?",
2756
+ createTitle: "New config scheme",
2757
+ renameTitle: "Rename config scheme",
2758
+ fromCurrent: "Copy active scheme",
2759
+ fromDefault: "Start from plugin defaults"
2760
+ }
2761
+ };
2762
+ var styles = [
2763
+ ".__P__-cfg-schemes{display:flex;flex-direction:column;gap:10px;padding:13px 16px;border:1px solid var(--cfg-line);border-radius:12px;background:var(--cfg-surface)}",
2764
+ ".__P__-cfg-schemes-main{display:flex;align-items:center;gap:10px;flex-wrap:wrap}",
2765
+ ".__P__-cfg-schemes-label{font-size:12.5px;font-weight:660;white-space:nowrap}",
2766
+ ".__P__-cfg-schemes-select{border:1px solid var(--cfg-line);background:var(--dsw-alias-bg-base,transparent);color:inherit;border-radius:9px;padding:7px 11px;font:inherit;font-size:13px;line-height:1.4;outline:none;min-width:190px;max-width:100%}",
2767
+ ".__P__-cfg-schemes-select:focus{border-color:var(--dsw-alias-brand-primary,currentColor);box-shadow:0 0 0 3px color-mix(in oklab,var(--dsw-alias-brand-primary,currentColor) 14%,transparent)}",
2768
+ ".__P__-cfg-schemes-actions{display:flex;gap:7px;flex-wrap:wrap}",
2769
+ ".__P__-cfg-sbtn{appearance:none;border:1px solid var(--cfg-line);background:transparent;color:inherit;border-radius:8px;padding:5px 11px;font:inherit;font-size:12px;line-height:1.5;cursor:pointer;transition:background .12s ease,border-color .12s ease}",
2770
+ ".__P__-cfg-sbtn:hover:not(:disabled){background:var(--cfg-hover);border-color:color-mix(in oklab,currentColor 28%,var(--cfg-line))}",
2771
+ ".__P__-cfg-sbtn:disabled{opacity:.5;cursor:default}",
2772
+ ".__P__-cfg-sbtn.danger{color:var(--dsw-alias-state-error-primary,inherit)}",
2773
+ ".__P__-cfg-schemes-form{display:flex;align-items:center;gap:7px;flex-wrap:wrap}",
2774
+ ".__P__-cfg-schemes-input{flex:1 1 180px;min-width:120px;border:1px solid var(--cfg-line);background:var(--dsw-alias-bg-base,transparent);color:inherit;border-radius:8px;padding:6px 10px;font:inherit;font-size:13px;outline:none}",
2775
+ ".__P__-cfg-schemes-input:focus{border-color:var(--dsw-alias-brand-primary,currentColor)}",
2776
+ ".__P__-cfg-schemes-hint{font-size:11.5px;color:var(--cfg-muted);line-height:1.6}"
2777
+ ];
2778
+ function SchemeBar(props) {
2779
+ var lang = props.lang === "en" ? "en" : "zh";
2780
+ var c = COPY[lang];
2781
+ var prefix = props.prefix || "wb";
2782
+ var cls = function(suffix) {
2783
+ return "." + prefix + "-cfg-" + suffix;
2784
+ };
2785
+ var schemes = Array.isArray(props.schemes) ? props.schemes : [];
2786
+ var activeId = props.activeId || "";
2787
+ var busy = Boolean(props.busy);
2788
+ var [editing, setEditing] = React.useState(null);
2789
+ var [name, setName] = React.useState("");
2790
+ var [from, setFrom] = React.useState("current");
2791
+ function begin(mode) {
2792
+ var current = schemes.filter(function(s) {
2793
+ return s.id === activeId;
2794
+ })[0];
2795
+ setName(mode === "rename" && current ? current.name : "");
2796
+ setFrom(current ? "current" : "default");
2797
+ setEditing(mode);
2798
+ }
2799
+ function submit(event) {
2800
+ if (event && event.preventDefault) event.preventDefault();
2801
+ var value = String(name || "").trim();
2802
+ if (!value || busy) return;
2803
+ if (editing === "rename") props.onRename(activeId, value);
2804
+ else props.onCreate(value, from);
2805
+ setEditing(null);
2806
+ setName("");
2807
+ }
2808
+ function remove() {
2809
+ if (typeof window !== "undefined" && window.confirm && !window.confirm(c.confirmDelete)) return;
2810
+ props.onDelete(activeId);
2811
+ }
2812
+ return h(
2813
+ "div",
2814
+ { className: prefix + "-cfg-schemes" },
2815
+ h(
2816
+ "div",
2817
+ { className: prefix + "-cfg-schemes-main" },
2818
+ h("span", { className: prefix + "-cfg-schemes-label" }, c.title),
2819
+ h(
2820
+ "select",
2821
+ {
2822
+ className: prefix + "-cfg-schemes-select",
2823
+ value: activeId,
2824
+ disabled: busy,
2825
+ onChange: function(event) {
2826
+ props.onActivate(event.target.value || null);
2827
+ },
2828
+ "aria-label": c.title
2829
+ },
2830
+ h("option", { value: "" }, c.none),
2831
+ schemes.map(function(s) {
2832
+ return h("option", { key: s.id, value: s.id }, s.name);
2833
+ })
2834
+ ),
2835
+ h("span", { className: prefix + "-cfg-schemes-hint" }, activeId ? c.current : "")
2836
+ ),
2837
+ editing ? h(
2838
+ "form",
2839
+ { className: prefix + "-cfg-schemes-form", onSubmit: submit },
2840
+ h("span", { className: prefix + "-cfg-schemes-label" }, editing === "rename" ? c.renameTitle : c.createTitle),
2841
+ h("input", {
2842
+ className: prefix + "-cfg-schemes-input",
2843
+ value: name,
2844
+ placeholder: c.namePlaceholder,
2845
+ maxLength: 64,
2846
+ autoFocus: true,
2847
+ disabled: busy,
2848
+ onChange: function(event) {
2849
+ setName(event.target.value);
2850
+ }
2851
+ }),
2852
+ editing === "create" && activeId ? h(
2853
+ "select",
2854
+ { className: prefix + "-cfg-schemes-select", value: from, disabled: busy, onChange: function(event) {
2855
+ setFrom(event.target.value);
2856
+ }, "aria-label": c.createTitle },
2857
+ h("option", { value: "current" }, c.fromCurrent),
2858
+ h("option", { value: "default" }, c.fromDefault)
2859
+ ) : null,
2860
+ h("button", { className: prefix + "-cfg-sbtn", type: "submit", disabled: busy || !String(name || "").trim() }, c.ok),
2861
+ h("button", { className: prefix + "-cfg-sbtn", type: "button", disabled: busy, onClick: function() {
2862
+ setEditing(null);
2863
+ } }, c.cancel)
2864
+ ) : h(
2865
+ "div",
2866
+ { className: prefix + "-cfg-schemes-actions" },
2867
+ h("button", { className: prefix + "-cfg-sbtn", type: "button", disabled: busy, onClick: function() {
2868
+ begin("create");
2869
+ } }, c.create),
2870
+ activeId ? h("button", { className: prefix + "-cfg-sbtn", type: "button", disabled: busy, onClick: function() {
2871
+ begin("rename");
2872
+ } }, c.rename) : null,
2873
+ activeId ? h("button", { className: prefix + "-cfg-sbtn danger", type: "button", disabled: busy, onClick: remove }, c.remove) : null
2874
+ ),
2875
+ h("div", { className: prefix + "-cfg-schemes-hint" }, activeId ? c.syncedHint : c.defaultHint)
2876
+ );
2877
+ }
2878
+ module2.exports = { SchemeBar, styles, COPY };
2879
+ }
2880
+ });
2881
+
2718
2882
  // packages/addons/tasks/client/views/config.cjs
2719
2883
  var require_config = __commonJS({
2720
2884
  "packages/addons/tasks/client/views/config.cjs"(exports2, module2) {
@@ -2722,12 +2886,13 @@ var require_config = __commonJS({
2722
2886
  var h = React.createElement;
2723
2887
  var SW = require_switch();
2724
2888
  var NF = require_number_field();
2889
+ var SB = require_config_scheme_bar();
2725
2890
  var DOMAIN = "tasks";
2726
2891
  var PREFIX = "wbt";
2727
2892
  var COPY = {
2728
2893
  zh: {
2729
2894
  title: "\u914D\u7F6E",
2730
- note: "\u8FD9\u4E9B\u504F\u597D\u53EA\u4FDD\u5B58\u5728\u672C\u673A\uFF0C\u4E0D\u53C2\u4E0E\u4E91\u7AEF\u540C\u6B65",
2895
+ note: "\u914D\u7F6E\u65B9\u6848\u4F1A\u968F\u4E91\u7AEF\u540C\u6B65\u4E0E\u5907\u4EFD\uFF1B\u5F53\u524D\u9009\u4E2D\u54EA\u4E2A\u65B9\u6848\u53EA\u4FDD\u5B58\u5728\u672C\u673A",
2731
2896
  loading: "\u8BFB\u53D6\u914D\u7F6E\u4E2D\u2026",
2732
2897
  loadFail: "\u914D\u7F6E\u670D\u52A1\u672A\u5C31\u7EEA\uFF08\u63D2\u4EF6\u7248\u672C\u8FC7\u65E7\uFF1F\uFF09",
2733
2898
  retry: "\u91CD\u8BD5",
@@ -2749,7 +2914,7 @@ var require_config = __commonJS({
2749
2914
  },
2750
2915
  en: {
2751
2916
  title: "Config",
2752
- note: "Preferences stay on this machine; they are not synced to the cloud",
2917
+ note: "Schemes sync to the cloud and travel with backups; which scheme this device uses stays local",
2753
2918
  loading: "Loading config\u2026",
2754
2919
  loadFail: "Config service not ready (plugin too old?)",
2755
2920
  retry: "Retry",
@@ -2821,7 +2986,7 @@ var require_config = __commonJS({
2821
2986
  ".__P__-cfg-error{display:flex;align-items:center;gap:10px;flex-wrap:wrap;border:1px solid color-mix(in oklab,var(--dsw-alias-state-error-primary,currentColor) 32%,var(--cfg-line));border-radius:10px;padding:10px 13px;color:var(--dsw-alias-state-error-primary,inherit);font-size:12.5px;line-height:1.5}",
2822
2987
  "@media (max-width:820px){.__P__-cfg-row{grid-template-columns:1fr;gap:10px;padding:13px 16px}.__P__-cfg-control{justify-content:flex-start}.__P__-cfg-multi{justify-content:flex-start}.__P__-cfg-gtitle{padding:11px 16px}}",
2823
2988
  "@media (prefers-reduced-motion:reduce){.__P__-cfg-sk{animation:none;transition:none}}"
2824
- ].concat(SW.styles).concat(NF.styles).join("\n").split("__P__").join(PREFIX);
2989
+ ].concat(SW.styles).concat(NF.styles).concat(SB.styles).join("\n").split("__P__").join(PREFIX);
2825
2990
  function injectCss() {
2826
2991
  if (typeof document === "undefined") return;
2827
2992
  var old = document.getElementById("wb-addon-" + DOMAIN + "-config-css");
@@ -2978,6 +3143,24 @@ var require_config = __commonJS({
2978
3143
  setNotice({ id, text: t.saveFail + String(e && e.message || e), error: true });
2979
3144
  }).finally(release);
2980
3145
  }
3146
+ function schemeCall(action, args) {
3147
+ if (!acquire()) return;
3148
+ setBusy(true);
3149
+ setNotice({ id: "", text: t.saving, error: false });
3150
+ return Promise.resolve().then(function() {
3151
+ return api.call(DOMAIN + "." + action, args || {});
3152
+ }).then(function(r) {
3153
+ var body2 = unwrap(r);
3154
+ if (r && r.ok === false) throw new Error(r && r.error || "scheme failed");
3155
+ if (mounted.current && body2 && body2.state) setState(body2.state);
3156
+ setNotice({ id: "", text: t.saved, error: false });
3157
+ }).catch(function(e) {
3158
+ setNotice({ id: "", text: t.saveFail + String(e && e.message || e), error: true });
3159
+ }).finally(function() {
3160
+ setBusy(false);
3161
+ release();
3162
+ });
3163
+ }
2981
3164
  function resetItem(id) {
2982
3165
  if (!acquire()) return;
2983
3166
  var prevState = state;
@@ -3118,6 +3301,26 @@ var require_config = __commonJS({
3118
3301
  ));
3119
3302
  }
3120
3303
  if (state) {
3304
+ body.push(h(SB.SchemeBar, {
3305
+ key: "schemes",
3306
+ lang,
3307
+ prefix: PREFIX,
3308
+ schemes: state.schemes || [],
3309
+ activeId: state.scheme ? state.scheme.id : "",
3310
+ busy,
3311
+ onActivate: function(id) {
3312
+ schemeCall("configSchemeActivate", { id: id || "" });
3313
+ },
3314
+ onCreate: function(name, from) {
3315
+ schemeCall("configSchemeCreate", { name, from: from || "current" });
3316
+ },
3317
+ onRename: function(id, name) {
3318
+ schemeCall("configSchemeRename", { id, name });
3319
+ },
3320
+ onDelete: function(id) {
3321
+ schemeCall("configSchemeDelete", { id });
3322
+ }
3323
+ }));
3121
3324
  var items = state.items || [];
3122
3325
  var groups = state.groups || [];
3123
3326
  var byGroup = {};
@@ -14961,7 +15164,7 @@ var require_timeline_view = __commonJS({
14961
15164
  var TimelineView = require_timeline_view()({ React, COPY, injectCss, statusInfo, configView, CFG_FALLBACK, TaskItem, EditModal, DoneModal, TimelineModal, CreateModal });
14962
15165
  host.registerAddon({
14963
15166
  id: "wb-addon-tasks",
14964
- version: "5.2.47",
15167
+ version: "5.3.0",
14965
15168
  icon: ICONS["list-checks"],
14966
15169
  // 贡献声明写在 registerAddon 里:卸载任务插件时宿主按 addonId 一起撤销,
14967
15170
  // 不会在邮件里留一个渲染即报错的孤儿按钮。
@@ -9,7 +9,7 @@
9
9
  import { logChange, tableExists, tx } from "../db/db.js";
10
10
  import { LEGACY_TYPE_TAGS, addTaskTagByName, tagsAvailable } from "../tagging/tagging.js";
11
11
 
12
- const SCHEMA_VERSION = 5;
12
+ const SCHEMA_VERSION = 6;
13
13
 
14
14
  function asText(value, fallback) {
15
15
  if (value === undefined || value === null) return fallback === undefined ? "" : fallback;
@@ -97,6 +97,18 @@ function workspaceLinkValues(row) {
97
97
  return [expected, workspaceId, taskId, ...stamps(row)];
98
98
  }
99
99
 
100
+ function schemeValues(row) {
101
+ const id = identity(row.id, "task_config_scheme.id");
102
+ const name = asText(row.name).trim();
103
+ if (!name) throw new Error("配置方案名称为空:" + id);
104
+ let data = row.data;
105
+ if (typeof data === "string") {
106
+ try { data = JSON.parse(data); } catch { throw new Error("配置方案 JSON 损坏:" + id); }
107
+ }
108
+ if (!data || typeof data !== "object" || Array.isArray(data)) throw new Error("配置方案内容不是对象:" + id);
109
+ return [id, name, JSON.stringify(data), ...stamps(row)];
110
+ }
111
+
100
112
  const DEFINITIONS = [
101
113
  ["task_node", "taskNodes", "id,name,description,type,parent_id,path,sort,depth,created_at,updated_at,deleted_at,rev", nodeValues],
102
114
  ["task_timeline", "taskTimelines", "id,title,description,created_at,updated_at,deleted_at,rev", timelineValues],
@@ -104,6 +116,7 @@ const DEFINITIONS = [
104
116
  ["task_timeline_link", "taskTimelineLinks", "id,task_id,timeline_id,created_at,updated_at,deleted_at,rev", linkValues],
105
117
  ["task_workspace", "taskWorkspaces", "id,title,name,description,dir,created_at,updated_at,deleted_at,rev", workspaceValues],
106
118
  ["task_workspace_link", "taskWorkspaceLinks", "id,workspace_id,task_id,created_at,updated_at,deleted_at,rev", workspaceLinkValues],
119
+ ["task_config_scheme", "configSchemes", "id,name,data,created_at,updated_at,deleted_at,rev", schemeValues],
107
120
  ];
108
121
 
109
122
  function identity(value, field) {
@@ -138,14 +151,15 @@ export function exportTasksData(api) {
138
151
  }
139
152
 
140
153
  function prepareBackup(data) {
141
- if (!data || ![1, 2, 3, 4, 5].includes(Number(data.schemaVersion))) throw new Error("任务备份版本不兼容:需要 1、2、3、4 或 5");
154
+ if (!data || ![1, 2, 3, 4, 5, 6].includes(Number(data.schemaVersion))) throw new Error("任务备份版本不兼容:需要 1 到 6");
142
155
  const droppedDeps = !Array.isArray(data.taskDeps) ? 0 : data.taskDeps.length;
143
156
  const legacy = Number(data.schemaVersion) === 1;
144
157
  const prepared = [];
145
158
  for (const [table, key, columnText, convert] of DEFINITIONS) {
146
159
  if (legacy && (table === "task_timeline" || table === "task_timeline_link")) continue;
147
- // 5 之前的备份没有工作区两张表,缺键按空数组处理,不阻断恢复。
148
- const rows = key === "taskDeps" ? [] : (data[key] === undefined && table === "task_workspace" || data[key] === undefined && table === "task_workspace_link" ? [] : data[key]);
160
+ // 旧版备份没有后来新增的表(工作区两张、配置方案),缺键按空数组处理,不阻断恢复。
161
+ const optional = table === "task_workspace" || table === "task_workspace_link" || table === "task_config_scheme";
162
+ const rows = key === "taskDeps" ? [] : (data[key] === undefined && optional ? [] : data[key]);
149
163
  if (!Array.isArray(rows)) throw new Error("任务备份结构不完整:" + key);
150
164
  const columns = columnText.split(",");
151
165
  const ids = new Set();
@@ -1,16 +1,22 @@
1
1
  /**
2
- * 任务插件的配置项:白名单 schema + 本地读写。
2
+ * 任务插件的配置项:白名单 schema + 配置方案读写。
3
3
  *
4
- * 配置只存覆盖项,默认值留在代码里;存放位置是已有的 meta 表
5
- * (键 `config.tasks.<id>`),因此不建新表、不参与云端同步,也不进入备份。
4
+ * 每个设备可以「不使用方案」(等于插件默认值),也可以从配置方案表
5
+ * `task_config_scheme` 里选一个方案。方案只存覆盖项(默认值留在代码里),
6
+ * 参与云端同步与备份;本机当前选中哪个方案只写 meta
7
+ * (`configScheme.tasks.active`),不同步、不进备份,所以每台设备可以各选各的。
6
8
  * 读取永远宽松:表缺失/JSON 坏/取值越界都回退默认值并给出 warnings;
7
- * 只有 configSet 会抛错(带 id 与原因)。
9
+ * 只有写入会抛错(带 id 与原因)。旧版本直接写在 meta 里的覆盖项会在首次
10
+ * 加载时自动收进「本机原有配置」方案,避免升级后设置丢失。
8
11
  */
9
12
 
10
- import { tableExists, tx } from "../db/db.js";
13
+ import { logChange, tableExists, tx } from "../db/db.js";
11
14
 
12
15
  export const DOMAIN = "tasks";
13
16
  export const SCHEMA_VERSION = 1;
17
+ export const SCHEME_TABLE = "task_config_scheme";
18
+ const ACTIVE_KEY = "configScheme.tasks.active";
19
+ const DEFAULT_SCHEME_NAME = "本机配置";
14
20
 
15
21
  const GROUPS = [
16
22
  { id: "ui", zh: "界面", en: "Interface" },
@@ -230,6 +236,159 @@ function metaRows(db) {
230
236
  }
231
237
  }
232
238
 
239
+ function metaGet(db, key) {
240
+ try {
241
+ const row = db.prepare("SELECT v FROM meta WHERE k=?").get(String(key));
242
+ return row ? String(row.v) : "";
243
+ } catch (e) {
244
+ return "";
245
+ }
246
+ }
247
+
248
+ function ensureSchemeTable(db) {
249
+ db.exec(
250
+ "CREATE TABLE IF NOT EXISTS " + SCHEME_TABLE + " (" +
251
+ "id TEXT PRIMARY KEY, name TEXT NOT NULL, data TEXT NOT NULL, " +
252
+ "created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, " +
253
+ "deleted_at INTEGER, rev INTEGER NOT NULL DEFAULT 1)"
254
+ );
255
+ }
256
+
257
+ function newSchemeId() {
258
+ return "cs_" + Date.now().toString(36) + Math.random().toString(36).slice(2, 8);
259
+ }
260
+
261
+ function activeSchemeId(db) {
262
+ return metaGet(db, ACTIVE_KEY);
263
+ }
264
+
265
+ function schemeRow(db, id) {
266
+ if (!id || !tableExists(db, SCHEME_TABLE)) return null;
267
+ return db.prepare("SELECT * FROM " + SCHEME_TABLE + " WHERE id=? AND deleted_at IS NULL").get(String(id)) || null;
268
+ }
269
+
270
+ function listSchemes(db) {
271
+ if (!tableExists(db, SCHEME_TABLE)) return [];
272
+ return db
273
+ .prepare("SELECT id,name,created_at,updated_at,rev FROM " + SCHEME_TABLE + " WHERE deleted_at IS NULL ORDER BY created_at ASC, id ASC")
274
+ .all()
275
+ .map(function (row) {
276
+ return { id: String(row.id), name: String(row.name), createdAt: row.created_at, updatedAt: row.updated_at, rev: row.rev };
277
+ });
278
+ }
279
+
280
+ function normalizeSchemeName(name) {
281
+ const text = String(name === undefined || name === null ? "" : name).trim();
282
+ if (!text) throw new Error("方案名称不能为空");
283
+ if (text.length > 64) throw new Error("方案名称不能超过 64 个字符");
284
+ return text;
285
+ }
286
+
287
+ /** 解析方案 data 列:坏 JSON/未知项/越界值都只记 warning,不让整份配置不可用。 */
288
+ function parseSchemeData(row) {
289
+ const data = Object.create(null);
290
+ const warnings = [];
291
+ let raw;
292
+ try {
293
+ raw = JSON.parse(String((row && row.data) || "{}"));
294
+ } catch (e) {
295
+ warnings.push({ code: "scheme_bad_json", message: "配置方案的 JSON 损坏,已回退默认值" });
296
+ return { data: data, warnings: warnings };
297
+ }
298
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
299
+ warnings.push({ code: "scheme_bad_shape", message: "配置方案内容不是对象,已回退默认值" });
300
+ return { data: data, warnings: warnings };
301
+ }
302
+ for (const id of Object.keys(raw)) {
303
+ const item = BY_ID[id];
304
+ if (!item) {
305
+ warnings.push({ code: "config_unknown_key", id: id, message: "方案里的未知配置项已忽略:" + id });
306
+ continue;
307
+ }
308
+ try {
309
+ data[id] = coerceValue(item, raw[id]);
310
+ } catch (e) {
311
+ warnings.push({ code: "config_invalid", id: id, message: String((e && e.message) || e) + ",已回退默认" });
312
+ }
313
+ }
314
+ return { data: data, warnings: warnings };
315
+ }
316
+
317
+ function logScheme(db, id, origin, ts) {
318
+ if (!tableExists(db, "sync_change_log")) return;
319
+ const row = db.prepare("SELECT * FROM " + SCHEME_TABLE + " WHERE id=?").get(String(id));
320
+ if (!row) return;
321
+ logChange(db, SCHEME_TABLE, id, row.deleted_at === null || row.deleted_at === undefined ? "upsert" : "delete", row, String(origin || "ui"), Number(ts) || Date.now());
322
+ }
323
+
324
+ function insertScheme(db, id, name, data, ts) {
325
+ db.prepare(
326
+ "INSERT INTO " + SCHEME_TABLE + " (id,name,data,created_at,updated_at,deleted_at,rev) VALUES(?,?,?,?,?,NULL,1)"
327
+ ).run(String(id), String(name), JSON.stringify(data || {}), ts, ts);
328
+ }
329
+
330
+ function writeSchemeData(db, id, data, ts) {
331
+ db.prepare("UPDATE " + SCHEME_TABLE + " SET data=?, updated_at=?, rev=rev+1 WHERE id=? AND deleted_at IS NULL").run(
332
+ JSON.stringify(data || {}),
333
+ ts,
334
+ String(id)
335
+ );
336
+ }
337
+
338
+ function createScheme(db, name, data) {
339
+ const ts = Date.now();
340
+ const id = newSchemeId();
341
+ tx(db, function () {
342
+ insertScheme(db, id, normalizeSchemeName(name), data, ts);
343
+ metaSet(db, ACTIVE_KEY, id);
344
+ });
345
+ return id;
346
+ }
347
+
348
+ function ensureActiveScheme(db, name) {
349
+ const active = activeSchemeId(db);
350
+ if (active && schemeRow(db, active)) return active;
351
+ return createScheme(db, name || DEFAULT_SCHEME_NAME, {});
352
+ }
353
+
354
+ /**
355
+ * 建表 + 旧数据迁移:旧版本把覆盖项直接写在 meta 的 `config.tasks.<id>` 里,
356
+ * 升级后没有选中方案时把这些值收进「本机原有配置」并设为当前方案,避免用户设置丢失。
357
+ * 没改过配置的设备不会创建任何方案。
358
+ */
359
+ function ensureConfigSchemes(db) {
360
+ ensureMeta(db);
361
+ ensureSchemeTable(db);
362
+ const active = activeSchemeId(db);
363
+ if (active) {
364
+ if (schemeRow(db, active)) return active;
365
+ metaDelete(db, ACTIVE_KEY);
366
+ }
367
+ const overrides = Object.create(null);
368
+ const legacyKeys = [];
369
+ const prefix = "config." + DOMAIN + ".";
370
+ for (const row of metaRows(db)) {
371
+ const id = String(row.k).slice(prefix.length);
372
+ const item = BY_ID[id];
373
+ if (!item) continue;
374
+ try {
375
+ overrides[id] = coerceValue(item, JSON.parse(String(row.v)));
376
+ legacyKeys.push(String(row.k));
377
+ } catch (e) {
378
+ // 坏值继续留在 meta 里等人工处理,不阻塞迁移。
379
+ }
380
+ }
381
+ if (!legacyKeys.length) return "";
382
+ const id = newSchemeId();
383
+ const ts = Date.now();
384
+ tx(db, function () {
385
+ insertScheme(db, id, "本机原有配置", overrides, ts);
386
+ metaSet(db, ACTIVE_KEY, id);
387
+ for (const key of legacyKeys) metaDelete(db, key);
388
+ });
389
+ return id;
390
+ }
391
+
233
392
  function optionsOf(item) {
234
393
  return (item.values || []).map(function (o) {
235
394
  return { value: o.value, zh: o.zh, en: o.en };
@@ -296,7 +455,7 @@ export function coerceValue(item, raw) {
296
455
  }
297
456
 
298
457
  /**
299
- * 读取配置:返回覆盖项 + 合并默认值后的值 + warnings。
458
+ * 读取配置:默认值 ← 当前方案的覆盖项,再附 warnings。
300
459
  * 坏 JSON、非法值、未知/已废弃 id 都不会抛错。
301
460
  */
302
461
  export function loadConfig(db) {
@@ -304,37 +463,26 @@ export function loadConfig(db) {
304
463
  const values = Object.create(null);
305
464
  const warnings = [];
306
465
  for (const item of ITEMS) values[item.id] = item.default;
307
- let rows = [];
308
- try {
309
- rows = metaRows(db);
310
- } catch (e) {
311
- warnings.push({ code: "config_unreadable", message: "配置表不可读,使用默认值" });
312
- rows = [];
466
+ const active = activeSchemeId(db);
467
+ if (!active) return { values: values, overrides: overrides, warnings: warnings, scheme: null };
468
+ const row = schemeRow(db, active);
469
+ if (!row) {
470
+ // 方案被其他设备删掉后,本机指针悬空:清掉指针,回到默认配置。
471
+ metaDelete(db, ACTIVE_KEY);
472
+ return { values: values, overrides: overrides, warnings: warnings, scheme: null };
313
473
  }
314
- const prefix = "config." + DOMAIN + ".";
315
- for (const row of rows) {
316
- const id = String(row.k).slice(prefix.length);
317
- const item = BY_ID[id];
318
- if (!item) {
319
- warnings.push({ code: "config_unknown_key", id: id, message: "未知配置项已忽略:" + id });
320
- continue;
321
- }
322
- let parsed;
323
- try {
324
- parsed = JSON.parse(String(row.v));
325
- } catch (e) {
326
- warnings.push({ code: "config_bad_json", id: id, message: "配置值不是合法 JSON,已回退默认:" + id });
327
- continue;
328
- }
329
- try {
330
- const v = coerceValue(item, parsed);
331
- overrides[id] = v;
332
- values[id] = v;
333
- } catch (e) {
334
- warnings.push({ code: "config_invalid", id: id, message: String((e && e.message) || e) + ",已回退默认" });
335
- }
474
+ const parsed = parseSchemeData(row);
475
+ for (const id of Object.keys(parsed.data)) {
476
+ overrides[id] = parsed.data[id];
477
+ values[id] = parsed.data[id];
336
478
  }
337
- return { values: values, overrides: overrides, warnings: warnings };
479
+ for (const warning of parsed.warnings) warnings.push(warning);
480
+ return {
481
+ values: values,
482
+ overrides: overrides,
483
+ warnings: warnings,
484
+ scheme: { id: String(row.id), name: String(row.name), updatedAt: row.updated_at },
485
+ };
338
486
  }
339
487
 
340
488
  /** 服务端只关心值。 */
@@ -352,7 +500,9 @@ export function configState(db) {
352
500
  values: loaded.values,
353
501
  overrides: loaded.overrides,
354
502
  warnings: loaded.warnings,
355
- table: "meta",
503
+ table: SCHEME_TABLE,
504
+ scheme: loaded.scheme,
505
+ schemes: listSchemes(db),
356
506
  };
357
507
  }
358
508
 
@@ -367,15 +517,22 @@ function writeValues(db, raw, origin) {
367
517
  prepared.push({ id: id, value: coerceValue(item, raw[id]) });
368
518
  }
369
519
  const ts = Date.now();
370
- tx(db, function () {
371
- for (const row of prepared) metaSet(db, keyOf(row.id), JSON.stringify(row.value));
372
- });
520
+ // 没有选中方案时,第一次写入会自动建一个「本机配置」方案并切过去,
521
+ // 既保留 agent 直接调用 configSet 的旧用法,也不会让默认值本身被偷偷改掉。
522
+ const schemeId = ensureActiveScheme(db, DEFAULT_SCHEME_NAME);
523
+ const row = schemeRow(db, schemeId);
524
+ if (!row) throw new Error("配置方案不存在,请重新选择方案");
525
+ const data = parseSchemeData(row).data;
526
+ for (const item of prepared) data[item.id] = item.value;
527
+ writeSchemeData(db, schemeId, data, ts);
528
+ logScheme(db, schemeId, origin, ts);
373
529
  return {
374
530
  changed: prepared.map(function (r) { return r.id; }),
375
531
  values: (function () { const v = Object.create(null); for (const p of prepared) v[p.id] = p.value; return v; })(),
376
532
  state: configState(db),
377
533
  updatedAt: ts,
378
534
  origin: String(origin || "ui"),
535
+ schemeId: schemeId,
379
536
  };
380
537
  }
381
538
 
@@ -387,10 +544,63 @@ function resetValues(db, ids) {
387
544
  for (const id of targets) {
388
545
  if (!BY_ID[id]) throw new Error("未知配置项:" + id);
389
546
  }
547
+ const active = activeSchemeId(db);
548
+ const row = active ? schemeRow(db, active) : null;
549
+ if (!row) return { reset: targets, state: configState(db) };
550
+ const data = parseSchemeData(row).data;
551
+ for (const id of targets) delete data[id];
552
+ const ts = Date.now();
553
+ writeSchemeData(db, active, data, ts);
554
+ logScheme(db, active, "ui", ts);
555
+ return { reset: targets, state: configState(db) };
556
+ }
557
+
558
+ function schemeCreate(db, name, from, origin) {
559
+ const source = from === "default" ? {} : loadConfig(db).overrides;
560
+ const id = createScheme(db, name, source, origin);
561
+ return { id: id, state: configState(db) };
562
+ }
563
+
564
+ function schemeRename(db, id, name) {
565
+ if (!id) throw new Error("缺少方案 id");
566
+ const row = schemeRow(db, id);
567
+ if (!row) throw new Error("配置方案不存在:" + id);
568
+ const ts = Date.now();
569
+ db.prepare("UPDATE " + SCHEME_TABLE + " SET name=?, updated_at=?, rev=rev+1 WHERE id=?").run(
570
+ normalizeSchemeName(name),
571
+ ts,
572
+ String(id)
573
+ );
574
+ logScheme(db, id, "ui", ts);
575
+ return { id: String(id), state: configState(db) };
576
+ }
577
+
578
+ function schemeDelete(db, id) {
579
+ if (!id) throw new Error("缺少方案 id");
580
+ const row = schemeRow(db, id);
581
+ if (!row) throw new Error("配置方案不存在:" + id);
582
+ const ts = Date.now();
390
583
  tx(db, function () {
391
- for (const id of targets) metaDelete(db, keyOf(id));
584
+ db.prepare("UPDATE " + SCHEME_TABLE + " SET deleted_at=?, updated_at=?, rev=rev+1 WHERE id=?").run(
585
+ ts,
586
+ ts,
587
+ String(id)
588
+ );
589
+ if (activeSchemeId(db) === String(id)) metaDelete(db, ACTIVE_KEY);
590
+ logScheme(db, id, "ui", ts);
392
591
  });
393
- return { reset: targets, state: configState(db) };
592
+ return { id: String(id), state: configState(db) };
593
+ }
594
+
595
+ function schemeActivate(db, id) {
596
+ if (id === undefined || id === null || id === "") {
597
+ metaDelete(db, ACTIVE_KEY);
598
+ return { id: null, state: configState(db) };
599
+ }
600
+ const row = schemeRow(db, id);
601
+ if (!row) throw new Error("配置方案不存在:" + id);
602
+ metaSet(db, ACTIVE_KEY, String(id));
603
+ return { id: String(id), state: configState(db) };
394
604
  }
395
605
 
396
606
  function renderItems(items) {
@@ -407,10 +617,10 @@ function configItemSchema() {
407
617
 
408
618
  export function registerConfigActions(api) {
409
619
  const db = api.db;
410
- ensureMeta(db);
620
+ ensureConfigSchemes(db);
411
621
 
412
622
  api.action("tasks.configGet", {
413
- description: "读取任务插件的全部配置项(含默认值、覆盖值、校验告警);配置只存本地 meta 表,不参与云端同步",
623
+ description: "读取任务插件的配置项、当前配置方案与全部方案;方案参与云端同步与备份,当前选中哪个方案只在本机",
414
624
  input: { type: "object", properties: {} },
415
625
  execute: function () { return configState(db); },
416
626
  render: function (s) {
@@ -422,7 +632,7 @@ export function registerConfigActions(api) {
422
632
  });
423
633
 
424
634
  api.action("tasks.configSet", {
425
- description: "保存一项或多项配置(白名单校验,整批原子);值非法会报错并指出配置 id",
635
+ description: "保存一项或多项配置到当前方案(白名单校验,整批原子);未选方案时自动新建「本机配置」方案",
426
636
  input: { type: "object", properties: { values: configItemSchema() }, required: ["values"] },
427
637
  examples: [{ values: { "list.pageSize": 24, "stats.range": "90d" } }],
428
638
  execute: function (args, ctx) {
@@ -433,7 +643,7 @@ export function registerConfigActions(api) {
433
643
  });
434
644
 
435
645
  api.action("tasks.configReset", {
436
- description: "删除配置覆盖项,恢复默认值;ids 省略时重置全部",
646
+ description: "从当前方案里删除配置覆盖项(回到插件默认值);未选方案时不做任何修改",
437
647
  input: {
438
648
  type: "object",
439
649
  properties: { ids: { type: "array", items: { type: "string" }, description: "要重置的配置 id" } },
@@ -442,6 +652,45 @@ export function registerConfigActions(api) {
442
652
  render: function (r) { return "已恢复默认:" + renderItems(r.reset); },
443
653
  });
444
654
 
655
+ api.action("tasks.configSchemeCreate", {
656
+ description: "新建配置方案并立即切换过去;from=current 复制当前值,from=default 从插件默认值开始",
657
+ input: {
658
+ type: "object",
659
+ properties: {
660
+ name: { type: "string", description: "方案名称,1–64 个字符" },
661
+ from: { type: "string", enum: ["current", "default"], description: "初始内容来源,默认 current" },
662
+ },
663
+ required: ["name"],
664
+ },
665
+ execute: function (args, context) { return schemeCreate(db, args && args.name, args && args.from, context && context.origin); },
666
+ render: function (r) { return "已新建并启用配置方案:" + r.state.scheme.name; },
667
+ });
668
+
669
+ api.action("tasks.configSchemeRename", {
670
+ description: "重命名配置方案",
671
+ input: {
672
+ type: "object",
673
+ properties: { id: { type: "string" }, name: { type: "string" } },
674
+ required: ["id", "name"],
675
+ },
676
+ execute: function (args) { return schemeRename(db, args && args.id, args && args.name); },
677
+ render: function (r) { return "配置方案已改名:" + r.state.scheme.name; },
678
+ });
679
+
680
+ api.action("tasks.configSchemeDelete", {
681
+ description: "软删除配置方案;删除的是当前方案时回到「不使用方案」",
682
+ input: { type: "object", properties: { id: { type: "string" } }, required: ["id"] },
683
+ execute: function (args) { return schemeDelete(db, args && args.id); },
684
+ render: function () { return "配置方案已删除"; },
685
+ });
686
+
687
+ api.action("tasks.configSchemeActivate", {
688
+ description: "切换当前设备的配置方案;id 传空表示不使用方案、回到插件默认配置",
689
+ input: { type: "object", properties: { id: { type: "string", description: "方案 id;省略或空串表示不使用方案" } } },
690
+ execute: function (args) { return schemeActivate(db, args && args.id); },
691
+ render: function (r) { return r.id ? "已切换配置方案" : "已改为不使用方案(插件默认配置)"; },
692
+ });
693
+
445
694
  api.action("tasks.configOptions", {
446
695
  description: "列出可配置项、类型与默认值(给 agent 查 id 用)",
447
696
  input: { type: "object", properties: {} },
package/lib/infra/sync.js CHANGED
@@ -185,5 +185,7 @@ export const taskSync = {
185
185
  { name: "task_workspace", mode: "row" },
186
186
  { name: "task_workspace_link", mode: "row", normalizeIncoming: normalizeWorkspaceLink },
187
187
  { name: "task_node", mode: "tree", reconcile: reconcileTaskTree },
188
+ // 配置方案参与同步:只同步方案行,本机选哪个方案由 meta 指针决定,不跨设备。
189
+ { name: "task_config_scheme", mode: "row" },
188
190
  ],
189
191
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-workbench-tasks",
3
- "version": "5.2.47",
3
+ "version": "5.3.0",
4
4
  "description": "个人工作台任务插件:任务树、优先级、截止日、时间线与重启",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -17,7 +17,7 @@
17
17
  "id": "wb-addon-tasks",
18
18
  "title": "任务",
19
19
  "description": "任务树 + 统计/记录/时间线/工作区/配置/数据/命令/Action 八个视图:紧急/已关闭/已开启/已完成/总览,可按标签筛选,附统计报表与表数据浏览",
20
- "version": "5.2.47",
20
+ "version": "5.3.0",
21
21
  "minShell": "0.8.4",
22
22
  "tables": [
23
23
  "task",