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 {confirmDialog} from "../dialog/confirmDialog";
import {showMessage} from "../dialog/message";
import {progressLoading} from "../dialog/progress";
import {waitForPendingTransactions} from "../protyle/util/transactionQueue";
import {fetchSyncPost} from "./fetch";
export interface IBlockRefCheckOptions {
scope: "blocks" | "documents" | "notebook";
ids?: string[];
exactIDs?: string[];
deletedIDs?: string[];
paths?: string[];
notebook?: string;
}
const pendingChecks = new WeakSet<IProtyle>();
let activeChecks = 0;
let progressTimeout: number;
const progressID = "checkBlockRefProgress";
export const checkBlockRef = async (options: IBlockRefCheckOptions, protyle?: IProtyle) => {
if (window.siyuan.config.editor.checkBlockRef === false) {
return false;
}
activeChecks++;
try {
if (activeChecks === 1) {
// 检查持续一段时间后再显示遮罩,避免快速删除或剪切时闪屏。
progressTimeout = window.setTimeout(() => {
progressLoading({code: 1, msg: window.siyuan.languages.checkBlockRefProgress}, progressID);
}, 300);
}
if (protyle) {
await waitForPendingTransactions(protyle);
}
// process=false:该请求由本函数统一处理错误提示,避免 fetchSyncPost 内部再弹一次
const response = await fetchSyncPost("/api/block/checkBlockRef", options, undefined, false);
if (response.code !== 0) {
// 检查失败时给出内核原因(例如加密笔记本已锁定);字段校验类报错属于调用方参数问题,不打扰用户
if (response.msg && !response.msg.startsWith("Field [")) {
showMessage(response.msg, 6000, "error");
}
return;
}
return response.data === true;
} catch (error) {
console.warn("Check block ref failed:", error);
showMessage(error instanceof Error ? error.message : String(error), 7000, "error");
} finally {
activeChecks--;
if (activeChecks === 0) {
window.clearTimeout(progressTimeout);
progressLoading({code: 2, msg: ""}, progressID);
}
}
};
export const confirmBlockRef = async (options: IBlockRefCheckOptions, protyle?: IProtyle) => {
if (protyle?.lite) {
return true;
}
if (protyle && pendingChecks.has(protyle)) {
return false;
}
if (protyle) {
pendingChecks.add(protyle);
}
try {
const hasRef = await checkBlockRef(options, protyle);
if (hasRef === undefined) {
return false;
}
if (!hasRef) {
return true;
}
return await new Promise<boolean>((resolve) => {
confirmDialog(window.siyuan.languages.deleteOpConfirm, window.siyuan.languages.deleteRefConfirm,
() => resolve(true), () => resolve(false), true);
});
} finally {
if (protyle) {
pendingChecks.delete(protyle);
}
}
};
export const getBlockRefWarningHTML = () => {
return `<div class="fn__hr"></div>
<div class="ft__smaller ft__on-surface">${window.siyuan.languages.deleteRefConfirm}</div>`;
};