dsh-workbench-danmaku 0.4.0 → 0.4.2
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/backup.js +85 -0
- package/lib/client.js +1 -1
- package/lib/index.js +7 -3
- package/package.json +3 -3
package/lib/backup.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// 弹幕库的备份导入导出:只搬 danmaku 一张表,行内字段与库表同形(含墓碑),
|
|
3
|
+
// 导入按 id 幂等 upsert,同一份备份重复导入不会多出一行。
|
|
4
|
+
|
|
5
|
+
const COLUMNS = [
|
|
6
|
+
"id", "body", "tone", "enabled", "card", "font_size", "speed", "opacity", "font_weight",
|
|
7
|
+
"font_family", "band", "prefix", "chance", "sort_order", "created_at", "updated_at", "deleted_at", "rev",
|
|
8
|
+
];
|
|
9
|
+
|
|
10
|
+
export function exportDanmakuData(db) {
|
|
11
|
+
const rows = db.prepare("SELECT * FROM danmaku ORDER BY sort_order ASC, created_at ASC, id ASC").all();
|
|
12
|
+
return {
|
|
13
|
+
formatVersion: 1,
|
|
14
|
+
danmaku: rows.map((row) => {
|
|
15
|
+
const out = {};
|
|
16
|
+
for (const key of COLUMNS) out[key] = row[key] === undefined ? null : row[key];
|
|
17
|
+
return out;
|
|
18
|
+
}),
|
|
19
|
+
note: "只含弹幕库本体;播放节奏与桌面层开关属于本机配置,不进备份。",
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function importDanmakuData(db, raw, policy) {
|
|
24
|
+
let payload = raw;
|
|
25
|
+
if (typeof raw === "string") {
|
|
26
|
+
try {
|
|
27
|
+
payload = JSON.parse(raw);
|
|
28
|
+
} catch {
|
|
29
|
+
return { ok: false, imported: 0, failed: 0, skipped: 0, message: "备份内容不是合法 JSON" };
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const rows = Array.isArray(payload && payload.danmaku) ? payload.danmaku : [];
|
|
33
|
+
const select = db.prepare("SELECT id, updated_at, deleted_at FROM danmaku WHERE id=?");
|
|
34
|
+
const selectBody = db.prepare("SELECT id FROM danmaku WHERE body=? AND deleted_at IS NULL");
|
|
35
|
+
const upsert = db.prepare(
|
|
36
|
+
"INSERT INTO danmaku (" + COLUMNS.join(",") + ") VALUES (" + COLUMNS.map(() => "?").join(",") + ") " +
|
|
37
|
+
"ON CONFLICT(id) DO UPDATE SET " + COLUMNS.filter((c) => c !== "id").map((c) => c + "=excluded." + c).join(", "),
|
|
38
|
+
);
|
|
39
|
+
let imported = 0;
|
|
40
|
+
let skipped = 0;
|
|
41
|
+
let failed = 0;
|
|
42
|
+
for (const row of rows) {
|
|
43
|
+
const id = String(row && row.id ? row.id : "").trim();
|
|
44
|
+
if (!id) {
|
|
45
|
+
failed += 1;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
const local = select.get(id);
|
|
50
|
+
if (local && policy === "skip") {
|
|
51
|
+
skipped += 1;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
// 未删除的行按 updated_at 判新旧;墓碑(deleted_at 非空)只做落库,不做「更新」判断。
|
|
55
|
+
if (local && policy === "newest" && row.deleted_at === null) {
|
|
56
|
+
const localTs = Number(local.updated_at) || 0;
|
|
57
|
+
const remoteTs = Number(row.updated_at) || 0;
|
|
58
|
+
if (local.deleted_at === null && localTs > remoteTs) {
|
|
59
|
+
skipped += 1;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// body 上有「未删除时唯一」的索引:同一句话换了 id 再导入会被索引拦下,
|
|
64
|
+
// 按同一行处理(跳过),避免整包因为一条重复内容失败。
|
|
65
|
+
if (!local && row.deleted_at === null) {
|
|
66
|
+
const same = selectBody.get(String(row.body || ""));
|
|
67
|
+
if (same && String(same.id) !== id) {
|
|
68
|
+
skipped += 1;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
upsert.run(...COLUMNS.map((key) => (row[key] === undefined ? null : row[key])));
|
|
73
|
+
imported += 1;
|
|
74
|
+
} catch (error) {
|
|
75
|
+
failed += 1;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
ok: failed === 0,
|
|
80
|
+
imported,
|
|
81
|
+
failed,
|
|
82
|
+
skipped,
|
|
83
|
+
message: "导入弹幕 " + imported + " 条" + (skipped ? ",跳过 " + skipped + " 条" : "") + (failed ? ",失败 " + failed + " 条" : ""),
|
|
84
|
+
};
|
|
85
|
+
}
|
package/lib/client.js
CHANGED
package/lib/index.js
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
import { bindClock, ensureSchema } from "./db/db.js";
|
|
19
|
+
import { exportDanmakuData, importDanmakuData } from "./backup.js";
|
|
19
20
|
import { createRequire } from "node:module";
|
|
20
21
|
|
|
21
22
|
// 版本号只认 package.json:首页插件卡读的就是这里注册的 version。
|
|
@@ -128,9 +129,12 @@ export function apply(ctx) {
|
|
|
128
129
|
// 更新来源:宿主「插件」页按它查新版本并安装(只认官方 npm registry)。
|
|
129
130
|
update: { source: "npm", package: "dsh-workbench-danmaku" },
|
|
130
131
|
category: "comm",
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
//
|
|
132
|
+
// 弹幕库跟着云同步与备份走(表里已有 updated_at/deleted_at,可直接走行级 LWW)。
|
|
133
|
+
tables: [{ name: TABLES[0], persistence: "persistent", backupKey: TABLES[0] }],
|
|
134
|
+
// 只同步/备份弹幕库本体;播放节奏与桌面层开关属于本机配置,留在配置中心。
|
|
135
|
+
sync: { tables: [{ name: TABLES[0], mode: "row" }] },
|
|
136
|
+
exportData: () => exportDanmakuData(host.db),
|
|
137
|
+
importData: (payload, _ctx, options) => importDanmakuData(host.db, payload, options && options.policy),
|
|
134
138
|
config: configDecl(),
|
|
135
139
|
views: [{ id: "library", title: { zh: "弹幕库", en: "Library" } }],
|
|
136
140
|
capability: {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-workbench-danmaku",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"description": "个人工作台弹幕插件:把自建弹幕轮播在整个 DSH 界面,并可叠加到整个桌面",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"id": "wb-addon-danmaku",
|
|
18
18
|
"title": "弹幕",
|
|
19
19
|
"description": "在整个 DSH 界面(含工作台业务页与宿主弹窗之外的所有区域)与桌面透明层轮播自建弹幕;含弹幕库视图,配置项由宿主统一配置中心渲染",
|
|
20
|
-
"version": "0.4.
|
|
20
|
+
"version": "0.4.2",
|
|
21
21
|
"minShell": "0.8.0",
|
|
22
22
|
"tables": [
|
|
23
23
|
"danmaku"
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"./package.json": "./package.json"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"dsh-workbench-host": "^0.
|
|
48
|
+
"dsh-workbench-host": "^0.30.0"
|
|
49
49
|
},
|
|
50
50
|
"dsh": {
|
|
51
51
|
"bundle": {
|