Skip to content

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

:bug: Exclude non-content editor areas from drag selection https://github.com/siyuan-note/siyuan/issues/18594

Danield9441144c230
diff --git a/app/src/protyle/wysiwyg/blockDragSelect.test.ts b/app/src/protyle/wysiwyg/blockDragSelect.test.ts
index ac84068588..0abeedfff1 100644
--- a/app/src/protyle/wysiwyg/blockDragSelect.test.ts
+++ b/app/src/protyle/wysiwyg/blockDragSelect.test.ts
@@ -1,6 +1,7 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
import {
+ clampBlockDragSelectY,
getBlockDragSelectBlock,
getBlockDragSelectProbeX,
isBlockDragSelectBottomReached,
@@ -132,6 +133,20 @@ describe("isBlockDragSelectTopReached", () => {
});
});
+describe("clampBlockDragSelectY", () => {
+ it("stops upward selection at the editor top", () => {
+ assert.equal(clampBlockDragSelectY(120, 50, 700, 200, 650), 200);
+ });
+
+ it("stops downward selection at the editor bottom", () => {
+ assert.equal(clampBlockDragSelectY(680, 50, 700, 200, 650), 650);
+ });
+
+ it("keeps selection inside the editor unchanged", () => {
+ assert.equal(clampBlockDragSelectY(400, 50, 700, 200, 650), 400);
+ });
+});
+
describe("resolveBlockDragSelectStart", () => {
it("keeps the fallback block after the selection start scrolls outside the viewport", () => {
const fallback = new TestElement("fallback", true);
diff --git a/app/src/protyle/wysiwyg/blockDragSelect.ts b/app/src/protyle/wysiwyg/blockDragSelect.ts
index e15629ce17..8b09fc0905 100644
--- a/app/src/protyle/wysiwyg/blockDragSelect.ts
+++ b/app/src/protyle/wysiwyg/blockDragSelect.ts
@@ -46,6 +46,13 @@ export const isBlockDragSelectBottomReached = (selectBottom: number, containerBo
export const isBlockDragSelectTopReached = (selectTop: number, containerTop: number, firstChildTop: number) =>
selectTop < Math.max(containerTop, firstChildTop);
+export const clampBlockDragSelectY = (clientY: number, viewportTop: number, viewportBottom: number,
+ wysiwygTop: number, wysiwygBottom: number) => {
+ const top = Math.max(viewportTop, wysiwygTop);
+ const bottom = Math.min(viewportBottom, wysiwygBottom);
+ return Math.max(top, Math.min(clientY, bottom));
+};
+
const getDirectDescendantBlock = (containerBlock: Element, block: Element,
getBlock: TResolveDragSelectBlockOptions["getBlock"]) => {
let directBlock = block;
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 7ad5e55e91..3872717fcb 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -178,6 +178,7 @@ import {
} from "../render/av/rangeSelect";
import {getAVColumnResizeWidth} from "../render/av/columnWidth";
import {
+ clampBlockDragSelectY,
getBlockDragSelectBlock,
getBlockDragSelectProbeX,
isBlockDragSelectBottomReached,
@@ -1045,6 +1046,7 @@ export class WYSIWYG {
const startsFromPadding = event.clientX < mostLeft - 1 || event.clientX > mostRight + 2 ||
event.clientY < wysiwygRect.top + (parseFloat(wysiwygStyle.paddingTop) || 0) ||
event.clientY > wysiwygRect.bottom - (parseFloat(wysiwygStyle.paddingBottom) || 0);
+ const isBottomBacklink = !!protyle.element.closest(".sy__backlink--bottom");
// 按住 Ctrl/Command 从边缘空白处划选时,以按下时的选区为基线切换块
// https://github.com/siyuan-note/siyuan/issues/15006
const isToggleBlockDrag = isOnlyMeta(event) && !event.shiftKey && !event.altKey && startsFromPadding;
@@ -1830,6 +1832,11 @@ export class WYSIWYG {
return;
}
+ // 编辑器底部反链仅用于浏览和编辑,不参与块、数据库或表格框选
+ if (isBottomBacklink && (startsFromPadding || tableBlockElement)) {
+ return;
+ }
+
// 内容区域使用浏览器原生选区,跨块选择时保留各行内元素自身的选中样式。
if (!startsFromPadding && !tableBlockElement) {
documentSelf.onmouseup = (mouseUpEvent) => {
@@ -2161,7 +2168,9 @@ export class WYSIWYG {
}
const scrollTop = protyle.contentElement.scrollTop;
const startY = y + selectStartScrollTop;
- const moveY = Math.max(mostTop, Math.min(moveEvent.clientY, mostBottom)) + scrollTop;
+ const wysiwygMoveRect = protyle.wysiwyg.element.getBoundingClientRect();
+ const moveY = clampBlockDragSelectY(moveEvent.clientY, mostTop, mostBottom,
+ wysiwygMoveRect.top, wysiwygMoveRect.bottom) + scrollTop;
const isAVItemMode = avDragSelectRange &&
Math.min(startY, moveY) >= avDragSelectRange.top &&
Math.max(startY, moveY) <= avDragSelectRange.bottom;
@@ -2364,14 +2373,22 @@ export class WYSIWYG {
syncDragSelectBlocks(selectElements);
};
- documentSelf.onmouseup = (mouseUpEvent) => {
+ let dragSelectFinished = false;
+ const finishDragSelect = (mouseUpEvent: MouseEvent) => {
+ if (dragSelectFinished) {
+ return;
+ }
+ dragSelectFinished = true;
+ documentSelf.removeEventListener("mouseup", finishDragSelect, true);
+ if (documentSelf.onmouseup === finishDragSelect) {
+ documentSelf.onmouseup = null;
+ }
protyle.contentElement.removeEventListener("scroll", selectScrollEvent);
flushAVDragSelect();
if (startsFromPadding) {
stopScrollAnimation();
}
documentSelf.onmousemove = null;
- documentSelf.onmouseup = null;
documentSelf.ondragstart = null;
documentSelf.onselectstart = null;
documentSelf.onselect = null;
@@ -2785,6 +2802,9 @@ export class WYSIWYG {
}
}
};
+ // 底部反链包含嵌套编辑器,捕获阶段结束框选,避免内部事件阻断后选区无法清理
+ documentSelf.onmouseup = finishDragSelect;
+ documentSelf.addEventListener("mouseup", finishDragSelect, {capture: true, once: true});
});
}