import { App, Modal } from "obsidian"; import { DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT } from "diff-match-patch"; import { diff_result } from "./lib/src/types"; import { escapeStringToHTML } from "./lib/src/utils"; export class ConflictResolveModal extends Modal { // result: Array<[number, string]>; result: diff_result; callback: (remove_rev: string) => Promise; constructor(app: App, diff: diff_result, callback: (remove_rev: string) => Promise) { super(app); this.result = diff; this.callback = callback; } onOpen() { const { contentEl } = this; contentEl.empty(); contentEl.createEl("h2", { text: "This document has conflicted changes." }); const div = contentEl.createDiv(""); div.addClass("op-scrollable"); let diff = ""; for (const v of this.result.diff) { const x1 = v[0]; const x2 = v[1]; if (x1 == DIFF_DELETE) { diff += "" + escapeStringToHTML(x2) + ""; } else if (x1 == DIFF_EQUAL) { diff += "" + escapeStringToHTML(x2) + ""; } else if (x1 == DIFF_INSERT) { diff += "" + escapeStringToHTML(x2) + ""; } } diff = diff.replace(/\n/g, "
"); div.innerHTML = diff; const div2 = contentEl.createDiv(""); const date1 = new Date(this.result.left.mtime).toLocaleString(); const date2 = new Date(this.result.right.mtime).toLocaleString(); div2.innerHTML = ` A:${date1}
B:${date2}
`; contentEl.createEl("button", { text: "Keep A" }, (e) => { e.addEventListener("click", async () => { await this.callback(this.result.right.rev); this.callback = null; this.close(); }); }); contentEl.createEl("button", { text: "Keep B" }, (e) => { e.addEventListener("click", async () => { await this.callback(this.result.left.rev); this.callback = null; this.close(); }); }); contentEl.createEl("button", { text: "Concat both" }, (e) => { e.addEventListener("click", async () => { await this.callback(""); this.callback = null; this.close(); }); }); contentEl.createEl("button", { text: "Not now" }, (e) => { e.addEventListener("click", () => { this.close(); }); }); } onClose() { const { contentEl } = this; contentEl.empty(); if (this.callback != null) { this.callback(null); } } }