/// #if !MOBILE import {Tab} from "../Tab"; import {setPanelFocus} from "../util"; import {getDockByType} from "../tabUtil"; /// #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 {getDisplayName, movePathTo, pathPosix} from "../../util/pathName"; import {App} from "../../index"; import {getCloudURL} from "../../config/util/about"; import {hasClosestByClassName} from "../../protyle/util/hasClosest"; import {escapeHtml} from "../../util/escape"; import {emitOpenMenu} from "../../plugin/EventBus"; export class Inbox extends Model { private element: Element; private selectIds: string[] = []; private currentPage = 1; private pageCount = 1; private data: { [key: string]: IInbox } = {}; constructor(app: App, tab: Tab | Element) { super({app, 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('[data-type="selectall"]'); this.element.lastElementChild.addEventListener("contextmenu", (event: MouseEvent) => { const itemElement = hasClosestByClassName(event.target as Element, "b3-list-item"); if (itemElement) { this.more(event, itemElement); } }); this.element.addEventListener("click", (event: MouseEvent) => { /// #if !MOBILE setPanelFocus(this.element); /// #endif let target = event.target as HTMLElement; while (target && !target.isEqualNode(this.element)) { if (target.tagName === "A") { event.stopPropagation(); break; } const type = target.getAttribute("data-type"); if (type === "min") { getDockByType("inbox").toggleModel("inbox"); event.preventDefault(); break; } else if (type === "selectall") { const useElement = target.querySelector("use"); if (useElement.getAttribute("xlink:href") === "#iconUncheck") { this.element.lastElementChild.querySelectorAll(".b3-list-item").forEach(item => { item.querySelector("use").setAttribute("xlink:href", "#iconCheck"); this.selectIds.push(item.getAttribute("data-id")); this.selectIds = [...new Set(this.selectIds)]; }); useElement.setAttribute("xlink:href", "#iconCheck"); } else { this.element.lastElementChild.querySelectorAll(".b3-list-item").forEach(item => { item.querySelector("use").setAttribute("xlink:href", "#iconUncheck"); this.selectIds.splice(this.selectIds.indexOf(item.getAttribute("data-id")), 1); }); useElement.setAttribute("xlink:href", "#iconUncheck"); } countElement.innerHTML = `${this.selectIds.length.toString()}/${this.pageCount.toString()}`; window.siyuan.menus.menu.remove(); event.stopPropagation(); break; } else if (type === "select") { const useElement = target.querySelector("use"); if (useElement.getAttribute("xlink:href") === "#iconUncheck") { this.selectIds.push(target.parentElement.getAttribute("data-id")); this.selectIds = [...new Set(this.selectIds)]; useElement.setAttribute("xlink:href", "#iconCheck"); } else { this.selectIds.splice(this.selectIds.indexOf(target.parentElement.getAttribute("data-id")), 1); useElement.setAttribute("xlink:href", "#iconUncheck"); } countElement.innerHTML = `${this.selectIds.length.toString()}/${this.pageCount.toString()}`; selectAllElement.querySelector("use").setAttribute("xlink:href", this.element.lastElementChild.querySelectorAll('[*|href="#iconCheck"]').length === this.element.lastElementChild.querySelectorAll(".b3-list-item").length ? "#iconCheck" : "#iconUncheck"); window.siyuan.menus.menu.remove(); event.stopPropagation(); break; } else if (type === "previous") { if (target.getAttribute("disabled") !== "disabled") { this.currentPage--; this.update(); } event.preventDefault(); break; } else if (type === "next") { if (target.getAttribute("disabled") !== "disabled") { this.currentPage++; this.update(); } event.preventDefault(); break; } else if (type === "back") { this.back(); event.preventDefault(); break; } else if (type === "more") { this.more(event); event.stopPropagation(); event.preventDefault(); break; } else if (target.classList.contains("b3-list-item")) { const data = this.data[target.getAttribute("data-id")]; selectAllElement.classList.add("fn__none"); this.element.firstElementChild.querySelector('[data-type="previous"]').classList.add("fn__none"); this.element.firstElementChild.querySelector('[data-type="next"]').classList.add("fn__none"); detailsElement.innerHTML = this.genDetail(data); detailsElement.setAttribute("data-id", data.oId); detailsElement.classList.remove("fn__none"); detailsElement.scrollTop = 0; this.element.lastElementChild.classList.add("fn__none"); event.preventDefault(); break; } target = target.parentElement; } }); this.update(); } private back() { this.element.firstElementChild.querySelector('[data-type="selectall"]').classList.remove("fn__none"); this.element.firstElementChild.querySelector('[data-type="previous"]').classList.remove("fn__none"); this.element.firstElementChild.querySelector('[data-type="next"]').classList.remove("fn__none"); this.element.querySelector(".inboxDetails").classList.add("fn__none"); this.element.lastElementChild.classList.remove("fn__none"); } private genDetail(data: IInbox) { let linkHTML = ""; /// #if MOBILE if (data.shorthandURL) { linkHTML = ` `; } return `
${data.shorthandTitle} ${linkHTML}
${data.shorthandContent}
`; /// #else if (data.shorthandURL) { linkHTML = ` `; } return `
${linkHTML}
${data.shorthandContent}
`; /// #endif } private genItemHTML(item: IInbox) { return `
  • ${item.shorthandTitle} ${item.hCreated}
  • `; } private more(event: MouseEvent, itemElement?: HTMLElement) { const detailsElement = this.element.querySelector(".inboxDetails"); window.siyuan.menus.menu.remove(); window.siyuan.menus.menu.append(new MenuItem({ label: window.siyuan.languages.refresh, icon: "iconRefresh", click: () => { if (itemElement) { fetchPost("/api/inbox/getShorthand", { id: itemElement.dataset.id }, (response) => { this.data[response.data.oId] = response.data; itemElement.outerHTML = this.genItemHTML(response.data); }); } else if (detailsElement.classList.contains("fn__none")) { this.currentPage = 1; this.update(); } else { fetchPost("/api/inbox/getShorthand", { id: detailsElement.getAttribute("data-id") }, (response) => { this.data[response.data.oId] = response.data; detailsElement.innerHTML = this.genDetail(response.data); detailsElement.scrollTop = 0; }); } } }).element); let ids: string[] = []; if (itemElement) { ids = [itemElement.dataset.id]; } else if (detailsElement.classList.contains("fn__none")) { ids = this.selectIds; } else { ids = [detailsElement.getAttribute("data-id")]; } if (ids.length > 0) { window.siyuan.menus.menu.append(new MenuItem({ label: window.siyuan.languages.move, icon: "iconMove", click: () => { this.move(ids); } }).element); window.siyuan.menus.menu.append(new MenuItem({ label: window.siyuan.languages.remove, icon: "iconTrashcan", click: () => { let removeTitle = ""; ids.forEach((id, index) => { removeTitle += '' + escapeHtml(this.data[id].shorthandTitle) + "" + (index === ids.length - 1 ? "" : ", "); }); confirmDialog(window.siyuan.languages.deleteOpConfirm, `${window.siyuan.languages.confirmDelete} ${removeTitle}?`, () => { if (itemElement) { this.remove(itemElement.dataset.id); } else if (detailsElement.classList.contains("fn__none")) { this.remove(); } else { this.remove(detailsElement.getAttribute("data-id")); } }, undefined, true); } }).element); } if (this.app.plugins) { emitOpenMenu({ plugins: this.app.plugins, type: "open-menu-inbox", detail: { ids, element: itemElement || detailsElement, }, separatorPosition: "top", }); } window.siyuan.menus.menu.popup({x: event.clientX, y: event.clientY + 16}); } 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.currentPage = 1; this.update(); }); } private move(ids: string[]) { movePathTo((toPath, toNotebook) => { ids.forEach(item => { fetchPost("/api/inbox/getShorthand", { id: item }, (response) => { this.data[response.data.oId] = response.data; fetchPost("/api/filetree/createDoc", { notebook: toNotebook[0], path: pathPosix().join(getDisplayName(toPath[0], false, true), Lute.NewNodeID() + ".sy"), title: replaceFileName(response.data.shorthandTitle), md: response.data.shorthandMd, }, () => { this.remove(item); }); }); }); }); } private update() { const loadingElement = this.element.querySelector(".fn__loading"); if (needSubscribe("")) { this.element.lastElementChild.innerHTML = ``; loadingElement.classList.add("fn__none"); return; } if (!loadingElement.classList.contains("fn__none")) { return; } loadingElement.classList.remove("fn__none"); fetchPost("/api/inbox/getShorthands", {page: this.currentPage}, (response) => { loadingElement.classList.add("fn__none"); 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('[data-type="selectall"] use').setAttribute("xlink:href", (this.element.lastElementChild.querySelectorAll('[*|href="#iconCheck"]').length === selectCount && selectCount !== 0) ? "#iconCheck" : "#iconUncheck"); this.element.lastElementChild.scrollTop = 0; }); } }