An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:sparkles: Support focusing individual tab items https://github.com/siyuan-note/siyuan/issues/20077
Daniel87b457dd2387
diff --git a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-h361q1i/20200924093441-ft2rhps.sy b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-h361q1i/20200924093441-ft2rhps.syindex fd6b7380ec..14be2ac927 100644--- a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-h361q1i/20200924093441-ft2rhps.sy+++ b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-h361q1i/20200924093441-ft2rhps.sy@@ -8,7 +8,7 @@"title": "Formatting elements","title-img": "background-color:#269;background-image: linear-gradient(white 2px, transparent 2px),linear-gradient(90deg, white 2px, transparent 2px),linear-gradient(rgba(255,255,255,.3) 1px, transparent 1px),linear-gradient(90deg, rgba(255,255,255,.3) 1px, transparent 1px);background-size: 100px 100px, 100px 100px, 20px 20px, 20px 20px;background-position:-2px -2px, -2px -2px, -1px -1px, -1px -1px;","type": "doc",- "updated": "20261003225941"+ "updated": "20261003234156"},"Children": [{@@ -3324,7 +3324,7 @@"Type": "NodeHeading","Properties": {"id": "20260905124213-top2oxl",- "updated": "20260905124213"+ "updated": "20261003234156"},"HeadingLevel": 2,"Children": [@@ -3362,6 +3362,38 @@}]},+ {+ "ID": "20261003234156-ki04gzc",+ "Type": "NodeParagraph",+ "Properties": {+ "id": "20261003234156-ki04gzc",+ "updated": "20261003234156"+ },+ "Children": [+ {+ "Type": "NodeText",+ "Data": "Use a tab item's menu to choose "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "Zoom in"+ },+ {+ "Type": "NodeText",+ "Data": ". Only that item and its title are displayed, and backlinks follow that item. Whether backlinks include its child blocks follows the existing setting. Choose "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "Exit focus"+ },+ {+ "Type": "NodeText",+ "Data": " to restore the full tab group. Nested tab groups inside the focused item can still be switched normally."+ }+ ]+ },{"ID": "20260905124213-kvygzam","Type": "NodeTabs",diff --git a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180320-abz7w6k/20200825162036-4dx365o.sy b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180320-abz7w6k/20200825162036-4dx365o.syindex d4584c1e53..ec8e90e46d 100644--- a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180320-abz7w6k/20200825162036-4dx365o.sy+++ b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180320-abz7w6k/20200825162036-4dx365o.sy@@ -8,7 +8,7 @@"title": "排版元素","title-img": "background-color: hsl(2, 57%, 40%);background-image: repeating-linear-gradient(transparent, transparent 50px, rgba(0,0,0,.4) 50px, rgba(0,0,0,.4) 53px, transparent 53px, transparent 63px, rgba(0,0,0,.4) 63px, rgba(0,0,0,.4) 66px, transparent 66px, transparent 116px, rgba(0,0,0,.5) 116px, rgba(0,0,0,.5) 166px, rgba(255,255,255,.2) 166px, rgba(255,255,255,.2) 169px, rgba(0,0,0,.5) 169px, rgba(0,0,0,.5) 179px, rgba(255,255,255,.2) 179px, rgba(255,255,255,.2) 182px, rgba(0,0,0,.5) 182px, rgba(0,0,0,.5) 232px, transparent 232px),repeating-linear-gradient(270deg, transparent, transparent 50px, rgba(0,0,0,.4) 50px, rgba(0,0,0,.4) 53px, transparent 53px, transparent 63px, rgba(0,0,0,.4) 63px, rgba(0,0,0,.4) 66px, transparent 66px, transparent 116px, rgba(0,0,0,.5) 116px, rgba(0,0,0,.5) 166px, rgba(255,255,255,.2) 166px, rgba(255,255,255,.2) 169px, rgba(0,0,0,.5) 169px, rgba(0,0,0,.5) 179px, rgba(255,255,255,.2) 179px, rgba(255,255,255,.2) 182px, rgba(0,0,0,.5) 182px, rgba(0,0,0,.5) 232px, transparent 232px),repeating-linear-gradient(125deg, transparent, transparent 2px, rgba(0,0,0,.2) 2px, rgba(0,0,0,.2) 3px, transparent 3px, transparent 5px, rgba(0,0,0,.2) 5px);","type": "doc",- "updated": "20261003225941"+ "updated": "20261003234156"},"Children": [{@@ -3327,7 +3327,7 @@"Type": "NodeHeading","Properties": {"id": "20260905124213-nhaajjp",- "updated": "20260905124213"+ "updated": "20261003234156"},"HeadingLevel": 2,"Children": [@@ -3365,6 +3365,38 @@}]},+ {+ "ID": "20261003234156-yiv6tou",+ "Type": "NodeParagraph",+ "Properties": {+ "id": "20261003234156-yiv6tou",+ "updated": "20261003234156"+ },+ "Children": [+ {+ "Type": "NodeText",+ "Data": "在页签项菜单中选择 "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "聚焦"+ },+ {+ "Type": "NodeText",+ "Data": ",只显示该项及其标题,反向链接也限定到该项;是否包含子块沿用现有设置。选择 "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "退出聚焦"+ },+ {+ "Type": "NodeText",+ "Data": " 后恢复完整页签组,聚焦项内部的嵌套页签仍可正常切换。"+ }+ ]+ },{"ID": "20260905124213-wgaiosf","Type": "NodeTabs",diff --git a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226120854-dr1jfx2/20211226121319-emrk2yy.sy b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226120854-dr1jfx2/20211226121319-emrk2yy.syindex d354ebd52f..95923a7d56 100644--- a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226120854-dr1jfx2/20211226121319-emrk2yy.sy+++ b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226120854-dr1jfx2/20211226121319-emrk2yy.sy@@ -8,7 +8,7 @@"title": "排版元素","title-img": "background-color: hsl(2, 57%, 40%);background-image: repeating-linear-gradient(transparent, transparent 50px, rgba(0,0,0,.4) 50px, rgba(0,0,0,.4) 53px, transparent 53px, transparent 63px, rgba(0,0,0,.4) 63px, rgba(0,0,0,.4) 66px, transparent 66px, transparent 116px, rgba(0,0,0,.5) 116px, rgba(0,0,0,.5) 166px, rgba(255,255,255,.2) 166px, rgba(255,255,255,.2) 169px, rgba(0,0,0,.5) 169px, rgba(0,0,0,.5) 179px, rgba(255,255,255,.2) 179px, rgba(255,255,255,.2) 182px, rgba(0,0,0,.5) 182px, rgba(0,0,0,.5) 232px, transparent 232px),repeating-linear-gradient(270deg, transparent, transparent 50px, rgba(0,0,0,.4) 50px, rgba(0,0,0,.4) 53px, transparent 53px, transparent 63px, rgba(0,0,0,.4) 63px, rgba(0,0,0,.4) 66px, transparent 66px, transparent 116px, rgba(0,0,0,.5) 116px, rgba(0,0,0,.5) 166px, rgba(255,255,255,.2) 166px, rgba(255,255,255,.2) 169px, rgba(0,0,0,.5) 169px, rgba(0,0,0,.5) 179px, rgba(255,255,255,.2) 179px, rgba(255,255,255,.2) 182px, rgba(0,0,0,.5) 182px, rgba(0,0,0,.5) 232px, transparent 232px),repeating-linear-gradient(125deg, transparent, transparent 2px, rgba(0,0,0,.2) 2px, rgba(0,0,0,.2) 3px, transparent 3px, transparent 5px, rgba(0,0,0,.2) 5px);","type": "doc",- "updated": "20261003225941"+ "updated": "20261003234156"},"Children": [{@@ -3325,7 +3325,7 @@"Type": "NodeHeading","Properties": {"id": "20260905124213-9j816g5",- "updated": "20260905124213"+ "updated": "20261003234156"},"HeadingLevel": 2,"Children": [@@ -3363,6 +3363,38 @@}]},+ {+ "ID": "20261003234156-6krnh0y",+ "Type": "NodeParagraph",+ "Properties": {+ "id": "20261003234156-6krnh0y",+ "updated": "20261003234156"+ },+ "Children": [+ {+ "Type": "NodeText",+ "Data": "在頁籤項選單中選擇 "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "聚焦"+ },+ {+ "Type": "NodeText",+ "Data": ",只顯示該項及其標題,反向鏈接也限定於該項;是否包含子區塊沿用現有設定。選擇 "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "退出聚焦"+ },+ {+ "Type": "NodeText",+ "Data": " 後恢復完整頁籤群組,聚焦項內部的巢狀頁籤仍可正常切換。"+ }+ ]+ },{"ID": "20260905124213-egek3ii","Type": "NodeTabs",diff --git a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-hnu6o79/20240530101000-3qhz7br.sy b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-hnu6o79/20240530101000-3qhz7br.syindex 546ec2a27c..bddc4e6a92 100644--- a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-hnu6o79/20240530101000-3qhz7br.sy+++ b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-hnu6o79/20240530101000-3qhz7br.sy@@ -9,7 +9,7 @@"title": "書式要素","title-img": "background-color:#269;background-image: linear-gradient(white 2px, transparent 2px),linear-gradient(90deg, white 2px, transparent 2px),linear-gradient(rgba(255,255,255,.3) 1px, transparent 1px),linear-gradient(90deg, rgba(255,255,255,.3) 1px, transparent 1px);background-size: 100px 100px, 100px 100px, 20px 20px, 20px 20px;background-position:-2px -2px, -2px -2px, -1px -1px, -1px -1px;","type": "doc",- "updated": "20261003225941"+ "updated": "20261003234156"},"Children": [{@@ -3456,7 +3456,7 @@"Type": "NodeHeading","Properties": {"id": "20260905124213-ar71gu2",- "updated": "20260905124213"+ "updated": "20261003234156"},"HeadingLevel": 2,"Children": [@@ -3494,6 +3494,38 @@}]},+ {+ "ID": "20261003234156-3qtc9d0",+ "Type": "NodeParagraph",+ "Properties": {+ "id": "20261003234156-3qtc9d0",+ "updated": "20261003234156"+ },+ "Children": [+ {+ "Type": "NodeText",+ "Data": "タブ項目のメニューで "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "ズームイン"+ },+ {+ "Type": "NodeText",+ "Data": " を選ぶと、その項目とタイトルだけが表示され、バックリンクの対象もその項目に限定されます。子ブロックを含めるかどうかは既存の設定に従います。"+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "フォーカス解除"+ },+ {+ "Type": "NodeText",+ "Data": " を選ぶとタブグループ全体に戻ります。フォーカス中の項目内にある入れ子のタブは通常どおり切り替えられます。"+ }+ ]+ },{"ID": "20260905124213-539fwxw","Type": "NodeTabs",diff --git a/app/src/menus/navigationFocus.test.ts b/app/src/menus/navigationFocus.test.tsindex 328e6b4478..45d51fc275 100644--- a/app/src/menus/navigationFocus.test.ts+++ b/app/src/menus/navigationFocus.test.ts@@ -17,6 +17,8 @@ const fixture = (tablet: boolean, alreadyZoomed = false, missingTarget = false)const pending: Promise<void>[] = [];let focused = 0;let scrolled = 0;+ let backlinksUpdated = 0;+ const siyuan: any = {config: {editor: {dynamicLoadBlocks: 64, backlinkShowBottom: true}}};const target = {getBoundingClientRect: () => ({height: 20}),classList: {contains: () => false},@@ -31,7 +33,7 @@ const fixture = (tablet: boolean, alreadyZoomed = false, missingTarget = false)Constants: {CB_GET_HTML: "html", CB_GET_ALL: "all", CB_GET_FOCUS: "focus", CB_GET_UNUNDO: "unundo",},- window: {siyuan: {config: {editor: {dynamicLoadBlocks: 64, backlinkShowBottom: true}}}},+ window: {siyuan},fetchPost: (_url: string, _data: any, callback: (data: any) => Promise<void>) => {pending.push(Promise.resolve(callback({data: {}})));},@@ -57,6 +59,11 @@ const fixture = (tablet: boolean, alreadyZoomed = false, missingTarget = false)loads,focused: () => focused,scrolled: () => scrolled,+ backlinksUpdated: () => backlinksUpdated,+ setMobileEditor: (primary = true) => {+ siyuan.mobile = {editor: {protyle: primary ? protyle : {}},+ docks: {backlink: {update: () => backlinksUpdated++}}};+ },settle: () => Promise.all(pending),zoom: (options: any = {}) => exports.zoomOut({protyle, id: "target", ...options}),};@@ -114,3 +121,24 @@ test("desktop breadcrumb navigation retains focus", () => {assert.equal(f.focused(), 1);assert.equal(f.scrolled(), 1);});++test("mobile backlinks follow focus and exit-focus after rendering without dropping the caller callback", () => {+ for (const id of ["target", "root"]) {+ const f = fixture(true);+ let callback = 0;+ f.zoom({id, callback: () => callback++});+ f.setMobileEditor();+ assert.equal(f.backlinksUpdated(), 0);+ f.loads[0].afterCB();+ assert.equal(f.backlinksUpdated(), 1);+ assert.equal(callback, 1);+ }+});++test("secondary editor focus does not change the mobile main editor's backlinks", () => {+ const f = fixture(true);+ f.zoom();+ f.setMobileEditor(false);+ f.loads[0].afterCB();+ assert.equal(f.backlinksUpdated(), 0);+});diff --git a/app/src/menus/protyle.ts b/app/src/menus/protyle.tsindex f61d6fc16e..b0f7e5ea1a 100644--- a/app/src/menus/protyle.ts+++ b/app/src/menus/protyle.ts@@ -1124,7 +1124,14 @@ export const zoomOut = (options: {action,scrollAttr: getZoomFocusScrollAttr(options.id, options.focusId, options.focusPosition),scrollPosition: options.focusId ? "start" : undefined,- afterCB: options.callback,+ afterCB: () => {+ /// #if MOBILE+ if (window.siyuan.mobile?.editor?.protyle === options.protyle) {+ window.siyuan.mobile.docks.backlink?.update();+ }+ /// #endif+ options.callback?.();+ },dataDocType: options.dataDocType,focusAfterZoom,suppressFocus,diff --git a/app/src/protyle/render/tabsRender.ts b/app/src/protyle/render/tabsRender.tsindex 533357f807..94a46b7078 100644--- a/app/src/protyle/render/tabsRender.ts+++ b/app/src/protyle/render/tabsRender.ts@@ -4,6 +4,7 @@ import {escapeHtml} from "../../util/escape";import {clearTabsAttributes, renderTabsAttributes} from "./tabsAttributes";export interface ITabsRenderOptions {+ prepare?: () => void;readonly?: (tabs?: Element) => boolean;taskReadonly?: (tabs?: Element) => boolean;label?: string;@@ -176,6 +177,7 @@ export const tabsRender = (element: Element, options: ITabsRenderOptions = {}) =}controller.observer.disconnect();controller.resize.disconnect();+ controller.options.prepare?.();const focusedAttributes = document.activeElement?.closest<HTMLElement>(".tabs-attributes");const focusedAttributesID = focusedAttributes && element.contains(focusedAttributes) ?focusedAttributes.dataset.blockId : undefined;diff --git a/app/src/protyle/ui/initUI.ts b/app/src/protyle/ui/initUI.tsindex c2a63008df..62e932319f 100644--- a/app/src/protyle/ui/initUI.ts+++ b/app/src/protyle/ui/initUI.ts@@ -250,7 +250,8 @@ export const initUI = (protyle: IProtyle) => {}const tabsHeader = hasClosestByClassName(event.target, "tabs-header");const nodeElement = tabsHeader ? hasClosestBlock(tabsHeader.parentElement) : hasClosestBlock(event.target);- if (!tabsHeader && nodeElement && nodeElement.getAttribute("data-type") === "NodeTabItem") {+ if (!tabsHeader && nodeElement && nodeElement.getAttribute("data-type") === "NodeTabItem" &&+ nodeElement.parentElement.classList.contains("tabs")) {hideElements(["gutter"], protyle);return;}diff --git a/app/src/protyle/wysiwyg/tabs.ts b/app/src/protyle/wysiwyg/tabs.tsindex f40b98816e..d92e5590d3 100644--- a/app/src/protyle/wysiwyg/tabs.ts+++ b/app/src/protyle/wysiwyg/tabs.ts@@ -16,6 +16,10 @@ import {copySubMenu, openAttr} from "../../menus/commonMenuItem";import {isTaskListMarker, nextTaskListMarker} from "./taskListMarker";import {hideElements} from "../ui/hideElements";import {getTaskStatusItems} from "./taskStatusDialog";+import {canFocusTabItem, getFocusedTabItem, prepareFocusedTabItem} from "./tabsFocus";+import {zoomOut} from "../../menus/protyle";++const boundFocusedTitles = new WeakSet<HTMLElement>();export const toggleTabsTasks = (protyle: IProtyle, tabs: HTMLElement) => {const items = getTabItems(tabs);@@ -159,6 +163,9 @@ export const openTabsMenu = (protyle: IProtyle, tabs: HTMLElement, item: HTMLEleconst lang = window.siyuan.languages;const menu = new Menu();menu.addItem({icon: "iconCopy", label: lang.copy, submenu: copySubMenu([item.dataset.nodeId], false)});+ if (canFocusTabItem(protyle, item)) {+ menu.addItem({icon: "iconEnter", label: lang.enter, click: () => zoomOut({protyle, id: item.dataset.nodeId})});+ }if (canEdit(protyle, tabs)) {if (getTabTask(item) !== null) {menu.addSeparator({id: "separator_taskStatusBefore"});@@ -167,6 +174,8 @@ export const openTabsMenu = (protyle: IProtyle, tabs: HTMLElement, item: HTMLElemenu.addSeparator({id: "separator_taskStatus"});}menu.addItem({icon: "iconEdit", label: lang.rename, click: () => renameTab(protyle, item)});+ }+ if (canEdit(protyle, tabs) && tabs.classList.contains("tabs")) {menu.addItem({icon: "iconCopy", label: lang.duplicateCopy, click: () => {const copy = item.cloneNode(true) as HTMLElement;const ids = new Map<string, string>();@@ -199,7 +208,20 @@ export const openTabsMenu = (protyle: IProtyle, tabs: HTMLElement, item: HTMLEleexport const initEditorTabs = (protyle: IProtyle) => {const root = protyle.wysiwyg.element;const measured = new WeakSet<Element>();+ if (!boundFocusedTitles.has(root)) {+ boundFocusedTitles.add(root);+ root.addEventListener("contextmenu", event => {+ const item = getFocusedTabItem(protyle);+ const info = (event.target as Element).closest(".tab-item-info");+ if (item?.parentElement === root && info?.parentElement === item) {+ event.preventDefault();+ event.stopPropagation();+ openTabsMenu(protyle, item, item, info as HTMLElement);+ }+ });+ }tabsRender(root, {+ prepare: () => prepareFocusedTabItem(protyle),readonly: tabs => !canEdit(protyle, tabs || root),label: window.siyuan.languages.tabItem,addLabel: window.siyuan.languages.newTabItem,diff --git a/app/src/protyle/wysiwyg/tabsFocus.ts b/app/src/protyle/wysiwyg/tabsFocus.tsnew file mode 100644index 0000000000..3b2a8b08fc--- /dev/null+++ b/app/src/protyle/wysiwyg/tabsFocus.ts@@ -0,0 +1,49 @@+import {getTabContent, getTabItems, getTabTask, getTabTitle} from "../render/tabsRender";+import {Constants} from "../../constants";++export const canFocusTabItem = (protyle: IProtyle, item: Element) => !protyle.lite &&+ !protyle.options.backlinkData && !protyle.element.closest(".block__popover") &&+ !protyle.options.action.includes(Constants.CB_GET_HISTORY) &&+ !item.closest(".protyle-wysiwyg__embed, .mindmap-view__preview-block") &&+ item.closest(".protyle-wysiwyg") === protyle.wysiwyg.element;++export const getFocusedTabItem = (protyle: IProtyle) => {+ const root = protyle.wysiwyg.element;+ if (!protyle.block.showAll || !canFocusTabItem(protyle, root)) {+ return undefined;+ }+ for (const block of Array.from(root.children)) {+ if (block.classList.contains("tab-item") && block.getAttribute("data-node-id") === protyle.block.id) {+ return block as HTMLElement;+ }+ if (block.classList.contains("tabs")) {+ const item = getTabItems(block).find(item => item.dataset.nodeId === protyle.block.id);+ if (item) {+ return item;+ }+ }+ }+};++// 聚焦编辑器只保留目标项,块选择和事务沿用该项的 ID,不影响文档中的同组页签。+export const prepareFocusedTabItem = (protyle: IProtyle) => {+ const item = getFocusedTabItem(protyle);+ if (!item) {+ return;+ }+ const root = protyle.wysiwyg.element;+ if (item.parentElement !== root || root.children.length !== 1) {+ const task = getTabTask(item);+ if (task !== null) {+ item.setAttribute("tabs-task", task);+ }+ root.replaceChildren(item);+ item.removeAttribute("data-tabs-hidden");+ const info = item.querySelector<HTMLElement>(":scope > .tab-item-info");+ info?.classList.remove("tabs-title-editor");+ info?.removeAttribute("style");+ }+ const content = getTabContent(item);+ ["id", "role", "aria-labelledby", "tabindex"].forEach(name => content?.removeAttribute(name));+ getTabTitle(item)?.setAttribute("contenteditable", String(!protyle.disabled));+};diff --git a/app/tests/tabsFocus.test.js b/app/tests/tabsFocus.test.jsnew file mode 100644index 0000000000..f47a94e252--- /dev/null+++ b/app/tests/tabsFocus.test.js@@ -0,0 +1,230 @@+const assert = require("node:assert/strict");+const {readFileSync, mkdtempSync, rmSync} = require("node:fs");+const path = require("node:path");+const os = require("node:os");++const sources = () => {+ const ts = require("typescript");+ const compile = (file, names) => {+ const source = ts.createSourceFile(file, readFileSync(path.join(__dirname, "../src", file), "utf8"),+ ts.ScriptTarget.Latest, true);+ const statements = source.statements.filter(statement => names ? ts.isVariableStatement(statement) &&+ statement.declarationList.declarations.some(declaration => names.includes(declaration.name.getText(source))) :+ !ts.isImportDeclaration(statement));+ return ts.transpileModule(statements.map(statement => statement.getText(source)).join("\n")+ .replace(/^export /gm, ""), {compilerOptions: {target: ts.ScriptTarget.ES2021}}).outputText;+ };+ return [+ compile("protyle/render/tabsState.ts"),+ compile("protyle/render/tabsDrag.ts"),+ compile("protyle/render/tabsAttributes.ts"),+ compile("util/escape.ts", ["escapeHtml"]),+ compile("protyle/render/tabsVisibility.ts", ["isHiddenTabContent"]),+ compile("protyle/render/tabsRender.ts"),+ compile("protyle/wysiwyg/tabsFocus.ts"),+ compile("protyle/wysiwyg/tabsRemoval.ts", ["repairActiveTab"]),+ compile("protyle/wysiwyg/tabs.ts", ["boundFocusedTitles", "canEdit", "changeTabs", "renameTab", "openTabsMenu", "initEditorTabs"]),+ compile("protyle/util/selection.ts", ["selectAll"]),+ ].join("\n");+};++const cases = async (source) => {+ const check = require("node:assert/strict");+ const lute = window.Lute.New();+ lute.SetTabs(true);+ lute.SetKramdownIAL(true);+ lute.SetProtyleWYSIWYG(true);+ window.siyuan = {languages: {enter: "Zoom in", copy: "Copy", delete: "Delete", rename: "Rename"},+ config: {editor: {spellcheck: false}}};+ const style = document.createElement("style");+ style.textContent = ".fn__none, .tab-item[data-tabs-hidden=\"true\"] {display: none !important;} " ++ ".tabs {width: 600px;} .tabs-list {display: flex;}";+ document.head.append(style);+ let menu;+ let navigations;+ let transactions;+ let selectedIDs;+ class Menu {+ constructor() { this.items = []; menu = this.items; }+ addItem(item) { this.items.push(item); }+ addSeparator() {}+ open() {}+ }+ const api = new Function("Constants", "Menu", "copySubMenu", "transaction", "zoomOut", "hideElements",+ "focusBlock", "focusByRange", "queueTransaction", "fetchPost", "openAttr", "processRender", "avRender",+ "getSelectAllBlockAction", "countBlockWord", "getTaskStatusItems",+ source + "; return {initEditorTabs, openTabsMenu, changeTabs, selectAll, destroyTabsRender};")(+ {CB_GET_HISTORY: "history", ATTRIBUTE_EDITING: "data-editing"}, Menu, () => [],+ (_protyle, forward, undo) => { transactions.push({forward, undo}); },+ options => { navigations.push(options.id); }, () => {}, () => {}, range => {+ window.getSelection().removeAllRanges();+ window.getSelection().addRange(range);+ }, (_protyle, task) => task(), () => {}, () => {}, () => {}, () => {},+ () => "expand", ids => { selectedIDs = ids; }, () => []);++ for (const readonly of [false, true]) {+ for (const position of ["top", "left"]) {+ navigations = [];+ transactions = [];+ const element = document.createElement("div");+ const root = document.createElement("div");+ root.className = "protyle-wysiwyg";+ element.append(root);+ document.body.append(element);+ root.innerHTML = lute.Md2BlockDOM("::: tabs\n@tab A\n\nBody A\n@tab B\n\nBody B\n:::\n");+ const tabs = root.firstElementChild;+ const [a, b] = Array.from(tabs.children).filter(item => item.classList.contains("tab-item"));+ check.ok(a && b);+ a.querySelector(".tab-item-content").insertAdjacentHTML("beforeend", lute.Md2BlockDOM(+ "::: tabs\n@tab Inner A\n\nInner body A\n@tab Inner B\n\nInner body B\n:::\n"));+ const protyle = {element, wysiwyg: {element: root}, disabled: readonly, options: {action: []},+ toolbar: {}, block: {id: "doc", rootID: "doc", showAll: false}};+ tabs.setAttribute("tabs-position", position);+ tabs.setAttribute("tabs-active-id", b.dataset.nodeId);+ const fullGroup = tabs.outerHTML;+ api.initEditorTabs(protyle);+ api.openTabsMenu(protyle, tabs, a, tabs);+ menu.find(item => item.label === "Zoom in").click();+ check.deepEqual(navigations, [a.dataset.nodeId]);++ // 聚焦编辑器只加载目标项,原始组的其他内容仍保留在来源数据中。+ protyle.block.id = a.dataset.nodeId;+ protyle.block.showAll = true;+ api.initEditorTabs(protyle);+ check.equal(root.children.length, 1);+ check.equal(root.firstElementChild, a);+ check.equal(root.contains(b), false);+ check.equal(a.hasAttribute("data-tabs-hidden"), false);+ check.equal(a.querySelector(".tab-item-content").hasAttribute("aria-labelledby"), false);+ check.equal(root.querySelector(":scope > .tabs-header"), null);+ check.equal(a.querySelector(".tab-item-title").getAttribute("contenteditable"), String(!readonly));+ check.equal(tabs.getAttribute("tabs-active-id"), b.dataset.nodeId);+ root.append(b.cloneNode(true));+ api.initEditorTabs(protyle);+ check.equal(root.children.length, 1);+ check.equal(root.firstElementChild, a);+ const serialized = lute.BlockDOM2StdMd(root.innerHTML);+ check.match(serialized, /Body A/);+ check.doesNotMatch(serialized, /Body B/);+ check.match(fullGroup, /Body B/);+ const roundtrip = document.createElement("div");+ roundtrip.innerHTML = lute.SpinBlockDOM(root.innerHTML);+ check.equal(roundtrip.firstElementChild.dataset.nodeId, a.dataset.nodeId);+ check.equal(roundtrip.firstElementChild.dataset.type, "NodeTabItem");++ api.selectAll(protyle, a, document.createRange());+ check.deepEqual(selectedIDs, [a.dataset.nodeId]);+ a.classList.remove("protyle-wysiwyg--select");+ a.querySelector(".tab-item-title").dispatchEvent(new MouseEvent("contextmenu", {bubbles: true, cancelable: true}));+ check.equal(menu.some(item => item.label === "Delete"), false);+ if (!readonly) {+ menu.find(item => item.label === "Rename").click();+ api.changeTabs(protyle, [a], () => { a.querySelector(".tab-item-title").textContent = "Edited A"; });+ check.equal(transactions.length, 1);+ check.equal(transactions[0].forward[0].id, a.dataset.nodeId);+ check.doesNotMatch(transactions[0].forward[0].data, /Body B/);+ check.match(transactions[0].undo[0].data, /Body A/);+ }++ const inner = a.querySelector(".tabs");+ const innerButtons = inner.querySelectorAll(":scope > .tabs-header > .tabs-list > .tabs-tab");+ check.equal(innerButtons.length, 2);+ innerButtons[1].click();+ check.equal(innerButtons[1].getAttribute("aria-selected"), "true");++ // 重新加载容器时继续提取目标项,并保留从容器继承的任务状态。+ root.innerHTML = fullGroup;+ root.firstElementChild.setAttribute("tabs-task", "true");+ api.initEditorTabs(protyle);+ check.equal(root.firstElementChild.dataset.nodeId, a.dataset.nodeId);+ check.equal(root.firstElementChild.dataset.type, "NodeTabItem");+ check.equal(root.firstElementChild.getAttribute("tabs-task"), " ");+ check.doesNotMatch(root.innerHTML, /Body B/);++ root.innerHTML = inner.outerHTML;+ protyle.block.id = innerButtons[1].dataset.tabId;+ api.initEditorTabs(protyle);+ check.equal(root.firstElementChild.dataset.nodeId, protyle.block.id);+ check.equal(root.firstElementChild.dataset.type, "NodeTabItem");+ check.match(root.innerHTML, /Inner body B/);+ check.doesNotMatch(root.innerHTML, /Inner body A/);++ protyle.block.id = "doc";+ protyle.block.showAll = false;+ root.innerHTML = fullGroup;+ api.initEditorTabs(protyle);+ const restoredTabs = root.firstElementChild;+ check.equal(restoredTabs.querySelectorAll(":scope > .tabs-header > .tabs-list > .tabs-tab").length, 2);+ check.equal(restoredTabs.querySelector(`[data-node-id="${b.dataset.nodeId}"]`).dataset.tabsHidden, "false");+ check.equal(restoredTabs.dataset.tabsOrientation, position === "left" ? "vertical" : "horizontal");++ for (const surface of ["preview", "backlink", "lite", "history"]) {+ root.innerHTML = fullGroup;+ protyle.block.id = a.dataset.nodeId;+ protyle.block.showAll = true;+ element.classList.toggle("block__popover", surface === "preview");+ protyle.options.backlinkData = surface === "backlink" ? {} : undefined;+ protyle.lite = surface === "lite";+ protyle.options.action = surface === "history" ? ["history"] : [];+ api.initEditorTabs(protyle);+ const group = root.firstElementChild;+ check.equal(group.dataset.type, "NodeTabs");+ check.equal(group.querySelectorAll(":scope > .tabs-header > .tabs-list > .tabs-tab").length, 2);+ api.openTabsMenu(protyle, group, group.querySelector(".tab-item"), group);+ check.equal(menu.some(item => item.label === "Zoom in"), false);+ }+ api.destroyTabsRender(root);+ element.remove();+ }+ }+ style.remove();+ return "Tab item focus cases passed";+};++const run = async () => {+ const {app, BrowserWindow} = require("electron");+ app.setPath("userData", process.argv[2]);+ app.commandLine.appendSwitch("disable-gpu");+ await app.whenReady();+ const win = new BrowserWindow({show: false, webPreferences: {+ nodeIntegration: true, contextIsolation: false, offscreen: true,+ }});+ let code = 0;+ try {+ await win.loadURL("data:text/html,<html><body></body></html>");+ await win.webContents.executeJavaScript(readFileSync(path.join(__dirname,+ "../stage/protyle/js/lute/lute.min.js"), "utf8"));+ const result = await win.webContents.executeJavaScript(`(async () => { try {+ return await (${cases.toString()})(${JSON.stringify(sources())});+ } catch (error) { return error.stack; } })()`);+ assert.equal(result, "Tab item focus cases passed");+ console.log(result);+ } catch (error) {+ console.error(error);+ code = 1;+ } finally {+ win.destroy();+ app.exit(code);+ }+};++if (process.versions.electron && process.type === "browser") {+ run().catch(error => { console.error(error); require("electron").app.exit(1); });+} else {+ require("node:test").test("tab item focus isolates display, preserves data, and restores navigation", {+ skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,+ timeout: 45000,+ }, async () => {+ const profile = mkdtempSync(path.join(os.tmpdir(), "siyuan-tabs-focus-test-"));+ const env = {...process.env};+ delete env.ELECTRON_RUN_AS_NODE;+ try {+ const {stdout} = await require("node:util").promisify(require("node:child_process").execFile)(+ require("electron"), [__filename, profile], {env, windowsHide: true, timeout: 40000});+ assert.match(stdout, /Tab item focus cases passed/);+ } finally {+ assert.ok(path.resolve(profile).startsWith(path.resolve(os.tmpdir()) + path.sep));+ rmSync(profile, {recursive: true, force: true});+ }+ });+}