Skip to content

Build your personal knowledge base with Trilium Notes

fix(geomap): let a circle and a rectangle be drawn by a finger

Elian Dorand4b636f952d5
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 02ad27dca4..dde38674c2 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -763,7 +763,7 @@
"end_time": "The time an event ends at, read against <code>#endDate</code> where there is one and <code>#startDate</code> otherwise.",
"recurrence": "A CalDAV <code>RRULE</code> string making the event repeat. The start is taken from <code>#startDate</code> and <code>#startTime</code>, so the rule leaves <code>DTSTART</code> out.",
"geolocation": "The latitude and longitude the note is placed at on a geo map.",
- "geo_shape": "A shape the note is drawn as on a geo map: the kind of shape, then the latitude and longitude of each point, e.g. <code>line:48.85,2.29 48.86,2.35</code> — or a circle as its centre and reach in metres, <code>circle:48.85,2.29 500</code>.",
+ "geo_shape": "A shape the note is drawn as on a geo map: the kind of shape, then the latitude and longitude of each point, e.g. <code>line:48.85,2.29 48.86,2.35</code> — or a circle as its center and reach in meters, <code>circle:48.85,2.29 500</code>.",
"map_type": "Which note map is drawn: <code>link</code> for the relations between notes, or <code>tree</code> for the hierarchy.",
"map_style": "Which style a geo map is drawn in, chosen from the raster and vector styles offered in the collection properties.",
"map_dark_style": "Tells a geo map that the custom style given in <code>map:style</code> is a dark one, so that the marker titles are drawn light over it. The styles offered in the collection properties say so for themselves and need no such label.",
@@ -3022,10 +3022,10 @@
"draw-polygon-instruction": "Click on the map to add corners. Click the first corner again or press Enter to finish, or press Escape to cancel.",
"draw-rectangle": "Draw a rectangle on the map",
"draw-rectangle-toast-title": "Draw a rectangle",
- "draw-rectangle-instruction": "Click on the map at one corner, then click again at the opposite corner. Press Escape to cancel.",
+ "draw-rectangle-instruction": "Click on the map at one corner, then click again at the opposite corner. You can also drag from one corner to the other. Press Escape to cancel.",
"draw-circle": "Draw a circle on the map",
"draw-circle-toast-title": "Draw a circle",
- "draw-circle-instruction": "Click on the map at the centre, then click again at the distance the circle should reach. Press Escape to cancel.",
+ "draw-circle-instruction": "Click on the map at the center, then click again at the distance the circle should reach. You can also drag out from the center. Press Escape to cancel.",
"move-marker-instruction": "Click on the map to move the marker to that location or press Escape to dismiss.",
"move-marker-toast-title": "Move marker",
"unable-to-load-map": "Unable to load map.",
diff --git a/apps/client/src/widgets/collections/geomap/DrawShape.spec.tsx b/apps/client/src/widgets/collections/geomap/DrawShape.spec.tsx
index 511dae8ed4..2d776a42dc 100644
--- a/apps/client/src/widgets/collections/geomap/DrawShape.spec.tsx
+++ b/apps/client/src/widgets/collections/geomap/DrawShape.spec.tsx
@@ -39,12 +39,18 @@ vi.mock("terra-draw", () => {
}
}
+ /** A mode, holding the options it was built with so the spec can read them back. */
+ const mode = (name: string) => class {
+ mode = name;
+ constructor(public options?: { drawInteraction?: string }) {}
+ };
+
return {
TerraDraw,
- TerraDrawLineStringMode: class { mode = "linestring"; },
- TerraDrawPolygonMode: class { mode = "polygon"; },
- TerraDrawRectangleMode: class { mode = "rectangle"; },
- TerraDrawCircleMode: class { mode = "circle"; }
+ TerraDrawLineStringMode: mode("linestring"),
+ TerraDrawPolygonMode: mode("polygon"),
+ TerraDrawRectangleMode: mode("rectangle"),
+ TerraDrawCircleMode: mode("circle")
};
});
@@ -56,6 +62,8 @@ import { TerraDraw } from "terra-draw";
type FakeTerraDraw = InstanceType<typeof TerraDraw> & {
finish(id: string, action: string, feature: unknown): void;
+ /** The modes the session was built with, each holding the options it was given. */
+ config: { modes: { mode: string; options?: { drawInteraction?: string } }[] };
started: boolean;
stopped: boolean;
modeName: string | null;
@@ -119,6 +127,33 @@ describe("DrawShape", () => {
expect(session.stopped).toBe(true);
});
+ /**
+ * A circle and a rectangle are two positions, and Terra Draw reads the second one from pointer
+ * movement between two clicks unless told otherwise. A finger makes no such movement, so on a
+ * touchscreen the tap that placed the first position left nothing able to size the shape.
+ * Asking for the drag as well is what lets a finger draw one.
+ */
+ it("lets the two-corner tools be drawn by dragging, which is all a finger can do", () => {
+ for (const tool of [ "circle", "rectangle" ] as const) {
+ instances().length = 0;
+ renderSession(tool);
+
+ const [ builtMode ] = instances()[0].config.modes;
+ expect(builtMode.options?.drawInteraction).toBe("click-move-or-drag");
+ act(() => render(null, container));
+ }
+
+ // The tools that take a position per click are left alone: every tap is already a vertex.
+ for (const tool of [ "line", "polygon" ] as const) {
+ instances().length = 0;
+ renderSession(tool);
+
+ const [ builtMode ] = instances()[0].config.modes;
+ expect(builtMode.options).toBeUndefined();
+ act(() => render(null, container));
+ }
+ });
+
it("waits for a style to draw on, like every layer-adding child of the map", () => {
renderSession("line", { styleLoaded: false });
expect(instances()).toHaveLength(0);
diff --git a/apps/client/src/widgets/collections/geomap/DrawShape.tsx b/apps/client/src/widgets/collections/geomap/DrawShape.tsx
index 1ca361e93d..a7e0f8eb74 100644
--- a/apps/client/src/widgets/collections/geomap/DrawShape.tsx
+++ b/apps/client/src/widgets/collections/geomap/DrawShape.tsx
@@ -69,6 +69,22 @@ export default function DrawShape({ tool, onFinish }: DrawShapeProps) {
return <div />;
}
+/**
+ * How the two-corner tools are drawn, which is both ways rather than Terra Draw's default of
+ * `click-move` alone.
+ *
+ * A circle and a rectangle are made of two positions, and under `click-move` the second one is
+ * only ever read from pointer movement *between* two clicks. A finger produces no such movement:
+ * it is either down or it is not, and the moves it does make while down are reported as a drag,
+ * which that setting ignores. So on a touchscreen the first tap placed a centre and nothing could
+ * ever size the shape. Allowing the drag as well leaves the mouse exactly as it was and gives
+ * touch the press-and-drag it can actually perform.
+ *
+ * The tools that take a position per click — the line and the polygon — need nothing here: every
+ * tap is a vertex, and movement only previews the next one.
+ */
+const TWO_CORNER_INTERACTION = "click-move-or-drag";
+
/** The Terra Draw mode a tool draws with — each knows its own name for `setMode`. */
function buildMode(tool: DrawTool) {
switch (tool) {
@@ -77,9 +93,9 @@ function buildMode(tool: DrawTool) {
case "polygon":
return new TerraDrawPolygonMode();
case "rectangle":
- return new TerraDrawRectangleMode();
+ return new TerraDrawRectangleMode({ drawInteraction: TWO_CORNER_INTERACTION });
case "circle":
- return new TerraDrawCircleMode();
+ return new TerraDrawCircleMode({ drawInteraction: TWO_CORNER_INTERACTION });
}
}