Skip to content

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

:bug: Keep block gutters aligned with moved blocks https://github.com/siyuan-note/siyuan/issues/12321

Daniel0bffce84b1dd
diff --git a/app/src/layout/tabUtil.ts b/app/src/layout/tabUtil.ts
index 706c02432f..5ef3fd2809 100644
--- a/app/src/layout/tabUtil.ts
+++ b/app/src/layout/tabUtil.ts
@@ -193,6 +193,7 @@ export const switchTabByIndex = (index: number) => {
let resizeTimeout: number;
export const resizeTabs = (isSaveLayout = true) => {
+ hideAllElements(["gutter"]);
clearTimeout(resizeTimeout);
// .layout .fn__flex-shrink {width .15s cubic-bezier(0, 0, .2, 1) 0ms} 时需要再次计算 padding
// PDF 避免分屏多次调用后,页码跳转到1 https://github.com/siyuan-note/siyuan/issues/5646
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index 42f737ef3d..60aa809f9b 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -134,6 +134,8 @@ export class Gutter {
// 普通块标提示模板(含 ${x} 块类型占位符),反链面板使用 gutterTipBacklink
private gutterTip: string;
private gutterTipBacklink: string;
+ private renderKey = "";
+ private naturalWidth = 0;
constructor(protyle: IProtyle) {
if (isMac()) {
@@ -328,6 +330,7 @@ export class Gutter {
const blockButtonElement = buttonElement.previousElementSibling || buttonElement.nextElementSibling;
const foldElement = this.getNodeElement(protyle, blockButtonElement);
if (!foldElement) {
+ hideElements(["gutter"], protyle);
return;
}
if (event.altKey && foldElement.getAttribute("data-type") === "NodeHeading") {
@@ -388,6 +391,7 @@ export class Gutter {
(buttonElement.firstElementChild as HTMLElement).style.transform = "rotate(90deg)";
}
}
+ hideElements(["gutter"], protyle);
hideElements(["select"], protyle);
window.siyuan.menus.menu.remove();
return;
@@ -400,6 +404,7 @@ export class Gutter {
}
const nodeElement = this.getNodeElement(protyle, activeBlockButton);
if (!nodeElement) {
+ hideElements(["gutter"], protyle);
return;
}
hideElements(["gutter"], protyle);
@@ -414,6 +419,7 @@ export class Gutter {
}
});
if (!rowElement) {
+ hideElements(["gutter"], protyle);
return;
}
const blockElement = hasClosestBlock(rowElement);
@@ -496,11 +502,11 @@ export class Gutter {
} else if (event.altKey) {
const foldElement = this.getNodeElement(protyle, buttonElement);
if (!foldElement) {
+ hideElements(["gutter"], protyle);
return;
}
if (buttonElement.getAttribute("data-type") === "NodeHeading") {
foldHeadingGroup(protyle, foldElement, "siblings");
- hideElements(["gutter"], protyle);
} else if (buttonElement.getAttribute("data-type") === "NodeListItem" && foldElement.parentElement.getAttribute("data-node-id")) {
// 折叠同级
let hasFold = true;
@@ -547,6 +553,7 @@ export class Gutter {
}
}
foldElement.classList.remove("protyle-wysiwyg--hl");
+ hideElements(["gutter"], protyle);
} else if (event.shiftKey && !protyle.disabled && !isEncryptedBox(protyle.notebookId)) {
// 不使用 window.siyuan.shiftIsPressed ,否则窗口未激活时按 Shift 点击块标无法打开属性面板 https://github.com/siyuan-note/siyuan/issues/15075
openAttr(this.getNodeElement(protyle, buttonElement), "bookmark", protyle);
@@ -590,6 +597,8 @@ export class Gutter {
h: gutterRect.height,
isLeft: true
});
+ } else {
+ hideElements(["gutter"], protyle);
}
} else if (buttonElement.dataset.type !== "NodeAttributeViewRow") {
this.renderMenu(protyle, buttonElement);
@@ -1423,6 +1432,7 @@ export class Gutter {
const id = buttonElement.getAttribute("data-node-id");
const nodeElement = this.getNodeElement(protyle, buttonElement);
if (!nodeElement) {
+ hideElements(["gutter"], protyle);
return;
}
const editableElement = getContenteditableElement(nodeElement);
@@ -3432,37 +3442,18 @@ data-type="fold" style="cursor:inherit;"><svg style="width: 10px;${fold && fold
}
const shouldRenderInsert = !embedContext && !isMultiSelect;
const insertElementCount = this.element.querySelectorAll(".protyle-gutters__line, .protyle-gutters__plus").length;
- let match = isGutterInsertStateMatched(insertElementCount, shouldRenderInsert);
- // 统计时排除块标边缘框线与+号元素,它们由 render 末尾单独追加,不参与防抖比较
- const buttonsElement = this.element.querySelectorAll("button:not(.protyle-gutters__line):not(.protyle-gutters__plus)");
- if (buttonsElement.length !== html.split("</button>").length - 1) {
- match = false;
- } else {
- Array.from(buttonsElement).find(item => {
- if (item.getAttribute("data-node-id") && (item as HTMLElement).dataset.embedId !== embedID) {
- match = false;
- return true;
- }
- const id = item.getAttribute("data-node-id");
- if (id && html.indexOf(id) === -1) {
- match = false;
- return true;
- }
- const rowId = item.getAttribute("data-row-id");
- if ((rowId && html.indexOf(rowId) === -1) || (!rowId && html.indexOf("NodeAttributeViewRowMenu") > -1)) {
- match = false;
- return true;
- }
- });
- }
- // 防止抖动 https://github.com/siyuan-note/siyuan/issues/4166
- if (match && this.element.childElementCount > 0) {
- this.element.classList.remove("fn__none");
- return;
- }
- this.element.innerHTML = html;
+ const renderKey = `${shouldRenderInsert ? "1" : "0"}${html}`;
+ let shouldRenderContent = this.renderKey !== renderKey || this.element.childElementCount === 0 ||
+ !isGutterInsertStateMatched(insertElementCount, shouldRenderInsert);
+ // 相同内容保留现有 DOM 防止抖动,但仍需继续更新位置 https://github.com/siyuan-note/siyuan/issues/12321
+ const wasCompressed = this.element.style.width === "24px";
this.element.classList.remove("fn__none");
- this.element.style.width = "";
+ if (shouldRenderContent) {
+ this.element.innerHTML = html;
+ this.element.style.width = "";
+ this.naturalWidth = this.element.clientWidth;
+ this.renderKey = renderKey;
+ }
let contentTop = protyle.contentElement.getBoundingClientRect().top;
if (protyle.options.backlinkData) {
const backlinkElement = protyle.element.closest(".backlinkList, .backlinkMList");
@@ -3478,53 +3469,62 @@ data-type="fold" style="cursor:inherit;"><svg style="width: 10px;${fold && fold
} else if (nodeElement.getAttribute("data-type") === "NodeBlockQueryEmbed") {
rect = nodeElement.getBoundingClientRect();
space = 0;
- } else 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") &&
- contentTop < rect.top) {
- marginHeight = 8;
- }
}
- const foldElement = hasClosestByAttribute(element.parentElement, "fold", "1") as HTMLElement;
- this.element.style.top = `${Math.max(rect.top + marginHeight, contentTop, foldElement ? foldElement.getBoundingClientRect().top : 0)}px`;
- let left = rect.left - this.element.clientWidth - space;
- if ((nodeElement.getAttribute("data-type") === "NodeBlockQueryEmbed" && this.element.childElementCount === 1)) {
- // 嵌入块为列表时
- left = nodeElement.getBoundingClientRect().left - this.element.clientWidth - space;
- } else if (element.classList.contains("av__row")) {
- // 为数据库行
- left = nodeElement.getBoundingClientRect().left - this.element.clientWidth - space + parseInt(getComputedStyle(nodeElement).paddingLeft);
+ const buttonCount = html.split("</button>").length - 1;
+ const getNaturalLeft = (width: number) => {
+ let left = rect.left - width - space;
+ if (nodeElement.getAttribute("data-type") === "NodeBlockQueryEmbed" && buttonCount === 1) {
+ // 嵌入块为列表时
+ left = nodeElement.getBoundingClientRect().left - width - space;
+ } else if (element.classList.contains("av__row")) {
+ // 为数据库行
+ left = nodeElement.getBoundingClientRect().left - width - space + parseInt(getComputedStyle(nodeElement).paddingLeft);
+ }
+ return left;
+ };
+ let naturalLeft = getNaturalLeft(this.naturalWidth || this.element.clientWidth);
+ let compressed = naturalLeft < this.element.parentElement.getBoundingClientRect().left;
+ if (!shouldRenderContent && compressed !== wasCompressed) {
+ this.element.innerHTML = html;
+ this.element.style.width = "";
+ this.naturalWidth = this.element.clientWidth;
+ naturalLeft = getNaturalLeft(this.naturalWidth);
+ compressed = naturalLeft < this.element.parentElement.getBoundingClientRect().left;
+ shouldRenderContent = true;
}
- this.element.style.left = `${left}px`;
- if (left < this.element.parentElement.getBoundingClientRect().left) {
+ if (shouldRenderContent && compressed) {
this.element.style.width = "24px";
- // 需加 2,否则和折叠标题无法对齐
- this.element.style.left = `${rect.left - this.element.clientWidth - space / 2 + 3}px`;
- html = "";
+ let compressedHTML = "";
Array.from(this.element.children).reverse().forEach((item, index) => {
- // 跳过块标边缘框线与+号元素,避免被压缩重排
- if (item.classList.contains("protyle-gutters__line") || item.classList.contains("protyle-gutters__plus")) {
- return;
- }
if (index !== 0) {
(item.firstElementChild as HTMLElement).style.height = "14px";
}
- html += item.outerHTML;
+ compressedHTML += item.outerHTML;
});
- this.element.innerHTML = html;
- } else {
+ this.element.innerHTML = compressedHTML;
+ } else if (shouldRenderContent) {
+ this.element.style.width = "";
this.element.querySelectorAll("svg").forEach(item => {
item.style.height = "";
});
}
- // 追加块标边缘悬浮触发的插入元素(默认隐藏,悬浮块标显示线条,悬浮线条变+号),由 mousemove 定位
- // 追加块标边缘的框线(悬浮块标显示)与+号(悬浮框线显示),默认隐藏,由 mousemove 定位
- // 双元素:框线贴块标边缘不移动(避免闪烁),+号独立定位在外偏位置,tooltip 基于+号元素对齐
- if (shouldRenderInsert) {
+ if (shouldRenderContent && shouldRenderInsert) {
+ // 双元素:框线贴块标边缘不移动,+号独立定位在外偏位置,tooltip 基于+号元素对齐
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") &&
+ contentTop < rect.top) {
+ marginHeight = 8;
+ }
+ }
+ const foldElement = hasClosestByAttribute(element.parentElement, "fold", "1") as HTMLElement;
+ this.element.style.top = `${Math.max(rect.top + marginHeight, contentTop, foldElement ? foldElement.getBoundingClientRect().top : 0)}px`;
+ // 压缩模式需加 2,否则和折叠标题无法对齐
+ this.element.style.left = `${compressed ? rect.left - this.element.clientWidth - space / 2 + 3 : getNaturalLeft(this.element.clientWidth)}px`;
}
}
diff --git a/app/src/protyle/ui/initUI.ts b/app/src/protyle/ui/initUI.ts
index 704068fcc4..a9fa2d7f3f 100644
--- a/app/src/protyle/ui/initUI.ts
+++ b/app/src/protyle/ui/initUI.ts
@@ -298,29 +298,51 @@ export const initUI = (protyle: IProtyle) => {
const buttonElement = hasClosestByTag(event.target, "BUTTON");
if (buttonElement && buttonElement.parentElement.classList.contains("protyle-gutters")) {
const type = buttonElement.getAttribute("data-type");
- if (type === "fold" || type === "NodeAttributeViewRow") {
+ if (buttonElement.classList.contains("protyle-gutters__line") ||
+ buttonElement.classList.contains("protyle-gutters__plus")) {
+ return;
+ }
+ const targetButtonElement = type === "fold" ?
+ buttonElement.previousElementSibling || buttonElement.nextElementSibling : buttonElement;
+ if (!targetButtonElement) {
+ hideElements(["gutter"], protyle);
+ return;
+ }
+ const gutterNodeElement = protyle.gutter.getNodeElement(protyle, targetButtonElement);
+ if (!gutterNodeElement) {
+ hideElements(["gutter"], protyle);
+ return;
+ }
+ if (type === "fold") {
Array.from(protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--hl, .av__row--hl")).forEach(item => {
item.classList.remove("protyle-wysiwyg--hl", "av__row--hl");
});
return;
}
- const gutterNodeElement = protyle.gutter.getNodeElement(protyle, buttonElement);
- if (gutterNodeElement) {
- const bodyQueryClass = (buttonElement.dataset.groupId && buttonElement.dataset.groupId !== "undefined") ? `.av__body[data-group-id="${buttonElement.dataset.groupId}"] ` : "";
- const rowItem = gutterNodeElement.querySelector(bodyQueryClass + `.av__row[data-id="${buttonElement.dataset.rowId}"]`);
- Array.from(protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--hl, .av__row--hl")).forEach(hlItem => {
- if (gutterNodeElement !== hlItem) {
- hlItem.classList.remove("protyle-wysiwyg--hl");
- }
- if (rowItem && rowItem !== hlItem) {
- rowItem.classList.remove("av__row--hl");
- }
+ const bodyQueryClass = (buttonElement.dataset.groupId && buttonElement.dataset.groupId !== "undefined") ? `.av__body[data-group-id="${buttonElement.dataset.groupId}"] ` : "";
+ const rowItem = gutterNodeElement.querySelector(bodyQueryClass + `.av__row[data-id="${buttonElement.dataset.rowId}"]`);
+ if ((type === "NodeAttributeViewRow" || type === "NodeAttributeViewRowMenu") && !rowItem) {
+ hideElements(["gutter"], protyle);
+ return;
+ }
+ if (type === "NodeAttributeViewRow") {
+ Array.from(protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--hl, .av__row--hl")).forEach(item => {
+ item.classList.remove("protyle-wysiwyg--hl", "av__row--hl");
});
- if (type === "NodeAttributeViewRowMenu") {
- rowItem.classList.add("av__row--hl");
- } else {
- gutterNodeElement.classList.add("protyle-wysiwyg--hl");
+ return;
+ }
+ Array.from(protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--hl, .av__row--hl")).forEach(hlItem => {
+ if (gutterNodeElement !== hlItem) {
+ hlItem.classList.remove("protyle-wysiwyg--hl");
+ }
+ if (rowItem && rowItem !== hlItem) {
+ rowItem.classList.remove("av__row--hl");
}
+ });
+ if (type === "NodeAttributeViewRowMenu") {
+ rowItem.classList.add("av__row--hl");
+ } else {
+ gutterNodeElement.classList.add("protyle-wysiwyg--hl");
}
event.preventDefault();
return;
diff --git a/app/src/protyle/util/onGet.ts b/app/src/protyle/util/onGet.ts
index 1fcc488dcc..5bf7efd943 100644
--- a/app/src/protyle/util/onGet.ts
+++ b/app/src/protyle/util/onGet.ts
@@ -512,6 +512,7 @@ export const enableProtyle = (protyle: IProtyle) => {
protyle.breadcrumb.element.parentElement.querySelector('[data-type="outdent"]').classList.remove("fn__none");
}
}
+ hideElements(["gutter"], protyle);
hideTooltip();
};