sage-mem 2.33.4 → 3.0.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.en.md +38 -1
- package/README.md +38 -1
- package/lib/assembly/tools-mount.js +4 -1
- package/lib/autodream/tools.js +5 -0
- package/lib/client.js +674 -0
- package/lib/index.js +89 -3
- package/lib/memory/conflicts.js +53 -4
- package/lib/shared/codes.js +28 -0
- package/lib/sync/base.js +124 -0
- package/lib/sync/gate.js +303 -0
- package/lib/sync/instance.js +138 -0
- package/lib/sync/policy.js +212 -0
- package/lib/sync/pull.js +6 -0
- package/lib/sync/push.js +522 -0
- package/lib/sync/remote.js +139 -10
- package/lib/sync/resolve.js +358 -0
- package/lib/sync/upload.js +372 -0
- package/lib/tools/sync-tool.js +127 -1
- package/lib/typert/schemas.js +38 -0
- package/lib/typert.host.js +16 -0
- package/package.json +1 -1
package/lib/client.js
CHANGED
|
@@ -1952,6 +1952,43 @@ const StarmapRuntime = (function (require, _react) {
|
|
|
1952
1952
|
return v;
|
|
1953
1953
|
},
|
|
1954
1954
|
};
|
|
1955
|
+
// ── 3.0.0:跨设备裁决(applyResolution)──
|
|
1956
|
+
// 入参 `{id, choice}`:**正好两键**。多传的键宿主 strict 会剥掉,但界面那一层
|
|
1957
|
+
// 「以为它生效了」最难查 —— 所以在这里就拒。`choice` 只校字符串、**不校枚举**:
|
|
1958
|
+
// 客户端只负责传,合法取值是宿主 schema(z.enum(RESOLVE_CHOICES))的事,
|
|
1959
|
+
// 客户端再抄一份枚举,多出来的那一种裁法就会在面板上永远点不动。
|
|
1960
|
+
const conflictApplyOptsSchema = {
|
|
1961
|
+
parse(v) {
|
|
1962
|
+
if (v === null || typeof v !== "object" || Array.isArray(v)) throw new Error("sage-mem: expected object");
|
|
1963
|
+
const keys = Object.keys(v);
|
|
1964
|
+
if (keys.length !== 2 || !("id" in v) || !("choice" in v)) {
|
|
1965
|
+
throw new Error("sage-mem: expected exactly {id, choice}");
|
|
1966
|
+
}
|
|
1967
|
+
isStr(v.id);
|
|
1968
|
+
isStr(v.choice);
|
|
1969
|
+
return v;
|
|
1970
|
+
},
|
|
1971
|
+
};
|
|
1972
|
+
// 结果:六个「回声」字段(choice / wrote / baseUpdated / status / recordId + ok 本身)
|
|
1973
|
+
// 成功失败都在 —— 界面要靠 `wrote` 分辨「写了远端」与「一个字节都没动」。
|
|
1974
|
+
// 失败分支仍先认 `{ok:false, code, error?}` 这一支(与上面六条同一条纪律:
|
|
1975
|
+
// 混合版本环境里,旧宿主 / 半态收据不能被 strict 判成坏结果)。
|
|
1976
|
+
const conflictApplySchema = {
|
|
1977
|
+
parse(v) {
|
|
1978
|
+
if (v === null || typeof v !== "object" || Array.isArray(v)) throw new Error("sage-mem: invalid conflict result");
|
|
1979
|
+
if (conflictFailure(v)) return v;
|
|
1980
|
+
isBool(v.ok);
|
|
1981
|
+
isStr(v.choice);
|
|
1982
|
+
isBool(v.baseUpdated);
|
|
1983
|
+
isStr(v.status);
|
|
1984
|
+
isStr(v.recordId);
|
|
1985
|
+
if (v.wrote !== "remote" && v.wrote !== "local" && v.wrote !== "none") {
|
|
1986
|
+
throw new Error("sage-mem: invalid wrote");
|
|
1987
|
+
}
|
|
1988
|
+
if ("baseError" in v) isStr(v.baseError);
|
|
1989
|
+
return v;
|
|
1990
|
+
},
|
|
1991
|
+
};
|
|
1955
1992
|
// 5 个方法都是「一个对象参数」:{id} / {id,file?,note?}。可选字段传了就必须是字符串,
|
|
1956
1993
|
// 没传就交给宿主按空串处理 —— 不在这里编默认值。
|
|
1957
1994
|
const conflictOptsSchema = {
|
|
@@ -2443,6 +2480,7 @@ const StarmapRuntime = (function (require, _react) {
|
|
|
2443
2480
|
// (宿主 remote 清单是构建期产物),不在这里声明 `ctx.get("remote.conflict")`
|
|
2444
2481
|
// 就永远是 undefined —— 界面表现为「一整个页签消失」。
|
|
2445
2482
|
// 6 条都在同一个 `conflict` 面上:列表 / 读正文 / 标失效 / 标记已裁决 / 撤销 / 删记录。
|
|
2483
|
+
// 3.0.0 再加第 7 条 `applyResolution`(跨设备冲突的三种裁决),见本段末尾。
|
|
2446
2484
|
{
|
|
2447
2485
|
id: "sage-mem#conflict/list",
|
|
2448
2486
|
service: "conflict",
|
|
@@ -2521,6 +2559,20 @@ const StarmapRuntime = (function (require, _react) {
|
|
|
2521
2559
|
result: { mode: "strict", typeSymbol: "sage-mem/types#ConflictRemoveResult", schema: conflictRemoveSchema, create: () => conflictRemoveSchema },
|
|
2522
2560
|
sourceLocation: { "file": "lib/index.js", "line": 1, "column": 1 },
|
|
2523
2561
|
},
|
|
2562
|
+
{
|
|
2563
|
+
// 3.0.0:跨设备冲突的裁决 —— 留本地(推上去)/ 留远端(写回本地)/ 手工合并(不写)。
|
|
2564
|
+
// 这是这一面唯一**会改记忆正文**的方法,也是唯一带 `choice` 入参的方法。
|
|
2565
|
+
id: "sage-mem#conflict/applyResolution",
|
|
2566
|
+
service: "conflict",
|
|
2567
|
+
namespace: "conflict",
|
|
2568
|
+
method: "applyResolution",
|
|
2569
|
+
invocation: { kind: "direct" },
|
|
2570
|
+
parameters: [
|
|
2571
|
+
{ name: "opts", wire: "opts", source: "json", codec: { mode: "strict", typeSymbol: "sage-mem/types#ConflictApplyArgs", schema: conflictApplyOptsSchema, create: () => conflictApplyOptsSchema } },
|
|
2572
|
+
],
|
|
2573
|
+
result: { mode: "strict", typeSymbol: "sage-mem/types#ConflictApplyResult", schema: conflictApplySchema, create: () => conflictApplySchema },
|
|
2574
|
+
sourceLocation: { "file": "lib/index.js", "line": 1, "column": 1 },
|
|
2575
|
+
},
|
|
2524
2576
|
],
|
|
2525
2577
|
};
|
|
2526
2578
|
|
|
@@ -4469,6 +4521,45 @@ h("span", { key: "sep", className: "smem-actions-sep", "aria-hidden": "true" }),
|
|
|
4469
4521
|
"ext.intake.err.noRemote": "宿主侧还没有外部来件接口(插件版本较旧)",
|
|
4470
4522
|
"ext.intake.err.rejected": "处理被拒绝",
|
|
4471
4523
|
"ext.intake.err.noClipboard": "剪贴板不可用,请手动复制",
|
|
4524
|
+
// ── 3.0.0 跨设备差异(第 4 步的裁决 UI)─────────────────────
|
|
4525
|
+
// 只描述「跨设备同步」这一类记录。措辞里不出现「冲突」二字是有意的:
|
|
4526
|
+
// 这一屏列的是**同一份记忆在两台机器上各长出一版**,不是逻辑矛盾。
|
|
4527
|
+
"ext.sync.title": "跨设备差异",
|
|
4528
|
+
"ext.sync.count": "{count} 条待裁决",
|
|
4529
|
+
"ext.sync.countNone": "无待裁决",
|
|
4530
|
+
"ext.sync.empty": "没有待裁决的跨设备差异",
|
|
4531
|
+
"ext.sync.what": "跨设备同步撞车的地方:两边都改过同一个文件,或者各自新建了同名的一个。这里只列这一种记录 —— 自动整理造出来的、以及没有来源标记的老记录都不在这张卡里。",
|
|
4532
|
+
"ext.sync.refresh": "刷新",
|
|
4533
|
+
"ext.sync.path": "远端路径:{path}",
|
|
4534
|
+
"ext.sync.truncated": "记录里的正文被截断过,对比看到的是留下来的那一段",
|
|
4535
|
+
"ext.sync.diffOn": "逐行对比",
|
|
4536
|
+
"ext.sync.diffOff": "收起对比",
|
|
4537
|
+
"ext.sync.diffReading": "正在取两份正文…",
|
|
4538
|
+
"ext.sync.diffLines": "本地 {a} 行 · 远端 {b} 行",
|
|
4539
|
+
"ext.sync.diffAll": "展开全部",
|
|
4540
|
+
"ext.sync.diffFold": "只看差异",
|
|
4541
|
+
"ext.sync.skip": "…(跳过 {count} 行)",
|
|
4542
|
+
"ext.sync.same": "两份逐行相同(差别在元数据或行尾,不在正文)",
|
|
4543
|
+
"ext.sync.tooBig": "两份各 {a} / {b} 行,太大不做逐行对比;用 read_memory 分别看",
|
|
4544
|
+
"ext.sync.side.local": "本地",
|
|
4545
|
+
"ext.sync.side.remote": "远端",
|
|
4546
|
+
"ext.sync.local": "留本地",
|
|
4547
|
+
"ext.sync.remote": "留远端",
|
|
4548
|
+
"ext.sync.manual": "手工合并",
|
|
4549
|
+
"ext.sync.busy": "处理中…",
|
|
4550
|
+
"ext.sync.confirm.localTitle": "用本地这一版覆盖远端?",
|
|
4551
|
+
"ext.sync.confirm.localBody": "远端那份会被覆盖,改不回来;本地这一版不受影响。裁决后这条记录标成已裁决。",
|
|
4552
|
+
"ext.sync.confirm.remoteTitle": "用远端这一版覆盖本地?",
|
|
4553
|
+
"ext.sync.confirm.remoteBody": "本地那份会被覆盖,改不回来;这一版的正文还留在这条记录里,需要时能对照。裁决后这条记录标成已裁决。",
|
|
4554
|
+
"ext.sync.confirm.yes": "覆盖并裁决",
|
|
4555
|
+
"ext.sync.notice.local": "已把本地这一版推到远端,记录标成已裁决",
|
|
4556
|
+
"ext.sync.notice.remote": "已把远端这一版写回本地,记录标成已裁决",
|
|
4557
|
+
"ext.sync.notice.manual": "已标记,去改文件;改完跑一次检查",
|
|
4558
|
+
"ext.sync.err.list": "读不到冲突队列:{code} {error}",
|
|
4559
|
+
"ext.sync.err.read": "读不到这条记录:{code} {error}",
|
|
4560
|
+
"ext.sync.err.act": "裁决没有完成:{code} {error}",
|
|
4561
|
+
"ext.sync.err.base": "文件已经写成,但基线没更新:{error}(下次检查会把同一个冲突再造一次)",
|
|
4562
|
+
"ext.sync.err.noAct": "这个宿主没有 applyResolution(升级宿主后再试)",
|
|
4472
4563
|
"lab.title": "MMR 去重(实验性)",
|
|
4473
4564
|
"lab.switch": "启用 MMR 去重",
|
|
4474
4565
|
"lab.switch.onTitle": "现在开启:注入时优先让不同主题的记忆占名额",
|
|
@@ -5210,6 +5301,42 @@ h("span", { key: "sep", className: "smem-actions-sep", "aria-hidden": "true" }),
|
|
|
5210
5301
|
"ext.intake.err.noRemote": "The host has no incoming-submissions API yet (older plugin version)",
|
|
5211
5302
|
"ext.intake.err.rejected": "The action was rejected",
|
|
5212
5303
|
"ext.intake.err.noClipboard": "Clipboard unavailable, please copy manually",
|
|
5304
|
+
"ext.sync.title": "Cross-device differences",
|
|
5305
|
+
"ext.sync.count": "{count} waiting",
|
|
5306
|
+
"ext.sync.countNone": "None waiting",
|
|
5307
|
+
"ext.sync.empty": "No cross-device differences waiting",
|
|
5308
|
+
"ext.sync.what": "Where sync collided: both sides changed the same file, or each created one with the same name. Only those records are listed here - conflicts made by auto-organize, and old records without an origin, stay out.",
|
|
5309
|
+
"ext.sync.refresh": "Refresh",
|
|
5310
|
+
"ext.sync.path": "Remote path: {path}",
|
|
5311
|
+
"ext.sync.truncated": "The body stored in this record was truncated; the diff shows what is left",
|
|
5312
|
+
"ext.sync.diffOn": "Line diff",
|
|
5313
|
+
"ext.sync.diffOff": "Hide diff",
|
|
5314
|
+
"ext.sync.diffReading": "Reading both versions...",
|
|
5315
|
+
"ext.sync.diffLines": "local {a} lines, remote {b} lines",
|
|
5316
|
+
"ext.sync.diffAll": "Show all",
|
|
5317
|
+
"ext.sync.diffFold": "Diff only",
|
|
5318
|
+
"ext.sync.skip": "... {count} lines skipped",
|
|
5319
|
+
"ext.sync.same": "Both sides are identical line by line (they differ in metadata or line endings, not in text)",
|
|
5320
|
+
"ext.sync.tooBig": "local {a} / remote {b} lines - too large for a line diff; open each one with read_memory",
|
|
5321
|
+
"ext.sync.side.local": "local",
|
|
5322
|
+
"ext.sync.side.remote": "remote",
|
|
5323
|
+
"ext.sync.local": "Keep local",
|
|
5324
|
+
"ext.sync.remote": "Keep remote",
|
|
5325
|
+
"ext.sync.manual": "Merge by hand",
|
|
5326
|
+
"ext.sync.busy": "Working...",
|
|
5327
|
+
"ext.sync.confirm.localTitle": "Overwrite the remote copy with this local one?",
|
|
5328
|
+
"ext.sync.confirm.localBody": "The remote copy will be overwritten and cannot be restored; the local copy stays as it is. The record is then marked resolved.",
|
|
5329
|
+
"ext.sync.confirm.remoteTitle": "Overwrite the local copy with this remote one?",
|
|
5330
|
+
"ext.sync.confirm.remoteBody": "The local copy will be overwritten and cannot be restored; this version stays inside the record for reference. The record is then marked resolved.",
|
|
5331
|
+
"ext.sync.confirm.yes": "Overwrite and resolve",
|
|
5332
|
+
"ext.sync.notice.local": "Local version pushed to the remote; the record is marked resolved",
|
|
5333
|
+
"ext.sync.notice.remote": "Remote version written back locally; the record is marked resolved",
|
|
5334
|
+
"ext.sync.notice.manual": "Marked; go edit the file, then run a check",
|
|
5335
|
+
"ext.sync.err.list": "Cannot read the conflict queue: {code} {error}",
|
|
5336
|
+
"ext.sync.err.read": "Cannot read this record: {code} {error}",
|
|
5337
|
+
"ext.sync.err.act": "Resolution did not complete: {code} {error}",
|
|
5338
|
+
"ext.sync.err.base": "File written, but the baseline was not updated: {error} (the next check will raise the same conflict again)",
|
|
5339
|
+
"ext.sync.err.noAct": "This host has no applyResolution (upgrade the host and retry)",
|
|
5213
5340
|
"lab.title": "MMR de-duplication (experimental)",
|
|
5214
5341
|
"lab.switch": "Enable MMR de-duplication",
|
|
5215
5342
|
"lab.switch.onTitle": "On: injection prefers memories on different topics for the same slots",
|
|
@@ -8567,6 +8694,11 @@ return h("div", { className: "smem-external smem-stack" }, [
|
|
|
8567
8694
|
// 与主卡**平级**:以前它嵌在主卡里面,三层边框(外卡 → 分区 → 内卡)挤在一起(审计 P2-3)。
|
|
8568
8695
|
h(ExternalIntakeCard, { key: "intake", ctx: props.ctx }),
|
|
8569
8696
|
|
|
8697
|
+
// ── 3.0.0 跨设备同步:撞车的地方 ──
|
|
8698
|
+
// 与来件卡平级(同一件事的两个方向:外面的东西进不进我的库 / 我的库与别的设备
|
|
8699
|
+
// 对不上时听谁的)。卡本身在 `70-ext-sync.js` —— 它要跟 `smemLineDiff` 一起看。
|
|
8700
|
+
h(ExternalSyncCard, { key: "sync", ctx: props.ctx }),
|
|
8701
|
+
|
|
8570
8702
|
err ? h("div", { className: "smem-err", key: "err" }, "⚠ " + err) : null,
|
|
8571
8703
|
notice ? h("div", { className: "smem-ok", key: "ok" }, "✓ " + notice) : null,
|
|
8572
8704
|
]);
|
|
@@ -8777,6 +8909,548 @@ return h("div", { className: "smem-external smem-stack" }, [
|
|
|
8777
8909
|
notice ? h("div", { className: "smem-ok", key: "ok" }, "✓ " + notice) : null,
|
|
8778
8910
|
]);
|
|
8779
8911
|
}
|
|
8912
|
+
// ════════════════════════════════════════════════════════════════════════
|
|
8913
|
+
// 3.0.0 第 4 步 · 跨设备差异卡(外部访问页)+ 行级 diff 纯函数
|
|
8914
|
+
// ════════════════════════════════════════════════════════════════════════
|
|
8915
|
+
//
|
|
8916
|
+
// 这一屏**不发明任何新机制**:记录的形状 / 状态机 / 读写函数都是 `conflicts.js` 里
|
|
8917
|
+
// 跑了很久的那一套(面板「库健康 → 冲突队列」是它的第一个消费者)。这里做的是三件事:
|
|
8918
|
+
// ① 把「跨设备造成的冲突」从同一张队列里**按来源筛出来**(`origin === 'sync'`);
|
|
8919
|
+
// ② 让两份正文能**逐行对比**,而不只是摊开两段并排;
|
|
8920
|
+
// ③ 给三个裁决动作(留本地 / 留远端 / 手工合并)接上界面,语义照设计稿 §3.4。
|
|
8921
|
+
//
|
|
8922
|
+
// 为什么单独一个分片:它同时有**纯函数**(diff)与**一张卡**,而 68 只是「挂载点」。
|
|
8923
|
+
// 顺序上 68 在前、本分片在后 —— 没关系:整个 client 是一个 IIFE,所有分片在首次
|
|
8924
|
+
// 渲染之前就全部求值完,68 的渲染函数里引用 `ExternalSyncCard` 时它早就存在了。
|
|
8925
|
+
|
|
8926
|
+
// ── 行级 diff(纯函数,无 React、无 IO)────────────────────────────────
|
|
8927
|
+
|
|
8928
|
+
/** 单侧超过这个行数就不做逐行对比:只报行数,让用户各自去看(设计稿 §3.3)。 */
|
|
8929
|
+
const SMEM_DIFF_MAX_LINES = 2000;
|
|
8930
|
+
|
|
8931
|
+
/**
|
|
8932
|
+
* 剥掉公共前后缀之后,中间还剩超过这个行数就退化成「整块删 + 整块增」。
|
|
8933
|
+
*
|
|
8934
|
+
* 为什么需要这道退化:LCS 是 O(n·m)。两侧各 800 行就是 64 万个格子(Int32 表约
|
|
8935
|
+
* 2.5 MB),已经是我们愿意在面板里花的极限。退化之后仍然回答得了「哪一段两边不一样」,
|
|
8936
|
+
* 只是不再逐行对齐 —— 而 800 行以上的大段改写,逐行对齐本来也没有人看得下去。
|
|
8937
|
+
*/
|
|
8938
|
+
const SMEM_DIFF_LCS_MAX = 800;
|
|
8939
|
+
|
|
8940
|
+
/** 差异块前后各保留几行上下文;其余折成一行「跳过 N 行」。 */
|
|
8941
|
+
const SMEM_DIFF_CONTEXT = 3;
|
|
8942
|
+
|
|
8943
|
+
/**
|
|
8944
|
+
* 正文 → 行数组。
|
|
8945
|
+
*
|
|
8946
|
+
* 行尾先归一(`\r\n` 与单独 `\r` 都变 `\n`):同一份记忆在两台机器上各存过一次,
|
|
8947
|
+
* 就可能一边 CRLF 一边 LF。按行尾切会得到「每一行都不同」——那是**假差异**,
|
|
8948
|
+
* 比不做 diff 更误导人。
|
|
8949
|
+
*
|
|
8950
|
+
* 末尾那一个换行不算多一行:`"a\n"` 与 `"a"` 都是 1 行(编辑器里的行号也是这么数的)。
|
|
8951
|
+
*/
|
|
8952
|
+
function smemDiffLinesOf(text) {
|
|
8953
|
+
const s = String(text == null ? "" : text).replace(/\r\n?/g, "\n");
|
|
8954
|
+
if (s === "") return [];
|
|
8955
|
+
const lines = s.split("\n");
|
|
8956
|
+
if (lines.length > 1 && lines[lines.length - 1] === "") lines.pop();
|
|
8957
|
+
return lines;
|
|
8958
|
+
}
|
|
8959
|
+
|
|
8960
|
+
/**
|
|
8961
|
+
* 两段(已剥掉公共前后缀的)行数组的 LCS 对齐。
|
|
8962
|
+
*
|
|
8963
|
+
* 返回 `{ tag: 'ctx'|'del'|'ins', text, i?, j? }`,`i` / `j` 是**段内**下标,
|
|
8964
|
+
* 由调用方加上前后缀的偏移换成 1 起算的行号。
|
|
8965
|
+
*/
|
|
8966
|
+
function smemDiffLcsOf(a, b) {
|
|
8967
|
+
const n = a.length;
|
|
8968
|
+
const m = b.length;
|
|
8969
|
+
const w = m + 1;
|
|
8970
|
+
// 从后往前填:dp[i][j] = a[i..] 与 b[j..] 的 LCS 长度。表 ≤ (800+1)² 个 Int32。
|
|
8971
|
+
const dp = new Int32Array((n + 1) * w);
|
|
8972
|
+
for (let i = n - 1; i >= 0; i--) {
|
|
8973
|
+
for (let j = m - 1; j >= 0; j--) {
|
|
8974
|
+
dp[i * w + j] = a[i] === b[j]
|
|
8975
|
+
? dp[(i + 1) * w + (j + 1)] + 1
|
|
8976
|
+
: Math.max(dp[(i + 1) * w + j], dp[i * w + (j + 1)]);
|
|
8977
|
+
}
|
|
8978
|
+
}
|
|
8979
|
+
const out = [];
|
|
8980
|
+
let i = 0;
|
|
8981
|
+
let j = 0;
|
|
8982
|
+
while (i < n && j < m) {
|
|
8983
|
+
if (a[i] === b[j]) { out.push({ tag: "ctx", text: a[i], i: i, j: j }); i++; j++; }
|
|
8984
|
+
// 相等时先「删」再「增」:差异块里永远是 − 在上、+ 在下(设计稿 §3.3 的示意顺序)。
|
|
8985
|
+
else if (dp[(i + 1) * w + j] >= dp[i * w + (j + 1)]) { out.push({ tag: "del", text: a[i], i: i }); i++; }
|
|
8986
|
+
else { out.push({ tag: "ins", text: b[j], j: j }); j++; }
|
|
8987
|
+
}
|
|
8988
|
+
while (i < n) { out.push({ tag: "del", text: a[i], i: i }); i++; }
|
|
8989
|
+
while (j < m) { out.push({ tag: "ins", text: b[j], j: j }); j++; }
|
|
8990
|
+
return out;
|
|
8991
|
+
}
|
|
8992
|
+
|
|
8993
|
+
/**
|
|
8994
|
+
* 退化路径:整块删 + 整块增,不做逐行对齐(见 `SMEM_DIFF_LCS_MAX` 的理由)。
|
|
8995
|
+
*
|
|
8996
|
+
* 保留它是**有意的**:一段 2000 行的改写用 LCS 会慢,而结果对人也没有意义 ——
|
|
8997
|
+
* 「这一整段被换掉了」就是事实。退化时行号仍然准确(删的行给本地号、增的行给远端号)。
|
|
8998
|
+
*/
|
|
8999
|
+
function smemDiffBlockOf(a, b) {
|
|
9000
|
+
const out = [];
|
|
9001
|
+
for (let i = 0; i < a.length; i++) out.push({ tag: "del", text: a[i], i: i });
|
|
9002
|
+
for (let j = 0; j < b.length; j++) out.push({ tag: "ins", text: b[j], j: j });
|
|
9003
|
+
return out;
|
|
9004
|
+
}
|
|
9005
|
+
|
|
9006
|
+
/**
|
|
9007
|
+
* 行级逐行对比(U3 / U4 的被测对象)。
|
|
9008
|
+
*
|
|
9009
|
+
* @param {string} aText 「本地」那一版(`conflict.read` 的 `onDisk`)
|
|
9010
|
+
* @param {string} bText 「远端」那一版(`conflict.read` 的 `incoming`)
|
|
9011
|
+
* @param {{ context?: number | 'all' }} [options] 上下文行数,默认 3;`'all'` = 不折叠
|
|
9012
|
+
* @returns {{
|
|
9013
|
+
* tooBig: boolean,
|
|
9014
|
+
* aLines: number, bLines: number,
|
|
9015
|
+
* same: boolean,
|
|
9016
|
+
* ops: Array<{tag: 'ctx'|'del'|'ins'|'skip', text: string, aLine?: number, bLine?: number, count?: number}>,
|
|
9017
|
+
* full: Array<{tag: 'ctx'|'del'|'ins', text: string, aLine?: number, bLine?: number}>,
|
|
9018
|
+
* folded: number,
|
|
9019
|
+
* }}
|
|
9020
|
+
*
|
|
9021
|
+
* 三种「特殊返回」都写在这里,免得调用方各猜一套:
|
|
9022
|
+
* - `tooBig: true` → 任一侧超过 2000 行,`ops` / `full` 为空,界面只说行数;
|
|
9023
|
+
* - `same: true` → 两侧逐行相同,`ops` 为空(铺一屏一样的正文没有意义),`full` 是全文;
|
|
9024
|
+
* - `tag: 'skip'` → 折叠标记,`count` 是被跳过(两侧一致)的行数,`text` 恒为空串。
|
|
9025
|
+
*
|
|
9026
|
+
* 行号一律 1 起算:`del` 给 `aLine`、`ins` 给 `bLine`、`ctx` 两个都给。
|
|
9027
|
+
*/
|
|
9028
|
+
function smemLineDiff(aText, bText, options) {
|
|
9029
|
+
const opts = options || {};
|
|
9030
|
+
const context = opts.context === "all"
|
|
9031
|
+
? -1
|
|
9032
|
+
: (typeof opts.context === "number" && opts.context >= 0
|
|
9033
|
+
? Math.floor(opts.context)
|
|
9034
|
+
: SMEM_DIFF_CONTEXT);
|
|
9035
|
+
const a = smemDiffLinesOf(aText);
|
|
9036
|
+
const b = smemDiffLinesOf(bText);
|
|
9037
|
+
|
|
9038
|
+
if (a.length > SMEM_DIFF_MAX_LINES || b.length > SMEM_DIFF_MAX_LINES) {
|
|
9039
|
+
return { tooBig: true, aLines: a.length, bLines: b.length, same: false, ops: [], full: [], folded: 0 };
|
|
9040
|
+
}
|
|
9041
|
+
|
|
9042
|
+
// ① 公共前后缀:跨设备冲突绝大多数只是「某一处不一样」,先剥掉两头,LCS 只看见那一段。
|
|
9043
|
+
const shorter = Math.min(a.length, b.length);
|
|
9044
|
+
let head = 0;
|
|
9045
|
+
while (head < shorter && a[head] === b[head]) head++;
|
|
9046
|
+
let tail = 0;
|
|
9047
|
+
while (tail < shorter - head && a[a.length - 1 - tail] === b[b.length - 1 - tail]) tail++;
|
|
9048
|
+
|
|
9049
|
+
const amid = a.slice(head, a.length - tail);
|
|
9050
|
+
const bmid = b.slice(head, b.length - tail);
|
|
9051
|
+
const rows = [];
|
|
9052
|
+
for (let k = 0; k < head; k++) rows.push({ tag: "ctx", text: a[k], aLine: k + 1, bLine: k + 1 });
|
|
9053
|
+
const mid = (amid.length > SMEM_DIFF_LCS_MAX || bmid.length > SMEM_DIFF_LCS_MAX)
|
|
9054
|
+
? smemDiffBlockOf(amid, bmid)
|
|
9055
|
+
: smemDiffLcsOf(amid, bmid);
|
|
9056
|
+
for (let k = 0; k < mid.length; k++) {
|
|
9057
|
+
const m = mid[k];
|
|
9058
|
+
if (m.tag === "ctx") rows.push({ tag: "ctx", text: m.text, aLine: head + m.i + 1, bLine: head + m.j + 1 });
|
|
9059
|
+
else if (m.tag === "del") rows.push({ tag: "del", text: m.text, aLine: head + m.i + 1 });
|
|
9060
|
+
else rows.push({ tag: "ins", text: m.text, bLine: head + m.j + 1 });
|
|
9061
|
+
}
|
|
9062
|
+
// 后缀:a 的第 k 行对应 b 的第 `bLen - (aLen - k)` 行(两边尾部是同一段)。
|
|
9063
|
+
for (let k = a.length - tail; k < a.length; k++) {
|
|
9064
|
+
rows.push({ tag: "ctx", text: a[k], aLine: k + 1, bLine: b.length - (a.length - k) + 1 });
|
|
9065
|
+
}
|
|
9066
|
+
|
|
9067
|
+
const same = rows.every(function (r) { return r.tag === "ctx"; });
|
|
9068
|
+
if (same) {
|
|
9069
|
+
return { tooBig: false, aLines: a.length, bLines: b.length, same: true, ops: [], full: rows, folded: 0 };
|
|
9070
|
+
}
|
|
9071
|
+
if (context < 0) {
|
|
9072
|
+
return { tooBig: false, aLines: a.length, bLines: b.length, same: false, ops: rows.slice(), full: rows, folded: 0 };
|
|
9073
|
+
}
|
|
9074
|
+
|
|
9075
|
+
// ② 只留差异块 ±context 行:**逐个**差异行标记,两个差异块离得远时中间会折起来。
|
|
9076
|
+
const keep = [];
|
|
9077
|
+
for (let i = 0; i < rows.length; i++) keep.push(false);
|
|
9078
|
+
for (let i = 0; i < rows.length; i++) {
|
|
9079
|
+
if (rows[i].tag === "ctx") continue;
|
|
9080
|
+
const from = Math.max(0, i - context);
|
|
9081
|
+
const to = Math.min(rows.length - 1, i + context);
|
|
9082
|
+
for (let k = from; k <= to; k++) keep[k] = true;
|
|
9083
|
+
}
|
|
9084
|
+
const ops = [];
|
|
9085
|
+
let folded = 0;
|
|
9086
|
+
let skipped = 0;
|
|
9087
|
+
for (let i = 0; i < rows.length; i++) {
|
|
9088
|
+
if (keep[i]) {
|
|
9089
|
+
if (skipped > 0) { ops.push({ tag: "skip", text: "", count: skipped }); folded += skipped; skipped = 0; }
|
|
9090
|
+
ops.push(rows[i]);
|
|
9091
|
+
} else skipped++;
|
|
9092
|
+
}
|
|
9093
|
+
if (skipped > 0) { ops.push({ tag: "skip", text: "", count: skipped }); folded += skipped; }
|
|
9094
|
+
|
|
9095
|
+
return { tooBig: false, aLines: a.length, bLines: b.length, same: false, ops: ops, full: rows, folded: folded };
|
|
9096
|
+
}
|
|
9097
|
+
|
|
9098
|
+
// ── 卡片自己的样式 ─────────────────────────────────────────────────────
|
|
9099
|
+
//
|
|
9100
|
+
// 为什么不并进 `40-styles.js` 的那一份:那一份是「拼接时就固定的字符串」,本分片在它
|
|
9101
|
+
// 之后求值,改不动它。所以这里照 `11-starmap-styles.js` 的老办法自带一段、幂等注入
|
|
9102
|
+
// (同一个 `data-plugin-css` 只挂一次)—— 唯一的代价是它进快照里的 `<style>`,
|
|
9103
|
+
// 于是改这里会让**全部屏**的哈希一起变(快照套件头注已说明这一点)。
|
|
9104
|
+
const EXT_SYNC_CSS = [
|
|
9105
|
+
".smem-sync-list{display:flex;flex-direction:column;gap:8px;}",
|
|
9106
|
+
".smem-sync-item{display:flex;flex-direction:column;gap:4px;padding:8px 10px;border:1px solid var(--dsw-alias-border-l1);border-radius:8px;}",
|
|
9107
|
+
".smem-sync-line{display:flex;align-items:center;gap:4px;flex-wrap:wrap;font-size:12px;}",
|
|
9108
|
+
".smem-sync-host{font-weight:600;color:var(--dsw-alias-label-primary);}",
|
|
9109
|
+
".smem-sync-file{font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:12px;color:var(--dsw-alias-label-primary);overflow-wrap:anywhere;}",
|
|
9110
|
+
".smem-sync-why{font-size:11.5px;color:var(--dsw-alias-label-secondary);overflow-wrap:anywhere;}",
|
|
9111
|
+
".smem-sync-acts{display:flex;gap:6px;flex-wrap:wrap;margin-top:2px;}",
|
|
9112
|
+
".smem-sync-diff{display:flex;flex-direction:column;gap:4px;margin-top:4px;}",
|
|
9113
|
+
".smem-sync-diffhead{display:flex;align-items:center;gap:8px;flex-wrap:wrap;font-size:11.5px;}",
|
|
9114
|
+
// 单栏 diff 流:行号 / 符号 / 正文三列,**不并排双栏**(面板 420px 起,双栏中文会碎成一列字)。
|
|
9115
|
+
".smem-sync-diffbody{border:1px solid var(--dsw-alias-border-l1);border-radius:8px;background:var(--dsw-alias-bg-layer-1);max-height:320px;overflow:auto;padding:4px 0;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:11.5px;line-height:1.55;}",
|
|
9116
|
+
".smem-diff-row{display:flex;gap:6px;padding:0 8px;white-space:pre-wrap;overflow-wrap:anywhere;}",
|
|
9117
|
+
".smem-diff-n{flex:none;width:34px;text-align:right;color:var(--dsw-alias-label-secondary);font-variant-numeric:tabular-nums;}",
|
|
9118
|
+
".smem-diff-sign{flex:none;width:9px;text-align:center;color:var(--dsw-alias-label-secondary);}",
|
|
9119
|
+
".smem-diff-text{flex:1;min-width:0;color:var(--dsw-alias-label-primary);}",
|
|
9120
|
+
// 差异行只强调**符号那一位**:整行染红会让一屏里几十行都在喊(彩色面积要小)。
|
|
9121
|
+
".smem-diff-del{background:var(--dsw-alias-bg-layer-2);}",
|
|
9122
|
+
".smem-diff-ins{background:var(--dsw-alias-bg-layer-2);}",
|
|
9123
|
+
".smem-diff-del .smem-diff-sign{color:var(--dsw-alias-state-error-primary);font-weight:600;}",
|
|
9124
|
+
".smem-diff-ins .smem-diff-sign{color:var(--dsw-alias-state-success-primary);font-weight:600;}",
|
|
9125
|
+
".smem-diff-skip{padding:2px 8px;font-size:11px;color:var(--dsw-alias-label-secondary);}",
|
|
9126
|
+
].join("\n");
|
|
9127
|
+
|
|
9128
|
+
/** 幂等注入(同一个 id 只挂一次;`document` 不在时安静跳过,测试环境就这么用)。 */
|
|
9129
|
+
function injectExtSyncStyle() {
|
|
9130
|
+
try {
|
|
9131
|
+
if (typeof document === "undefined" || !document || typeof document.createElement !== "function") return;
|
|
9132
|
+
const tagId = "sage-mem/ext-sync.css";
|
|
9133
|
+
if (typeof document.querySelector === "function"
|
|
9134
|
+
&& document.querySelector("style[data-plugin-css=" + JSON.stringify(tagId) + "]") !== null) return;
|
|
9135
|
+
const tag = document.createElement("style");
|
|
9136
|
+
if (tag.dataset) { tag.dataset.plugin = "sage-mem"; tag.dataset.pluginCss = tagId; }
|
|
9137
|
+
tag.textContent = EXT_SYNC_CSS;
|
|
9138
|
+
if (document.head && typeof document.head.appendChild === "function") document.head.appendChild(tag);
|
|
9139
|
+
} catch (e) { /* 样式注入失败不该让界面白屏 */ }
|
|
9140
|
+
}
|
|
9141
|
+
injectExtSyncStyle();
|
|
9142
|
+
|
|
9143
|
+
// ── 卡片 ───────────────────────────────────────────────────────────────
|
|
9144
|
+
|
|
9145
|
+
/**
|
|
9146
|
+
* 「跨设备差异」(3.0.0 第 4 步)—— 挂在「外部访问」页的来件卡之后。
|
|
9147
|
+
*
|
|
9148
|
+
* 三条不许变的产品规则:
|
|
9149
|
+
* 1. **只列 `origin === 'sync'` 且还开着的记录**。`autodream` 造的冲突、以及没有
|
|
9150
|
+
* `origin` 的老记录一条都不许进来 —— 后者是「同步撞车」吗?不知道,那就别猜。
|
|
9151
|
+
* 也不能靠 `kind` 猜:拉取与推送两边都写 `kind:'external_change'`,而 autodream
|
|
9152
|
+
* 的 `external_change` 语义上也是「两边都动过」。类别词在 `note` 里,原样显示。
|
|
9153
|
+
* 2. **留本地 / 留远端必须二次确认**(两个动作都会盖掉一份真实文件),「手工合并」
|
|
9154
|
+
* 不弹模态 —— 它一个字节都不写。
|
|
9155
|
+
* 3. **失败不许吞**:`code` + `error` 落在**出错的那一条卡片里**,不是整屏一条。
|
|
9156
|
+
*
|
|
9157
|
+
* 宿主没有 conflict 面(旧宿主)时整张卡不渲染:这一卡的全部数据都来自那个面,
|
|
9158
|
+
* 画一个空壳只会让人以为「同步功能坏了」。
|
|
9159
|
+
*/
|
|
9160
|
+
function ExternalSyncCard(props) {
|
|
9161
|
+
const remote = stableRemote(props.ctx, "remote.conflict");
|
|
9162
|
+
const listable = !!remote && typeof remote.list === "function";
|
|
9163
|
+
const sItems = react.useState(null); const items = sItems[0], setItems = sItems[1];
|
|
9164
|
+
const sErr = react.useState(""); const err = sErr[0], setErr = sErr[1];
|
|
9165
|
+
const sNotice = react.useState(""); const notice = sNotice[0], setNotice = sNotice[1];
|
|
9166
|
+
const sBusy = react.useState(""); const busy = sBusy[0], setBusy = sBusy[1];
|
|
9167
|
+
const sOpen = react.useState({}); const open = sOpen[0], setOpen = sOpen[1];
|
|
9168
|
+
const sDiffs = react.useState({}); const diffs = sDiffs[0], setDiffs = sDiffs[1];
|
|
9169
|
+
const sAll = react.useState({}); const allRows = sAll[0], setAllRows = sAll[1];
|
|
9170
|
+
const sConfirm = react.useState(null); const confirm = sConfirm[0], setConfirm = sConfirm[1];
|
|
9171
|
+
const sItemErr = react.useState({}); const itemErr = sItemErr[0], setItemErr = sItemErr[1];
|
|
9172
|
+
|
|
9173
|
+
/** 拉队列并**只留跨设备那几条**。 */
|
|
9174
|
+
function load() {
|
|
9175
|
+
if (!listable) return;
|
|
9176
|
+
Promise.resolve(remote.list()).then(function (res) {
|
|
9177
|
+
const d = unwrap(res) || {};
|
|
9178
|
+
if (d.ok === false) {
|
|
9179
|
+
setItems(null);
|
|
9180
|
+
setErr(t("ext.sync.err.list", { code: String(d.code || "?"), error: String(d.error || "") }));
|
|
9181
|
+
return;
|
|
9182
|
+
}
|
|
9183
|
+
const rows = Array.isArray(d.items) ? d.items : [];
|
|
9184
|
+
setItems(rows.filter(function (it) {
|
|
9185
|
+
return !!it && it.origin === "sync" && it.status === "open";
|
|
9186
|
+
}));
|
|
9187
|
+
setErr("");
|
|
9188
|
+
}, function (e) {
|
|
9189
|
+
setItems(null);
|
|
9190
|
+
setErr(t("ext.sync.err.list", { code: "", error: detail(e) }));
|
|
9191
|
+
});
|
|
9192
|
+
}
|
|
9193
|
+
|
|
9194
|
+
react.useEffect(function () { load(); }, []);
|
|
9195
|
+
|
|
9196
|
+
/** 记一条**这一条**的错。 */
|
|
9197
|
+
function failOf(id, text) {
|
|
9198
|
+
setItemErr(function (prev) {
|
|
9199
|
+
const next = Object.assign({}, prev);
|
|
9200
|
+
next[id] = text;
|
|
9201
|
+
return next;
|
|
9202
|
+
});
|
|
9203
|
+
}
|
|
9204
|
+
|
|
9205
|
+
function clearOf(id) {
|
|
9206
|
+
setItemErr(function (prev) {
|
|
9207
|
+
if (!Object.prototype.hasOwnProperty.call(prev, id)) return prev;
|
|
9208
|
+
const next = Object.assign({}, prev);
|
|
9209
|
+
delete next[id];
|
|
9210
|
+
return next;
|
|
9211
|
+
});
|
|
9212
|
+
}
|
|
9213
|
+
|
|
9214
|
+
/** 展开 / 收起逐行对比。第一次展开才去取两份正文(列表里不搬运 96 KB 的记录)。 */
|
|
9215
|
+
function toggleDiff(item) {
|
|
9216
|
+
const id = String(item && item.id ? item.id : "");
|
|
9217
|
+
if (id === "") return;
|
|
9218
|
+
if (open[id] === true) {
|
|
9219
|
+
const next = Object.assign({}, open);
|
|
9220
|
+
delete next[id];
|
|
9221
|
+
setOpen(next);
|
|
9222
|
+
return;
|
|
9223
|
+
}
|
|
9224
|
+
const opened = Object.assign({}, open);
|
|
9225
|
+
opened[id] = true;
|
|
9226
|
+
setOpen(opened);
|
|
9227
|
+
if (diffs[id]) return; // 取过就不再打一次 RPC
|
|
9228
|
+
if (!remote || typeof remote.read !== "function") {
|
|
9229
|
+
failOf(id, t("ext.sync.err.read", { code: "unsupported", error: "conflict.read" }));
|
|
9230
|
+
return;
|
|
9231
|
+
}
|
|
9232
|
+
setBusy(id);
|
|
9233
|
+
clearOf(id);
|
|
9234
|
+
Promise.resolve(remote.read({ id: id })).then(function (res) {
|
|
9235
|
+
setBusy("");
|
|
9236
|
+
const d = unwrap(res) || {};
|
|
9237
|
+
if (d.ok === false || !d.record) {
|
|
9238
|
+
failOf(id, t("ext.sync.err.read", { code: String(d.code || "?"), error: String(d.error || "") }));
|
|
9239
|
+
return;
|
|
9240
|
+
}
|
|
9241
|
+
// a = 本地(onDisk)、b = 远端(incoming):看的是「本地这一版 vs 远端那一版」。
|
|
9242
|
+
const diff = smemLineDiff(String(d.record.onDisk || ""), String(d.record.incoming || ""));
|
|
9243
|
+
setDiffs(function (prev) {
|
|
9244
|
+
const next = Object.assign({}, prev);
|
|
9245
|
+
next[id] = diff;
|
|
9246
|
+
return next;
|
|
9247
|
+
});
|
|
9248
|
+
}, function (e) {
|
|
9249
|
+
setBusy("");
|
|
9250
|
+
failOf(id, t("ext.sync.err.read", { code: "", error: detail(e) }));
|
|
9251
|
+
});
|
|
9252
|
+
}
|
|
9253
|
+
|
|
9254
|
+
/** 三个动作的唯一出口。`choice` 的语义在宿主 `sync/resolve.js`,这里只负责传。 */
|
|
9255
|
+
function resolve(id, choice) {
|
|
9256
|
+
if (!remote || typeof remote.applyResolution !== "function") {
|
|
9257
|
+
setConfirm(null);
|
|
9258
|
+
failOf(id, t("ext.sync.err.noAct"));
|
|
9259
|
+
return;
|
|
9260
|
+
}
|
|
9261
|
+
setBusy(id);
|
|
9262
|
+
setNotice("");
|
|
9263
|
+
clearOf(id);
|
|
9264
|
+
Promise.resolve(remote.applyResolution({ id: id, choice: choice })).then(function (res) {
|
|
9265
|
+
setBusy("");
|
|
9266
|
+
setConfirm(null);
|
|
9267
|
+
const r = unwrap(res) || {};
|
|
9268
|
+
if (r.ok === false) {
|
|
9269
|
+
failOf(id, t("ext.sync.err.act", { code: String(r.code || "?"), error: String(r.error || "") }));
|
|
9270
|
+
load();
|
|
9271
|
+
return;
|
|
9272
|
+
}
|
|
9273
|
+
setNotice(choice === "manual"
|
|
9274
|
+
? t("ext.sync.notice.manual")
|
|
9275
|
+
: (choice === "local" ? t("ext.sync.notice.local") : t("ext.sync.notice.remote")));
|
|
9276
|
+
// 基线没跟上是**部分成功**:文件已经写成,所以不能说「失败了」;但它会
|
|
9277
|
+
// 让同一个冲突在下次检查时复活,所以也不能不提(设计稿 §4.1 / §4.3)。
|
|
9278
|
+
if (r.baseError) setErr(t("ext.sync.err.base", { error: String(r.baseError) }));
|
|
9279
|
+
load();
|
|
9280
|
+
}, function (e) {
|
|
9281
|
+
setBusy("");
|
|
9282
|
+
setConfirm(null);
|
|
9283
|
+
failOf(id, t("ext.sync.err.act", { code: "", error: detail(e) }));
|
|
9284
|
+
});
|
|
9285
|
+
}
|
|
9286
|
+
|
|
9287
|
+
/** 确认框:只接「会写文件」的两个动作;文案必须说清哪一份会被盖掉。 */
|
|
9288
|
+
function confirmBlock() {
|
|
9289
|
+
if (!confirm) return null;
|
|
9290
|
+
const isLocal = confirm.choice === "local";
|
|
9291
|
+
return h(Confirm, {
|
|
9292
|
+
key: "confirm",
|
|
9293
|
+
title: isLocal ? t("ext.sync.confirm.localTitle") : t("ext.sync.confirm.remoteTitle"),
|
|
9294
|
+
confirmLabel: t("ext.sync.confirm.yes"),
|
|
9295
|
+
busyLabel: t("ext.sync.busy"),
|
|
9296
|
+
busy: busy === confirm.id,
|
|
9297
|
+
danger: true,
|
|
9298
|
+
onCancel: function () { setConfirm(null); },
|
|
9299
|
+
onConfirm: function () { resolve(confirm.id, confirm.choice); },
|
|
9300
|
+
}, [
|
|
9301
|
+
h("div", { key: "body" }, isLocal ? t("ext.sync.confirm.localBody") : t("ext.sync.confirm.remoteBody")),
|
|
9302
|
+
]);
|
|
9303
|
+
}
|
|
9304
|
+
|
|
9305
|
+
/** 单栏 diff 流的一行:行号 / 符号 / 正文。 */
|
|
9306
|
+
function diffRowOf(row, i) {
|
|
9307
|
+
if (row.tag === "skip") {
|
|
9308
|
+
return h("div", { className: "smem-diff-row smem-diff-skip", key: "k" + i },
|
|
9309
|
+
t("ext.sync.skip", { count: row.count }));
|
|
9310
|
+
}
|
|
9311
|
+
const num = row.tag === "ins" ? row.bLine : row.aLine;
|
|
9312
|
+
const sign = row.tag === "del" ? "-" : (row.tag === "ins" ? "+" : " ");
|
|
9313
|
+
return h("div", { className: "smem-diff-row smem-diff-" + row.tag, key: "r" + i }, [
|
|
9314
|
+
h("span", { className: "smem-diff-n", key: "n" }, num == null ? "" : String(num)),
|
|
9315
|
+
h("span", { className: "smem-diff-sign", key: "s" }, sign),
|
|
9316
|
+
// 空行给一个空格:否则那一行高度塌掉,diff 看起来像少了几行。
|
|
9317
|
+
h("span", { className: "smem-diff-text", key: "t" }, row.text === "" ? " " : row.text),
|
|
9318
|
+
]);
|
|
9319
|
+
}
|
|
9320
|
+
|
|
9321
|
+
function diffBoxOf(item) {
|
|
9322
|
+
const id = String(item && item.id ? item.id : "");
|
|
9323
|
+
const diff = diffs[id] || null;
|
|
9324
|
+
if (!diff) {
|
|
9325
|
+
return busy === id
|
|
9326
|
+
? h("div", { className: "smem-sync-diff smem-muted", key: "diff" }, t("ext.sync.diffReading"))
|
|
9327
|
+
: null;
|
|
9328
|
+
}
|
|
9329
|
+
if (diff.tooBig) {
|
|
9330
|
+
return h("div", { className: "smem-sync-diff", key: "diff" }, [
|
|
9331
|
+
h("div", { className: "smem-muted", key: "big" },
|
|
9332
|
+
t("ext.sync.tooBig", { a: diff.aLines, b: diff.bLines })),
|
|
9333
|
+
]);
|
|
9334
|
+
}
|
|
9335
|
+
const head = h("div", { className: "smem-sync-diffhead", key: "head" }, [
|
|
9336
|
+
h("span", { className: "smem-muted", key: "n" },
|
|
9337
|
+
t("ext.sync.diffLines", { a: diff.aLines, b: diff.bLines })),
|
|
9338
|
+
h("span", { className: "smem-muted", key: "side" },
|
|
9339
|
+
t("ext.sync.side.local") + " - / " + t("ext.sync.side.remote") + " +"),
|
|
9340
|
+
diff.same
|
|
9341
|
+
? null
|
|
9342
|
+
: h("button", {
|
|
9343
|
+
type: "button", className: "smem-btn smem-btn-xs", key: "all",
|
|
9344
|
+
onClick: function () {
|
|
9345
|
+
const next = Object.assign({}, allRows);
|
|
9346
|
+
next[id] = true;
|
|
9347
|
+
setAllRows(next);
|
|
9348
|
+
},
|
|
9349
|
+
}, t("ext.sync.diffAll")),
|
|
9350
|
+
allRows[id] === true
|
|
9351
|
+
? h("button", {
|
|
9352
|
+
type: "button", className: "smem-btn smem-btn-xs", key: "fold",
|
|
9353
|
+
onClick: function () {
|
|
9354
|
+
const next = Object.assign({}, allRows);
|
|
9355
|
+
delete next[id];
|
|
9356
|
+
setAllRows(next);
|
|
9357
|
+
},
|
|
9358
|
+
}, t("ext.sync.diffFold"))
|
|
9359
|
+
: null,
|
|
9360
|
+
]);
|
|
9361
|
+
if (diff.same) {
|
|
9362
|
+
return h("div", { className: "smem-sync-diff", key: "diff" }, [
|
|
9363
|
+
head,
|
|
9364
|
+
h("div", { className: "smem-muted", key: "same" }, t("ext.sync.same")),
|
|
9365
|
+
]);
|
|
9366
|
+
}
|
|
9367
|
+
const rows = allRows[id] === true ? diff.full : diff.ops;
|
|
9368
|
+
return h("div", { className: "smem-sync-diff", key: "diff" }, [
|
|
9369
|
+
head,
|
|
9370
|
+
h("div", { className: "smem-sync-diffbody", key: "body" }, rows.map(diffRowOf)),
|
|
9371
|
+
]);
|
|
9372
|
+
}
|
|
9373
|
+
|
|
9374
|
+
function renderItem(item) {
|
|
9375
|
+
const id = String(item.id || "");
|
|
9376
|
+
const acting = busy === id;
|
|
9377
|
+
const expanded = open[id] === true;
|
|
9378
|
+
const canAct = !!remote && typeof remote.applyResolution === "function";
|
|
9379
|
+
const actTitle = canAct ? undefined : t("ext.sync.err.noAct");
|
|
9380
|
+
const parts = [];
|
|
9381
|
+
|
|
9382
|
+
// 折叠态:`<host> · <file>` + note。类别词在 note 里(宿主写的),不在这里按 kind 猜。
|
|
9383
|
+
parts.push(h("div", { className: "smem-sync-line", key: "line" }, [
|
|
9384
|
+
h("span", { className: "smem-sync-host", key: "h" }, String(item.host || "?")),
|
|
9385
|
+
h("span", { className: "smem-muted", key: "sep" }, "·"),
|
|
9386
|
+
h("span", { className: "smem-sync-file", key: "f" }, String(item.file || "")),
|
|
9387
|
+
item.at ? h("span", { className: "smem-muted", key: "at" }, String(item.at)) : null,
|
|
9388
|
+
]));
|
|
9389
|
+
if (item.note) parts.push(h("div", { className: "smem-sync-why", key: "why" }, String(item.note)));
|
|
9390
|
+
if (item.remotePath) {
|
|
9391
|
+
parts.push(h("div", { className: "smem-sync-why", key: "path" },
|
|
9392
|
+
t("ext.sync.path", { path: String(item.remotePath) })));
|
|
9393
|
+
}
|
|
9394
|
+
if (item.truncated) {
|
|
9395
|
+
parts.push(h("div", { className: "smem-conf-warn", key: "tr" }, t("ext.sync.truncated")));
|
|
9396
|
+
}
|
|
9397
|
+
|
|
9398
|
+
parts.push(h("div", { className: "smem-sync-acts", key: "acts" }, [
|
|
9399
|
+
h("button", {
|
|
9400
|
+
type: "button", className: "smem-btn smem-btn-xs", key: "diff", disabled: acting,
|
|
9401
|
+
onClick: function () { toggleDiff(item); },
|
|
9402
|
+
}, expanded ? t("ext.sync.diffOff") : t("ext.sync.diffOn")),
|
|
9403
|
+
h("button", {
|
|
9404
|
+
type: "button", className: "smem-btn smem-btn-xs", key: "local",
|
|
9405
|
+
disabled: acting || !canAct, title: actTitle,
|
|
9406
|
+
onClick: function () { setConfirm({ id: id, choice: "local" }); },
|
|
9407
|
+
}, t("ext.sync.local")),
|
|
9408
|
+
h("button", {
|
|
9409
|
+
type: "button", className: "smem-btn smem-btn-xs", key: "remote",
|
|
9410
|
+
disabled: acting || !canAct, title: actTitle,
|
|
9411
|
+
onClick: function () { setConfirm({ id: id, choice: "remote" }); },
|
|
9412
|
+
}, t("ext.sync.remote")),
|
|
9413
|
+
// 手工合并不写文件 → 不设二次确认(那也是「多点一次」的成本,换不来任何保护)。
|
|
9414
|
+
h("button", {
|
|
9415
|
+
type: "button", className: "smem-btn smem-btn-xs", key: "manual",
|
|
9416
|
+
disabled: acting || !canAct, title: actTitle,
|
|
9417
|
+
onClick: function () { resolve(id, "manual"); },
|
|
9418
|
+
}, t("ext.sync.manual")),
|
|
9419
|
+
]));
|
|
9420
|
+
|
|
9421
|
+
if (expanded) parts.push(diffBoxOf(item));
|
|
9422
|
+
if (itemErr[id]) parts.push(h("div", { className: "smem-err", key: "ierr" }, "⚠ " + itemErr[id]));
|
|
9423
|
+
return h("div", { className: "smem-sync-item", key: id }, parts);
|
|
9424
|
+
}
|
|
9425
|
+
|
|
9426
|
+
if (!listable) return null;
|
|
9427
|
+
const list = items || [];
|
|
9428
|
+
|
|
9429
|
+
return h("div", { className: "smem-sync smem-module-card" }, [
|
|
9430
|
+
h("div", { className: "smem-modcard-head", key: "head" }, [
|
|
9431
|
+
h("span", { className: "smem-modcard-title", key: "t" }, t("ext.sync.title")),
|
|
9432
|
+
// 数字只在**真的取到队列**之后才算:加载中 / 失败时写「0 条」等于撒谎。
|
|
9433
|
+
h("span", { className: "smem-modcard-status", key: "s" },
|
|
9434
|
+
items === null || list.length === 0
|
|
9435
|
+
? t("ext.sync.countNone")
|
|
9436
|
+
: t("ext.sync.count", { count: list.length })),
|
|
9437
|
+
h("button", {
|
|
9438
|
+
type: "button", className: "smem-btn smem-btn-xs", key: "re",
|
|
9439
|
+
disabled: busy !== "", onClick: function () { load(); },
|
|
9440
|
+
}, t("ext.sync.refresh")),
|
|
9441
|
+
]),
|
|
9442
|
+
h("div", { className: "smem-autodream-note", key: "what" }, t("ext.sync.what")),
|
|
9443
|
+
err ? h("div", { className: "smem-err", key: "err" }, "⚠ " + err) : null,
|
|
9444
|
+
items !== null && list.length === 0
|
|
9445
|
+
? h("div", { className: "smem-muted", key: "empty" }, t("ext.sync.empty"))
|
|
9446
|
+
: null,
|
|
9447
|
+
list.length > 0
|
|
9448
|
+
? h("div", { className: "smem-sync-list", key: "list" }, list.map(renderItem))
|
|
9449
|
+
: null,
|
|
9450
|
+
confirmBlock(),
|
|
9451
|
+
notice ? h("div", { className: "smem-ok", key: "ok" }, "✓ " + notice) : null,
|
|
9452
|
+
]);
|
|
9453
|
+
}
|
|
8780
9454
|
// ── 文件列表这一屏(1.5.3 从 Section 里抽出;D23 计划的 72 片)──────────
|
|
8781
9455
|
// 只负责「按状态/筛选把卡片摆出来 + 四种空态」;状态、筛选语义、动作回调全部由 Section 传。
|
|
8782
9456
|
// 抽出的判据:它是 Section 里最大的一块纯渲染,而卡片组件(Card / ArchivedCard)本来就在 50 共享层。
|