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
fix(share): keep a shared view's stored config on the page
Elian Doran4570bafbdef4
diff --git a/apps/client/src/services/froca-interface.ts b/apps/client/src/services/froca-interface.tsindex fe4d0b772e..58631115cb 100644--- a/apps/client/src/services/froca-interface.ts+++ b/apps/client/src/services/froca-interface.ts@@ -18,6 +18,15 @@ export interface NoteMapFilters {includeRelations: string[];}+/** An attachment a view saves on a note, such as its stored config. */+export interface SavedAttachment {+ role: string;+ title: string;+ mime: string;+ content: string;+ position: number;+}+/** The rows of notes with their branches and attributes, which `froca.addResp()` takes. */export interface SubtreeResponse {notes: FNoteRow[];@@ -44,6 +53,9 @@ export interface FrocaSource {getRelationMap(relationMapNoteId: string, noteIds: string[]): Promise<RelationMapPostResponse>;/** The frontend bundle of the script note `noteId` and its modules, or nothing if it has none. */getScriptBundle(noteId: string): Promise<Bundle | undefined>;+ /** Saves `attachment` on the note, in place of the one of the same title if there is one. */+ saveAttachment(noteId: string, attachment: SavedAttachment): Promise<void>;+ removeAttachment(attachmentId: string): Promise<void>;}export interface Froca {@@ -67,4 +79,6 @@ export interface Froca {getNoteMap(mapRootNoteId: string, mapType: "tree" | "link", filters: NoteMapFilters): Promise<NoteMapPostResponse>;getRelationMap(relationMapNoteId: string, noteIds: string[]): Promise<RelationMapPostResponse>;getScriptBundle(noteId: string): Promise<Bundle | undefined>;+ saveAttachment(noteId: string, attachment: SavedAttachment): Promise<void>;+ removeAttachment(attachmentId: string): Promise<void>;}diff --git a/apps/client/src/services/froca.ts b/apps/client/src/services/froca.tsindex f326750c5a..1b9ab69060 100644--- a/apps/client/src/services/froca.ts+++ b/apps/client/src/services/froca.ts@@ -6,7 +6,7 @@ import FBlob, { type FBlobRow } from "../entities/fblob.js";import FBranch, { type FBranchRow } from "../entities/fbranch.js";import FNote, { type FNoteRow } from "../entities/fnote.js";import type { Bundle } from "./bundle.js";-import type { Froca, FrocaSource, NoteMapFilters, SubtreeResponse } from "./froca-interface.js";+import type { Froca, FrocaSource, NoteMapFilters, SavedAttachment, SubtreeResponse } from "./froca-interface.js";import server from "./server.js";import { isPreAuthScreen } from "./utils.js";@@ -416,6 +416,14 @@ class FrocaImpl implements Froca {return this.source.getScriptBundle(noteId);}+ saveAttachment(noteId: string, attachment: SavedAttachment) {+ return this.source.saveAttachment(noteId, attachment);+ }++ removeAttachment(attachmentId: string) {+ return this.source.removeAttachment(attachmentId);+ }+async getBlob(entityType: string, entityId: string): Promise<FBlob | null> {// I'm not sure why we're not using blobIds directly, it would save us this composite key ...// perhaps one benefit is that we're always requesting the latest blob, not relying on perhaps faulty/slow@@ -457,7 +465,13 @@ const SERVER_SOURCE: FrocaSource = {server.post<NoteMapPostResponse>(`note-map/${mapRootNoteId}/${mapType}`, filters),getRelationMap: (relationMapNoteId, noteIds) =>server.post<RelationMapPostResponse>("relation-map", { noteIds, relationMapNoteId }),- getScriptBundle: (noteId) => server.postWithSilentInternalServerError<Bundle>(`script/bundle/${noteId}`)+ getScriptBundle: (noteId) => server.postWithSilentInternalServerError<Bundle>(`script/bundle/${noteId}`),+ saveAttachment: async (noteId, attachment) => {+ await server.post(`notes/${noteId}/attachments?matchBy=title`, attachment);+ },+ removeAttachment: async (attachmentId) => {+ await server.remove(`attachments/${attachmentId}`);+ }};const froca = new FrocaImpl();diff --git a/apps/client/src/widgets/collections/view_mode_storage.ts b/apps/client/src/widgets/collections/view_mode_storage.tsindex a3eba2a166..dcbc7a50f9 100644--- a/apps/client/src/widgets/collections/view_mode_storage.ts+++ b/apps/client/src/widgets/collections/view_mode_storage.ts@@ -1,5 +1,5 @@import type FNote from "../../entities/fnote";-import server from "../../services/server";+import froca from "../../services/froca";import { ViewTypeOptions } from "../collections/interface";const ATTACHMENT_ROLE = "viewConfig";@@ -61,10 +61,9 @@ export default class ViewModeStorage<T extends object> {// Caught before the queue is extended, so a write that fails does not hold back the next// one; the failure is still reported to whoever asked for this write.- const url = `notes/${this.note.noteId}/attachments?matchBy=title`;this.lastWrite = this.lastWrite.catch(() => {})- .then(() => server.post(url, payload));+ .then(() => froca.saveAttachment(this.note.noteId, payload));try {await this.lastWrite;@@ -121,7 +120,7 @@ export default class ViewModeStorage<T extends object> {if (existingAttachments.length > 1) {// Clean up duplicates.- await Promise.all(existingAttachments.slice(1).map(async a => await server.remove(`attachments/${a.attachmentId}`)));+ await Promise.all(existingAttachments.slice(1).map((a) => froca.removeAttachment(a.attachmentId)));}const attachment = existingAttachments[0];diff --git a/packages/share-theme/src/content/share_froca_source.ts b/packages/share-theme/src/content/share_froca_source.tsindex d7dcbbec4d..5feb1a6ad9 100644--- a/packages/share-theme/src/content/share_froca_source.ts+++ b/packages/share-theme/src/content/share_froca_source.ts@@ -27,6 +27,9 @@ export function createShareFrocaSource(links: Record<string, string>): FrocaSourconst { results } = await getJson<{ results: { noteId: string }[] }>(`api/notes?${params}`);return results.map((result) => result.noteId);},+ // A visitor cannot change notes, so what a view saves holds only while the page is open.+ saveAttachment: async () => {},+ removeAttachment: async () => {},getNoteMap: (mapRootNoteId, mapType, { excludeRelations, includeRelations }) => {const params = new URLSearchParams([...excludeRelations.map((name) => [ "excludeRelation", name ]),