/// #if !MOBILE import {Tab} from "../Tab"; import {getDockByType, setPanelFocus} from "../util"; /// #endif import {fetchPost} from "../../util/fetch"; import {updateHotkeyTip} from "../../protyle/util/compatibility"; import {Model} from "../Model"; import {needSubscribe} from "../../util/needSubscribe"; import {MenuItem} from "../../menus/Menu"; import {confirmDialog} from "../../dialog/confirmDialog"; import {replaceFileName} from "../../editor/rename"; import {escapeHtml} from "../../util/escape"; import {unicode2Emoji} from "../../emoji"; import {Constants} from "../../constants"; import {isMobile} from "../../util/functions"; export class Inbox extends Model { private element: Element; private selectIds: string[] = []; private currentPage = 1; private pageCount = 1; private data: { [key: string]: IInbox } = {}; constructor(tab: Tab | Element) { super({id: tab.id}); if (tab instanceof Element) { this.element = tab; } else { this.element = tab.panelElement; } /// #if MOBILE this.element.innerHTML = `
${window.siyuan.languages.inbox}
`; /// #else this.element.classList.add("fn__flex-column", "file-tree", "sy__inbox"); this.element.innerHTML = `
`; /// #endif const countElement = this.element.querySelector(".inboxSelectCount"); const detailsElement = this.element.querySelector(".inboxDetails"); const selectAllElement = this.element.firstElementChild.querySelector("input"); this.element.addEventListener("click", (event: MouseEvent) => { /// #if !MOBILE setPanelFocus(this.element); /// #endif let target = event.target as HTMLElement; while (target && !target.isEqualNode(this.element)) { const type = target.getAttribute("data-type"); if (type === "min") { getDockByType("inbox").toggleModel("inbox"); event.stopPropagation(); event.preventDefault(); break; } else if (type === "selectall") { if ((target as HTMLInputElement).checked) { this.element.lastElementChild.querySelectorAll(".b3-list-item").forEach(item => { item.querySelector("input").checked = true; this.selectIds.push(item.getAttribute("data-id")); this.selectIds = [...new Set(this.selectIds)]; }); } else { this.element.lastElementChild.querySelectorAll(".b3-list-item").forEach(item => { item.querySelector("input").checked = false; this.selectIds.splice(this.selectIds.indexOf(item.getAttribute("data-id")), 1); }); } this.updateAction(); countElement.innerHTML = `${this.selectIds.length.toString()}/${this.pageCount.toString()}`; event.stopPropagation(); break; } else if (type === "select") { if ((target.firstElementChild.nextElementSibling as HTMLInputElement).checked) { this.selectIds.push(target.parentElement.getAttribute("data-id")); this.selectIds = [...new Set(this.selectIds)]; } else { this.selectIds.splice(this.selectIds.indexOf(target.parentElement.getAttribute("data-id")), 1); } this.updateAction(); countElement.innerHTML = `${this.selectIds.length.toString()}/${this.pageCount.toString()}`; selectAllElement.checked = this.element.lastElementChild.querySelectorAll("input:checked").length === this.element.lastElementChild.querySelectorAll(".b3-list-item").length; event.stopPropagation(); break; } else if (type === "previous") { if (target.getAttribute("disabled") !== "disabled") { this.currentPage--; this.update(); } event.stopPropagation(); event.preventDefault(); break; } else if (type === "next") { if (target.getAttribute("disabled") !== "disabled") { this.currentPage++; this.update(); } event.stopPropagation(); event.preventDefault(); break; } else if (type === "refresh") { this.currentPage = 1; this.update(); event.stopPropagation(); event.preventDefault(); break; } else if (type === "back") { this.back(); event.stopPropagation(); event.preventDefault(); break; } else if (type === "more") { this.more(event); event.stopPropagation(); event.preventDefault(); break; } else if (type === "refreshDetails") { fetchPost("/api/inbox/getShorthand", { id: detailsElement.getAttribute("data-id") }, (response) => { detailsElement.innerHTML = `

${response.data.shorthandTitle}

${response.data.shorthandURL}
${(Lute.New()).MarkdownStr("", response.data.shorthandContent)}
`; detailsElement.scrollTop = 0; }); event.stopPropagation(); event.preventDefault(); break; } else if (type === "delete") { confirmDialog(window.siyuan.languages.deleteOpConfirm, window.siyuan.languages.confirmDelete + "?", () => { this.remove(detailsElement.getAttribute("data-id")); }); event.stopPropagation(); event.preventDefault(); break; } else if (type === "move") { window.siyuan.menus.menu.remove(); window.siyuan.notebooks.forEach((item) => { if (!item.closed) { window.siyuan.menus.menu.append(new MenuItem({ iconHTML: `${unicode2Emoji(item.icon || Constants.SIYUAN_IMAGE_NOTE, false, "b3-menu__icon", true)}`, label: escapeHtml(item.name), click: () => { this.move(item.id, detailsElement.getAttribute("data-id")); } }).element); } }); window.siyuan.menus.menu.popup({x: event.clientX, y: event.clientY}); window.siyuan.menus.menu.element.style.zIndex = "221"; // 移动端被右侧栏遮挡 event.stopPropagation(); event.preventDefault(); break; } else if (target.classList.contains("b3-list-item")) { const data = this.data[target.getAttribute("data-id")]; this.element.querySelector('[data-type="back"]').parentElement.classList.remove("fn__none"); this.element.querySelector('[data-type="more"]').parentElement.classList.add("fn__none"); detailsElement.innerHTML = `

