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): offer only drawable images in the image picker
Elian Doran16e05eaf619f
diff --git a/apps/client/src/services/note_autocomplete.spec.ts b/apps/client/src/services/note_autocomplete.spec.tsindex f9638268f3..e775b6565b 100644--- a/apps/client/src/services/note_autocomplete.spec.ts+++ b/apps/client/src/services/note_autocomplete.spec.ts@@ -76,6 +76,10 @@ describe("getNoteSuggestions", () => {// A limit is passed on for the server to stop at; left out, none is sent.await getNoteSuggestions("a", { limit: 10 });expect(server.get).toHaveBeenLastCalledWith("autocomplete?query=a&activeNoteId=activeNote&fastSearch=true&limit=10");+ // So is a filter, encoded as the search query it is.+ await getNoteSuggestions("a", { filter: "note.type = image" });+ expect(server.get).toHaveBeenLastCalledWith("autocomplete?query=a&activeNoteId=activeNote"+ + "&fastSearch=true&filter=note.type%20%3D%20image");expect(getInboxTarget).not.toHaveBeenCalled();});diff --git a/apps/client/src/services/note_autocomplete.ts b/apps/client/src/services/note_autocomplete.tsindex d16df7e8fc..370fe9ba46 100644--- a/apps/client/src/services/note_autocomplete.ts+++ b/apps/client/src/services/note_autocomplete.ts@@ -53,20 +53,25 @@ export interface NoteSuggestionOptions {fastSearch?: boolean;/** Lists at most this many notes, which the server then stops at. The action rows come on top. */limit?: number;+ /**+ * A search query the notes must also match, such as `note.type = image`. With a blank term, the+ * notes it matches are listed instead of the recent ones.+ */+ filter?: string;}/*** Returns the notes matching `term`, or the recently visited notes when `term` is blank, with the* action rows the options ask for.*/-export async function getNoteSuggestions(term: string, { allowCreatingNotes, allowJumpToSearchNotes, allowExternalLinks, fastSearch = true, limit }: NoteSuggestionOptions = {}): Promise<Suggestion[]> {+export async function getNoteSuggestions(term: string, { allowCreatingNotes, allowJumpToSearchNotes, allowExternalLinks, fastSearch = true, limit, filter }: NoteSuggestionOptions = {}): Promise<Suggestion[]> {const activeNoteId = appContext.tabManager.getActiveContextNoteId();const hasTerm = term.trim().length >= 1;// Runs concurrently with the search, so naming the destination costs a request but no wait.const pendingInboxTarget = hasTerm && allowCreatingNotes ? getInboxTarget() : null;- const results: Suggestion[] = await server.get<AutocompleteResult[]>(`autocomplete?query=${encodeURIComponent(term)}&activeNoteId=${activeNoteId}&fastSearch=${fastSearch}${limit ? `&limit=${limit}` : ""}`);+ const results: Suggestion[] = await server.get<AutocompleteResult[]>(`autocomplete?query=${encodeURIComponent(term)}&activeNoteId=${activeNoteId}&fastSearch=${fastSearch}${limit ? `&limit=${limit}` : ""}${filter ? `&filter=${encodeURIComponent(filter)}` : ""}`);const before: Suggestion[] = [];const after: Suggestion[] = [];diff --git a/apps/client/src/widgets/collections/geomap/GeoMapProperties.spec.tsx b/apps/client/src/widgets/collections/geomap/GeoMapProperties.spec.tsxindex 6f618b59ec..6914c5299d 100644--- a/apps/client/src/widgets/collections/geomap/GeoMapProperties.spec.tsx+++ b/apps/client/src/widgets/collections/geomap/GeoMapProperties.spec.tsx@@ -38,12 +38,13 @@ vi.mock("../../../services/attributes", () => ({// NoteAutocomplete is tested in its own spec. A click on the stub picks `image2`.vi.mock("../../react/NoteAutocomplete", () => ({- default: ({ noteId, noteIdChanged }: {- noteId?: string, noteIdChanged: (noteId: string) => void+ default: ({ noteId, filter, noteIdChanged }: {+ noteId?: string, filter?: string, noteIdChanged: (noteId: string) => void}) => (<divclassName="autocomplete-stub"data-note-id={noteId ?? ""}+ data-filter={filter}onClick={() => noteIdChanged("image2")}/>)@@ -246,6 +247,11 @@ describe("GeoMapProperties", () => {it("measures in pixels until custom corners are chosen, starting them at the size", () => {const note = draw({ "~map:image": "image1" }, LOADED_IMAGE);expect(query(".autocomplete-stub")?.dataset.noteId).toBe("image1");+ // Only the images a map can be drawn over are offered.+ expect(query(".autocomplete-stub")?.dataset.filter).toBe(+ "note.type = image AND (note.mime = 'image/png' OR note.mime = 'image/jpeg' "+ + "OR note.mime = 'image/jpg' OR note.mime = 'image/webp' "+ + "OR note.mime = 'image/gif')");expect(optionTitles(".geo-map-properties-background")).toEqual(["geo-map.background-type", "geo-map.image-note","geo-map.coordinates", "geo-map.dark-image"diff --git a/apps/client/src/widgets/collections/geomap/GeoMapProperties.tsx b/apps/client/src/widgets/collections/geomap/GeoMapProperties.tsxindex c4af8c096d..31aaf623e5 100644--- a/apps/client/src/widgets/collections/geomap/GeoMapProperties.tsx+++ b/apps/client/src/widgets/collections/geomap/GeoMapProperties.tsx@@ -38,6 +38,16 @@ interface BackgroundOption {unavailable?: string;}+/**+ * The media types of the image notes a map can be drawn over: MapLibre decodes these, and refuses+ * SVG. `image/jpg` is not a registered type, but it is how Trilium stores a JPEG it compressed.+ */+const MAP_IMAGE_MIMES = [ "image/png", "image/jpeg", "image/jpg", "image/webp", "image/gif" ];++/** The search query the image picker is limited to. */+const IMAGE_NOTE_FILTER = `note.type = image AND (${+ MAP_IMAGE_MIMES.map((mime) => `note.mime = '${mime}'`).join(" OR ")})`;+/** The longest side, in pixels, that practically every graphics card can draw an image at. */const SAFE_IMAGE_SIDE = 4096;@@ -190,6 +200,7 @@ function ImageSettings({ note, image, imageNoteId, onPickImage }: {<NoteAutocompletenoteId={imageNoteId ?? undefined}placeholder={t("geo-map.image-note-placeholder")}+ filter={IMAGE_NOTE_FILTER}noteIdChanged={onPickImage}/></OptionCardSection>diff --git a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsxindex cebb262ecf..84bf9d917d 100644--- a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx+++ b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx@@ -650,6 +650,20 @@ describe("NoteAutocomplete's suggestion list", () => {expect(fullTextRow()).toBeUndefined();});+ it("asks for the notes its filter matches, the content search included", async () => {+ const input = await mount({ filter: "note.type = image" });+ await type(input, "al");+ expect(getNoteSuggestions).toHaveBeenLastCalledWith(+ "al", expect.objectContaining({ filter: "note.type = image" }));++ const row = fullTextRow();+ if (!row) throw new Error("no full-text search row rendered");+ await act(async () => { row.click(); });+ await settle();+ expect(getNoteSuggestions).toHaveBeenLastCalledWith(+ "al", expect.objectContaining({ filter: "note.type = image", fastSearch: false }));+ });+it("searches the content from its row, saying so until the results come", async () => {let finish: (rows: Suggestion[]) => void = () => {};const onChange = vi.fn();diff --git a/apps/client/src/widgets/react/NoteAutocomplete.tsx b/apps/client/src/widgets/react/NoteAutocomplete.tsxindex 606b2bcafb..0138fdf1a4 100644--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx@@ -48,6 +48,11 @@ export interface NoteAutocompleteProps {onBlur?: (newValue: string) => void;noteIdChanged?: (noteId: string) => void;noteId?: string;+ /**+ * A search query the suggested notes must also match, such as `note.type = image`, whatever is+ * typed. With an empty field, the notes it matches are suggested instead of the recent ones.+ */+ filter?: string;/** Shows the selected note without allowing a different one to be picked. */readOnly?: boolean;/** Keeps an enclosing form from submitting while the field is empty. */@@ -83,7 +88,7 @@ export interface NoteAutocompleteHandle {clear(): void;}-export default function NoteAutocomplete({ id, inputRef: externalInputRef, text, placeholder, container, containerStyle, opts, onChange, onTextChange, onKeyDown, onBlur, noteIdChanged, noteId, readOnly, required, tabIndex, handleRef, anchorRef, searchFooter, onHighlight }: NoteAutocompleteProps) {+export default function NoteAutocomplete({ id, inputRef: externalInputRef, text, placeholder, container, containerStyle, opts, onChange, onTextChange, onKeyDown, onBlur, noteIdChanged, noteId, filter, readOnly, required, tabIndex, handleRef, anchorRef, searchFooter, onHighlight }: NoteAutocompleteProps) {const inputRef = useSyncedRef<HTMLInputElement>(externalInputRef);const groupRef = useRef<HTMLDivElement>(null);const [ value, setValue ] = useState("");@@ -107,7 +112,8 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,const options = {allowCreatingNotes,allowJumpToSearchNotes: allowJumpToSearchNotes && !hasSearchFooter,- allowExternalLinks+ allowExternalLinks,+ filter};if (hasSearchFooter) {return getNoteSuggestions(query, includeContents ? { ...options, fastSearch: false } : options);@@ -124,7 +130,7 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,}}, [isCommandPalette, allowCreatingNotes, allowJumpToSearchNotes, allowExternalLinks, fullTextSearchCount,- hasSearchFooter, includeContents+ hasSearchFooter, includeContents, filter]);const schedule = useMemo(() => createSearchScheduler(), []);diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map/Image maps.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map/Image maps.htmlindex 76e3d74d9a..6c1932f5f5 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map/Image maps.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map/Image maps.html@@ -14,7 +14,9 @@from the <a class="reference-link" href="#root/_help_8YBEPzcpUgxw">Note menu</a> orfrom the settings menu of the <a class="reference-link" href="#root/_help_CssoWBu8I7jF">Collection Properties</a>.</li><li>Under <em>Background</em>, set <em>Type</em> to <em>Custom image</em>, then- choose the image note in <em>Image note</em>.</li>+ choose the image note in <em>Image note</em>. The list only offers image+ notes in a format the map can draw (PNG, JPEG, WebP or GIF), and shows+ all of them before anything is typed.</li></ol><p>The map then shows the image instead of the world map and opens with thewhole image in view. To go back to a world map, set <em>Type</em> back to <em>Vector & Raster</em>.diff --git a/docs/User Guide/User Guide/Collections/Geo Map/Image maps.md b/docs/User Guide/User Guide/Collections/Geo Map/Image maps.mdindex eedb6a3d92..dba36dff72 100644--- a/docs/User Guide/User Guide/Collections/Geo Map/Image maps.md+++ b/docs/User Guide/User Guide/Collections/Geo Map/Image maps.md@@ -6,7 +6,7 @@ An image map is a geo map drawn over an image of your own instead of a world map1. Upload the image as a note of its own, for example by dragging the file into the note tree. PNG, JPEG, WebP and GIF images are supported; SVG images are not.2. Create a geo map, or open an existing one.3. Open the <span class="tn-icon bx bx-cog"></span>_Geo map properties_ dialog from the <a class="reference-link" href="../../Basic%20Concepts%20and%20Features/UI%20Elements/Note%20menu.md">Note menu</a> or from the settings menu of the <a class="reference-link" href="../Collection%20Properties.md">Collection Properties</a>.-4. Under _Background_, set _Type_ to _Custom image_, then choose the image note in _Image note_.+4. Under _Background_, set _Type_ to _Custom image_, then choose the image note in _Image note_. The list only offers image notes in a format the map can draw (PNG, JPEG, WebP or GIF), and shows all of them before anything is typed.The map then shows the image instead of the world map and opens with the whole image in view. To go back to a world map, set _Type_ back to _Vector & Raster_. The image is switched off rather than removed: its relation is renamed to `~disabled:map:image`, so setting _Type_ to _Custom image_ again brings back the same image, with its coordinates. An image that the geo map inherits from a parent note or a template can only be switched off on the note that holds it.diff --git a/packages/trilium-core/src/routes/api/autocomplete.spec.ts b/packages/trilium-core/src/routes/api/autocomplete.spec.tsindex 06fd762afd..8a2e059aee 100644--- a/packages/trilium-core/src/routes/api/autocomplete.spec.ts+++ b/packages/trilium-core/src/routes/api/autocomplete.spec.ts@@ -157,6 +157,73 @@ describe("Autocomplete API (core)", () => {expect(res.status).toBe(400);});+ it("keeps the notes a filter matches, and lists its matches for an empty query", async () => {+ const stamp = `filtered${Date.now()}`;+ const { noteId: wanted } = await createTextNote(api, { title: `Wanted ${stamp}` });+ const { noteId: unwanted } = await createTextNote(api, { title: `Unwanted ${stamp}` });+ await api.put(`/api/notes/${wanted}/set-attribute`, {+ body: { type: "label", name: stamp, value: "" }+ });+ const noteIdsOf = (results: AutocompleteResult[]) =>+ results.map((result) => result.notePath.split("/").at(-1));++ const unfiltered = await api.get<AutocompleteResult[]>("/api/autocomplete", {+ query: { query: stamp }+ });+ expect(noteIdsOf(unfiltered.body))+ .toEqual(expect.arrayContaining([ wanted, unwanted ]));++ const filtered = await api.get<AutocompleteResult[]>("/api/autocomplete", {+ query: { query: stamp, filter: `#${stamp}` }+ });+ expect(filtered.status).toBe(200);+ expect(noteIdsOf(filtered.body)).toEqual([ wanted ]);++ // What is typed is searched apart from the filter, so text that is query syntax of its+ // own cannot loosen it.+ const syntax = await api.get<AutocompleteResult[]>("/api/autocomplete", {+ query: { query: `${stamp} OR`, filter: `#${stamp}` }+ });+ expect(noteIdsOf(syntax.body)).not.toContain(unwanted);++ const empty = await api.get<AutocompleteResult[]>("/api/autocomplete", {+ query: { query: "", filter: `#${stamp}` }+ });+ expect(noteIdsOf(empty.body)).toEqual([ wanted ]);+ });++ it("filters by note type and MIME type, grouped with OR", async () => {+ const stamp = `picture${Date.now()}`;+ const noteIdsOf = (results: AutocompleteResult[]) =>+ results.map((result) => result.notePath.split("/").at(-1));+ const create = async (mime: string) => {+ const res = await api.post<{ note: { noteId: string } }>(+ "/api/notes/root/children?target=into",+ { body: { title: `${stamp} ${mime}`, type: "image", mime, content: "" } });+ return res.body.note.noteId;+ };+ const png = await create("image/png");+ const webp = await create("image/webp");+ // How Trilium stores a JPEG it compressed itself.+ const jpg = await create("image/jpg");+ const svg = await create("image/svg+xml");+ const unfiltered = await api.get<AutocompleteResult[]>(+ "/api/autocomplete", { query: { query: stamp } });+ expect(noteIdsOf(unfiltered.body)).toContain(svg);++ const res = await api.get<AutocompleteResult[]>("/api/autocomplete", {+ query: {+ query: stamp,+ filter: "note.type = image AND (note.mime = 'image/png' "+ + "OR note.mime = 'image/jpeg' OR note.mime = 'image/jpg' "+ + "OR note.mime = 'image/webp' OR note.mime = 'image/gif')"+ }+ });+ const noteIds = noteIdsOf(res.body);+ expect(noteIds).toEqual(expect.arrayContaining([ png, webp, jpg ]));+ expect(noteIds).not.toContain(svg);+ });+it("400s when the query param is missing", async () => {const res = await api.get("/api/autocomplete");expect(res.status).toBe(400);diff --git a/packages/trilium-core/src/routes/api/autocomplete.ts b/packages/trilium-core/src/routes/api/autocomplete.tsindex 90fe351cfb..56d134476b 100644--- a/packages/trilium-core/src/routes/api/autocomplete.ts+++ b/packages/trilium-core/src/routes/api/autocomplete.ts@@ -20,6 +20,7 @@ function getAutocomplete(req: Request): AutocompleteResult[] {const query = (req.query.query || "").trim();const fastSearch = String(req.query.fastSearch).toLowerCase() !== "false";const limit = parseLimit(req.query.limit);+ const filter = parseFilter(req.query.filter);const activeNoteId = req.query.activeNoteId || "none";@@ -27,10 +28,13 @@ function getAutocomplete(req: Request): AutocompleteResult[] {const timestampStarted = Date.now();- if (query.length === 0 && typeof activeNoteId === "string") {+ if (query.length === 0 && filter) {+ // The recent notes would mostly not match, so the filter's own matches are listed instead.+ results = searchService.searchNotesForAutocomplete(filter, fastSearch, limit);+ } else if (query.length === 0 && typeof activeNoteId === "string") {results = getRecentNotes(activeNoteId, limit);} else {- results = searchService.searchNotesForAutocomplete(query, fastSearch, limit);+ results = searchService.searchNotesForAutocomplete(query, fastSearch, limit, filter);}const msTaken = Date.now() - timestampStarted;@@ -106,6 +110,20 @@ function parseLimit(limit: unknown): number | undefined {return parsed;}+/**+ * The `filter` query parameter: a search query every suggestion must also match, such as+ * `note.type = image`. Left out or blank, nothing is filtered.+ */+function parseFilter(filter: unknown): string | undefined {+ if (filter === undefined) {+ return undefined;+ }+ if (typeof filter !== "string") {+ throw new ValidationError("Invalid filter data type.");+ }+ return filter.trim() || undefined;+}+export default {getAutocomplete};diff --git a/packages/trilium-core/src/services/search/services/search.ts b/packages/trilium-core/src/services/search/services/search.tsindex 8613f23ea0..d0a3378f6d 100644--- a/packages/trilium-core/src/services/search/services/search.ts+++ b/packages/trilium-core/src/services/search/services/search.ts@@ -834,24 +834,50 @@ function extractAttributeSnippet(noteId: string, searchTokens: HighlightedTokenI// response, so the limit follows what a dropdown shows rather than what the query matched.const AUTOCOMPLETE_RESULT_LIMIT = 25;-/** Searches for the notes a dropdown lists, the first `limit` of them, at most {@link AUTOCOMPLETE_RESULT_LIMIT}. */-function searchNotesForAutocomplete(query: string, fastSearch: boolean = true, limit = AUTOCOMPLETE_RESULT_LIMIT) {+/**+ * Searches for the notes a dropdown lists, the first `limit` of them, at most+ * {@link AUTOCOMPLETE_RESULT_LIMIT}.+ *+ * @param filter a search query the notes must also match, such as `note.type = image`. It is run as+ * a search of its own rather than joined to `query`, so what is typed cannot change+ * what the filter means.+ */+function searchNotesForAutocomplete(+ query: string, fastSearch: boolean = true, limit = AUTOCOMPLETE_RESULT_LIMIT, filter?: string+) {+ const ancestorNoteId = hoistedNoteService.isHoistedInHiddenSubtree()+ ? "root"+ : hoistedNoteService.getHoistedNoteId();const searchContext = new SearchContext({fastSearch,includeArchivedNotes: false,includeHiddenNotes: true,fuzzyAttributeSearch: true,ignoreInternalAttributes: true,- ancestorNoteId: hoistedNoteService.isHoistedInHiddenSubtree() ? "root" : hoistedNoteService.getHoistedNoteId(),+ ancestorNoteId,// Typo tolerance is opt-in here: `searchEnableFuzzyMatching` covers quick search and the// search screen, and `searchAutocompleteFuzzy` decides it for the jump-to-note and note// selector dropdowns, which query on every keystroke.enableFuzzyMatching: optionService.getOptionBool("searchAutocompleteFuzzy"),- // Only the first `AUTOCOMPLETE_RESULT_LIMIT` results are ever read.- rankInTwoPasses: true+ // Only the first `AUTOCOMPLETE_RESULT_LIMIT` results are ever read, unless a filter drops+ // some of them first.+ rankInTwoPasses: !filter});- const trimmed = findResultsWithQuery(query, searchContext).slice(0, Math.min(limit, AUTOCOMPLETE_RESULT_LIMIT));+ let results = findResultsWithQuery(query, searchContext);+ if (filter) {+ const filterContext = new SearchContext({+ fastSearch,+ includeArchivedNotes: false,+ includeHiddenNotes: true,+ ancestorNoteId+ });+ const allowed = new Set(+ findResultsWithQuery(filter, filterContext).map((result) => result.noteId));+ results = results.filter((result) => allowed.has(result.noteId));+ }++ const trimmed = results.slice(0, Math.min(limit, AUTOCOMPLETE_RESULT_LIMIT));return buildSearchResultDetails(trimmed, searchContext);}