An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Prevent outline refresh anomalies after heading insertion https://github.com/siyuan-note/siyuan/issues/18259
Daniel10e2d341c616
diff --git a/app/src/layout/dock/Outline.ts b/app/src/layout/dock/Outline.tsindex 4dcde67ced..6d395748d4 100644--- a/app/src/layout/dock/Outline.ts+++ b/app/src/layout/dock/Outline.ts@@ -1041,21 +1041,26 @@ export class Outline extends Model {label: window.siyuan.languages.insertSameLevelHeadingBefore,click: () => {const data = this.getProtyleAndBlockElement(element);+ if (!data) {+ return;+ }const newId = Lute.NewNodeID();const html = `<div data-subtype="h${currentLevel}" data-node-id="${newId}" data-type="NodeHeading" class="h${currentLevel}"><div contenteditable="true" spellcheck="false"><wbr></div><div class="protyle-attr" contenteditable="false">${Constants.ZWSP}</div></div>`;+ const previousID = data.blockElement.previousElementSibling?.getAttribute("data-node-id");+ data.blockElement.insertAdjacentHTML("beforebegin", html);+ const newElement = data.blockElement.previousElementSibling;+ newElement.scrollIntoView();+ focusByWbr(newElement, document.createRange());transaction(data.protyle, [{action: "insert",data: html,id: newId,- previousID: data.blockElement.previousElementSibling?.getAttribute("data-node-id"),+ previousID,parentID: data.blockElement.parentElement.getAttribute("data-node-id") || data.protyle.block.parentID,}], [{action: "delete",id: newId}]);- data.blockElement.insertAdjacentHTML("beforebegin", html);- data.blockElement.previousElementSibling.scrollIntoView();- focusByWbr(data.blockElement.previousElementSibling, document.createRange());}}).element);@@ -1069,6 +1074,9 @@ export class Outline extends Model {id,}, (deleteResponse) => {const data = this.getProtyleAndBlockElement(element);+ if (!data || !deleteResponse.data?.doOperations?.length) {+ return;+ }const previousID = deleteResponse.data.doOperations[deleteResponse.data.doOperations.length - 1].id;const newId = Lute.NewNodeID();@@ -1103,10 +1111,13 @@ export class Outline extends Model {fetchPost("/api/block/getHeadingDeleteTransaction", {id,}, (deleteResponse) => {+ if (!deleteResponse.data?.doOperations?.length || !deleteResponse.data?.undoOperations) {+ return;+ }let previousID = deleteResponse.data.doOperations[deleteResponse.data.doOperations.length - 1].id;deleteResponse.data.undoOperations.find((operationsItem: IOperation, index: number) => {const startIndex = operationsItem.data.indexOf(' data-subtype="h');- if (startIndex > -1 && startIndex < 260 && parseInt(operationsItem.data.substring(startIndex + 16, startIndex + 17)) === currentLevel + 1) {+ if (index > 0 && startIndex > -1 && startIndex < 260 && parseInt(operationsItem.data.substring(startIndex + 16, startIndex + 17)) === currentLevel + 1) {previousID = deleteResponse.data.undoOperations[index - 1].id;return true;}@@ -1114,8 +1125,18 @@ export class Outline extends Model {const data = this.getProtyleAndBlockElement(element);+ if (!data) {+ return;+ }const newId = Lute.NewNodeID();const html = `<div data-subtype="h${currentLevel + 1}" data-node-id="${newId}" data-type="NodeHeading" class="h${currentLevel + 1}"><div contenteditable="true" spellcheck="false"><wbr></div><div class="protyle-attr" contenteditable="false">${Constants.ZWSP}</div></div>`;+ const previousElement = data.protyle.wysiwyg.element.querySelector(`[data-node-id="${previousID}"]`);+ if (previousElement) {+ previousElement.insertAdjacentHTML("afterend", html);+ const newElement = previousElement.nextElementSibling;+ newElement.scrollIntoView();+ focusByWbr(newElement, document.createRange());+ }transaction(data.protyle, [{action: "insert",data: html,@@ -1125,12 +1146,6 @@ export class Outline extends Model {action: "delete",id: newId}]);- const previousElement = data.protyle.wysiwyg.element.querySelector(`[data-node-id="${previousID}"]`);- if (previousElement) {- previousElement.insertAdjacentHTML("afterend", html);- previousElement.nextElementSibling.scrollIntoView();- focusByWbr(previousElement.nextElementSibling, document.createRange());- }});}}).element);diff --git a/app/src/mobile/dock/MobileOutline.ts b/app/src/mobile/dock/MobileOutline.tsindex 4a23b73585..2521082a0d 100644--- a/app/src/mobile/dock/MobileOutline.ts+++ b/app/src/mobile/dock/MobileOutline.ts@@ -14,8 +14,11 @@ import {focusBlock, focusByWbr} from "../../protyle/util/selection";import {openMobileFileById} from "../editor";import {Model} from "../../layout/Model";import {genUUID} from "../../util/genID";-import {isEncryptedBox} from "../../util/pathName";+import {getDocDisplayName, isEncryptedBox} from "../../util/pathName";import {dragOverScroll, stopScrollAnimation} from "../../boot/globalEvent/dragover";+import {closePanel} from "../util/closePanel";+import {escapeHtml} from "../../util/escape";+import {unicode2Emoji} from "../../emoji";export class MobileOutline extends Model {public tree: Tree;@@ -23,6 +26,7 @@ export class MobileOutline extends Model {public blockId: string;public isPreview: boolean;private preFilterExpandIds: string[] | null = null;+ private reloadId = 0;private touchDragState: {selectedElement: HTMLElement;startX: number;@@ -61,6 +65,7 @@ export class MobileOutline extends Model {<svg data-type="expand" class="toolbar__icon"><use xlink:href="#iconExpand"></use></svg><svg data-type="collapse" class="toolbar__icon"><use xlink:href="#iconContract"></use></svg></div>+<div class="b3-list-item fn__none" data-type="doc-title"></div><div class="fn__flex-1" style="padding: 3px 0 8px"></div>`;const inputElement = this.element.querySelector("input.b3-text-field.search__label") as HTMLInputElement;inputElement.addEventListener("blur", () => {@@ -195,18 +200,10 @@ export class MobileOutline extends Model {});this.bindSort();-- const outlineParam: IObject = {- id: this.blockId,- preview: this.isPreview- };- const mobileProtyle = window.siyuan.mobile.editor?.protyle;- if (mobileProtyle && mobileProtyle.block.rootID === this.blockId && isEncryptedBox(mobileProtyle.notebookId)) {- outlineParam.notebook = mobileProtyle.notebookId;- }- fetchPost("/api/outline/getDocOutline", outlineParam, response => {- this.update(response);+ this.element.querySelector('[data-type="doc-title"]').addEventListener("click", () => {+ openMobileFileById(this.app, this.blockId);});+ this.reload();}// 大纲拖拽排序:原生触摸事件实现,长按门槛避免与列表滚动冲突,排序逻辑对齐桌面端 Outline.bindSort@@ -394,6 +391,15 @@ export class MobileOutline extends Model {case "savedoc":this.onTransaction(data);break;+ case "rename":+ if (this.blockId === data.data.id) {+ this.updateDocTitle({+ title: data.data.title,+ icon: Constants.ZWSP,+ [Constants.CUSTOM_SY_TITLE_EMPTY]: data.data.empty ? "true" : "false"+ }, -1);+ }+ break;}}}@@ -478,16 +484,45 @@ export class MobileOutline extends Model {}}- public update(data: IWebSocketData, callbackId?: string) {+ public reload(callback?: () => void) {+ const protyle = window.siyuan.mobile.editor?.protyle;+ const blockId = protyle?.block.rootID || this.blockId;+ if (!blockId) {+ return;+ }+ const isPreview = protyle ? !protyle.preview.element.classList.contains("fn__none") : this.isPreview;+ if (blockId !== this.blockId) {+ this.tree.updateData(null);+ this.updateDocTitle();+ this.element.scrollTop = 0;+ }+ this.blockId = blockId;+ this.isPreview = isPreview;+ const reloadId = ++this.reloadId;+ const outlineParam: IObject = {+ id: blockId,+ preview: isPreview+ };+ if (protyle && isEncryptedBox(protyle.notebookId)) {+ outlineParam.notebook = protyle.notebookId;+ }+ fetchPost("/api/outline/getDocOutline", outlineParam, response => {+ if (reloadId !== this.reloadId || window.siyuan.mobile.editor?.protyle.block.rootID !== blockId) {+ return;+ }+ this.update(response);+ this.updateDocTitle(protyle?.background?.ial, response.data?.length || 0);+ callback?.();+ });+ }++ public update(data: IWebSocketData) {let currentElement = this.element.querySelector(".b3-list-item--focus");let currentId;if (currentElement) {currentId = currentElement.getAttribute("data-node-id");}const scrollTop = this.element.scrollTop;- if (typeof callbackId !== "undefined") {- this.blockId = callbackId;- }this.tree.updateData(data.data);if (this.isPreview) {@@ -510,6 +545,35 @@ export class MobileOutline extends Model {this.element.removeAttribute("data-loading");}+ private updateDocTitle(ial?: Record<string, string>, count?: number) {+ const docTitleElement = this.element.querySelector('[data-type="doc-title"]') as HTMLElement;+ if (this.isPreview || !ial) {+ docTitleElement.classList.add("fn__none");+ return;+ }+ let iconHTML = unicode2Emoji(ial.icon || window.siyuan.storage[Constants.LOCAL_IMAGES].file,+ "b3-list-item__graphic", true);+ if (ial.icon === Constants.ZWSP && docTitleElement.firstElementChild) {+ iconHTML = docTitleElement.firstElementChild.outerHTML;+ }+ const title = getDocDisplayName(ial.title, ial[Constants.CUSTOM_SY_TITLE_EMPTY] === "true");+ docTitleElement.innerHTML = `${iconHTML}<span class="b3-list-item__text">${escapeHtml(title)}</span>${docTitleElement.querySelector(".counter")?.outerHTML || ""}`;+ docTitleElement.setAttribute("title", title);+ docTitleElement.classList.remove("fn__none");+ if (typeof count === "number" && count !== -1) {+ const counterElement = docTitleElement.querySelector(".counter") as HTMLElement;+ if (count > 0) {+ if (counterElement) {+ counterElement.textContent = count.toString();+ } else {+ docTitleElement.insertAdjacentHTML("beforeend", `<span class="counter">${count.toString()}</span>`);+ }+ } else {+ counterElement?.remove();+ }+ }+ }+public saveExpendIds() {if (window.siyuan.config.readonly || window.siyuan.isPublish) {return;@@ -740,20 +804,7 @@ export class MobileOutline extends Model {});}if (needReload) {- const outlineParam: IObject = {- id: this.blockId,- preview: this.isPreview- };- const mobileProtyle = window.siyuan.mobile.editor?.protyle;- if (mobileProtyle && mobileProtyle.block.rootID === this.blockId && isEncryptedBox(mobileProtyle.notebookId)) {- outlineParam.notebook = mobileProtyle.notebookId;- }- fetchPost("/api/outline/getDocOutline", outlineParam, response => {- // 文档切换后不再更新原有推送 https://github.com/siyuan-note/siyuan/issues/13409- if (data.data.rootID !== this.blockId) {- return;- }- this.update(response);+ this.reload(() => {// https://github.com/siyuan-note/siyuan/issues/8372if (getSelection().rangeCount > 0) {const blockElement = hasClosestBlock(getSelection().getRangeAt(0).startContainer);@@ -786,6 +837,7 @@ export class MobileOutline extends Model {click: () => {const data = this.getProtyleAndBlockElement(element);if (data) {+ closePanel();turnsIntoTransaction({protyle: data.protyle,selectsElement: [data.blockElement],@@ -806,6 +858,7 @@ export class MobileOutline extends Model {click: () => {const data = this.getProtyleAndBlockElement(element);if (data) {+ closePanel();turnsIntoTransaction({protyle: data.protyle,selectsElement: [data.blockElement],@@ -856,21 +909,27 @@ export class MobileOutline extends Model {label: window.siyuan.languages.insertSameLevelHeadingBefore,click: () => {const data = this.getProtyleAndBlockElement(element);+ if (!data) {+ return;+ }+ closePanel();const newId = Lute.NewNodeID();const html = `<div data-subtype="h${currentLevel}" data-node-id="${newId}" data-type="NodeHeading" class="h${currentLevel}"><div contenteditable="true" spellcheck="false"><wbr></div><div class="protyle-attr" contenteditable="false">${Constants.ZWSP}</div></div>`;+ const previousID = data.blockElement.previousElementSibling?.getAttribute("data-node-id");+ data.blockElement.insertAdjacentHTML("beforebegin", html);+ const newElement = data.blockElement.previousElementSibling;+ newElement.scrollIntoView();+ focusByWbr(newElement, document.createRange());transaction(data.protyle, [{action: "insert",data: html,id: newId,- previousID: data.blockElement.previousElementSibling?.getAttribute("data-node-id"),+ previousID,parentID: data.blockElement.parentElement.getAttribute("data-node-id") || data.protyle.block.parentID,}], [{action: "delete",id: newId}]);- data.blockElement.insertAdjacentHTML("beforebegin", html);- data.blockElement.previousElementSibling.scrollIntoView();- focusByWbr(data.blockElement.previousElementSibling, document.createRange());}}).element);@@ -884,6 +943,10 @@ export class MobileOutline extends Model {id,}, (deleteResponse) => {const data = this.getProtyleAndBlockElement(element);+ if (!data || !deleteResponse.data?.doOperations?.length) {+ return;+ }+ closePanel();const previousID = deleteResponse.data.doOperations[deleteResponse.data.doOperations.length - 1].id;const newId = Lute.NewNodeID();@@ -918,10 +981,13 @@ export class MobileOutline extends Model {fetchPost("/api/block/getHeadingDeleteTransaction", {id,}, (deleteResponse) => {+ if (!deleteResponse.data?.doOperations?.length || !deleteResponse.data?.undoOperations) {+ return;+ }let previousID = deleteResponse.data.doOperations[deleteResponse.data.doOperations.length - 1].id;deleteResponse.data.undoOperations.find((operationsItem: IOperation, index: number) => {const startIndex = operationsItem.data.indexOf(' data-subtype="h');- if (startIndex > -1 && startIndex < 260 && parseInt(operationsItem.data.substring(startIndex + 16, startIndex + 17)) === currentLevel + 1) {+ if (index > 0 && startIndex > -1 && startIndex < 260 && parseInt(operationsItem.data.substring(startIndex + 16, startIndex + 17)) === currentLevel + 1) {previousID = deleteResponse.data.undoOperations[index - 1].id;return true;}@@ -929,8 +995,19 @@ export class MobileOutline extends Model {const data = this.getProtyleAndBlockElement(element);+ if (!data) {+ return;+ }+ closePanel();const newId = Lute.NewNodeID();const html = `<div data-subtype="h${currentLevel + 1}" data-node-id="${newId}" data-type="NodeHeading" class="h${currentLevel + 1}"><div contenteditable="true" spellcheck="false"><wbr></div><div class="protyle-attr" contenteditable="false">${Constants.ZWSP}</div></div>`;+ const previousElement = data.protyle.wysiwyg.element.querySelector(`[data-node-id="${previousID}"]`);+ if (previousElement) {+ previousElement.insertAdjacentHTML("afterend", html);+ const newElement = previousElement.nextElementSibling;+ newElement.scrollIntoView();+ focusByWbr(newElement, document.createRange());+ }transaction(data.protyle, [{action: "insert",data: html,@@ -940,12 +1017,6 @@ export class MobileOutline extends Model {action: "delete",id: newId}]);- const previousElement = data.protyle.wysiwyg.element.querySelector(`[data-node-id="${previousID}"]`);- if (previousElement) {- previousElement.insertAdjacentHTML("afterend", html);- previousElement.nextElementSibling.scrollIntoView();- focusByWbr(previousElement.nextElementSibling, document.createRange());- }});}}).element);@@ -1149,13 +1220,20 @@ export class MobileOutline extends Model {label: window.siyuan.languages["heading" + level],click: () => {const protyle = window.siyuan.mobile.editor?.protyle;- if (!protyle) {+ if (!protyle || !this.tree.element.querySelector(`[data-node-id="${id}"]`)) {return;}+ const rootID = protyle.block.rootID;+ closePanel();+ getSelection()?.removeAllRanges();fetchPost("/api/block/getHeadingLevelTransaction", {id,level}, (response) => {+ if (window.siyuan.mobile.editor?.protyle !== protyle || this.blockId !== rootID ||+ !response.data?.doOperations || !response.data?.undoOperations) {+ return;+ }response.data.doOperations.forEach((operation: any, index: number) => {protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${operation.id}"]`).forEach((itemElement: HTMLElement) => {itemElement.outerHTML = operation.data;@@ -1172,6 +1250,7 @@ export class MobileOutline extends Model {}});transaction(protyle, response.data.doOperations, response.data.undoOperations);+ focusBlock(protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`));});}};diff --git a/app/src/mobile/util/initFramework.ts b/app/src/mobile/util/initFramework.tsindex de7c79706f..4f0c4cd552 100644--- a/app/src/mobile/util/initFramework.ts+++ b/app/src/mobile/util/initFramework.ts@@ -8,7 +8,7 @@ import {fetchPost} from "../../util/fetch";import {setInlineStyle} from "../../util/assets";import {renderSnippet} from "../../config/util/snippets";import {setEmpty} from "./setEmpty";-import {getOpenNotebookCount, isEncryptedBox, parseUriInfo} from "../../util/pathName";+import {getOpenNotebookCount, parseUriInfo} from "../../util/pathName";import {popMenu} from "../menu";import {MobileFiles} from "../dock/MobileFiles";import {MobileOutline} from "../dock/MobileOutline";@@ -104,16 +104,7 @@ export const initFramework = (app: App, isStart: boolean) => {isPreview: window.siyuan.mobile.editor ? !window.siyuan.mobile.editor.protyle.preview.element.classList.contains("fn__none") : false});} else {- const outlineParam: IObject = {- id: window.siyuan.mobile.editor.protyle.block.rootID,- preview: window.siyuan.mobile.editor.protyle.preview.element.classList.contains("fn__none")- };- if (isEncryptedBox(window.siyuan.mobile.editor.protyle.notebookId)) {- outlineParam.notebook = window.siyuan.mobile.editor.protyle.notebookId;- }- fetchPost("/api/outline/getDocOutline", outlineParam, response => {- window.siyuan.mobile.docks.outline.update(response);- });+ window.siyuan.mobile.docks.outline.reload();}} else if (type === "sidebar-backlink-tab") {if (!window.siyuan.mobile.docks.backlink) {@@ -160,16 +151,7 @@ export const initFramework = (app: App, isStart: boolean) => {sidebarElement.style.transform = "translateX(0px)";const type = sidebarElement.querySelector(".toolbar--border .toolbar__icon--active").getAttribute("data-type");if (type === "sidebar-outline-tab") {- const outlineParam: IObject = {- id: window.siyuan.mobile.editor.protyle.block.rootID,- preview: window.siyuan.mobile.editor.protyle.preview.element.classList.contains("fn__none")- };- if (isEncryptedBox(window.siyuan.mobile.editor.protyle.notebookId)) {- outlineParam.notebook = window.siyuan.mobile.editor.protyle.notebookId;- }- fetchPost("/api/outline/getDocOutline", outlineParam, response => {- window.siyuan.mobile.docks.outline.update(response);- });+ window.siyuan.mobile.docks.outline.reload();} else if (type === "sidebar-backlink-tab") {window.siyuan.mobile.docks.backlink.update();} else if (type === "sidebar-bookmark-tab") {diff --git a/app/src/util/reloadSync.ts b/app/src/util/reloadSync.tsindex e00da2114a..7cd56d6321 100644--- a/app/src/util/reloadSync.ts+++ b/app/src/util/reloadSync.ts@@ -49,16 +49,7 @@ export const reloadSync = (// 同步刷新移动端大纲,避免大纲与重载后的编辑器数据不一致const outline = window.siyuan.mobile.docks.outline;if (outline) {- const outlineParam: IObject = {- id: window.siyuan.mobile.editor.protyle.block.rootID,- preview: window.siyuan.mobile.editor.protyle.preview.element.classList.contains("fn__none")- };- if (isEncryptedBox(window.siyuan.mobile.editor.protyle.notebookId)) {- outlineParam.notebook = window.siyuan.mobile.editor.protyle.notebookId;- }- fetchPost("/api/outline/getDocOutline", outlineParam, response => {- outline.update(response);- });+ outline.reload();}}}