Skip to content

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

:bug: Keep wrapped heading gutters selectable https://github.com/siyuan-note/siyuan/issues/18675

Daniel7ae8355603bf
diff --git a/app/src/block/popover.ts b/app/src/block/popover.ts
index 5b2874dad4..16aee4418b 100644
--- a/app/src/block/popover.ts
+++ b/app/src/block/popover.ts
@@ -74,6 +74,7 @@ export const initBlockPopover = (app: App) => {
hasClosestByClassName(event.target, "av__calc--ashow") ||
hasClosestByClassName(event.target, "av__cell");
if (aElement) {
+ const aElementParent = aElement.parentElement;
let tooltipClass = "";
let tip = aElement.getAttribute("aria-label") || "";
if (aElement.classList.contains("av__cell") && !aElement.classList.contains("ariaLabel")) {
@@ -103,7 +104,8 @@ export const initBlockPopover = (app: App) => {
aElement.style.overflow = "";
}
}
- } else if (aElement.parentElement.parentElement.classList.contains("av__views") && aElement.parentElement.classList.contains("layout-tab-bar")) {
+ } else if (aElementParent?.parentElement?.classList.contains("av__views") &&
+ aElementParent.classList.contains("layout-tab-bar")) {
const textElement = aElement.querySelector(".item__text");
const desc = aElement.getAttribute("data-desc");
if (textElement.scrollWidth > textElement.clientWidth + 0.5 || desc) {
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index a34d77ab6f..cdcfd1a679 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -93,6 +93,7 @@ import {clearSelect} from "../util/clear";
import {chartRender} from "../render/chartRender";
import {appendListItem, prependListItem} from "../wysiwyg/list";
import {applyHeadingLevelUpdates, getHeadingLevelUpdateOperations} from "../util/headingTransform";
+import {getGutterMarginHeight} from "./layout";
// 块类型 data-type 到本地化名称键的映射,用于块标提示中的 ${x}
const BLOCK_TYPE_LANG_KEYS: { [key: string]: string } = {
@@ -137,7 +138,9 @@ export class Gutter {
private gutterTip: string;
private gutterTipBacklink: string;
private renderKey = "";
+ // 缓存横排尺寸,压缩为纵排后仍使用横排尺寸计算位置
private naturalWidth = 0;
+ private naturalHeight = 0;
constructor(protyle: IProtyle) {
if (isMac()) {
@@ -3506,6 +3509,7 @@ data-type="fold" style="cursor:inherit;"><svg style="width: 10px;${fold && fold
this.element.innerHTML = html;
this.element.style.width = "";
this.naturalWidth = this.element.clientWidth;
+ this.naturalHeight = this.element.clientHeight;
this.renderKey = renderKey;
}
let contentTop = protyle.contentElement.getBoundingClientRect().top;
@@ -3542,6 +3546,7 @@ data-type="fold" style="cursor:inherit;"><svg style="width: 10px;${fold && fold
this.element.innerHTML = html;
this.element.style.width = "";
this.naturalWidth = this.element.clientWidth;
+ this.naturalHeight = this.element.clientHeight;
naturalLeft = getNaturalLeft(this.naturalWidth);
compressed = naturalLeft < this.element.parentElement.getBoundingClientRect().left;
shouldRenderContent = true;
@@ -3567,10 +3572,14 @@ data-type="fold" style="cursor:inherit;"><svg style="width: 10px;${fold && fold
this.element.insertAdjacentHTML("beforeend", `<button class="protyle-gutters__line" data-type="gutterLineBefore" style="display:none"></button><button class="protyle-gutters__line" data-type="gutterLineAfter" style="display:none"></button><button class="protyle-gutters__plus ariaLabel" data-type="gutterPlusBefore" data-position="4west" aria-label="${window.siyuan.languages.insertBefore}" style="display:none"><svg><use xlink:href="#iconAdd"></use></svg></button><button class="protyle-gutters__plus ariaLabel" data-type="gutterPlusAfter" data-position="4west" aria-label="${window.siyuan.languages.insertAfter}" style="display:none"><svg><use xlink:href="#iconAdd"></use></svg></button>`);
}
if (!element.classList.contains("av__row")) {
- if (rect.height < Math.floor(window.siyuan.config.editor.fontSize * 1.625) + 8 ||
- (rect.height > Math.floor(window.siyuan.config.editor.fontSize * 1.625) + 8 && rect.height < Math.floor(window.siyuan.config.editor.fontSize * 1.625) * 2 + 8)) {
- marginHeight = (rect.height - this.element.clientHeight) / 2;
- } else if ((nodeElement.getAttribute("data-type") === "NodeAttributeView" || element.getAttribute("data-type") === "NodeAttributeView") &&
+ const gutterMarginHeight = getGutterMarginHeight(
+ rect.height, this.element.clientHeight, this.naturalHeight,
+ window.siyuan.config.editor.fontSize);
+ if (typeof gutterMarginHeight === "number") {
+ marginHeight = gutterMarginHeight;
+ } else if (
+ (nodeElement.getAttribute("data-type") === "NodeAttributeView" ||
+ element.getAttribute("data-type") === "NodeAttributeView") &&
contentTop < rect.top) {
marginHeight = 8;
}
diff --git a/app/src/protyle/gutter/layout.test.ts b/app/src/protyle/gutter/layout.test.ts
new file mode 100644
index 0000000000..46c98bd8c7
--- /dev/null
+++ b/app/src/protyle/gutter/layout.test.ts
@@ -0,0 +1,24 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {getGutterMarginHeight} from "./layout";
+
+describe("getGutterMarginHeight", () => {
+ it("uses the natural height after heading gutter buttons wrap", () => {
+ const editorFontSize = 16;
+ const wrappedHeight = 56;
+ const naturalHeight = 34;
+
+ [1.38, 1.25, 1.13].forEach(scale => {
+ const headingHeight = editorFontSize * 1.625 * scale + 8;
+ const marginHeight = getGutterMarginHeight(
+ headingHeight, wrappedHeight, naturalHeight, editorFontSize);
+
+ assert.ok(marginHeight + 6 >= 0);
+ assert.ok(marginHeight + 6 <= headingHeight);
+ });
+ });
+
+ it("falls back to the current height before the natural height is measured", () => {
+ assert.equal(getGutterMarginHeight(44, 34, 0, 16), 5);
+ });
+});
diff --git a/app/src/protyle/gutter/layout.ts b/app/src/protyle/gutter/layout.ts
new file mode 100644
index 0000000000..e11f12bdbd
--- /dev/null
+++ b/app/src/protyle/gutter/layout.ts
@@ -0,0 +1,8 @@
+export const getGutterMarginHeight = (blockHeight: number, gutterHeight: number, naturalGutterHeight: number,
+ editorFontSize: number) => {
+ const lineHeight = Math.floor(editorFontSize * 1.625);
+ if (blockHeight < lineHeight + 8 ||
+ (blockHeight > lineHeight + 8 && blockHeight < lineHeight * 2 + 8)) {
+ return (blockHeight - (naturalGutterHeight || gutterHeight)) / 2;
+ }
+};