Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:zap: Share a single Lute instance across all editors to reduce multi-tab memory cost https://github.com/siyuan-note/siyuan/issues/17869

Daniel1b210889ae6a
diff --git a/app/src/emoji/index.ts b/app/src/emoji/index.ts
index 0dc0a7e0e8..4c15f4bde7 100644
--- a/app/src/emoji/index.ts
+++ b/app/src/emoji/index.ts
@@ -10,6 +10,7 @@ import {setNoteBook} from "../util/pathName";
import {Dialog} from "../dialog";
import {setPosition} from "../util/setPosition";
import {setStorageVal} from "../protyle/util/compatibility";
+import {getLuteInstance} from "../protyle/render/setLute";
import * as dayjs from "dayjs";
export const getRandomEmoji = () => {
@@ -760,20 +761,23 @@ export const getEmojiTitle = (index: number) => {
};
const putEmojis = (protyle: IProtyle) => {
- if (window.siyuan.emojis[0].items.length > 0) {
+ const lute = getLuteInstance();
+ if (lute && window.siyuan.emojis[0].items.length > 0) {
const emojis: IObject = {};
window.siyuan.emojis[0].items.forEach(emojiITem => {
emojis[emojiITem.keywords] = protyle.options.hint.emojiPath + "/" + emojiITem.unicode;
});
- protyle.lute.PutEmojis(emojis);
+ // Lute 已为所有编辑器共享单例,PutEmojis 只需调用一次
+ lute.PutEmojis(emojis);
}
};
export const reloadEmoji = () => {
fetchPost("/api/system/getEmojiConf", {}, response => {
window.siyuan.emojis = response.data as IEmoji[];
- getAllEditor().forEach(item => {
- putEmojis(item.protyle);
- });
+ const editors = getAllEditor();
+ if (editors.length > 0) {
+ putEmojis(editors[0].protyle);
+ }
});
};
diff --git a/app/src/protyle/index.ts b/app/src/protyle/index.ts
index 78290b3ed5..d0a2f15fb7 100644
--- a/app/src/protyle/index.ts
+++ b/app/src/protyle/index.ts
@@ -1,6 +1,6 @@
import {Constants} from "../constants";
import {Hint} from "./hint";
-import {setLute} from "./render/setLute";
+import {getLute} from "./render/setLute";
import {Preview} from "./preview";
import {addLoading, initUI, removeLoading} from "./ui/initUI";
import {Undo} from "./undo";
@@ -444,7 +444,7 @@ export class Protyle {
}
private init() {
- this.protyle.lute = setLute({
+ this.protyle.lute = getLute({
emojiSite: this.protyle.options.hint.emojiPath,
emojis: this.protyle.options.hint.emoji,
headingAnchor: false,
diff --git a/app/src/protyle/render/setLute.ts b/app/src/protyle/render/setLute.ts
index 3cf11f28a4..408c675cb3 100644
--- a/app/src/protyle/render/setLute.ts
+++ b/app/src/protyle/render/setLute.ts
@@ -1,4 +1,33 @@
-export const setLute = (options: ILuteOptions) => {
+// Lute 配置全部读取全局 window.siyuan.config / window.siyuan.emojis,跨编辑器一致,
+// 因此所有 Protyle 编辑器共用同一个 Lute 实例,将内存与初始化开销从 O(编辑器数) 降为 O(1)。
+// AgentChat 维持自身的独立 Lute.New() 实例,不经过此处。
+let luteInstance: Lute | undefined;
+
+/**
+ * 获取(首次调用时创建)共享 Lute 单例。
+ *
+ * 仅在首次创建时应用 options,后续调用直接返回已缓存的实例 ——
+ * Lute 配置本就源于全局 config,跨编辑器一致,无需按编辑器区分。
+ */
+export const getLute = (options: ILuteOptions): Lute => {
+ if (!luteInstance) {
+ luteInstance = setLute(options);
+ }
+ return luteInstance;
+};
+
+/**
+ * 直接获取已初始化的共享 Lute 单例。
+ * 供 emoji 等无需传入 options 的场景使用;尚未创建时返回 undefined。
+ */
+export const getLuteInstance = (): Lute | undefined => {
+ return luteInstance;
+};
+
+/**
+ * 根据全局配置与传入选项构建一个新的 Lute 实例,供共享单例初始化使用。
+ */
+const setLute = (options: ILuteOptions) => {
const lute: Lute = Lute.New();
lute.SetSpellcheck(window.siyuan.config.editor.spellcheck);
lute.SetProtyleMarkNetImg(window.siyuan.config.editor.displayNetImgMark);
diff --git a/app/src/protyle/util/paste.ts b/app/src/protyle/util/paste.ts
index 9a58e71e45..cf7385df90 100644
--- a/app/src/protyle/util/paste.ts
+++ b/app/src/protyle/util/paste.ts
@@ -214,6 +214,9 @@ export const pasteAsPlainText = async (protyle: IProtyle) => {
textPlain = textPlain.replace(/__@kbd@__/g, "<kbd>").replace(/__@\/kbd@__/g, "</kbd>");
textPlain = textPlain.replace(/__@u@__/g, "<u>").replace(/__@\/u@__/g, "</u>");
+ // 临界区:Lute 已是所有编辑器共享的单例,此处临时把 inline-syntax 标志置 true 再恢复。
+ // enable/transform/restore 必须保持同步执行,中间不得插入 await,否则并发编辑器的
+ // 转换调用(如实时输入的 SpinBlockDOM)会读到被改写的标志而产生错误输出。
enableLuteMarkdownSyntax(protyle);
const content = protyle.lute.BlockDOM2EscapeMarkerContent(protyle.lute.Md2BlockDOM(textPlain));
restoreLuteMarkdownSyntax(protyle);