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/18554

Vanessa89acce6f470b
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index 1ff2c1f160..e445ddd3fe 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -3157,7 +3157,8 @@ export class Gutter {
}
// 防止划选时触碰图标导致 hl 无法移除
const selectElement = protyle.element.querySelector(".protyle-select");
- if (selectElement && !selectElement.classList.contains("fn__none")) {
+ if (protyle.wysiwyg.element.classList.contains("fn__pointer-none") ||
+ (selectElement && !selectElement.classList.contains("fn__none"))) {
return;
}
document.querySelectorAll(".protyle-gutters").forEach(item => {
diff --git a/app/src/protyle/wysiwyg/blockDragSelect.test.ts b/app/src/protyle/wysiwyg/blockDragSelect.test.ts
new file mode 100644
index 0000000000..1c99de868f
--- /dev/null
+++ b/app/src/protyle/wysiwyg/blockDragSelect.test.ts
@@ -0,0 +1,193 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {
+ getBlockDragSelectBlock,
+ getBlockDragSelectProbeX,
+ isBlockDragSelectBottomReached,
+ isBlockDragSelectTopReached,
+ resolveBlockDragSelectStart
+} from "./blockDragSelect";
+
+class TestElement {
+ parentElement: TestElement | null = null;
+ children: TestElement[] = [];
+
+ constructor(public name: string, public block = false, public containerSurface = false) {
+ }
+
+ append(...elements: TestElement[]) {
+ elements.forEach((element) => {
+ element.parentElement = this;
+ this.children.push(element);
+ });
+ return this;
+ }
+
+ contains(element: TestElement) {
+ let currentElement: TestElement | null = element;
+ while (currentElement) {
+ if (currentElement === this) {
+ return true;
+ }
+ currentElement = currentElement.parentElement;
+ }
+ return false;
+ }
+}
+
+const asElement = (element: TestElement) => element as unknown as Element;
+
+const getBlock = (element: Element) => {
+ let currentElement = element as unknown as TestElement | null;
+ while (currentElement && !currentElement.block) {
+ currentElement = currentElement.parentElement;
+ }
+ return currentElement ? asElement(currentElement) : false;
+};
+
+const resolve = (points: Map<number, TestElement>, top = 0, bottom = 12, fallbackBlock?: TestElement) =>
+ resolveBlockDragSelectStart({
+ x: 100,
+ top,
+ bottom,
+ elementFromPoint: (_x, y) => {
+ const point = points.get(y);
+ return point ? asElement(point) : null;
+ },
+ getBlock,
+ isContainerSurface: (element) => (element as unknown as TestElement).containerSurface,
+ fallbackBlock: fallbackBlock && asElement(fallbackBlock),
+});
+
+describe("getBlockDragSelectProbeX", () => {
+ const selectRect = {left: 20, right: 180, top: 0, bottom: 20};
+
+ it("uses the edge approaching content when dragging from the left", () => {
+ assert.equal(getBlockDragSelectProbeX(10, selectRect, 50, 150), 150);
+ });
+
+ it("uses the edge approaching content when dragging from the right", () => {
+ assert.equal(getBlockDragSelectProbeX(190, selectRect, 50, 150), 50);
+ });
+
+ it("keeps the existing left-edge probe for vertical padding", () => {
+ assert.equal(getBlockDragSelectProbeX(100, selectRect, 50, 150), 50);
+ });
+});
+
+describe("getBlockDragSelectBlock", () => {
+ const isContainerBlock = (element: Element) => (element as unknown as TestElement).containerSurface;
+ const isListItem = (element: Element) => (element as unknown as TestElement).name === "listItem";
+
+ it("selects a list item instead of its content block", () => {
+ const editor = new TestElement("editor");
+ const list = new TestElement("list", true, true);
+ const listItem = new TestElement("listItem", true, true);
+ const paragraph = new TestElement("paragraph", true);
+ const content = new TestElement("content");
+ editor.append(list.append(listItem.append(paragraph.append(content))));
+
+ assert.equal(getBlockDragSelectBlock(asElement(content), asElement(editor), getBlock,
+ isContainerBlock, isListItem), asElement(listItem));
+ });
+
+ it("does not cross another container to select an outer list item", () => {
+ const editor = new TestElement("editor");
+ const listItem = new TestElement("listItem", true, true);
+ const blockquote = new TestElement("blockquote", true, true);
+ const paragraph = new TestElement("paragraph", true);
+ const content = new TestElement("content");
+ editor.append(listItem.append(blockquote.append(paragraph.append(content))));
+
+ assert.equal(getBlockDragSelectBlock(asElement(content), asElement(editor), getBlock,
+ isContainerBlock, isListItem), asElement(paragraph));
+ });
+});
+
+describe("isBlockDragSelectBottomReached", () => {
+ it("reaches a container when the selection enters its bottom padding", () => {
+ assert.equal(isBlockDragSelectBottomReached(105, 110, 100), true);
+ });
+
+ it("reaches a container at the document end without a following sibling", () => {
+ assert.equal(isBlockDragSelectBottomReached(140, 110, 110), true);
+ });
+
+ it("keeps child selection before the bottom padding", () => {
+ assert.equal(isBlockDragSelectBottomReached(100, 110, 100), false);
+ });
+});
+
+describe("isBlockDragSelectTopReached", () => {
+ it("reaches a container when the selection enters its top padding", () => {
+ assert.equal(isBlockDragSelectTopReached(95, 90, 100), true);
+ });
+
+ it("reaches a container at the document start without a preceding sibling", () => {
+ assert.equal(isBlockDragSelectTopReached(60, 90, 90), true);
+ });
+
+ it("keeps child selection after the top padding", () => {
+ assert.equal(isBlockDragSelectTopReached(100, 90, 100), false);
+ });
+});
+
+describe("resolveBlockDragSelectStart", () => {
+ it("keeps the parent block while the selection only covers its side", () => {
+ const parent = new TestElement("parent", true, true);
+
+ assert.equal(resolve(new Map([[0, parent], [4, parent], [8, parent], [12, parent]])), asElement(parent));
+ });
+
+ it("uses the direct child below a list gap", () => {
+ const list = new TestElement("list", true, true);
+ const listItem = new TestElement("listItem", true, true);
+ const paragraph = new TestElement("paragraph", true);
+ const content = new TestElement("content");
+ list.append(listItem.append(paragraph.append(content)));
+
+ assert.equal(resolve(new Map([[0, list], [4, list], [8, content]])), asElement(listItem));
+ });
+
+ ["blockquote", "callout", "superBlock"].forEach((name) => {
+ it(`uses the child below a ${name} gap`, () => {
+ const container = new TestElement(name, true, true);
+ const wrapper = name === "callout" ? new TestElement("contentWrapper", false, true) : container;
+ const child = new TestElement("child", true);
+ const content = new TestElement("content");
+ if (wrapper === container) {
+ container.append(child.append(content));
+ } else {
+ container.append(wrapper.append(child.append(content)));
+ }
+
+ assert.equal(resolve(new Map([[0, wrapper], [4, wrapper], [8, content]])), asElement(child));
+ });
+ });
+
+ it("keeps the parent when its bottom padding is followed by a sibling", () => {
+ const editor = new TestElement("editor");
+ const parent = new TestElement("parent", true, true);
+ const sibling = new TestElement("sibling", true);
+ editor.append(parent, sibling);
+
+ assert.equal(resolve(new Map([[0, parent], [4, parent], [8, sibling]])), asElement(parent));
+ });
+
+ it("finds the first block below editor padding", () => {
+ const editor = new TestElement("editor", false, true);
+ const child = new TestElement("child", true);
+ editor.append(child);
+
+ assert.equal(resolve(new Map([[0, editor], [4, editor], [8, child]])), asElement(child));
+ });
+
+ it("finds the first block below a non-block editor header", () => {
+ const editor = new TestElement("editor");
+ const header = new TestElement("header");
+ const child = new TestElement("child", true);
+ editor.append(header, child);
+
+ assert.equal(resolve(new Map([[0, header]]), 0, 12, child), asElement(child));
+ });
+});
diff --git a/app/src/protyle/wysiwyg/blockDragSelect.ts b/app/src/protyle/wysiwyg/blockDragSelect.ts
new file mode 100644
index 0000000000..2693d434a7
--- /dev/null
+++ b/app/src/protyle/wysiwyg/blockDragSelect.ts
@@ -0,0 +1,92 @@
+type TDragSelectRect = Pick<DOMRect, "left" | "right" | "top" | "bottom">;
+
+type TResolveDragSelectBlockOptions = {
+ x: number,
+ top: number,
+ bottom: number,
+ elementFromPoint: (x: number, y: number) => Element | null,
+ getBlock: (element: Element) => Element | false,
+ isContainerSurface: (element: Element) => boolean,
+ fallbackBlock?: Element | false,
+};
+
+export const getBlockDragSelectBlock = (element: Element, boundaryElement: Element,
+ getBlock: TResolveDragSelectBlockOptions["getBlock"],
+ isContainerBlock: (element: Element) => boolean,
+ isListItem: (element: Element) => boolean) => {
+ const block = getBlock(element);
+ if (!block || isContainerBlock(block)) {
+ return block;
+ }
+ let parentElement = block.parentElement;
+ while (parentElement && parentElement !== boundaryElement) {
+ if (isContainerBlock(parentElement)) {
+ return isListItem(parentElement) ? parentElement : block;
+ }
+ parentElement = parentElement.parentElement;
+ }
+ return block;
+};
+
+export const getBlockDragSelectProbeX = (startX: number, selectRect: TDragSelectRect,
+ contentLeft: number, contentRight: number) => {
+ let probeX = selectRect.left;
+ if (startX < contentLeft) {
+ probeX = selectRect.right;
+ } else if (startX > contentRight) {
+ probeX = selectRect.left;
+ }
+ return Math.max(contentLeft, Math.min(probeX, contentRight));
+};
+
+export const isBlockDragSelectBottomReached = (selectBottom: number, containerBottom: number,
+ lastChildBottom: number) =>
+ selectBottom > Math.min(containerBottom, lastChildBottom);
+
+export const isBlockDragSelectTopReached = (selectTop: number, containerTop: number, firstChildTop: number) =>
+ selectTop < Math.max(containerTop, firstChildTop);
+
+const getDirectDescendantBlock = (containerBlock: Element, block: Element,
+ getBlock: TResolveDragSelectBlockOptions["getBlock"]) => {
+ let directBlock = block;
+ while (directBlock.parentElement && directBlock.parentElement !== containerBlock) {
+ const parentBlock = getBlock(directBlock.parentElement);
+ if (!parentBlock || parentBlock === containerBlock) {
+ break;
+ }
+ directBlock = parentBlock;
+ }
+ return directBlock;
+};
+
+export const resolveBlockDragSelectStart = (options: TResolveDragSelectBlockOptions) => {
+ const firstPointElement = options.elementFromPoint(options.x, options.top);
+ if (!firstPointElement) {
+ return false;
+ }
+ const firstBlock = options.getBlock(firstPointElement);
+ if (!options.isContainerSurface(firstPointElement)) {
+ return firstBlock || options.fallbackBlock || false;
+ }
+
+ let probeY = options.top;
+ while (probeY < options.bottom) {
+ probeY = Math.min(probeY + 4, options.bottom);
+ const probeElement = options.elementFromPoint(options.x, probeY);
+ const probeBlock = probeElement && options.getBlock(probeElement);
+ if (!probeBlock) {
+ continue;
+ }
+ if (!firstBlock) {
+ return probeBlock;
+ }
+ if (probeBlock === firstBlock) {
+ continue;
+ }
+ if (firstBlock.contains(probeBlock)) {
+ return getDirectDescendantBlock(firstBlock, probeBlock, options.getBlock);
+ }
+ break;
+ }
+ return firstBlock;
+};
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 1890325d69..0c2bd6a52a 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -169,6 +169,13 @@ import {
setAVItemAnchor,
} from "../render/av/rangeSelect";
import {getAVColumnResizeWidth} from "../render/av/columnWidth";
+import {
+ getBlockDragSelectBlock,
+ getBlockDragSelectProbeX,
+ isBlockDragSelectBottomReached,
+ isBlockDragSelectTopReached,
+ resolveBlockDragSelectStart
+} from "./blockDragSelect";
interface IShiftClickBlockPoint {
blockElement: HTMLElement;
@@ -1926,10 +1933,19 @@ export class WYSIWYG {
});
};
this.element.classList.add("fn__pointer-none");
+ hideElements(["gutter"], protyle);
// 容器类元素判断(划选时 elementFromPoint 命中它们的边缘/空白需继续探测子块)
const isContainer = (el: Element) => el.classList.contains("protyle-wysiwyg") || el.classList.contains("list") ||
- el.classList.contains("li") || el.classList.contains("sb") ||
- el.classList.contains("callout") || el.classList.contains("bq");
+ el.classList.contains("li") || el.classList.contains("sb") || el.classList.contains("callout") ||
+ el.classList.contains("callout-content") || el.classList.contains("bq");
+ const getDragSelectBlock = (element: Element) => getBlockDragSelectBlock(element,
+ protyle.wysiwyg.element, (item) => hasClosestBlock(item), isContainerBlock,
+ (item) => item.getAttribute("data-type") === "NodeListItem");
+ const getFirstDragSelectBlock = () => {
+ const firstTopBlock = Array.from(protyle.wysiwyg.element.children).find(item =>
+ item.getAttribute("data-type")?.startsWith("Node"));
+ return firstTopBlock ? getDragSelectBlock(getFirstBlock(firstTopBlock)) : false;
+ };
let lastMoveEvent: MouseEvent;
const selectScrollEvent = () => lastMoveEvent && documentSelf.onmousemove?.(lastMoveEvent);
if (startsFromPadding) {
@@ -2132,11 +2148,21 @@ export class WYSIWYG {
clearAVDragSelection(avDragSelectElement);
}
avDragSelectMode = "blocks";
- // 矩形左边缘落在 padding 内时 elementFromPoint 会命中 wysiwyg 容器,需钳制到内容区
- const detectX = Math.max(mostLeft, Math.min(selectRect.left, mostRight));
- let firstElement;
+ // 从侧边开始划选时,使用矩形靠近内容的一侧动态命中块,使选区进入子块区域后可从父块切换为子块
+ const detectX = getBlockDragSelectProbeX(event.clientX, selectRect, mostLeft, mostRight);
+ let firstElement: Element | false;
const isDown = moveY > startY;
- if (isDown) {
+ if (startsFromPadding) {
+ firstElement = resolveBlockDragSelectStart({
+ x: detectX,
+ top: selectRect.top,
+ bottom: selectRect.bottom,
+ elementFromPoint: (pointX, pointY) => document.elementFromPoint(pointX, pointY),
+ getBlock: getDragSelectBlock,
+ isContainerSurface: isContainer,
+ fallbackBlock: isDown ? nodeElement : getFirstDragSelectBlock(),
+ });
+ } else if (isDown) {
firstElement = nodeElement;
} else {
firstElement = document.elementFromPoint(detectX, selectRect.top);
@@ -2147,7 +2173,7 @@ export class WYSIWYG {
}
// 向上划选且落点在 padding/缝隙时,elementFromPoint 易命中 wysiwyg 容器或容器类元素,
// 需沿 y 轴循环向下探测以定位到实际块,避免回退到文档首块导致误选上部所有块
- if (!isDown && isContainer(firstElement)) {
+ if (!startsFromPadding && !isDown && isContainer(firstElement)) {
let probeY = selectRect.top;
while (probeY < selectRect.bottom) {
probeY += 8;
@@ -2177,6 +2203,25 @@ export class WYSIWYG {
}
let selectElements: Element[] = [];
let currentElement: Element | false = firstBlockElement;
+ const isContainerBoundaryReached = (element: Element) => {
+ if (!["li", "sb", "callout", "bq"].some(className => element.classList.contains(className))) {
+ return false;
+ }
+ const contentElement = element.classList.contains("callout") ?
+ element.querySelector(":scope > .callout-content") : element;
+ const childElements = contentElement ? Array.from(contentElement.children).filter(item =>
+ item.hasAttribute("data-node-id")) : [];
+ const firstChildElement = childElements[0];
+ const lastChildElement = childElements[childElements.length - 1];
+ if (!firstChildElement || !lastChildElement) {
+ return false;
+ }
+ const containerRect = element.getBoundingClientRect();
+ return isBlockDragSelectTopReached(selectRect.top, containerRect.top,
+ firstChildElement.getBoundingClientRect().top) ||
+ isBlockDragSelectBottomReached(selectRect.bottom, containerRect.bottom,
+ lastChildElement.getBoundingClientRect().bottom);
+ };
if (currentElement) {
// 从下往上划选遇到嵌入块时,选中整个嵌入块
@@ -2213,6 +2258,9 @@ export class WYSIWYG {
break;
}
if (hasJump) {
+ if (isContainerBoundaryReached(currentElement)) {
+ selectElements = [currentElement];
+ }
const nextElement = currentElement.nextElementSibling;
if (!nextElement || nextElement.classList.contains("protyle-attr")) {
currentElement = hasClosestBlock(currentElement.parentElement);