Skip to content

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

:bug: Defer mobile drag selection scroll binding until editor initialization https://github.com/siyuan-note/siyuan/issues/19949

Danieldf54a2c6f341
diff --git a/app/src/protyle/wysiwyg/blockDragSelectionGesture.test.ts b/app/src/protyle/wysiwyg/blockDragSelectionGesture.test.ts
index 431c98bb8c..fe60cfa81a 100644
--- a/app/src/protyle/wysiwyg/blockDragSelectionGesture.test.ts
+++ b/app/src/protyle/wysiwyg/blockDragSelectionGesture.test.ts
@@ -44,7 +44,7 @@ class Surface {
}
}
-const fixture = () => {
+const fixture = (mountScroll = true) => {
const exports: any = {};
let time = 10000;
const window = new Surface();
@@ -62,7 +62,8 @@ const fixture = () => {
require: () => ({Constants: {SIZE_DRAG_THRESHOLD: 5, TIMEOUT_MULTIPLE_SELECT: 1500}}),
Date: {now: () => time},
});
- const dispose = exports.bindBlockDragSelectionGesture(element, scrollElement, {
+ let mountedScrollElement: Surface;
+ const dispose = exports.bindBlockDragSelectionGesture(element, () => mountedScrollElement, {
canStart: () => enabled,
getStartBlock: (target: object) => blocks.includes(target as typeof blocks[0]) ? target : undefined,
getBlockAtPoint: (point: {clientY: number}) => blocks[Math.floor(point.clientY / 100)],
@@ -71,6 +72,9 @@ const fixture = () => {
scroll: (point: unknown) => scrolls.push(point),
finish: (source: string, cancelled: boolean) => finishes.push({source, cancelled}),
});
+ if (mountScroll) {
+ mountedScrollElement = scrollElement;
+ }
const mouseDown = (properties = {}) => element.send("mousedown", {target: blocks[0], ...properties});
const touch = (type: string, y = 20, properties = {}) => {
const point = {identifier: 1, clientX: 20, clientY: y};
@@ -83,9 +87,23 @@ const fixture = () => {
advance: (milliseconds: number) => time += milliseconds,
disable: () => enabled = false,
enterMultiSelect: () => multiSelect = true,
+ mount: () => mountedScrollElement = scrollElement,
};
};
+test("scroll containers can be initialized after binding and disposal before a gesture is safe", () => {
+ const unused = fixture(false);
+ assert.doesNotThrow(unused.dispose);
+ assert.equal(unused.element.count(), 0);
+ const mounted = fixture(false);
+ mounted.mount();
+ mounted.touch("touchstart");
+ assert.equal(mounted.scrollElement.count(), 1);
+ mounted.touch("touchend");
+ assert.equal(mounted.scrollElement.count(), 0);
+ mounted.dispose();
+});
+
test("mouse text selection stays native in one block and becomes a reversible whole-block range across blocks", () => {
const f = fixture();
f.mouseDown();
diff --git a/app/src/protyle/wysiwyg/blockDragSelectionGesture.ts b/app/src/protyle/wysiwyg/blockDragSelectionGesture.ts
index 397526973f..be7220aa8f 100644
--- a/app/src/protyle/wysiwyg/blockDragSelectionGesture.ts
+++ b/app/src/protyle/wysiwyg/blockDragSelectionGesture.ts
@@ -14,7 +14,7 @@ interface IBlockDragSelectionOptions {
}
// 文字选区保留在单个编辑宿主内,跨块拖动由应用维护整块选区。
-export const bindBlockDragSelectionGesture = (element: HTMLElement, scrollElement: HTMLElement,
+export const bindBlockDragSelectionGesture = (element: HTMLElement, getScrollElement: () => HTMLElement,
options: IBlockDragSelectionOptions) => {
const ownerDocument = element.ownerDocument;
const ownerWindow = ownerDocument.defaultView;
@@ -25,6 +25,7 @@ export const bindBlockDragSelectionGesture = (element: HTMLElement, scrollElemen
let suppressClickUntil = 0;
let lastTouchTime = 0;
let composing = false;
+ let scrollElement: HTMLElement;
const finish = (cancelled: boolean) => {
const previous = gesture;
@@ -37,7 +38,8 @@ export const bindBlockDragSelectionGesture = (element: HTMLElement, scrollElemen
ownerDocument.removeEventListener("touchstart", additionalTouch, true);
ownerDocument.removeEventListener("keydown", keyDown, true);
ownerDocument.removeEventListener("dragstart", dragStart, true);
- scrollElement.removeEventListener("scroll", scroll);
+ scrollElement?.removeEventListener("scroll", scroll);
+ scrollElement = undefined;
ownerWindow.removeEventListener("blur", cancel);
if (previous?.active) {
suppressClickUntil = Date.now() + 500;
@@ -145,6 +147,8 @@ export const bindBlockDragSelectionGesture = (element: HTMLElement, scrollElemen
return;
}
gesture = {source, block, point, time: Date.now(), touchID, active: false, lastPoint: point};
+ // 正文宿主先于滚动容器初始化,开始手势时再绑定实际容器。
+ scrollElement = getScrollElement();
ownerDocument.addEventListener("keydown", keyDown, true);
ownerDocument.addEventListener("dragstart", dragStart, true);
ownerWindow.addEventListener("blur", cancel);
diff --git a/app/src/protyle/wysiwyg/boundedBlockDragSelect.test.ts b/app/src/protyle/wysiwyg/boundedBlockDragSelect.test.ts
index b234edd677..5e3d503771 100644
--- a/app/src/protyle/wysiwyg/boundedBlockDragSelect.test.ts
+++ b/app/src/protyle/wysiwyg/boundedBlockDragSelect.test.ts
@@ -42,12 +42,15 @@ const fixture = (mobile = false) => {
gutter: {}, disabled: false,
toolbar: {isMultiSelectMode: () => multiSelect, subElement: {},
showMultiSelectMode: () => multiSelect = true},
- contentElement: {getBoundingClientRect: () => ({left: 0, right: 100, top: 0, bottom: 100})},
};
+ let getScrollElement: Parameters<typeof bindBlockDragSelectionGesture>[1];
let options: Parameters<typeof bindBlockDragSelectionGesture>[2];
const modules: Record<string, unknown> = {
- "./blockDragSelectionGesture": {bindBlockDragSelectionGesture: (_element: unknown, _scroll: unknown,
- value: typeof options) => options = value},
+ "./blockDragSelectionGesture": {bindBlockDragSelectionGesture: (_element: unknown, scroll: typeof getScrollElement,
+ value: typeof options) => {
+ getScrollElement = scroll;
+ options = value;
+ }},
"./blockSelection": {
BLOCK_SELECTION_CLASS: "protyle-wysiwyg--select",
clearBlockSelectionMode: () => states.forEach(state => state.clear()),
@@ -71,6 +74,7 @@ const fixture = (mobile = false) => {
// 构造 WYSIWYG 时实例尚未赋给 protyle,绑定阶段不能从 protyle 读取编辑器元素。
exports.bindBoundedBlockDragSelect(protyle, element);
protyle.wysiwyg = {element};
+ protyle.contentElement = {getBoundingClientRect: () => ({left: 0, right: 100, top: 0, bottom: 100})};
const target = (excluded = false, editable = true, ownEditor = true) => ({
closest: (selector: string) => {
if (selector === ".protyle-wysiwyg") {
@@ -83,7 +87,7 @@ const fixture = (mobile = false) => {
},
} as unknown as HTMLElement);
return {
- options: options!, protyle, blocks: blocks as unknown as HTMLElement[], classes, states, target,
+ options: options!, getScrollElement: getScrollElement!, protyle, blocks: blocks as unknown as HTMLElement[], classes, states, target,
setEditable: (value: string) => rootEditable = value,
setBacklink: () => backlink = true,
detach: () => blockOwned = false,
@@ -93,6 +97,13 @@ const fixture = (mobile = false) => {
};
};
+test("bounded selection resolves the scroll container after editor initialization on both platforms", () => {
+ for (const mobile of [false, true]) {
+ const f = fixture(mobile);
+ assert.equal(f.getScrollElement(), f.protyle.contentElement);
+ }
+});
+
test("bounded mouse selection is enabled only for protected editor roots with block operations", () => {
const f = fixture();
assert.equal(f.options.canStart("mouse"), true);
diff --git a/app/src/protyle/wysiwyg/boundedBlockDragSelect.ts b/app/src/protyle/wysiwyg/boundedBlockDragSelect.ts
index 1ab9b12ee8..dbe5c9cc51 100644
--- a/app/src/protyle/wysiwyg/boundedBlockDragSelect.ts
+++ b/app/src/protyle/wysiwyg/boundedBlockDragSelect.ts
@@ -23,7 +23,7 @@ export const bindBoundedBlockDragSelect = (protyle: IProtyle, element: HTMLEleme
item => item.getAttribute("data-type") === "NodeListItem");
return block ? (isInEmbedBlock(block) || block) as HTMLElement : undefined;
};
- return bindBlockDragSelectionGesture(element, protyle.contentElement, {
+ return bindBlockDragSelectionGesture(element, () => protyle.contentElement, {
canStart: source => !!protyle.gutter && !protyle.disabled &&
!window.siyuan.touchDragActive && !element.closest(".sy__backlink--bottom") &&
!element.classList.contains("fn__pointer-none") &&