Skip to content

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

:art: https://github.com/siyuan-note/siyuan/issues/9521

Vanessae115dd3e60b3
diff --git a/app/src/assets/scss/protyle/_wysiwyg.scss b/app/src/assets/scss/protyle/_wysiwyg.scss
index 2010e0b20b..25caf6b95e 100644
--- a/app/src/assets/scss/protyle/_wysiwyg.scss
+++ b/app/src/assets/scss/protyle/_wysiwyg.scss
@@ -279,7 +279,6 @@
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
- column-gap: 1.5em;
& > div {
flex: 1 0 10%;
@@ -292,6 +291,24 @@
}
}
+ // 超级块内拖拽调整子块宽度的手柄 https://github.com/siyuan-note/siyuan/issues/9521
+ > .sb__resize {
+ align-self: stretch;
+ width: 4px;
+ border-radius: var(--b3-border-radius);
+ cursor: col-resize;
+ touch-action: none;
+ opacity: 0;
+ transition: var(--b3-color-transition);
+ margin-right: 16px;
+ z-index: 5;
+ }
+
+ &:hover > .sb__resize {
+ opacity: 1;
+ background-color: var(--b3-border-color);
+ }
+
.sb {
margin-top: 0;
margin-bottom: 0;
diff --git a/app/src/block/util.ts b/app/src/block/util.ts
index e2f08d294a..e6f192d096 100644
--- a/app/src/block/util.ts
+++ b/app/src/block/util.ts
@@ -50,6 +50,8 @@ export const cancelSB = async (protyle: IProtyle, nodeElement: Element, range?:
getContenteditableElement(nodeElement).insertAdjacentHTML("afterbegin", "<wbr>");
}
nodeElement.lastElementChild.remove();
+ // 清理残留的拖拽手柄,避免被当作兄弟块移出 https://github.com/siyuan-note/siyuan/issues/9521
+ nodeElement.querySelectorAll(".sb__resize").forEach(handle => handle.remove());
nodeElement.replaceWith(...nodeElement.children);
if (range) {
focusByWbr(protyle.wysiwyg.element, range);
@@ -94,6 +96,31 @@ export const genSBElement = (layout: string, id?: string, attrHTML?: string) =>
return sbElement;
};
+// 统计超级块内的真实子块数量(排除 sb__resize 手柄、protyle-attr 等装饰元素)
+// https://github.com/siyuan-note/siyuan/issues/9521
+export const getSbChildCount = (sbElement: Element) =>
+ sbElement.querySelectorAll(":scope > [data-node-id]").length;
+
+// 刷新超级块横向布局下的拖拽手柄:col 布局在每两个相邻子块间插入 sb__resize,非 col 移除全部
+// 手柄是纯装饰元素,回流时由 lute 的 genASTByBlockDOM 按 sb__resize class 忽略,不产生幽灵块
+// https://github.com/siyuan-note/siyuan/issues/9521
+export const refreshSbResize = (sbElement: Element) => {
+ if (!sbElement || !sbElement.classList.contains("sb")) {
+ return;
+ }
+ sbElement.querySelectorAll(":scope > .sb__resize").forEach(item => item.remove());
+ if (sbElement.getAttribute("data-sb-layout") !== "col") {
+ return;
+ }
+ const children = Array.from(sbElement.querySelectorAll(":scope > [data-node-id]"));
+ for (let i = 0; i < children.length - 1; i++) {
+ const handle = document.createElement("span");
+ handle.setAttribute("class", "sb__resize");
+ handle.setAttribute("contenteditable", "false");
+ children[i].after(handle);
+ }
+};
+
export const jumpToParent = (protyle: IProtyle, nodeElement: Element, type: "parent" | "next" | "previous") => {
fetchPost("/api/block/getBlockSiblingID", {id: nodeElement.getAttribute("data-node-id")}, (response) => {
const targetId = response.data[type];
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index 920cad81d7..003a59ecbb 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -2674,7 +2674,12 @@ data-type="fold" style="cursor:inherit;"><svg style="width: 10px;${fold && fold
if (["NodeBlockquote", "NodeCallout"].includes(type)) {
space += 10;
}
- if ((nodeElement.previousElementSibling && nodeElement.previousElementSibling.getAttribute("data-node-id")) ||
+ // 前一个块兄弟(跳过 sb__resize 拖拽手柄,手柄无 data-node-id)
+ let previousBlock = nodeElement.previousElementSibling;
+ while (previousBlock && !previousBlock.getAttribute("data-node-id")) {
+ previousBlock = previousBlock.previousElementSibling;
+ }
+ if ((previousBlock && previousBlock.getAttribute("data-node-id")) ||
nodeElement.parentElement.classList.contains("callout-content")) {
// 前一个块存在时,只显示到当前层级
hideParent = true;
diff --git a/app/src/protyle/util/blockFold.ts b/app/src/protyle/util/blockFold.ts
index 2bdcaa1701..840c11df7e 100644
--- a/app/src/protyle/util/blockFold.ts
+++ b/app/src/protyle/util/blockFold.ts
@@ -7,6 +7,7 @@ import {scrollCenter} from "../../util/highlightById";
import {clearSelect} from "./clear";
import {removeFoldHeading} from "./heading";
import {getTopAloneElement} from "../wysiwyg/getBlock";
+import {getSbChildCount} from "../../block/util";
export const setFold = (protyle: IProtyle, nodeElement: Element, isOpen?: boolean,
isRemove?: boolean, addLoading = true, getOperations = false) => {
@@ -98,7 +99,8 @@ const isFoldable = (el: Element) => {
const type = el.getAttribute("data-type");
return type === "NodeHeading" ||
(type === "NodeCallout" && el.querySelector(".callout-content").childElementCount > 1) ||
- ((type === "NodeListItem" || type === "NodeBlockquote" || type === "NodeSuperBlock") && el.childElementCount > 3);
+ ((type === "NodeListItem" || type === "NodeBlockquote") && el.childElementCount > 3) ||
+ (type === "NodeSuperBlock" && getSbChildCount(el) > 1);
};
export const foldBlocksRecursively = (protyle: IProtyle, nodeElements: Element[]) => {
diff --git a/app/src/protyle/util/editorCommonEvent.ts b/app/src/protyle/util/editorCommonEvent.ts
index 61b0603ffb..e22d4ed725 100644
--- a/app/src/protyle/util/editorCommonEvent.ts
+++ b/app/src/protyle/util/editorCommonEvent.ts
@@ -9,7 +9,7 @@ import {
} from "./hasClosest";
import {Constants} from "../../constants";
import {paste} from "./paste";
-import {cancelSB, genEmptyElement, genSBElement, insertEmptyBlock} from "../../block/util";
+import {cancelSB, genEmptyElement, genSBElement, getSbChildCount, insertEmptyBlock, refreshSbResize} from "../../block/util";
import {transaction, turnsIntoOneTransaction} from "../wysiwyg/transaction";
import {getParentBlock, getTopAloneElement} from "../wysiwyg/getBlock";
import {updateListOrder} from "../wysiwyg/list";
@@ -188,7 +188,7 @@ const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElemen
sameElement.remove();
}
}
- if (topSourceParentElement.classList.contains("sb") && topSourceParentElement.childElementCount === 2) {
+ if (topSourceParentElement.classList.contains("sb") && getSbChildCount(topSourceParentElement) === 1) {
// 拖拽后,sb 只剩下一个元素
if (isSameDoc) {
const sbData = await cancelSB(protyle, topSourceParentElement);
@@ -210,7 +210,7 @@ const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElemen
/// #endif
}
}
- } else if (oldSourceParentElement.classList.contains("sb") && oldSourceParentElement.childElementCount === 2) {
+ } else if (oldSourceParentElement.classList.contains("sb") && getSbChildCount(oldSourceParentElement) === 1) {
// 拖拽后,sb 只剩下一个元素
if (isSameDoc) {
const sbData = await cancelSB(protyle, oldSourceParentElement);
@@ -436,6 +436,8 @@ const dragSb = async (protyle: IProtyle, sourceElements: Element[], targetElemen
doOperations.push(...foldOperations.doOperations);
undoOperations.splice(0, 0, ...foldOperations.undoOperations);
});
+ // 子块移入完成后刷新拖拽手柄 https://github.com/siyuan-note/siyuan/issues/9521
+ refreshSbResize(sbElement);
if (isSameDoc || isCopy) {
transaction(protyle, doOperations, undoOperations);
} else {
diff --git a/app/src/protyle/wysiwyg/getBlock.ts b/app/src/protyle/wysiwyg/getBlock.ts
index 44ab2b20a2..244ea1d5e4 100644
--- a/app/src/protyle/wysiwyg/getBlock.ts
+++ b/app/src/protyle/wysiwyg/getBlock.ts
@@ -1,5 +1,6 @@
import {hasClosestBlock, isInEmbedBlock} from "../util/hasClosest";
import {Constants} from "../../constants";
+import {getSbChildCount} from "../../block/util";
export const getParentBlock = (element: Element) => {
if (element.parentElement.classList.contains("callout-content") ||
@@ -180,9 +181,9 @@ export const getTopAloneElement = (topSourceElement: Element) => {
break;
}
}
- } else if ("NodeSuperBlock" === topSourceElement.parentElement.getAttribute("data-type") && topSourceElement.parentElement.childElementCount === 2) {
+ } else if ("NodeSuperBlock" === topSourceElement.parentElement.getAttribute("data-type") && getSbChildCount(topSourceElement.parentElement) === 1) {
while (topSourceElement.parentElement && !topSourceElement.parentElement.classList.contains("protyle-wysiwyg")) {
- if (topSourceElement.parentElement.getAttribute("data-type") === "NodeSuperBlock" && topSourceElement.parentElement.childElementCount === 2) {
+ if (topSourceElement.parentElement.getAttribute("data-type") === "NodeSuperBlock" && getSbChildCount(topSourceElement.parentElement) === 1) {
topSourceElement = topSourceElement.parentElement;
} else {
topSourceElement = getTopAloneElement(topSourceElement);
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 7c9f3e6d15..ed186611fb 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -788,6 +788,53 @@ export class WYSIWYG {
const mostBottom = protyleRect.bottom;
const y = event.clientY;
const contentRect = protyle.contentElement.getBoundingClientRect();
+ // 超级块横向布局下拖拽调整子块宽度 https://github.com/siyuan-note/siyuan/issues/9521
+ if (!protyle.disabled && target.classList.contains("sb__resize")) {
+ const sbElement = target.parentElement;
+ const previousElement = target.previousElementSibling as HTMLElement;
+ if (!sbElement || !previousElement || !previousElement.hasAttribute("data-node-id") ||
+ sbElement.getAttribute("data-sb-layout") !== "col") {
+ return;
+ }
+ const x = event.clientX;
+ const sbWidth = sbElement.clientWidth;
+ const oldWidth = previousElement.clientWidth;
+ const oldHTML = previousElement.outerHTML;
+ target.classList.add("sb__resize--drag");
+ // @ts-ignore
+ previousElement.style.webkitUserModify = "read-only";
+ documentSelf.onmousemove = (moveEvent: MouseEvent) => {
+ let newWidth = oldWidth + (moveEvent.clientX - x);
+ // 最小宽度限制为超级块宽度的 10%
+ const minWidth = sbWidth * 0.1;
+ // 最大宽度限制为超级块宽度减去最小宽度(保证至少还有一个兄弟块的空间)
+ const maxWidth = sbWidth * 0.9;
+ newWidth = Math.max(minWidth, Math.min(maxWidth, newWidth));
+ // 用 style.width + flex:none,与 genWidths 菜单、图片缩放一致,菜单可自动回显/持久化
+ previousElement.style.width = newWidth + "px";
+ previousElement.style.flex = "none";
+ };
+ documentSelf.onmouseup = () => {
+ target.classList.remove("sb__resize--drag");
+ // @ts-ignore
+ previousElement.style.webkitUserModify = "";
+ documentSelf.onmousemove = null;
+ documentSelf.onmouseup = null;
+ documentSelf.ondragstart = null;
+ documentSelf.onselectstart = null;
+ documentSelf.onselect = null;
+ // 转为百分比写入 style.width + flex:none,复用既有 genWidths 菜单的回显与持久化
+ const finalWidth = previousElement.clientWidth;
+ const pct = Math.round(finalWidth / sbWidth * 1000) / 10;
+ previousElement.style.width = pct + "%";
+ previousElement.style.flex = "none";
+ previousElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
+ updateTransaction(protyle, previousElement, oldHTML);
+ };
+ this.preventClick = true;
+ event.preventDefault();
+ return;
+ }
// av col resize
if (!protyle.disabled && target.classList.contains("av__widthdrag")) {
if (!nodeElement) {
diff --git a/app/src/protyle/wysiwyg/remove.ts b/app/src/protyle/wysiwyg/remove.ts
index 6b6149668d..73db1709d9 100644
--- a/app/src/protyle/wysiwyg/remove.ts
+++ b/app/src/protyle/wysiwyg/remove.ts
@@ -16,7 +16,7 @@ import {
hasPreviousSibling
} from "./getBlock";
import {transaction, turnsIntoOneTransaction, turnsIntoTransaction, updateTransaction} from "./transaction";
-import {cancelSB, genEmptyElement} from "../../block/util";
+import {cancelSB, genEmptyElement, getSbChildCount} from "../../block/util";
import {listOutdent, updateListOrder} from "./list";
import {zoomOut} from "../../menus/protyle";
import {preventScroll} from "../scroll/preventScroll";
@@ -226,7 +226,7 @@ export const removeBlock = async (protyle: IProtyle, blockElement: Element, rang
}
}
if (deletes.length > 0) {
- if (topParentElement && topParentElement.getAttribute("data-type") === "NodeSuperBlock" && topParentElement.childElementCount === 2) {
+ if (topParentElement && topParentElement.getAttribute("data-type") === "NodeSuperBlock" && getSbChildCount(topParentElement) === 1) {
const sbData = await cancelSB(protyle, topParentElement, range);
transaction(protyle, deletes.concat(sbData.doOperations), sbData.undoOperations.concat(inserts.reverse()));
} else {
@@ -469,7 +469,7 @@ export const removeBlock = async (protyle: IProtyle, blockElement: Element, rang
}];
blockElement.remove();
// 取消超级块
- if (parentElement && parentElement.getAttribute("data-type") === "NodeSuperBlock" && parentElement.childElementCount === 2) {
+ if (parentElement && parentElement.getAttribute("data-type") === "NodeSuperBlock" && getSbChildCount(parentElement) === 1) {
const sbData = await cancelSB(protyle, parentElement);
transaction(protyle, doOperations.concat(sbData.doOperations), sbData.undoOperations.concat(undoOperations));
} else {
@@ -588,7 +588,7 @@ export const removeBlock = async (protyle: IProtyle, blockElement: Element, rang
id: previousId,
});
}
- if (parentElement && parentElement.getAttribute("data-type") === "NodeSuperBlock" && parentElement.childElementCount === 2) {
+ if (parentElement && parentElement.getAttribute("data-type") === "NodeSuperBlock" && getSbChildCount(parentElement) === 1) {
const sbData = await cancelSB(protyle, parentElement);
transaction(protyle, doOperations.concat(sbData.doOperations), sbData.undoOperations.concat(undoOperations));
} else {
diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.ts
index 7d0f9dddc9..88c1cc073b 100644
--- a/app/src/protyle/wysiwyg/transaction.ts
+++ b/app/src/protyle/wysiwyg/transaction.ts
@@ -13,7 +13,7 @@ import {getAllModels} from "../../layout/getAll";
/// #endif
import {avRender, refreshAV} from "../render/av/render";
import {removeFoldHeading} from "../util/heading";
-import {cancelSB, genEmptyElement, genSBElement} from "../../block/util";
+import {cancelSB, genEmptyElement, genSBElement, getSbChildCount, refreshSbResize} from "../../block/util";
import {hideElements} from "../ui/hideElements";
import {reloadProtyle} from "../util/reload";
import {countBlockWord} from "../../layout/status";
@@ -1049,8 +1049,15 @@ export const turnsIntoOneTransaction = async (options: {
blockRender(options.protyle, item);
}
});
+ // 子块移入完成后刷新超级块拖拽手柄 https://github.com/siyuan-note/siyuan/issues/9521
+ if (parentElement.classList.contains("sb")) {
+ refreshSbResize(parentElement);
+ } else if (parentElement.parentElement && parentElement.parentElement.classList.contains("sb")) {
+ // 引述/列表/标注嵌入超级块时刷新父超级块
+ refreshSbResize(parentElement.parentElement);
+ }
if ((["Blocks2Blockquote", "Blocks2Callout"].includes(options.type) || options.type.endsWith("Ls")) &&
- parentElement.parentElement.classList.contains("sb") && parentElement.parentElement.childElementCount === 2) {
+ parentElement.parentElement.classList.contains("sb") && getSbChildCount(parentElement.parentElement) === 1) {
const cancelOperations = await cancelSB(options.protyle, parentElement.parentElement);
doOperations.push(...cancelOperations.doOperations);
undoOperations.splice(0, 0, ...cancelOperations.undoOperations);
diff --git a/app/src/util/touchDragBridge.ts b/app/src/util/touchDragBridge.ts
index ddbdce9f6c..d608e77af3 100644
--- a/app/src/util/touchDragBridge.ts
+++ b/app/src/util/touchDragBridge.ts
@@ -40,6 +40,7 @@ const handleManualTouchStart = (e: TouchEvent) => {
!target.closest(".av__drag-fill") &&
!target.closest(".protyle-action__drag") &&
!target.closest(".table__resize") &&
+ !target.closest(".sb__resize") &&
!target.closest(".protyle-background__img") &&
!target.closest(".b3-chip")) return;