Skip to content

Commit

Permalink
🐞 fix(duplicate): 检查父文档树是否重复 close #175
Browse files Browse the repository at this point in the history
  • Loading branch information
frostime committed Nov 12, 2023
1 parent 0ba49b9 commit d24776d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
39 changes: 34 additions & 5 deletions src/func/dailynote/handle-duplicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async function mergeDocs(docs: DocBlock[], callback?: () => void) {
return latestDoc;
}

function buildShowDuplicateDocDom(docs: Block[], notebook: Notebook): string {
function buildShowDuplicateDocDom(docs: Block[], notebook: Notebook, ansestorDup?: boolean): string {

let confilctTable = [];
for (let doc of docs) {
Expand All @@ -65,6 +65,7 @@ function buildShowDuplicateDocDom(docs: Block[], notebook: Notebook): string {
style="margin: 0.5rem;"
>
${content}
${ansestorDup ? i18n.ConflictDiary.part3.join("\n") : ""}
</div>
<div class="fn__flex b3-label" style="border-top: 1px solid var(--b3-theme-surface-lighter);">
<div class="fn__flex-1"></div>
Expand All @@ -77,20 +78,40 @@ function buildShowDuplicateDocDom(docs: Block[], notebook: Notebook): string {
return html;
}

/**
* 检查这些文档是否祖先文档树不同
*/
function ifAncestorDiff(docs: DocBlock[]): boolean {
let paths: string[] = docs.map(doc => doc.path);
//trim base doc
paths = paths.map(path => path.slice(0, path.lastIndexOf("/")));
//check if identical
let identical = true;
for (let i = 1; i < paths.length; i++) {
if (paths[i] !== paths[0]) {
identical = false;
break;
}
}
return !identical;
}

/**
* 由于同步的问题,默认的笔记本中可能出现重复的日记,这里检查下是否有重复的日记
* @param notebook
* @param todayDiaryHpath
*/
export async function checkDuplicateDiary(): Promise<boolean> {
// ==================== 检查是否有重复 ====================
let notebook: Notebook = notebooks.default;
let hpath = notebook.dailynoteHpath!;
let docs = (await getDocsByHpath(hpath, notebook)) as DocBlock[];

if (docs.length <= 1) {
return false;
}
//莫名其妙出现了重复的 id, 所以还是去重一下

// ==================== 由于未知原因可能出现重复的 id, 需要去重 ====================
let idSet: Set<string> = new Set();
let uniqueDocs: Array<DocBlock> = [];
docs.forEach((doc) => {
Expand All @@ -105,16 +126,24 @@ export async function checkDuplicateDiary(): Promise<boolean> {
return false;
}

// ==================== 检查顶部文档树是否也有重复 ====================
const ascendantDiff = ifAncestorDiff(docs); //如果为 true,说明祖先的文档树也不同

// ==================== 合并今天的日记 ====================
console.warn(`Conflict daily note: ${notebook.name} ${hpath}`);
const html = buildShowDuplicateDocDom(docs, notebook);
const html = buildShowDuplicateDocDom(docs, notebook, ascendantDiff);
let dialog = new Dialog({
title: i18n.Name,
content: html,
width: isMobile ? "80%" : "50%",
});
let uniqueDNDoc = null;
dialog.element.querySelector("#merge")?.addEventListener("click", async () => {
uniqueDNDoc = mergeDocs(docs, () => { dialog.destroy() });
mergeDocs(docs, () => {
dialog.destroy();
if (ascendantDiff) {
showMessage("祖先", 10000, "info");
}
});
});
return true;
}
8 changes: 8 additions & 0 deletions src/i18n/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@
" You may have created a diary on another device before and then created a diary on this device, resulting in a synchronization conflict.",
" Please merge and delete redundant diaries manually: Open today's diary, locate the diary document, and the conflicting document will be nearby."
],
"part3": [
"<p style=\"font-weight: bold; color: red\">",
"We found that in addition to the dailynote document conflict, the parent document at the upper level seems to be in conflict as well, and \"Auto Merge\" cannot handle this complexity of merging document trees.",
"</p>",
"<p style=\"font-weight: bold; color: red\">",
"Of course, <u>you can still to use the \"Auto Merge\" feature</u> to merge dailynote, but it is recommended that you manually deal with duplicate parent documents after auto-merging.",
"</p>"
],
"AutoMerge": "Auto merge",
"HeadingMarkdown": "# Duplicate diaries are listed below",
"success": "Merge success",
Expand Down
8 changes: 8 additions & 0 deletions src/i18n/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@
"- 应该如何处理?",
" 请自行合并删除多余的日记: 打开今日的日记, 然后定位日记文档, 冲突的文档就在一旁"
],
"part3": [
"<p style=\"font-weight: bold; color: red\">",
"我们发现除了日记文档冲突外,上层的父文档似乎也出现了冲突,「自动合并」无法处理这种合并文档树的复杂情况。",
"</p>",
"<p style=\"font-weight: bold; color: red\">",
"当然,<u>你可以继续使用「自动合并」功能</u>来合并今日日记的文档,但是建议你在自动合并后手动处理一下重复的父文档。",
"</p>"
],
"AutoMerge": "自动合并",
"HeadingMarkdown": "# 以下为其他重复的日记",
"success": "自动合并成功",
Expand Down

0 comments on commit d24776d

Please sign in to comment.