Skip to content

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

:bug: Restore block selection from side margins on iPad https://github.com/siyuan-note/siyuan/issues/19295

Vanessa53850f8c2706
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 54d47d84e4..ae778ca7d9 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -208,6 +208,7 @@ import {
resolveBlockDragSelectStart
} from "./blockDragSelect";
import {isCrossBlockTextRange} from "../gutter/multiSelect";
+import {bindTouchBlockDragSelect} from "./touchBlockDragSelect";
import {formatPainter} from "../toolbar/FormatPainter";
import {shouldOpenListItemAttr} from "./listContext";
import {getBlockEdgeCaretRange, isCaretRangeInsideElement} from "./blockEdgeCaret";
@@ -495,6 +496,9 @@ export class WYSIWYG {
}
this.bindCommonEvent(protyle);
this.bindEvent(protyle);
+ if (!isMobile()) {
+ bindTouchBlockDragSelect(this.element, () => !protyle.toolbar.isMultiSelectMode());
+ }
if (protyle.options.action.includes(Constants.CB_GET_HISTORY)) {
return;
}
diff --git a/app/src/protyle/wysiwyg/touchBlockDragSelect.ts b/app/src/protyle/wysiwyg/touchBlockDragSelect.ts
new file mode 100644
index 0000000000..37c207ea18
--- /dev/null
+++ b/app/src/protyle/wysiwyg/touchBlockDragSelect.ts
@@ -0,0 +1,76 @@
+import {getBlockDragSelectContentBounds} from "./blockDragSelect";
+
+// 平板桌面布局的侧边触摸复用鼠标框选,正文内仍由浏览器处理滚动和文字选择。
+export const bindTouchBlockDragSelect = (element: HTMLElement, canStart: () => boolean) => {
+ let activeTouch: Touch | undefined;
+ let startTarget: HTMLElement | undefined;
+ let contentBounds: { left: number, right: number };
+ const dispatchMouse = (type: string, touch: Touch) => {
+ // 沿侧边上下滑动时,将框选终点投影到正文边界,保证选区能命中块。
+ const clientX = type === "mousemove" ?
+ Math.max(contentBounds.left, Math.min(touch.clientX, contentBounds.right)) : touch.clientX;
+ const target = type === "mousedown" ? startTarget :
+ document.elementFromPoint(clientX, touch.clientY) || element;
+ target.dispatchEvent(new MouseEvent(type, {
+ bubbles: true,
+ cancelable: true,
+ view: window,
+ button: 0,
+ buttons: type === "mouseup" ? 0 : 1,
+ clientX,
+ clientY: touch.clientY,
+ }));
+ };
+ const finish = (event: TouchEvent) => {
+ if (!activeTouch) {
+ return;
+ }
+ const touch = Array.from(event.changedTouches).find(item => item.identifier === activeTouch.identifier) ||
+ activeTouch;
+ activeTouch = undefined;
+ dispatchMouse("mouseup", touch);
+ startTarget = undefined;
+ event.preventDefault();
+ event.stopPropagation();
+ };
+ element.addEventListener("touchstart", (event: TouchEvent) => {
+ if (activeTouch) {
+ finish(event);
+ return;
+ }
+ const target = event.target as HTMLElement;
+ if (event.touches.length !== 1 || !canStart() || target.closest(".protyle-wysiwyg") !== element ||
+ element.closest(".sy__backlink--bottom")) {
+ return;
+ }
+ const touch = event.touches[0];
+ const rect = element.getBoundingClientRect();
+ const style = getComputedStyle(element);
+ const bounds = getBlockDragSelectContentBounds(rect.left, rect.right, style.paddingLeft, style.paddingRight);
+ if (touch.clientX >= bounds.left - 1 && touch.clientX <= bounds.right + 2) {
+ return;
+ }
+ activeTouch = touch;
+ startTarget = target;
+ contentBounds = bounds;
+ dispatchMouse("mousedown", touch);
+ event.preventDefault();
+ event.stopPropagation();
+ }, {passive: false});
+ element.addEventListener("touchmove", (event: TouchEvent) => {
+ if (!activeTouch) {
+ return;
+ }
+ const touch = Array.from(event.touches).find(item => item.identifier === activeTouch.identifier);
+ if (!touch || event.touches.length !== 1) {
+ finish(event);
+ return;
+ }
+ activeTouch = touch;
+ dispatchMouse("mousemove", touch);
+ event.preventDefault();
+ event.stopPropagation();
+ }, {passive: false});
+ element.addEventListener("touchend", finish, {passive: false});
+ element.addEventListener("touchcancel", finish, {passive: false});
+};