An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
import {toggleMenu} from "../../menus/menuToggle";
/// #if !MOBILE
import {Tab} from "../Tab";
import {setPanelFocus} from "../util";
import {getActiveTab, getDockByType} from "../tabUtil";
import {Editor} from "../../editor";
/// #endif
import {fetchPost, fetchSyncPost} from "../../util/fetch";
import {isInIOS, updateHotkeyAfterTip} from "../../protyle/util/compatibility";
import {Model} from "../Model";
import {needSubscribe} from "../../util/needSubscribe";
import {MenuItem} from "../../menus/Menu";
import {confirmDialog} from "../../dialog/confirmDialog";
import {normalizeDocumentTitle} from "../../editor/rename";
import {movePathTo} from "../../util/pathName";
import {documentCreateRequest} from "../../util/documentCreate";
import {createConfiguredDocument} from "../../util/configuredDocument";
import type {App} from "../../index";
import {getCloudURL} from "../../config/util/about";
import {hasClosestByClassName} from "../../protyle/util/hasClosest";
import {escapeHtml, escapeMarkdownPlainText} from "../../util/escape";
import {emitOpenMenu} from "../../plugin/EventBus";
import {sanitizeKernelHTML} from "../../util/hostCapabilities";
import {showMessage} from "../../dialog/message";
export class Inbox extends Model {
private element: Element;
private selectIds: string[] = [];
private currentPage = 1;
private pageCount = 1;
private data: { [key: string]: IInbox } = {};
private moveTargets = new Map<string, {notebook: string; path: string; id: string; committed?: boolean}>();
private moving = false;
constructor(app: App, tab: Tab | Element) {
super({app});
if (tab instanceof Element) {
this.element = tab;
} else {
this.element = tab.panelElement;
}
/// #if MOBILE
this.element.innerHTML = `<div class="toolbar toolbar--dark">
<div class="fn__space"></div>
<div class="toolbar__text">
${window.siyuan.languages.inbox}
<span class="fn__space"></span>
<span class="inboxSelectCount ft__smaller ft__on-surface"></span>
</div>
<span class="fn__space"></span>
<svg data-type="refresh" class="toolbar__icon"><use xlink:href="#iconRefresh"></use></svg>
<svg data-type="selectall" class="toolbar__icon"><use xlink:href="#iconUncheck"></use></svg>
<svg data-type="previous" disabled="disabled" class="toolbar__icon"><use xlink:href='#iconLeft'></use></svg>
<svg data-type="next" disabled="disabled" class="toolbar__icon"><use xlink:href='#iconRight'></use></svg>
<svg data-type="more" class="toolbar__icon fn__none"><use xlink:href='#iconMore'></use></svg>
</div>
<div class="fn__loading fn__none">
<span class="b3-loading" aria-hidden="true"></span></div>
</div>
<div class="fn__flex-1 fn__none inboxDetails fn__flex-column" style="min-height: auto;background-color: var(--b3-theme-background)"></div>
<div class="fn__flex-1"></div>`;
/// #else
this.element.classList.add("fn__flex-column", "file-tree", "sy__inbox", "dockPanel");
this.element.innerHTML = `<div class="block__icons">
<div class="block__logo fn__flex-1">
${window.siyuan.languages.inbox}
<span class="inboxSelectCount"></span>
</div>
<span data-type="selectall" class="block__icon"><svg><use xlink:href="#iconUncheck"></use></svg></span>
<span class="fn__space"></span>
<span data-type="previous" class="block__icon ariaLabel" disabled="disabled" data-position="north" aria-label="${window.siyuan.languages.previousLabel}"><svg><use xlink:href="#iconLeft"></use></svg></span>
<span class="fn__space"></span>
<span data-type="next" class="block__icon ariaLabel" disabled="disabled" data-position="north" aria-label="${window.siyuan.languages.nextLabel}"><svg><use xlink:href="#iconRight"></use></svg></span>
<span class="fn__space"></span>
<span data-type="more" data-menu="true" class="block__icon ariaLabel" data-position="north" aria-label="${window.siyuan.languages.more}"><svg><use xlink:href="#iconMore"></use></svg></span>
<span class="fn__space"></span>
<span data-type="min" class="block__icon ariaLabel" data-position="north" aria-label="${window.siyuan.languages.min}${updateHotkeyAfterTip(window.siyuan.config.keymap.general.closeTab.custom)}"><svg><use xlink:href="#iconMin"></use></svg></span>
</div>
<div class="fn__loading fn__none">
<span class="b3-loading" aria-hidden="true"></span></div>
</div>
<div class="fn__flex-1 fn__none inboxDetails fn__flex-column" style="min-height: auto;background-color: var(--b3-theme-background)"></div>
<div class="fn__flex-1"></div>`;
/// #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", false, true);
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()}`;
this.updateMoreVisibility();
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()}`;
this.updateMoreVisibility();
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 === "refresh") {
this.refresh();
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");
this.updateMoreVisibility();
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");
this.updateMoreVisibility();
}
private updateMoreVisibility() {
/// #if MOBILE
const detailsElement = this.element.querySelector(".inboxDetails");
this.element.firstElementChild.querySelector('[data-type="more"]').classList.toggle("fn__none",
detailsElement.classList.contains("fn__none") && this.selectIds.length === 0);
/// #endif
}
private genDetail(data: IInbox) {
let linkHTML = "";
/// #if MOBILE
if (data.shorthandURL) {
linkHTML = `<a href="${data.shorthandURL}" target="_blank">
<svg class="toolbar__icon" style="float: left"><use xlink:href="#iconLink"></use></svg>
</a>`;
}
return sanitizeKernelHTML(`<div class="toolbar">
<svg data-type="back" class="toolbar__icon"><use xlink:href="#iconLeft"></use></svg>
<span data-type="back" class="toolbar__text fn__flex-1">${data.shorthandTitle}</span>
${linkHTML}
</div>
<div class="b3-typography b3-typography--default" style="padding: 0 8px 8px">
${data.shorthandContent}
</div>`);
/// #else
if (data.shorthandURL) {
linkHTML = `<span class="fn__space"></span><a href="${data.shorthandURL}" target="_blank" class="block__icon block__icon--show ariaLabel" data-position="north" aria-label="${window.siyuan.languages.link}">
<svg><use xlink:href="#iconLink"></use></svg>
</a>`;
}
return sanitizeKernelHTML(`<div class="block__icons">
<div class="block__logo fn__pointer fn__flex-1" data-type="back">
<svg class="block__logoicon"><use xlink:href="#iconLeft"></use></svg><span class="ft__breakword">${data.shorthandTitle}</span>
</div>
${linkHTML}
</div>
<div class="b3-typography b3-typography--default" style="padding: 0 8px 8px;user-select: text" data-type="textMenu">
${data.shorthandContent}
</div>`);
/// #endif
}
private genItemHTML(item: IInbox) {
return sanitizeKernelHTML(`<li style="padding-left: 0" data-id="${item.oId}" class="b3-list-item">
<span data-type="select" class="b3-list-item__action">
<svg><use xlink:href="#icon${this.selectIds.includes(item.oId) ? "Check" : "Uncheck"}"></use></svg>
</span>
<span class="fn__space--small"></span>
<span class="b3-list-item__text" title="${item.shorthandTitle}${item.shorthandTitle === item.shorthandDesc ? "" : "\n" + item.shorthandDesc}">${item.shorthandTitle}</span>
<span class="b3-list-item__meta">${item.hCreated}</span>
</li>`);
}
private more(event: MouseEvent, itemElement?: HTMLElement) {
toggleMenu({
target: (event.target as Element).closest(".block__icon") || itemElement,
toggle: event.type !== "contextmenu",
build: (_menu, session) => {
const detailsElement = this.element.querySelector(".inboxDetails");
/// #if !MOBILE
window.siyuan.menus.menu.append(new MenuItem({
label: window.siyuan.languages.refresh,
icon: "iconRefresh",
click: () => {
this.refresh(itemElement);
}
}).element);
/// #endif
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);
let protyle: IProtyle;
/// #if MOBILE
protyle = window.siyuan.mobile.editor?.protyle;
/// #else
const tab = getActiveTab(false);
if (tab?.model instanceof Editor) {
protyle = tab.model.editor?.protyle;
}
/// #endif
if (protyle?.block.rootID && !protyle.disabled && !window.siyuan.config.readonly && !window.siyuan.isPublish) {
window.siyuan.menus.menu.append(new MenuItem({
label: window.siyuan.languages.insertToCurrentDoc,
icon: "iconAdd",
click: () => {
void this.insertToCurrentDoc(ids, protyle.block.rootID);
}
}).element);
}
window.siyuan.menus.menu.append(new MenuItem({
label: window.siyuan.languages.remove,
icon: "iconTrashcan",
click: () => {
let removeTitle = "";
ids.forEach((id, index) => {
removeTitle += '<code class="fn__code">' + escapeHtml(this.data[id].shorthandTitle) + "</code>" + (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);
}
emitOpenMenu({
type: "open-menu-inbox",
detail: {
ids,
element: itemElement || detailsElement,
},
separatorPosition: "top",
});
const button = (event.target as Element).closest("[data-type='more']");
const rect = (itemElement || button)?.getBoundingClientRect();
session.show(() => window.siyuan.menus.menu.popup({
x: !itemElement && rect ? rect.left : event.clientX,
y: rect ? rect.bottom : event.clientY + 16,
h: rect ? rect.height : 0,
}));
},
});
}
private refresh(itemElement?: HTMLElement) {
const detailsElement = this.element.querySelector(".inboxDetails");
if (itemElement) {
fetchPost("/api/inbox/getShorthand", {id: itemElement.dataset.id}, (response) => {
if (response.code !== 0 || !response.data) {
return;
}
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) => {
if (response.code !== 0 || !response.data) {
return;
}
this.data[response.data.oId] = response.data;
detailsElement.innerHTML = this.genDetail(response.data);
detailsElement.scrollTop = 0;
});
}
}
private remove(removeIds?: string[]) {
if (!removeIds) {
removeIds = this.selectIds;
}
fetchPost("/api/inbox/removeShorthands", {ids: removeIds}, (response) => {
if (response.code !== 0) {
return;
}
for (const id of removeIds || this.selectIds) {
this.moveTargets.delete(id);
}
if (removeIds) {
this.back();
for (let i = this.selectIds.length - 1; i >= 0; i--) {
if (removeIds.includes(this.selectIds[i])) {
this.selectIds.splice(i, 1);
}
}
this.updateMoreVisibility();
} else {
this.selectIds = [];
}
this.currentPage = 1;
this.update();
});
}
private move(ids: string[]) {
movePathTo({
cb: async (toPath, toNotebook) => {
if (this.moving) {
return;
}
this.moving = true;
const created: string[] = [];
let finished = false;
try {
for (let i = 0; i < ids.length; i++) {
const idItem = ids[i];
let target = this.moveTargets.get(idItem);
if (target && (target.notebook !== toNotebook[0] || target.path !== toPath[0])) {
showMessage(`${escapeHtml(window.siyuan.languages.documentCreationRecovery)} ` +
`<a href="siyuan://blocks/${target.id}">${target.id}</a>`, 0, "error");
break;
}
if (target?.committed) {
created.push(idItem);
continue;
}
const response = await fetchSyncPost("/api/inbox/getShorthand", {
id: idItem
});
if (response.code !== 0 || !response.data) {
break;
}
this.data[response.data.oId] = response.data;
let md = response.data.shorthandMd;
if ("" === md && "" === response.data.shorthandContent && "" != response.data.shorthandURL) {
md = "[" + response.data.shorthandTitle + "](" + response.data.shorthandURL + ")";
}
if (!target) {
if ([...this.moveTargets.values()].filter(value => !value.committed).length >= 32) {
showMessage(window.siyuan.languages.documentCreationLimit, 0, "error");
break;
}
target = {notebook: toNotebook[0], path: toPath[0], id: Lute.NewNodeID()};
this.moveTargets.set(idItem, target);
}
const request = documentCreateRequest(target.notebook, target.path, target.id,
normalizeDocumentTitle(response.data.shorthandTitle), md);
let completed = false;
await createConfiguredDocument({
notebook: target.notebook, contextID: request.parentID, purpose: "document",
placement: "child", id: target.id, title: request.title.text,
}, {
plan: {notebook: target.notebook, title: request.title.text, steps: [request]},
listDocTree: true,
onCreated: () => {
target.committed = true;
completed = true;
if (finished) {
this.remove([idItem]);
} else {
created.push(idItem);
}
},
});
if (!completed) {
break;
}
}
} catch (error) {
showMessage(escapeHtml(error instanceof Error ? error.message : String(error)), 0, "error");
} finally {
this.moving = false;
finished = true;
if (created.length) {
this.remove(created);
}
}
},
flashcard: false
});
}
private async insertToCurrentDoc(ids: string[], rootID: string) {
const insertedIds: string[] = [];
try {
for (const id of [...ids]) {
const shorthand = await fetchSyncPost("/api/inbox/getShorthand", {id});
if (shorthand.code !== 0 || !shorthand.data) {
break;
}
let md = shorthand.data.shorthandMd;
if (!md && !shorthand.data.shorthandContent && shorthand.data.shorthandURL) {
md = `[${shorthand.data.shorthandTitle}](${shorthand.data.shorthandURL})`;
}
const title = escapeMarkdownPlainText(shorthand.data.shorthandTitle.replace(/[\r\n]+/g, " ").trim());
if (title) {
md = `# ${title}\n\n${md}`;
}
if (!md.trim()) {
showMessage(window.siyuan.languages.empty);
break;
}
const response = await fetchSyncPost("/api/block/appendBlock", {
dataType: "markdown",
data: md,
parentID: rootID,
});
if (response.code !== 0) {
break;
}
insertedIds.push(id);
}
} catch (error) {
showMessage((error as Error).message, 6000, "error");
}
if (insertedIds.length > 0) {
this.remove(insertedIds);
}
}
private update() {
const loadingElement = this.element.querySelector(".fn__loading");
if (needSubscribe("")) {
this.element.lastElementChild.innerHTML = `<ul class="b3-list b3-list--background">
<li class="b3-list--empty">
${window.siyuan.languages.inboxTip}
</li>
<li class="b3-list--empty">
${isInIOS() ? window.siyuan.languages._kernel[295] : window.siyuan.languages._kernel[29].replaceAll("${accountServer}", getCloudURL(""))}
</li>
</ul>`;
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");
if (response.code !== 0 || !response.data) {
return;
}
let html = "";
if (response.data.data.shorthands.length === 0) {
html = `<ul class="b3-list b3-list--background"><li class="b3-list--empty">${window.siyuan.languages.inboxTip}</li></ul>`;
} else {
html = '<ul style="padding: 8px 0" class="b3-list b3-list--background">';
response.data.data.shorthands.forEach((item: IInbox) => {
html += this.genItemHTML(item);
this.data[item.oId] = item;
});
html += "</ul>";
}
this.element.lastElementChild.innerHTML = html;
this.pageCount = response.data.data.pagination.paginationRecordCount;
this.element.querySelector(".inboxSelectCount").innerHTML = `${this.selectIds.length}/${this.pageCount}`;
this.updateMoreVisibility();
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;
});
}
}