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(share): restore a shared map's saved view
Elian Doran99a8292d0f9a
diff --git a/apps/client/src/widgets/collections/geomap/index.tsx b/apps/client/src/widgets/collections/geomap/index.tsxindex b1fe935b7e..595db4af46 100644--- a/apps/client/src/widgets/collections/geomap/index.tsx+++ b/apps/client/src/widgets/collections/geomap/index.tsx@@ -65,7 +65,7 @@ const PLACEMENT_TOAST_ID = "geo-placement";*/const OUTLINE_DELAY_MS = 250;-interface MapData {+export interface MapData {view?: SavedView;/** The view of the same map drawn over its image (`~map:image`), whose positions say nothing* about the world map's, so that switching between the two restores each. */diff --git a/packages/share-theme/src/content/geomap_view.tsx b/packages/share-theme/src/content/geomap_view.tsxindex e2bdc7b22c..dab54bb8ea 100644--- a/packages/share-theme/src/content/geomap_view.tsx+++ b/packages/share-theme/src/content/geomap_view.tsx@@ -5,7 +5,7 @@ import Component from "@triliumnext/client/src/components/component.js";import TabManager from "@triliumnext/client/src/components/tab_manager.js";import froca from "@triliumnext/client/src/services/froca.js";import options, { type OptionValue } from "@triliumnext/client/src/services/options.js";-import GeoView from "@triliumnext/client/src/widgets/collections/geomap/index.js";+import GeoView, { type MapData } from "@triliumnext/client/src/widgets/collections/geomap/index.js";import { ParentComponent } from "@triliumnext/client/src/widgets/react/react_utils.js";import { render } from "preact";@@ -13,6 +13,7 @@ import type { FrocaPayload } from "./calendar_view.js";interface GeoMapPayload extends FrocaPayload {options: Record<string, OptionValue | null>;+ viewConfig?: MapData;}/**@@ -21,7 +22,7 @@ interface GeoMapPayload extends FrocaPayload {* control the view has.*/export default function mountGeoMap(container: HTMLElement, payload: GeoMapPayload) {- const { options: optionValues, links, ...rows } = payload;+ const { options: optionValues, links, viewConfig, ...rows } = payload;options.load(Object.fromEntries(Object.entries(optionValues).flatMap(([ name, value ]) => (value === null ? [] : [ [ name, value ] ]))));@@ -51,7 +52,7 @@ export default function mountGeoMap(container: HTMLElement, payload: GeoMapPaylonotePath={noteId}noteIds={note.getChildNoteIds()}highlightedTokens={null}- viewConfig={undefined}+ viewConfig={viewConfig}saveConfig={() => {}}media="screen"onReady={() => {}}diff --git a/packages/trilium-core/src/share/content_renderer.ts b/packages/trilium-core/src/share/content_renderer.tsindex 0323ef9fdb..1cc5fe1507 100644--- a/packages/trilium-core/src/share/content_renderer.ts+++ b/packages/trilium-core/src/share/content_renderer.ts@@ -913,11 +913,12 @@ function renderCalendar(result: Result, note: SNote) {/*** Renders a geo map collection as an element the share theme's script mounts the app's whole map- * view into, read-only, from the notes and the display options embedded next to it.+ * view into, read-only, from the notes, the display options and the saved view embedded next to it.*/function renderGeoMap(result: Result, note: SNote) {const payload = {...buildFrocaPayload(note),+ viewConfig: getViewConfig(note, "geoMap"),options: Object.fromEntries(SHARED_OPTIONS.map((name) => [ name, options.getOptionOrNull(name) ]))};const json = JSON.stringify(payload).replace(/</g, "\\u003c");@@ -925,6 +926,23 @@ function renderGeoMap(result: Result, note: SNote) {+ `<script type="application/json" class="share-froca">${json}</script>`;}+/**+ * The view a collection saved, such as a map's position and zoom, which the app keeps in a+ * `<viewType>.json` attachment.+ */+function getViewConfig(note: SNote, viewType: string): unknown {+ const attachment = note.getAttachmentByTitle(`${viewType}.json`);+ if (attachment?.role !== "viewConfig") {+ return undefined;+ }++ try {+ return JSON.parse(String(attachment.getContent()));+ } catch {+ return undefined;+ }+}+/** The options the app's collection views read, which a shared page needs to draw them alike. */const SHARED_OPTIONS = [ "locale", "formattingLocale", "firstDayOfWeek" ] as const;