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
client/popup editor: extend scope to note attachments as well
Adorian Doran1df7e73a2ae5
diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.tsindex 597dcd4e20..def968541b 100644--- a/apps/client/src/components/app_context.ts+++ b/apps/client/src/components/app_context.ts@@ -148,7 +148,8 @@ export type CommandMappings = {showImportDialog: CommandData & { noteId: string };openNewNoteSplit: NoteCommandData;openInWindow: NoteCommandData;- openInPopup: CommandData & { noteIdOrPath: string; };+ /** Opens a note in the quick-edit popup. A `viewScope` carrying an `attachmentId` opens that attachment instead of the note itself. */+ openInPopup: CommandData & { noteIdOrPath: string; viewScope?: ViewScope; };openInTreePopup: CommandData & { noteIdOrPath: string; hoistedNoteId: string; };openNoteInNewTab: CommandData;openNoteInNewSplit: CommandData;diff --git a/apps/client/src/menus/link_context_menu.ts b/apps/client/src/menus/link_context_menu.tsindex 9e431d8a84..d0f23d3ddd 100644--- a/apps/client/src/menus/link_context_menu.ts+++ b/apps/client/src/menus/link_context_menu.ts@@ -45,7 +45,7 @@ function handleLinkContextMenuItem(command: string | undefined, e: ContextMenuEvappContext.triggerCommand("openInWindow", { notePath, hoistedNoteId, viewScope });return true;} else if (command === "openNoteInPopup") {- appContext.triggerCommand("openInPopup", { noteIdOrPath: notePath });+ appContext.triggerCommand("openInPopup", { noteIdOrPath: notePath, viewScope });return true;}diff --git a/apps/client/src/services/link.spec.ts b/apps/client/src/services/link.spec.tsindex 2f0811c2e9..c8a7c10c0a 100644--- a/apps/client/src/services/link.spec.ts+++ b/apps/client/src/services/link.spec.ts@@ -382,7 +382,15 @@ describe("goToLinkExt", () => {it("opens in a popup when the url requests it", () => {goToLinkExt(leftClick(), "#root/aaaaaaaaaaaa?popup=1");- expect(triggerCommand).toHaveBeenCalledWith("openInPopup", { noteIdOrPath: "root/aaaaaaaaaaaa" });+ expect(triggerCommand).toHaveBeenCalledWith("openInPopup", { noteIdOrPath: "root/aaaaaaaaaaaa", viewScope: { viewMode: "default" } });+ });++ it("passes the attachment view scope along when opening in a popup", () => {+ goToLinkExt(leftClick(), "#root/aaaaaaaaaaaa?popup=1&viewMode=attachments&attachmentId=bbbbbbbbbbbb");+ expect(triggerCommand).toHaveBeenCalledWith("openInPopup", {+ noteIdOrPath: "root/aaaaaaaaaaaa",+ viewScope: { viewMode: "attachments", attachmentId: "bbbbbbbbbbbb" }+ });});it("opens in a new window on shift+left-click", () => {@@ -731,7 +739,7 @@ describe("module-level click handlers", () => {const $a = $("<a href='#root/aaaaaaaaaaaa'>link</a>");$("body").append($a);$a.trigger($.Event("contextmenu", { button: 2 }));- expect(triggerCommand).toHaveBeenCalledWith("openInPopup", { noteIdOrPath: "root/aaaaaaaaaaaa" });+ expect(triggerCommand).toHaveBeenCalledWith("openInPopup", { noteIdOrPath: "root/aaaaaaaaaaaa", viewScope: { viewMode: "default" } });spy.mockRestore();$a.remove();});diff --git a/apps/client/src/services/link.ts b/apps/client/src/services/link.tsindex 72c92405f9..24cac03e2d 100644--- a/apps/client/src/services/link.ts+++ b/apps/client/src/services/link.ts@@ -348,7 +348,7 @@ export function goToLinkExt(evt: MouseEvent | JQuery.ClickEvent | JQuery.MouseDoif (notePath) {if (isLeftClick && openInPopup) {- appContext.triggerCommand("openInPopup", { noteIdOrPath: notePath });+ appContext.triggerCommand("openInPopup", { noteIdOrPath: notePath, viewScope });} else if (openInNewWindow) {appContext.triggerCommand("openInWindow", { notePath, viewScope });} else if (openInNewTab) {@@ -426,7 +426,7 @@ function linkContextMenu(e: PointerEvent) {}if (utils.isCtrlKey(e) && e.button === 2) {- appContext.triggerCommand("openInPopup", { noteIdOrPath: notePath });+ appContext.triggerCommand("openInPopup", { noteIdOrPath: notePath, viewScope });e.preventDefault();return;}diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 6e59b94bd9..f733bd7ad0 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3087,7 +3087,8 @@"set-custom-color": "Set custom note color"},"popup-editor": {- "maximize": "Switch to full editor"+ "maximize": "Switch to full editor",+ "attachment_title": "Attachment: {{title}}"},"server": {"unknown_http_error_title": "Communication error with the server",diff --git a/apps/client/src/widgets/dialogs/PopupEditor.css b/apps/client/src/widgets/dialogs/PopupEditor.cssindex a81573e6a7..951c2fed35 100644--- a/apps/client/src/widgets/dialogs/PopupEditor.css+++ b/apps/client/src/widgets/dialogs/PopupEditor.css@@ -57,6 +57,18 @@ body.mobile .modal.popup-editor-dialog .modal-dialog {align-items: center;}+/* An attachment has no editable title, so its header is plain text aligned like the note title. */+.modal.popup-editor-dialog .modal-header .title-row.attachment-title-row {+ gap: 8px;++ .attachment-title {+ overflow: hidden;+ font-size: 1em;+ text-overflow: ellipsis;+ white-space: nowrap;+ }+}+.modal.popup-editor-dialog .modal-body {padding: 0;height: 75vh;diff --git a/apps/client/src/widgets/dialogs/PopupEditor.tsx b/apps/client/src/widgets/dialogs/PopupEditor.tsxindex 8a39300da9..41e4f301af 100644--- a/apps/client/src/widgets/dialogs/PopupEditor.tsx+++ b/apps/client/src/widgets/dialogs/PopupEditor.tsx@@ -19,6 +19,7 @@ import NoteTitleWidget from "../note_title";import NoteDetail from "../NoteDetail";import PromotedAttributes from "../PromotedAttributes";import { useContainedLinkNavigation, useNoteContext, useNoteLabel, useTriliumEvent } from "../react/hooks";+import Icon from "../react/Icon";import Modal from "../react/Modal";import { NoteContextContext, ParentComponent } from "../react/react_utils";import ReadOnlyNoteInfoBar from "../ReadOnlyNoteInfoBar";@@ -40,7 +41,7 @@ export default function PopupEditor() {return baseItems.filter(item => !POPUP_HIDDEN_FLOATING_BUTTONS.includes(item));}, [ isMobile ]);- useTriliumEvent("openInPopup", async ({ noteIdOrPath }) => {+ useTriliumEvent("openInPopup", async ({ noteIdOrPath, viewScope }) => {const noteId = tree.getNoteIdAndParentIdFromUrl(noteIdOrPath);if (!noteId.noteId) return;const note = await froca.getNote(noteId.noteId);@@ -59,7 +60,9 @@ export default function PopupEditor() {await noteContext.setNote(noteIdOrPath, {viewScope: {// Override auto-readonly notes to be editable, but respect user's choice to have a read-only note.- readOnlyTemporarilyDisabled: !hasUserSetNoteReadOnly+ readOnlyTemporarilyDisabled: !hasUserSetNoteReadOnly,+ // A view scope from the caller (e.g. an attachment link) decides what is actually displayed.+ ...viewScope},keepActiveDialog: true});@@ -110,8 +113,13 @@ export default function PopupEditor() {title: t("popup-editor.maximize"),onClick: async () => {if (!noteContext.noteId) return;- const { noteId, hoistedNoteId } = noteContext;- await appContext.tabManager.openInNewTab(noteId, hoistedNoteId, true);+ const { noteId, hoistedNoteId, viewScope } = noteContext;+ if (viewScope?.attachmentId) {+ // Keep showing the attachment rather than falling back to its owning note.+ await appContext.tabManager.openContextWithNote(noteId, { hoistedNoteId, viewScope, activate: true });+ } else {+ await appContext.tabManager.openInNewTab(noteId, hoistedNoteId, true);+ }setShown(false);}}]}@@ -153,6 +161,12 @@ export function DialogWrapper({ children }: { children: ComponentChildren }) {}export function TitleRow() {+ const { viewScope } = useNoteContext();++ if (viewScope?.attachmentId) {+ return <AttachmentTitleRow attachmentId={viewScope.attachmentId} />;+ }+return (<div className="title-row"><NoteIcon />@@ -161,3 +175,30 @@ export function TitleRow() {</div>);}++/**+ * The header shown when the popup displays an attachment instead of a note. Attachments are not+ * editable in place, so the title is plain read-only text, prefixed to make clear that what is+ * displayed is an attachment of the note and not the note itself.+ */+function AttachmentTitleRow({ attachmentId }: { attachmentId: string }) {+ const [ title, setTitle ] = useState<string>();++ function refresh() {+ froca.getAttachment(attachmentId).then(attachment => setTitle(attachment?.title));+ }++ useEffect(refresh, [ attachmentId ]);+ useTriliumEvent("entitiesReloaded", ({ loadResults }) => {+ if (loadResults.getAttachmentRows().some(row => row.attachmentId === attachmentId)) {+ refresh();+ }+ });++ return (+ <div className="title-row attachment-title-row">+ <Icon icon="bx bx-paperclip" />+ <span className="attachment-title">{t("popup-editor.attachment_title", { title })}</span>+ </div>+ );+}diff --git a/apps/client/src/widgets/dialogs/TreePopupEditor.tsx b/apps/client/src/widgets/dialogs/TreePopupEditor.tsxindex 78d98e75e8..411d7b55b5 100644--- a/apps/client/src/widgets/dialogs/TreePopupEditor.tsx+++ b/apps/client/src/widgets/dialogs/TreePopupEditor.tsx@@ -71,7 +71,7 @@ export default function TreePopupEditor() {if (notePath.split("/").includes(noteContext.hoistedNoteId)) {void noteContext.setNote(notePath, { viewScope, keepActiveDialog: true });} else {- void appContext.triggerCommand("openInPopup", { noteIdOrPath: notePath });+ void appContext.triggerCommand("openInPopup", { noteIdOrPath: notePath, viewScope });}}, [ noteContext ]));