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: Preserve active tabs when editing mindmap nodes https://github.com/siyuan-note/siyuan/issues/19945
Daniele2971798716c
diff --git a/app/src/protyle/render/listMindmap/editor.test.ts b/app/src/protyle/render/listMindmap/editor.test.tsindex a150e40e93..0675022798 100644--- a/app/src/protyle/render/listMindmap/editor.test.ts+++ b/app/src/protyle/render/listMindmap/editor.test.ts@@ -7,8 +7,12 @@ import test from "node:test";import {promisify} from "node:util";import {ScriptTarget, transpileModule} from "typescript";-const browserCases = async (sourceCode: string, css: string, cleanupSource: string) => {+const browserCases = async (sourceCode: string, css: string, cleanupSource: string, tabsSource: string) => {const check = require("node:assert/strict");+ const {tabsRender, destroyTabsRender} = new Function(tabsSource + "; return {tabsRender, destroyTabsRender};")();+ const baseStyle = document.createElement("style");+ baseStyle.textContent = css;+ document.head.append(baseStyle);const noop = (): void => undefined;const settle = async () => {await new Promise<void>(resolve => setTimeout(resolve, 0));@@ -49,6 +53,7 @@ const browserCases = async (sourceCode: string, css: string, cleanupSource: strihost.style.width = "120px";host.style.minWidth = "36px";const wysiwyg = document.createElement("div");+ wysiwyg.className = "protyle-wysiwyg";const hintElement = document.createElement("div");const toolbarElement = document.createElement("div");const subElement = document.createElement("div");@@ -56,6 +61,7 @@ const browserCases = async (sourceCode: string, css: string, cleanupSource: stricontainer.append(element, host, hintElement, toolbarElement, subElement);document.body.append(container);const state = {+ focusedText: "",flushed: 0,destroyed: 0,finished: 0,@@ -109,10 +115,15 @@ const browserCases = async (sourceCode: string, css: string, cleanupSource: striobserver.observe(wysiwyg, {subtree: true, childList: true, characterData: true});return {protyle, wysiwyg, hintElement,- focus: noop,+ focus: () => {+ const target = Array.from(wysiwyg.querySelectorAll<HTMLElement>('[contenteditable="true"]'))+ .reverse().find(item => item.getBoundingClientRect().width > 0);+ state.focusedText = target?.textContent || "";+ },getBlockHTML: () => wysiwyg.innerHTML,getMarkdown: () => wysiwyg.textContent,destroy: () => {+ destroyTabsRender(wysiwyg);observer.disconnect();state.destroyed++;},@@ -143,6 +154,7 @@ const browserCases = async (sourceCode: string, css: string, cleanupSource: strievent.altKey === binding.includes("⌥");},processRender: noop,+ initEditorTabs: () => tabsRender(wysiwyg, {readonly: () => false}),setCustomBlockRootReady: noop,avRender: noop,blockRender: noop,@@ -203,6 +215,15 @@ const browserCases = async (sourceCode: string, css: string, cleanupSource: stri// 直接键入只替换首个段落,保留其余内容块。const paragraph = (id: string, html: string) =>`<div data-type="NodeParagraph" data-node-id="${id}" class="p"><div contenteditable="true">${html}</div></div>`;+ for (const active of ["first", "second"]) {+ const tabs = create(`<div class="tabs" data-type="NodeTabs" data-node-id="tabs" tabs-active-id="${active}">+ ${["first", "second"].map(id => `<div class="tab-item" data-type="NodeTabItem" data-node-id="${id}">+ <div class="tab-item-info"><div class="tab-item-title">${id}</div></div>+ <div class="tab-item-content">${paragraph(id + "-body", id)}</div></div>`).join("")}</div>`);+ check.equal(tabs.state.focusedText, active, "opening an editor focuses the active tab instead of the last tab");+ check.equal(tabs.wysiwyg.querySelector(".tabs").getAttribute("tabs-active-id"), active);+ await tabs.remove();+ }let typed = create(paragraph("first", "Old <strong>rich</strong>") +paragraph("second", "Keep") + '<div data-type="NodeThematicBreak" data-node-id="special"></div>', "N");check.equal(typed.wysiwyg.querySelector('[data-node-id="first"] > [contenteditable="true"]').textContent, "N");@@ -676,6 +697,11 @@ test("list mindmap editor flushes pending input and preserves text across finishconst cleanupSource = transpileModule(modelSource.slice(modelSource.indexOf("const cleanListMindmapDOM ="),modelSource.indexOf("// 转换前将思维导图块还原为列表结构")).replace(/^export /gm, ""), {compilerOptions: {target: ScriptTarget.ES2021}}).outputText;+ const tabsSource = ["../../../util/escape.ts", "../tabsState.ts", "../tabsDrag.ts", "../tabsAttributes.ts", "../tabsRender.ts"]+ .map(file => transpileModule(readFileSync(path.join(__dirname, file), "utf8")+ .replace(/^import [\s\S]*?;\r?\n/gm, "").replace(/^export /gm, ""), {+ compilerOptions: {target: ScriptTarget.ES2021},+ }).outputText).join("\n");const code = `const {app, BrowserWindow, ipcMain} = require("electron");app.setPath("userData", ${JSON.stringify(path.join(temporary, "profile"))});app.commandLine.appendSwitch("disable-gpu");@@ -687,7 +713,7 @@ app.whenReady().then(async () => {try {await win.loadURL("data:text/html,<html><body></body></html>");const result = await win.webContents.executeJavaScript(${JSON.stringify(- `const __name = value => value; (${browserCases.toString()})(${JSON.stringify(source)}, ${JSON.stringify(css)}, ${JSON.stringify(cleanupSource)})`)});+ `const __name = value => value; (${browserCases.toString()})(${JSON.stringify(source)}, ${JSON.stringify(css)}, ${JSON.stringify(cleanupSource)}, ${JSON.stringify(tabsSource)})`)});console.log(result);win.destroy();app.exit(0);diff --git a/app/src/protyle/render/listMindmap/editor.ts b/app/src/protyle/render/listMindmap/editor.tsindex 336eedea13..be6e9de437 100644--- a/app/src/protyle/render/listMindmap/editor.ts+++ b/app/src/protyle/render/listMindmap/editor.ts@@ -13,6 +13,7 @@ import {getDefaultToolbar} from "../../toolbar/defaults";import {hideElements} from "../../ui/hideElements";import {matchHotKey} from "../../util/hotKey";import {processRender} from "../../util/processCode";+import {initEditorTabs} from "../../wysiwyg/tabs";import {avRender} from "../av/render";import {blockRender} from "../blockRender";import {highlightRender} from "../highlightRender";@@ -171,6 +172,8 @@ export const openListMindmapEditor = (options: ListMindmapEditorOptions) => {protyle.block.parentID = node.id;protyle.path = owner.path;setCustomBlockRootReady(element, true);+ // 聚焦前同步页签可见性,避免光标进入尚未隐藏的非活动页签。+ initEditorTabs(protyle);processRender(element);highlightRender(element);blockRender(protyle, element, undefined, options.onResize);diff --git a/app/src/protyle/render/listMindmap/model.test.ts b/app/src/protyle/render/listMindmap/model.test.tsindex 3904eb829f..f0b7f8c3ad 100644--- a/app/src/protyle/render/listMindmap/model.test.ts+++ b/app/src/protyle/render/listMindmap/model.test.ts@@ -2791,6 +2791,12 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,}});tabController.view = tabView;check.equal(tabHost.querySelector(".tabs-tab-label").textContent, "页签项块");+ const tabButton = tabHost.querySelector<HTMLElement>(".tabs-tab");+ tabButton.dispatchEvent(new MouseEvent("pointerover", {bubbles: true}));+ check.equal(tabHost.querySelector<HTMLElement>('[role="tooltip"]').hidden, true,+ "tab buttons do not trigger the mindmap toolbar tooltip");+ tabButton.dispatchEvent(new FocusEvent("focusin", {bubbles: true}));+ check.equal(tabHost.querySelector<HTMLElement>('[role="tooltip"]').hidden, true);const fragmentHost = document.createElement("div");fragmentHost.className = "mindmap-view";const fragmentRoot = document.createElement("div");diff --git a/app/src/protyle/render/listMindmap/view.ts b/app/src/protyle/render/listMindmap/view.tsindex 8a359d80f4..1db8404bbe 100644--- a/app/src/protyle/render/listMindmap/view.ts+++ b/app/src/protyle/render/listMindmap/view.ts@@ -307,8 +307,9 @@ export class ListMindmapView {private showButtonTooltip = (event: Event) => {const button = (event.target as Element).closest<HTMLButtonElement>("button[aria-label]");- if (!button || !this.options.host.contains(button) || button.classList.contains("mindmap-view__relation") ||- button.classList.contains("mindmap-view__summary")) {+ if (!button || button.closest(".mindmap-view") !== this.options.host ||+ !(this.toolbar.contains(button) || this.inspector.contains(button) ||+ button.classList.contains("mindmap-view__add-child") || button.classList.contains("mindmap-view__fold"))) {this.tooltip.hidden = true;return;}