${data.shorthandTitle}

${data.shorthandURL}
${(Lute.New()).MarkdownStr("", data.shorthandContent)}
`; detailsElement.setAttribute("data-id", data.oId); detailsElement.classList.remove("fn__none"); detailsElement.scrollTop = 0; this.element.lastElementChild.classList.add("fn__none"); event.stopPropagation(); event.preventDefault(); break; } target = target.parentElement; } }); this.update(); /// #if !MOBILE setPanelFocus(this.element); /// #endif } private updateAction() { if (this.selectIds.length === 0) { this.element.querySelector('[data-type="refresh"]').classList.remove("fn__none"); this.element.querySelector('[data-type="more"]').classList.add("fn__none"); } else { this.element.querySelector('[data-type="refresh"]').classList.add("fn__none"); this.element.querySelector('[data-type="more"]').classList.remove("fn__none"); } } private back() { this.element.querySelector('[data-type="back"]').parentElement.classList.add("fn__none"); this.element.querySelector('[data-type="more"]').parentElement.classList.remove("fn__none"); this.element.querySelector(".inboxDetails").classList.add("fn__none"); this.element.lastElementChild.classList.remove("fn__none"); } private more(event: MouseEvent) { window.siyuan.menus.menu.remove(); window.siyuan.menus.menu.append(new MenuItem({ label: window.siyuan.languages.refresh, icon: "iconRefresh", click: () => { this.currentPage = 1; this.update(); } }).element); const submenu: IMenu[] = []; window.siyuan.notebooks.forEach((item) => { if (!item.closed) { submenu.push({ iconHTML: `${unicode2Emoji(item.icon || Constants.SIYUAN_IMAGE_NOTE, false, "b3-menu__icon", true)}`, label: escapeHtml(item.name), click: () => { this.move(item.id); } }); } }); window.siyuan.menus.menu.append(new MenuItem({ label: window.siyuan.languages.move, icon: "iconMove", submenu }).element); window.siyuan.menus.menu.append(new MenuItem({ label: window.siyuan.languages.remove, icon: "iconTrashcan", click: () => { confirmDialog(window.siyuan.languages.deleteOpConfirm, window.siyuan.languages.confirmDelete + "?", () => { this.remove(); }); } }).element); window.siyuan.menus.menu.popup({x: event.clientX, y: event.clientY}); window.siyuan.menus.menu.element.style.zIndex = "221"; // 移动端被右侧栏遮挡 } private remove(id?: string) { let ids: string[]; if (id) { ids = [id]; } else { ids = this.selectIds; } fetchPost("/api/inbox/removeShorthands", {ids}, () => { if (id) { this.back(); this.selectIds.find((item, index) => { if (item === id) { this.selectIds.splice(index, 1); return true; } }); } else { this.selectIds = []; } this.updateAction(); this.currentPage = 1; this.update(); }); } private move(notebookId: string, id?: string) { let ids: string[]; if (id) { ids = [id]; } else { ids = this.selectIds; } ids.forEach(item => { fetchPost("/api/filetree/createDoc", { notebook: notebookId, path: `/${Lute.NewNodeID()}.sy`, title: replaceFileName(this.data[item].shorthandTitle), md: this.data[item].shorthandContent, }, () => { this.remove(item); }); }); } private update() { if (needSubscribe("")) { this.element.lastElementChild.innerHTML = ``; return; } const refreshElement = this.element.querySelector(`[data-type="refresh"]${isMobile() ? "" : " svg"}`); if (refreshElement.classList.contains("fn__rotate")) { return; } refreshElement.classList.add("fn__rotate"); fetchPost("/api/inbox/getShorthands", {page: this.currentPage}, (response) => { refreshElement.classList.remove("fn__rotate"); let html = ""; if (response.data.data.shorthands.length === 0) { html = ''; } else { html = '"; } this.element.lastElementChild.innerHTML = html; this.pageCount = response.data.data.pagination.paginationRecordCount; this.element.querySelector(".inboxSelectCount").innerHTML = `${this.selectIds.length}/${this.pageCount}`; const previousElement = this.element.querySelector('[data-type="previous"]'); const nextElement = this.element.querySelector('[data-type="next"]'); if (response.data.data.pagination.paginationPageCount > this.currentPage) { nextElement.removeAttribute("disabled"); } else { nextElement.setAttribute("disabled", "disabled"); } if (this.currentPage === 1) { previousElement.setAttribute("disabled", "disabled"); } else { previousElement.removeAttribute("disabled"); } const selectCount = this.element.lastElementChild.querySelectorAll(".b3-list-item").length; this.element.firstElementChild.querySelector("input").checked = this.element.lastElementChild.querySelectorAll("input:checked").length === selectCount && selectCount !== 0; this.element.lastElementChild.scrollTop = 0; }); } }