Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(geomap): rectangle drawing tool
Claude0ab97747aba5
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex d10819070a..ce2b9365b1 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2859,6 +2859,9 @@"draw-line-instruction": "Click on the map to add points. Click the last point again or press Enter to finish, or press Escape to cancel.","draw-polygon-toast-title": "Draw an area","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.","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.tsxindex a1368b1149..e3acb8df77 100644--- a/apps/client/src/widgets/collections/geomap/DrawShape.spec.tsx+++ b/apps/client/src/widgets/collections/geomap/DrawShape.spec.tsx@@ -41,7 +41,8 @@ vi.mock("terra-draw", () => {return {TerraDraw,TerraDrawLineStringMode: class { mode = "linestring"; },- TerraDrawPolygonMode: class { mode = "polygon"; }+ TerraDrawPolygonMode: class { mode = "polygon"; },+ TerraDrawRectangleMode: class { mode = "rectangle"; }};});@@ -128,6 +129,16 @@ describe("DrawShape", () => {expect(onFinish).toHaveBeenCalledWith({ type: "polygon", coordinates: [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] });});+ it("draws a rectangle with its own mode, but what it made is a polygon note", () => {+ renderSession("rectangle");++ const [ session ] = instances();+ expect(session.modeName).toBe("rectangle");++ act(() => session.finish("f1", "draw", RING_FEATURE));+ expect(onFinish).toHaveBeenCalledWith({ type: "polygon", coordinates: [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] });+ });+it("leaves alone what is not a drawing being finished", () => {renderSession("line");diff --git a/apps/client/src/widgets/collections/geomap/DrawShape.tsx b/apps/client/src/widgets/collections/geomap/DrawShape.tsxindex e30b5d3231..e804c49879 100644--- a/apps/client/src/widgets/collections/geomap/DrawShape.tsx+++ b/apps/client/src/widgets/collections/geomap/DrawShape.tsx@@ -1,12 +1,12 @@import { useContext, useEffect, useRef } from "preact/hooks";-import { type GeoJSONStoreFeatures, TerraDraw, TerraDrawLineStringMode, TerraDrawPolygonMode } from "terra-draw";+import { type GeoJSONStoreFeatures, TerraDraw, TerraDrawLineStringMode, TerraDrawPolygonMode, TerraDrawRectangleMode } from "terra-draw";import { TerraDrawMapLibreGLAdapter } from "terra-draw-maplibre-gl-adapter";import { MapStyleLoaded, ParentMap } from "./map";import { type GeoShape, polygonFromRing } from "./shapes";/** The drawing tools on offer, each arming Terra Draw with a mode of its own. */-export type DrawTool = "line" | "polygon";+export type DrawTool = "line" | "polygon" | "rectangle";interface DrawShapeProps {tool: DrawTool;@@ -76,6 +76,8 @@ function buildMode(tool: DrawTool) {return new TerraDrawLineStringMode();case "polygon":return new TerraDrawPolygonMode();+ case "rectangle":+ return new TerraDrawRectangleMode();}}diff --git a/apps/client/src/widgets/collections/geomap/EditToolbar.spec.tsx b/apps/client/src/widgets/collections/geomap/EditToolbar.spec.tsxindex 8bd0bf9a42..625e529b19 100644--- a/apps/client/src/widgets/collections/geomap/EditToolbar.spec.tsx+++ b/apps/client/src/widgets/collections/geomap/EditToolbar.spec.tsx@@ -43,7 +43,8 @@ function renderBar({ map = {} as never, isReadOnly = false, placing = false, dra/** Every drawing tool on the bar, by the icon its button wears. */const DRAW_TOOL_ICONS: { tool: DrawTool; icon: string }[] = [{ tool: "line", icon: "bx-vector" },- { tool: "polygon", icon: "bx-shape-polygon" }+ { tool: "polygon", icon: "bx-shape-polygon" },+ { tool: "rectangle", icon: "bx-rectangle" }];describe("geo map EditToolbar", () => {diff --git a/apps/client/src/widgets/collections/geomap/EditToolbar.tsx b/apps/client/src/widgets/collections/geomap/EditToolbar.tsxindex cc59030305..133f980821 100644--- a/apps/client/src/widgets/collections/geomap/EditToolbar.tsx+++ b/apps/client/src/widgets/collections/geomap/EditToolbar.tsx@@ -27,7 +27,8 @@ interface EditToolbarProps {/** The drawing tools in the order they stand on the bar, each wearing the shape it draws. */const DRAW_TOOLS: { tool: DrawTool; icon: string; title: () => string }[] = [{ tool: "line", icon: "bx-vector", title: () => t("geo-map.draw-line") },- { tool: "polygon", icon: "bx-shape-polygon", title: () => t("geo-map.draw-polygon") }+ { tool: "polygon", icon: "bx-shape-polygon", title: () => t("geo-map.draw-polygon") },+ { tool: "rectangle", icon: "bx-rectangle", title: () => t("geo-map.draw-rectangle") }];/**diff --git a/apps/client/src/widgets/collections/geomap/index.tsx b/apps/client/src/widgets/collections/geomap/index.tsxindex f845ab34d6..6e895159ac 100644--- a/apps/client/src/widgets/collections/geomap/index.tsx+++ b/apps/client/src/widgets/collections/geomap/index.tsx@@ -453,6 +453,12 @@ function drawingToast(tool: DrawTool): { icon: string; title: string; message: stitle: t("geo-map.draw-polygon-toast-title"),message: t("geo-map.draw-polygon-instruction")};+ case "rectangle":+ return {+ icon: "rectangle",+ title: t("geo-map.draw-rectangle-toast-title"),+ message: t("geo-map.draw-rectangle-instruction")+ };}}