import {Tree} from "../../util/Tree"; import {fetchPost} from "../../util/fetch"; import {Constants} from "../../constants"; import {hasClosestByClassName} from "../../protyle/util/hasClosest"; import {openMobileFileById} from "../editor"; import {confirmDialog} from "../../dialog/confirmDialog"; import {escapeHtml} from "../../util/escape"; import {Dialog} from "../../dialog"; export class MobileBookmarks { public element: HTMLElement; private tree: Tree; private openNodes: string[]; constructor() { this.element = document.querySelector('#sidebar [data-type="sidebar-bookmark"]'); this.element.innerHTML = `
${window.siyuan.languages.bookmark}
`; this.tree = new Tree({ element: this.element.querySelector(".bookmarkList") as HTMLElement, data: null, click: (element: HTMLElement, event: MouseEvent) => { const id = element.getAttribute("data-node-id"); const actionElement = hasClosestByClassName(event.target as HTMLElement, "b3-list-item__action"); if (actionElement) { const bookmark = (id ? element.parentElement.previousElementSibling : element).querySelector(".b3-list-item__text").textContent; if (actionElement.getAttribute("data-type") === "remove") { confirmDialog(window.siyuan.languages.deleteOpConfirm, `${window.siyuan.languages.confirmDelete} ${escapeHtml(bookmark)}?`, () => { if (id) { fetchPost("/api/attr/setBlockAttrs", {id, attrs: {bookmark: ""}}, () => { this.update(); }); document.querySelectorAll(`.protyle-wysiwyg [data-node-id="${id}"]`).forEach((item) => { item.setAttribute("bookmark", ""); const bookmarkElement = item.querySelector(".protyle-attr--bookmark"); if (bookmarkElement) { bookmarkElement.remove(); } }); } else { fetchPost("/api/bookmark/removeBookmark", {bookmark}); } }); } else { const dialog = new Dialog({ title: window.siyuan.languages.rename, content: `
`, width: "92vw", }); const btnsElement = dialog.element.querySelectorAll(".b3-button"); btnsElement[0].addEventListener("click", () => { dialog.destroy(); }); const inputElement = dialog.element.querySelector("input"); dialog.bindInput(inputElement, () => { (btnsElement[1] as HTMLButtonElement).click(); }); inputElement.value = bookmark; inputElement.focus(); inputElement.select(); btnsElement[1].addEventListener("click", () => { fetchPost("/api/bookmark/renameBookmark", { oldBookmark: bookmark, newBookmark: inputElement.value }, () => { dialog.destroy(); }); }); } } else { fetchPost("/api/block/checkBlockFold", {id}, (foldResponse) => { openMobileFileById(id, foldResponse.data ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL, Constants.CB_GET_HTML] : [Constants.CB_GET_FOCUS, Constants.CB_GET_SETID, Constants.CB_GET_CONTEXT, Constants.CB_GET_HTML]); }); } }, blockExtHTML: window.siyuan.config.readonly ? undefined : '', topExtHTML: window.siyuan.config.readonly ? undefined : '' }); this.element.addEventListener("click", (event) => { let target = event.target as HTMLElement; while (target && !target.isEqualNode(this.element)) { if (target.classList.contains("toolbar__icon")) { const type = target.getAttribute("data-type"); switch (type) { case "collapse": this.tree.collapseAll(); break; case "expand": this.tree.expandAll(); break; } } target = target.parentElement; } }); this.update(); } public update() { this.element.lastElementChild.classList.remove("fn__none"); fetchPost("/api/bookmark/getBookmark", {}, response => { if (this.openNodes) { this.openNodes = this.tree.getExpandIds(); } this.tree.updateData(response.data); if (this.openNodes) { this.tree.setExpandIds(this.openNodes); } else { this.openNodes = this.tree.getExpandIds(); } this.element.lastElementChild.classList.add("fn__none"); }); } }