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
refactor(geomap): lock through the read-only badge
Elian Doran5d5c080c8ffb
diff --git a/apps/client/src/components/note_context.spec.ts b/apps/client/src/components/note_context.spec.tsnew file mode 100644index 0000000000..75861a93f1--- /dev/null+++ b/apps/client/src/components/note_context.spec.ts@@ -0,0 +1,45 @@+import { beforeEach, describe, expect, it } from "vitest";++import { buildNote } from "../test/easy-froca.js";+import NoteContext from "./note_context.js";++describe("NoteContext read-only capability", () => {+ let noteContext: NoteContext;++ beforeEach(() => {+ noteContext = new NoteContext();+ });++ it("reports a collection read-only only where its view honours the label", async () => {+ // The geo map is the one view that reads #readOnly, so it is the one that can report it:+ // a table wearing the same label is still editable, and saying otherwise would put a+ // read-only badge over a collection that can be changed.+ for (const [ viewType, expected ] of [ [ "geoMap", true ], [ "table", false ] ] as const) {+ const note = buildNote({+ "title": viewType, "type": "book", "#viewType": viewType, "#readOnly": ""+ });+ noteContext.noteId = note.noteId;++ expect(await noteContext.isReadOnly()).toBe(expected);+ }+ });++ it("leaves a geo map editable unless locked, or while temporarily unlocked", async () => {+ const unlocked = buildNote({+ "title": "Unlocked map", "type": "book", "#viewType": "geoMap"+ });+ noteContext.noteId = unlocked.noteId;+ expect(await noteContext.isReadOnly()).toBe(false);++ const locked = buildNote({+ "title": "Locked map", "type": "book", "#viewType": "geoMap", "#readOnly": ""+ });+ noteContext.noteId = locked.noteId;+ expect(await noteContext.isReadOnly()).toBe(true);++ // What the read-only badge does when it is clicked: the label stays, the tab stops+ // honouring it.+ if (noteContext.viewScope) noteContext.viewScope.readOnlyTemporarilyDisabled = true;+ expect(await noteContext.isReadOnly()).toBe(false);+ });+});diff --git a/apps/client/src/components/note_context.ts b/apps/client/src/components/note_context.tsindex c39f51b9b7..c63dd6f976 100644--- a/apps/client/src/components/note_context.ts+++ b/apps/client/src/components/note_context.ts@@ -39,6 +39,16 @@ const READ_ONLY_CAPABLE_TYPES: string[] = ["spreadsheet"];+/**+ * Collection view types that honour `#readOnly`, making a `book` note read-only capable.+ *+ * Listed by view type rather than by note type because the other views ignore the label: reporting+ * a table or a board as read-only would put a badge over a collection that can still be edited.+ */+const READ_ONLY_CAPABLE_VIEW_TYPES: string[] = [+ "geoMap"+];+export interface NoteContextDataMap {toc: HeadingContext;highlights: HighlightContext;@@ -358,9 +368,13 @@ class NoteContext extends Component implements EventListener<"entitiesReloaded">return false;}- // Note types that support a read-only state (via the #readOnly label, source view, or auto-readonly).+ // What supports a read-only state at all, via the #readOnly label, the source view or+ // auto-readonly.const isPdf = this.note.type === "file" && this.note.mime === "application/pdf";- if (!isPdf && !READ_ONLY_CAPABLE_TYPES.includes(this.note.type)) {+ const isReadOnlyCapableCollection = this.note.type === "book"+ && READ_ONLY_CAPABLE_VIEW_TYPES.includes(this.note.getLabelValue("viewType") ?? "");+ if (!isPdf && !isReadOnlyCapableCollection+ && !READ_ONLY_CAPABLE_TYPES.includes(this.note.type)) {return false;}diff --git a/apps/client/src/widgets/collections/geomap/index.tsx b/apps/client/src/widgets/collections/geomap/index.tsxindex 0e3ecb032f..10df3e4295 100644--- a/apps/client/src/widgets/collections/geomap/index.tsx+++ b/apps/client/src/widgets/collections/geomap/index.tsx@@ -10,8 +10,7 @@ import { t } from "../../../services/i18n";import server from "../../../services/server";import toast from "../../../services/toast";import CollectionProperties from "../../note_bars/CollectionProperties";-import ActionButton from "../../react/ActionButton";-import { useCollectionTreeDrag, useNoteBlob, useNoteLabel, useNoteLabelBoolean, useNoteProperty, useSpacedUpdate } from "../../react/hooks";+import { useCollectionTreeDrag, useEffectiveReadOnly, useNoteBlob, useNoteContext, useNoteLabel, useNoteLabelBoolean, useNoteProperty, useSpacedUpdate } from "../../react/hooks";import { ViewModeProps } from "../interface";import { createNewNote, importGpxTrack, moveMarker } from "./api";import Buildings from "./Buildings";@@ -57,6 +56,7 @@ type Placement =| { mode: "move"; noteId: string };export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewModeProps<MapData>) {+ const { noteContext } = useNoteContext();const [ placement, setPlacement ] = useState<Placement>();// Which marker the detail pane stands for. Held here rather than in the pane so that creating a// note can open the pane on it (see createNoteAt below).@@ -69,7 +69,7 @@ export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewMconst [ hasScale ] = useNoteLabelBoolean(note, "map:scale");const [ hideLabels ] = useNoteLabelBoolean(note, "map:hideLabels");const [ clustered ] = useNoteLabelBoolean(note, "map:cluster");- const [ isReadOnly ] = useNoteLabelBoolean(note, "readOnly");+ const isReadOnly = useEffectiveReadOnly(note, noteContext);const [ includeArchived ] = useNoteLabelBoolean(note, "includeArchived");const [ notes, setNotes ] = useState<FNote[]>([]);const layerData = useLayerData(note);@@ -226,12 +226,10 @@ export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewMreturn (<div className={`geo-view ${placement ? "placing-note" : ""}`}>- {/* Only the lock at its end: adding a note lives on the map itself now (see- EditToolbar), where it survives the map going fullscreen without this bar. */}- <CollectionProperties- note={note}- rightChildren={<ToggleReadOnlyButton note={note} />}- />+ {/* Nothing of its own at the end: locking is the read-only badge's, and adding a note+ lives on the map itself (see EditToolbar), where it survives the map going+ fullscreen without this bar. */}+ <CollectionProperties note={note} />{ coordinates !== undefined && zoom !== undefined && <MapapiRef={apiRef} containerRef={containerRef}coordinates={coordinates}@@ -325,16 +323,6 @@ function useLayerData(note: FNote) {return layerData;}-function ToggleReadOnlyButton({ note }: { note: FNote }) {- const [ isReadOnly, setReadOnly ] = useNoteLabelBoolean(note, "readOnly");-- return <ActionButton- text={isReadOnly ? t("toggle_read_only_button.unlock-editing") : t("toggle_read_only_button.lock-editing")}- icon={isReadOnly ? "bx bx-lock-open-alt" : "bx bx-lock-alt"}- onClick={() => setReadOnly(!isReadOnly)}- />;-}-/*** A GPX note's track, where the note is one.*