Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions plugins/live-markdown/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.1.1 - 2026-11-25

### Fixed

- 修复 WebDAV 同步时不同工作区的同名文件会映射到同一远端路径、互相覆盖的问题:本设备记录每个云端文件绑定的本地文件和上次同步的内容,上传或拉取前先核对;会覆盖未同步内容时按冲突跳过并提示原因。
- 本地与云端在上次同步后都被修改时视为冲突,两侧都不写入;升级后首次同步时,内容不一致且没有同步记录的文件会被拦下,内容一致的文件直接建立绑定。
- 保存后自动上传遇到同名冲突时也会提示,避免本地与云端悄悄分叉。

### Changed

- 拆分 WebDAV 同步模块为传输层、同步记录与冲突判定、同步流程三部分;每次上传前先读取云端内容用于核对。

## 0.1.0 - 2026-11-17

### 工作区多文件夹支持与默认工作区移除(2026-11-17)
Expand Down
4 changes: 2 additions & 2 deletions plugins/live-markdown/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/live-markdown/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ztools-live-markdown",
"version": "0.1.0",
"version": "0.1.1",
"private": true,
"type": "module",
"description": "High-performance live-preview Markdown editor built in React 19 & TypeScript for ZTools.",
Expand Down
2 changes: 1 addition & 1 deletion plugins/live-markdown/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"title": "Live Markdown",
"description": "高性能沉浸式所见即所得 Markdown 编辑器:支持实时预览/源码模式切换、大纲与文件树、全局搜索、数学公式、Mermaid 图表、交互式表格、专注模式、打字机模式与 6 套经典排版主题",
"author": "harris",
"version": "0.1.0",
"version": "0.1.1",
"main": "dist/index.html",
"preload": "preload/services.cjs",
"logo": "logo.svg",
Expand Down
147 changes: 147 additions & 0 deletions plugins/live-markdown/preload/webdav-sync-ledger.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
"use strict";

// WebDAV 同步台账与冲突守卫。
//
// 远端文件只按文件名寻址,不同工作区的同名文件会映射到同一个远端路径。
// 台账按「服务器 + 远端目录 + 文件名」记录本设备上次同步时绑定的本地文件和内容哈希,
// 任何写入(上传覆盖云端、拉取覆盖本地)都必须证明被覆盖的一侧正是上次同步的版本,
// 否则视为冲突并跳过,绝不静默覆盖。

const crypto = require("node:crypto");
const path = require("node:path");

const LEDGER_STORAGE_KEY = "live_markdown_webdav_sync_ledger_v1";

function hashContent(content) {
return crypto.createHash("sha256").update(String(content ?? ""), "utf8").digest("hex");
}

function normalizeLocalPath(filePath) {
if (!filePath || typeof filePath !== "string") return null;
const resolved = path.resolve(filePath);
return process.platform === "win32" ? resolved.toLowerCase() : resolved;
}

function remoteKey(config, fileName) {
const server = String(config.serverUrl || "").trim().replace(/\/+$/, "");
return `${server}|${config.remotePath}|${fileName}`;
}

function resolveDefaultStorage() {
const ztools = typeof window !== "undefined" ? window.ztools : null;
if (ztools?.dbStorage && typeof ztools.dbStorage.getItem === "function" && typeof ztools.dbStorage.setItem === "function") {
return ztools.dbStorage;
}
if (typeof localStorage !== "undefined" && typeof localStorage.getItem === "function" && typeof localStorage.setItem === "function") {
return localStorage;
}
return null;
}

function createMemoryStorage() {
const map = new Map();
return {
getItem: (key) => (map.has(key) ? map.get(key) : null),
setItem: (key, value) => {
map.set(key, String(value));
}
};
}

function createWebdavSyncLedger(storage) {
const store = storage || resolveDefaultStorage() || createMemoryStorage();

// 台账损坏时按“没有任何同步记录”处理:只会产生更多冲突提示,不会放行覆盖。
function readAll() {
try {
const raw = store.getItem(LEDGER_STORAGE_KEY);
if (!raw) return {};
const parsed = typeof raw === "string" ? JSON.parse(raw) : raw;
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
} catch {
return {};
}
}

function writeAll(entries) {
store.setItem(LEDGER_STORAGE_KEY, JSON.stringify(entries));
}

return {
get(config, fileName) {
const entry = readAll()[remoteKey(config, fileName)];
return entry && typeof entry === "object" ? entry : null;
},
record(config, fileName, { localPath, contentHash }) {
const all = readAll();
// 本地路径为空(未保存文档)表示“未绑定”:守卫只按内容哈希判定,之后另存为的文件可以正常接管。
all[remoteKey(config, fileName)] = {
localPath: normalizeLocalPath(localPath),
contentHash,
syncedAt: Date.now()
};
writeAll(all);
},
forget(config, fileName) {
const all = readAll();
delete all[remoteKey(config, fileName)];
writeAll(all);
}
};
}

/** 拉取:云端内容将写入 localPath。localHash 为 null 表示本地文件不存在。 */
function decidePullWrite({ binding, localPath, localHash, remoteHash }) {
if (localHash === null || localHash === remoteHash) return { allowed: true };
const normalized = normalizeLocalPath(localPath);
if (binding && binding.localPath && binding.localPath !== normalized) {
return { allowed: false, reason: "bound-elsewhere" };
}
if (binding && binding.localPath === normalized) {
return binding.contentHash === localHash
? { allowed: true }
: { allowed: false, reason: "local-modified" };
}
return { allowed: false, reason: "local-unknown" };
}

/** 上传:localPath 的内容将覆盖云端。remoteHash 为 null 表示云端文件不存在。 */
function decidePushWrite({ binding, localPath, localHash, remoteHash }) {
if (remoteHash === null || remoteHash === localHash) return { allowed: true };
if (!binding) return { allowed: false, reason: "unknown-remote" };
const normalized = normalizeLocalPath(localPath);
if (binding.localPath && binding.localPath !== normalized) {
return { allowed: false, reason: "bound-elsewhere" };
}
return binding.contentHash === remoteHash
? { allowed: true }
: { allowed: false, reason: "remote-changed" };
}

function describeConflict(direction, fileName, reason) {
const action = direction === "push" ? "上传" : "拉取";
switch (reason) {
case "bound-elsewhere":
return `《${fileName}》已与另一个本地文件同步(可能是其他工作区的同名文件),已跳过${action}以免互相覆盖。请重命名其中一个文件,或为该工作区使用独立的 WebDAV 远端目录。`;
case "remote-changed":
return `云端《${fileName}》在上次同步后已被其他设备或工作区修改,已跳过上传以免覆盖云端内容,请先核对差异。`;
case "unknown-remote":
return `云端已存在内容不同的《${fileName}》,且本设备没有它的同步记录,已跳过上传以免覆盖云端内容。确认以本地为准时,可先在 WebDAV 面板删除云端副本再同步。`;
case "local-modified":
return `本地《${fileName}》有尚未同步的修改,与云端内容不一致,已跳过拉取以免覆盖本地文件。`;
case "local-unknown":
default:
return `本地已存在内容不同的《${fileName}》,且本设备没有它的同步记录,已跳过${action}以免覆盖。确认以云端为准时,可先移走或重命名本地文件再拉取。`;
}
}

module.exports = {
LEDGER_STORAGE_KEY,
hashContent,
normalizeLocalPath,
createWebdavSyncLedger,
createMemoryStorage,
decidePullWrite,
decidePushWrite,
describeConflict
};
Loading
Loading