Skip to content

Build your personal knowledge base with Trilium Notes

fix(web-clipper): keep the crop rectangle on the cursor when the drag reverses

Elian Doran4c052fb58e90
diff --git a/apps/web-clipper/entrypoints/content/index.spec.ts b/apps/web-clipper/entrypoints/content/index.spec.ts
index 6493cff7a2..6ddc6a6c17 100644
--- a/apps/web-clipper/entrypoints/content/index.spec.ts
+++ b/apps/web-clipper/entrypoints/content/index.spec.ts
@@ -150,6 +150,24 @@ describe("content script", () => {
});
});
+ it("keeps the rectangle between the start and the cursor when the drag reverses", async () => {
+ vi.useFakeTimers();
+ const response = send({ name: "trilium-get-rectangle-for-screenshot" });
+ const [ overlay, , selection ] = [ ...document.body.children ] as HTMLElement[];
+
+ mouse(overlay, "mousedown", 200, 150);
+ mouse(overlay, "mousemove", 100, 100);
+ mouse(overlay, "mousemove", 300, 250);
+ const { left, top, width, height } = selection?.style ?? {};
+ expect([ left, top, width, height ]).toEqual([ "200px", "150px", "100px", "100px" ]);
+ mouse(overlay, "mouseup", 300, 250);
+
+ await vi.advanceTimersByTimeAsync(100);
+ await expect(response).resolves.toMatchObject({
+ rect: { x: 200, y: 150, width: 100, height: 100 }
+ });
+ });
+
it("returns no rectangle without a drag on the overlay or after Escape", async () => {
const release = send({ name: "trilium-get-rectangle-for-screenshot" });
mouse(document.body.firstElementChild, "mouseup", 10, 10);
diff --git a/apps/web-clipper/entrypoints/content/index.ts b/apps/web-clipper/entrypoints/content/index.ts
index 0b91a95d7e..e286b3c01c 100644
--- a/apps/web-clipper/entrypoints/content/index.ts
+++ b/apps/web-clipper/entrypoints/content/index.ts
@@ -132,14 +132,8 @@ export default defineContentScript({
function setSelectionSizeFromMouse(event: MouseEvent) {
if (!draggingStartPos) return;
- if (event.clientX < draggingStartPos.x) {
- selectionArea.x = event.clientX;
- }
-
- if (event.clientY < draggingStartPos.y) {
- selectionArea.y = event.clientY;
- }
-
+ selectionArea.x = Math.min(event.clientX, draggingStartPos.x);
+ selectionArea.y = Math.min(event.clientY, draggingStartPos.y);
selectionArea.width = Math.max(1, Math.abs(event.clientX - draggingStartPos.x));
selectionArea.height = Math.max(1, Math.abs(event.clientY - draggingStartPos.y));
updateSelection();