Skip to content

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

:bug: Prevent tab block attributes from being obscured by tab headers

Daniel6ac89763bd2d
diff --git a/app/src/assets/scss/protyle/_tabs.scss b/app/src/assets/scss/protyle/_tabs.scss
index 118175fb26..3b8915fafd 100644
--- a/app/src/assets/scss/protyle/_tabs.scss
+++ b/app/src/assets/scss/protyle/_tabs.scss
@@ -327,6 +327,93 @@
}
.protyle-wysiwyg {
+ .tabs-attributes {
+ display: none;
+ align-items: center;
+ gap: 4px;
+ flex: 0 1 auto;
+ min-width: 24px;
+ max-width: 40%;
+ height: 24px;
+ padding: 2px 4px;
+ box-sizing: border-box;
+ border: 0;
+ border-radius: var(--b3-border-radius);
+ background: transparent;
+ color: var(--b3-theme-on-surface);
+ font: 12px/20px var(--b3-font-family);
+ cursor: pointer;
+
+ &:hover {
+ background: var(--b3-theme-surface);
+ }
+
+ svg {
+ width: 12px;
+ height: 12px;
+ flex-shrink: 0;
+ }
+
+ &__icon {
+ display: none;
+ }
+
+ &__summary {
+ display: flex;
+ min-width: 0;
+ gap: 6px;
+
+ > span {
+ display: flex;
+ align-items: center;
+ gap: 2px;
+ min-width: 0;
+ overflow: hidden;
+ white-space: nowrap;
+ }
+ }
+
+ &--compact {
+ flex-shrink: 0;
+
+ .tabs-attributes__icon {
+ display: block;
+ }
+
+ .tabs-attributes__summary {
+ display: none;
+ }
+ }
+ }
+
+ .tabs-header > .tabs-attributes {
+ margin-left: auto;
+ }
+
+ // 首个子块沿用普通属性样式,仅将属性移入内容区以避开吸顶栏。
+ .tabs[data-tabs-ready="true"][data-tabs-orientation="horizontal"] > .tab-item >
+ .tab-item-content > [data-node-id]:first-child > .protyle-attr {
+ top: 0;
+ }
+
+ @media screen {
+ &--attr {
+ .tabs-attributes {
+ display: flex;
+ }
+
+ // 原属性保留供事务读取,隐藏时不与通用属性的文本截断显示规则竞争。
+ [data-tabs-attributes] > .protyle-attr {
+ visibility: hidden;
+
+ > .protyle-attr--refcount {
+ visibility: visible;
+ }
+ }
+
+ }
+ }
+
.protyle-wysiwyg--select > .tabs-header::after,
.protyle-wysiwyg--select-mode > .tabs-header::after,
.protyle-wysiwyg--hl > .tabs-header::after {
diff --git a/app/src/protyle/render/tabsAttributes.ts b/app/src/protyle/render/tabsAttributes.ts
new file mode 100644
index 0000000000..1916f0b48c
--- /dev/null
+++ b/app/src/protyle/render/tabsAttributes.ts
@@ -0,0 +1,63 @@
+interface ITabsAttributesOptions {
+ label: string;
+ open: (block: HTMLElement, focus: string) => void;
+}
+
+export const clearTabsAttributes = (root: Element) => {
+ root.querySelectorAll(".tabs-attributes").forEach(button => button.remove());
+ root.querySelectorAll("[data-tabs-attributes]").forEach(block => block.removeAttribute("data-tabs-attributes"));
+};
+
+// 属性控件仅投影原始属性,保留块内属性节点供事务、复制及导出读取。
+export const renderTabsAttributes = (tabs: HTMLElement, item: HTMLElement, header: HTMLElement,
+ options?: ITabsAttributesOptions) => {
+ if (!options || tabs.dataset.tabsOrientation !== "horizontal") {
+ return;
+ }
+ const render = (block: HTMLElement) => {
+ const source = block?.querySelector(":scope > .protyle-attr");
+ const attrs = source && Array.from(source.children).filter(attr =>
+ !attr.classList.contains("protyle-attr--refcount"));
+ if (!attrs?.length) {
+ return;
+ }
+ const button = document.createElement("button");
+ button.type = "button";
+ button.className = "tabs-attributes protyle-action";
+ button.dataset.blockId = block.dataset.nodeId;
+ button.setAttribute("contenteditable", "false");
+ button.setAttribute("aria-label", options.label);
+ button.innerHTML = '<svg class="tabs-attributes__icon"><use xlink:href="#iconAttr"></use></svg>';
+ const summary = document.createElement("span");
+ summary.className = "tabs-attributes__summary";
+ attrs.forEach(attr => {
+ const value = document.createElement("span");
+ const focus = ["bookmark", "name", "alias", "memo", "av"].find(key =>
+ attr.classList.contains(`protyle-attr--${key}`));
+ value.dataset.focus = focus || "bookmark";
+ const icon = attr.querySelector("svg");
+ if (icon) {
+ value.appendChild(icon.cloneNode(true));
+ }
+ const text = attr.textContent.trim() || attr.getAttribute("aria-label") || "";
+ value.appendChild(document.createTextNode(text));
+ value.title = text;
+ summary.appendChild(value);
+ });
+ button.appendChild(summary);
+ button.title = Array.from(summary.children).map(child => child.textContent).join(" · ");
+ button.addEventListener("mousedown", event => {
+ event.preventDefault();
+ event.stopPropagation();
+ });
+ button.addEventListener("click", event => {
+ event.preventDefault();
+ event.stopPropagation();
+ options.open(block, (event.target as Element).closest<HTMLElement>("[data-focus]")?.dataset.focus || "bookmark");
+ });
+ block.dataset.tabsAttributes = "header";
+ header.appendChild(button);
+ button.classList.toggle("tabs-attributes--compact", tabs.clientWidth < 420);
+ };
+ render(item);
+};
diff --git a/app/src/protyle/render/tabsRender.ts b/app/src/protyle/render/tabsRender.ts
index 9d74907b7e..8272906d69 100644
--- a/app/src/protyle/render/tabsRender.ts
+++ b/app/src/protyle/render/tabsRender.ts
@@ -1,6 +1,7 @@
import {resolveTabID, tabKeyboardTarget} from "./tabsState";
import {bindTabsDrag, cancelTabsDrag, isDraggingTabs} from "./tabsDrag";
import {escapeHtml} from "../../util/escape";
+import {clearTabsAttributes, renderTabsAttributes} from "./tabsAttributes";
export interface ITabsRenderOptions {
readonly?: (tabs?: Element) => boolean;
@@ -17,6 +18,7 @@ export interface ITabsRenderOptions {
taskMenu?: (item: HTMLElement) => void;
taskLabel?: string;
endEdit?: () => void;
+ attributes?: {label: string, open: (block: HTMLElement, focus: string) => void};
}
interface ITabState {
@@ -159,6 +161,10 @@ export const tabsRender = (element: Element, options: ITabsRenderOptions = {}) =
}
controller.observer.disconnect();
controller.resize.disconnect();
+ const focusedAttributes = document.activeElement?.closest<HTMLElement>(".tabs-attributes");
+ const focusedAttributesID = focusedAttributes && element.contains(focusedAttributes) ?
+ focusedAttributes.dataset.blockId : undefined;
+ clearTabsAttributes(element);
const shown: HTMLElement[] = [];
getTabs().forEach(tabs => {
const items = getTabItems(tabs);
@@ -441,6 +447,8 @@ export const tabsRender = (element: Element, options: ITabsRenderOptions = {}) =
}
});
tabs.setAttribute("data-tabs-ready", "true");
+ renderTabsAttributes(tabs, items.find(item => itemID(item) === state.active), header,
+ controller.options.attributes);
// 从容器读取主题设置的实际边框,吸顶标题沿用相同宽度及圆角。
const tabsStyle = getComputedStyle(tabs);
["top-width", "right-width", "left-width", "top-left-radius", "top-right-radius"].forEach(property => {
@@ -497,6 +505,10 @@ export const tabsRender = (element: Element, options: ITabsRenderOptions = {}) =
shown.push(items.find(item => itemID(item) === state.active));
}
});
+ if (focusedAttributesID) {
+ Array.from(element.querySelectorAll<HTMLElement>(".tabs-attributes"))
+ .find(button => button.dataset.blockId === focusedAttributesID)?.focus({preventScroll: true});
+ }
controller.observer.observe(element, {
childList: true, subtree: true, characterData: true, attributes: true,
attributeFilter: ["tabs-active-id", "tabs-position", "tabs-task", "data-readonly", "data-tabs-editing", "contenteditable"],
@@ -523,6 +535,7 @@ export const tabsRender = (element: Element, options: ITabsRenderOptions = {}) =
destroyed = true;
controller.observer.disconnect();
controller.resize.disconnect();
+ clearTabsAttributes(element);
element.removeEventListener("focusout", onFocusOut);
element.removeEventListener("scroll", schedule, true);
document.removeEventListener("scroll", onAncestorScroll, true);
diff --git a/app/src/protyle/render/tabsVisibility.ts b/app/src/protyle/render/tabsVisibility.ts
index 90519c2d9b..7799b9d967 100644
--- a/app/src/protyle/render/tabsVisibility.ts
+++ b/app/src/protyle/render/tabsVisibility.ts
@@ -5,7 +5,8 @@ export const isHiddenTabContent = (element: Element) =>
export const visibleTabsSelectionHTML = (html: string) => {
const template = document.createElement("template");
template.innerHTML = html;
- template.content.querySelectorAll('.tab-item[data-tabs-hidden="true"], .tabs-header, .tabs-divider').forEach(item => item.remove());
+ template.content.querySelectorAll('.tab-item[data-tabs-hidden="true"], .tabs-header, .tabs-divider, .tabs-attributes').forEach(item => item.remove());
+ template.content.querySelectorAll("[data-tabs-attributes]").forEach(item => item.removeAttribute("data-tabs-attributes"));
template.content.querySelectorAll('.tabs[data-tabs-ready="true"] > .tab-item > .tab-item-info').forEach(item => {
if ((item.parentElement as HTMLElement).dataset.tabsEditing !== "true") {
item.remove();
diff --git a/app/src/protyle/wysiwyg/tabs.ts b/app/src/protyle/wysiwyg/tabs.ts
index 9b48b54bfd..ade22f3e17 100644
--- a/app/src/protyle/wysiwyg/tabs.ts
+++ b/app/src/protyle/wysiwyg/tabs.ts
@@ -12,7 +12,7 @@ import {isHiddenTabContent} from "../render/tabsVisibility";
import {queueTransaction} from "../util/transactionQueue";
import {remapTabsDOMIDs} from "../util/tabsCopy";
import {remapListMindmapIDs} from "../render/listMindmap/model";
-import {copySubMenu} from "../../menus/commonMenuItem";
+import {copySubMenu, openAttr} from "../../menus/commonMenuItem";
import {isTaskListMarker, nextTaskListMarker} from "./taskListMarker";
import {hideElements} from "../ui/hideElements";
import {getTaskStatusItems} from "./taskStatusDialog";
@@ -202,6 +202,7 @@ export const initEditorTabs = (protyle: IProtyle) => {
label: window.siyuan.languages.tabItem,
addLabel: window.siyuan.languages.newTabItem,
taskLabel: window.siyuan.languages.task,
+ attributes: {label: window.siyuan.languages.attr, open: (block, focus) => openAttr(block, focus, protyle)},
task: item => setTabTask(protyle, item, nextTaskListMarker(getTabTask(item))),
endEdit: () => hideElements(["toolbar"], protyle),
activate: item => {