Skip to content

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

:art: Improve PDF rectangle annotation interactions https://github.com/siyuan-note/siyuan/issues/15974

Vanessa54c5a27ce6e8
diff --git a/app/src/asset/anno.ts b/app/src/asset/anno.ts
index d0179f9209..0790d393cb 100644
--- a/app/src/asset/anno.ts
+++ b/app/src/asset/anno.ts
@@ -11,10 +11,11 @@ import {isMobile} from "../util/functions";
import {confirmDialog} from "../dialog/confirmDialog";
import {escapeAttr, escapeHtml} from "../util/escape";
import {filesize} from "filesize";
-import {resizeRectBounds} from "./rectAnnotationResize";
+import {hideRectResizeHandles, moveRectBounds, resizeRectBounds} from "./rectAnnotationResize";
import type {RectBounds, RectResizeDirection} from "./rectAnnotationResize";
const RECT_RESIZE_MIN_SIZE = 8;
+const RECT_DRAG_THRESHOLD = 3;
export const initAnno = (element: HTMLElement, pdf: any) => {
getConfig(pdf);
@@ -133,14 +134,19 @@ export const initAnno = (element: HTMLElement, pdf: any) => {
}
};
});
+ let ignoreRectClick = false;
element.firstElementChild.addEventListener("mousedown", (event: MouseEvent) => {
- const handleElement = (event.target as HTMLElement).closest(".pdf__rect-resize") as HTMLElement;
- if (!handleElement || event.button !== 0) {
+ if (event.button !== 0 || rectAnnoElement.classList.contains("toggled")) {
return;
}
- const target = handleElement.closest(".pdf__rect") as HTMLElement;
- const direction = handleElement.dataset.direction as RectResizeDirection;
- if (!target || !direction) {
+ const eventTarget = event.target as HTMLElement;
+ const handleElement = eventTarget.closest(".pdf__rect-resize") as HTMLElement;
+ const target = eventTarget.closest(".pdf__rect") as HTMLElement;
+ if (!target || (!handleElement && !isRectAnnotationElement(target))) {
+ return;
+ }
+ const direction = handleElement?.dataset.direction as RectResizeDirection;
+ if (handleElement && !direction) {
return;
}
const pageElement = hasClosestByClassName(target, "page");
@@ -167,37 +173,61 @@ export const initAnno = (element: HTMLElement, pdf: any) => {
right: canvasRect.right,
bottom: canvasRect.bottom,
};
- let resized = initial;
+ const startX = event.clientX;
+ const startY = event.clientY;
+ let bounds = initial;
let position: number[];
const updateAnnotationElement = () => {
position = page.viewport.convertToPdfPoint(
- resized.left - canvasRect.left,
- resized.top - canvasRect.top,
+ bounds.left - canvasRect.left,
+ bounds.top - canvasRect.top,
).concat(page.viewport.convertToPdfPoint(
- resized.right - canvasRect.left,
- resized.bottom - canvasRect.top,
+ bounds.right - canvasRect.left,
+ bounds.bottom - canvasRect.top,
));
setRectPosition(annotationElement, page, position);
};
- updateAnnotationElement();
- element.querySelector(".pdf__util").classList.add("fn__none");
+ let moved = false;
const mousemove = (moveEvent: MouseEvent) => {
- resized = resizeRectBounds(initial, boundary, direction, moveEvent.clientX, moveEvent.clientY,
- RECT_RESIZE_MIN_SIZE);
+ const deltaX = moveEvent.clientX - startX;
+ const deltaY = moveEvent.clientY - startY;
+ if (!moved && Math.hypot(deltaX, deltaY) < RECT_DRAG_THRESHOLD) {
+ return;
+ }
+ if (!moved) {
+ moved = true;
+ if (!target.classList.contains("pdf__rect--selected")) {
+ showToolbar(element, undefined, target);
+ }
+ hideToolbarMenu(element);
+ target.classList.add("pdf__rect--dragging");
+ }
+ bounds = handleElement ?
+ resizeRectBounds(initial, boundary, direction, moveEvent.clientX, moveEvent.clientY,
+ RECT_RESIZE_MIN_SIZE) :
+ moveRectBounds(initial, boundary, deltaX, deltaY);
updateAnnotationElement();
};
const mouseup = () => {
document.removeEventListener("mousemove", mousemove);
document.removeEventListener("mouseup", mouseup);
+ target.classList.remove("pdf__rect--dragging");
+ if (!moved) {
+ return;
+ }
+ ignoreRectClick = true;
+ setTimeout(() => {
+ ignoreRectClick = false;
+ });
const config = getConfig(pdf);
const id = target.getAttribute("data-node-id");
const annoItem = config?.[id] as IPdfAnno;
const pageItem = annoItem?.pages?.find(item => item.index === pageIndex);
if (!pageItem) {
- resized = initial;
+ bounds = initial;
updateAnnotationElement();
- showToolbar(element, undefined, target);
+ hideToolbarMenu(element);
return;
}
pageItem.positions = [position];
@@ -207,7 +237,7 @@ export const initAnno = (element: HTMLElement, pdf: any) => {
path: pdf.appConfig.file.replace(location.origin, "").substr(1) + ".sya",
data: JSON.stringify(config),
});
- showToolbar(element, undefined, target);
+ hideToolbarMenu(element);
};
document.addEventListener("mousemove", mousemove);
document.addEventListener("mouseup", mouseup);
@@ -217,6 +247,17 @@ export const initAnno = (element: HTMLElement, pdf: any) => {
element.firstElementChild.addEventListener("click", (event: MouseEvent) => {
let processed = false;
let target = event.target as HTMLElement;
+ if (ignoreRectClick) {
+ ignoreRectClick = false;
+ event.preventDefault();
+ event.stopPropagation();
+ return;
+ }
+ if (target.closest(".pdf__rect-resize")) {
+ event.preventDefault();
+ event.stopPropagation();
+ return;
+ }
if (typeof event.detail === "string") {
window.siyuan.storage[Constants.LOCAL_PDFTHEME].annoColor = event.detail === "0" ?
(window.siyuan.storage[Constants.LOCAL_PDFTHEME].annoColor || "var(--b3-pdf-background1)")
@@ -455,15 +496,12 @@ const setRelation = (pdf: any) => {
};
const hideToolbar = (element: HTMLElement) => {
- element.querySelector(".pdf__util").classList.add("fn__none");
+ hideToolbarMenu(element);
hideRectResizeHandles(element);
};
-const hideRectResizeHandles = (element: HTMLElement) => {
- element.querySelectorAll(".pdf__rect--selected").forEach(item => {
- item.classList.remove("pdf__rect--selected");
- item.querySelectorAll(".pdf__rect-resize").forEach(handle => handle.remove());
- });
+const hideToolbarMenu = (element: HTMLElement) => {
+ element.querySelector(".pdf__util").classList.add("fn__none");
};
const isRectAnnotationElement = (element: HTMLElement) => element.dataset.mode === "rect" ||
@@ -511,7 +549,7 @@ const showToolbar = (element: HTMLElement, range: Range, target?: HTMLElement) =
}
utilElement.classList.remove("pdf__util--hide");
const targetRect = target.firstElementChild.getBoundingClientRect();
- setPosition(utilElement, targetRect.left, targetRect.top + targetRect.height + 4);
+ setPosition(utilElement, targetRect.left, targetRect.bottom + 4, targetRect.height + 8);
};
const getTextNode = (element: HTMLElement, isFirst: boolean) => {
diff --git a/app/src/asset/rectAnnotationResize.test.ts b/app/src/asset/rectAnnotationResize.test.ts
index 4a2b51a723..b64dd3fcc2 100644
--- a/app/src/asset/rectAnnotationResize.test.ts
+++ b/app/src/asset/rectAnnotationResize.test.ts
@@ -1,6 +1,6 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
-import {resizeRectBounds} from "./rectAnnotationResize";
+import {hideRectResizeHandles, moveRectBounds, resizeRectBounds} from "./rectAnnotationResize";
const initial = {left: 20, top: 30, right: 80, bottom: 90};
const boundary = {left: 0, top: 0, right: 100, bottom: 120};
@@ -17,6 +17,47 @@ describe("rectangle annotation resizing", () => {
{left: 20, top: 30, right: 95, bottom: 110});
});
+ it("removes resize handles when the PDF selection is cleared", () => {
+ let selectedClassRemoved = false;
+ let handleRemoved = false;
+ const selected = {
+ classList: {
+ remove(className: string) {
+ selectedClassRemoved = className === "pdf__rect--selected";
+ },
+ },
+ querySelectorAll(selector: string) {
+ assert.equal(selector, ".pdf__rect-resize");
+ return [{remove: () => {
+ handleRemoved = true;
+ }}];
+ },
+ };
+ const container = {
+ querySelectorAll(selector: string) {
+ assert.equal(selector, ".pdf__rect--selected");
+ return [selected];
+ },
+ } as unknown as ParentNode;
+
+ hideRectResizeHandles(container);
+
+ assert.equal(selectedClassRemoved, true);
+ assert.equal(handleRemoved, true);
+ });
+
+ it("moves a rectangle without changing its size", () => {
+ assert.deepEqual(moveRectBounds(initial, boundary, 10, 15),
+ {left: 30, top: 45, right: 90, bottom: 105});
+ });
+
+ it("keeps moved rectangles within the page", () => {
+ assert.deepEqual(moveRectBounds(initial, boundary, -50, -60),
+ {left: 0, top: 0, right: 60, bottom: 60});
+ assert.deepEqual(moveRectBounds(initial, boundary, 80, 90),
+ {left: 40, top: 60, right: 100, bottom: 120});
+ });
+
it("keeps resized rectangles within the page", () => {
assert.deepEqual(resizeRectBounds(initial, boundary, "nw", -20, -10, 8),
{left: 0, top: 0, right: 80, bottom: 90});
diff --git a/app/src/asset/rectAnnotationResize.ts b/app/src/asset/rectAnnotationResize.ts
index c2a1f6e114..db7279c5ee 100644
--- a/app/src/asset/rectAnnotationResize.ts
+++ b/app/src/asset/rectAnnotationResize.ts
@@ -26,3 +26,23 @@ export const resizeRectBounds = (initial: RectBounds, boundary: RectBounds, dire
}
return result;
};
+
+export const moveRectBounds = (initial: RectBounds, boundary: RectBounds, deltaX: number, deltaY: number) => {
+ const width = initial.right - initial.left;
+ const height = initial.bottom - initial.top;
+ const left = clamp(initial.left + deltaX, boundary.left, boundary.right - width);
+ const top = clamp(initial.top + deltaY, boundary.top, boundary.bottom - height);
+ return {
+ left,
+ top,
+ right: left + width,
+ bottom: top + height,
+ };
+};
+
+export const hideRectResizeHandles = (element: ParentNode) => {
+ element.querySelectorAll(".pdf__rect--selected").forEach(item => {
+ item.classList.remove("pdf__rect--selected");
+ item.querySelectorAll(".pdf__rect-resize").forEach(handle => handle.remove());
+ });
+};
diff --git a/app/src/assets/scss/pdf/_pdf.scss b/app/src/assets/scss/pdf/_pdf.scss
index 30b1c02508..07feddbec9 100644
--- a/app/src/assets/scss/pdf/_pdf.scss
+++ b/app/src/assets/scss/pdf/_pdf.scss
@@ -88,8 +88,17 @@
}
}
+ &[data-mode="rect"] > div {
+ cursor: move;
+ }
+
&--selected > div {
z-index: 2;
+ cursor: move;
+ }
+
+ &--dragging > div {
+ cursor: grabbing;
}
&-resize {
diff --git a/app/src/protyle/ui/hideElements.ts b/app/src/protyle/ui/hideElements.ts
index 2ace482022..4b658b3ef8 100644
--- a/app/src/protyle/ui/hideElements.ts
+++ b/app/src/protyle/ui/hideElements.ts
@@ -1,4 +1,5 @@
import {getAllEditor} from "../../layout/getAll";
+import {hideRectResizeHandles} from "../../asset/rectAnnotationResize";
import {isIPhone} from "../util/compatibility";
import {hideGutterElements} from "./gutterVisibility";
@@ -89,6 +90,7 @@ export const hideAllElements = (types: string[]) => {
document.querySelectorAll(".pdf__util").forEach(item => {
item.classList.add("fn__none");
});
+ hideRectResizeHandles(document);
}
if (types.includes("gutter")) {
document.querySelectorAll(".protyle-gutters").forEach(item => {