Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/include notes: disable context menu commands that may cause modification while the host note is read-only

Adorian Dorand81c1b5d19a4
diff --git a/apps/client/src/menus/link_context_menu.spec.ts b/apps/client/src/menus/link_context_menu.spec.ts
index 3ddddc59eb..21547b1dc3 100644
--- a/apps/client/src/menus/link_context_menu.spec.ts
+++ b/apps/client/src/menus/link_context_menu.spec.ts
@@ -15,6 +15,7 @@ const mocks = vi.hoisted(() => ({
activeNtxId: "ntx-active" as string | null,
/** False when no tab is open at all, which leaves both the hoisting and the split unresolvable. */
hasActiveContext: true,
+ noteContexts: [] as { ntxId: string; isReadOnly: () => Promise<boolean> }[],
getAttachmentOfNote: vi.fn(),
getAttachmentActionGroups: vi.fn(),
getTextEditorContaining: vi.fn(),
@@ -60,7 +61,8 @@ vi.mock("../components/app_context", () => ({
: undefined,
getNoteContextById: () => ({
getMainContext: () => ({ getSubContexts: () => mocks.subContexts })
- })
+ }),
+ getNoteContexts: () => mocks.noteContexts
}
}
}));
@@ -85,6 +87,7 @@ beforeEach(() => {
mocks.subContexts = [ { ntxId: "ntx-first" }, { ntxId: "ntx-last" } ];
mocks.activeNtxId = "ntx-active";
mocks.hasActiveContext = true;
+ mocks.noteContexts = [];
mocks.getAttachmentOfNote.mockResolvedValue(null);
});
@@ -257,7 +260,9 @@ describe("openContextMenu", () => {
await linkContextMenu.openContextMenu("root/n1", contextMenuEvent(), VIEW_SCOPE);
expect(mocks.getAttachmentOfNote).toHaveBeenCalledWith("n1", "att-1");
- expect(mocks.getAttachmentActionGroups).toHaveBeenCalledWith(attachment);
+ expect(mocks.getAttachmentActionGroups).toHaveBeenCalledWith(attachment, {
+ isReadOnly: false
+ });
const { items } = mocks.show.mock.calls[0][0];
expect(items.slice(4)).toMatchObject([
{ kind: "separator" },
@@ -270,6 +275,28 @@ describe("openContextMenu", () => {
expect(download).toHaveBeenCalledOnce();
});
+ it("asks for the actions of a read-only note in a pane showing one", async () => {
+ const attachment = { attachmentId: "att-1" };
+ mocks.getAttachmentOfNote.mockResolvedValue(attachment);
+ mocks.getAttachmentActionGroups.mockReturnValue([]);
+ mocks.noteContexts = [
+ { ntxId: "ntx-read-only", isReadOnly: async () => true },
+ { ntxId: "ntx-editable", isReadOnly: async () => false }
+ ];
+ const event = contextMenuEvent(document.createElement("div"));
+
+ for (const ntxId of [ "ntx-read-only", "ntx-editable", null ]) {
+ mocks.getClosestNtxId.mockReturnValue(ntxId);
+ await linkContextMenu.openContextMenu("root/n1", event, VIEW_SCOPE);
+ }
+
+ expect(mocks.getAttachmentActionGroups.mock.calls).toEqual([
+ [ attachment, { isReadOnly: true } ],
+ [ attachment, { isReadOnly: false } ],
+ [ attachment, { isReadOnly: false } ]
+ ]);
+ });
+
it("ends with converting an attachment link to an embed, only in a note being edited", async () => {
const execute = vi.fn();
const attachment = { attachmentId: "att-1" };
diff --git a/apps/client/src/menus/link_context_menu.ts b/apps/client/src/menus/link_context_menu.ts
index 3be00de9ef..6362415f5f 100644
--- a/apps/client/src/menus/link_context_menu.ts
+++ b/apps/client/src/menus/link_context_menu.ts
@@ -151,7 +151,8 @@ function handleLinkContextMenuItem(command: string | undefined, e: LinkMenuOrigi
/**
* The actions on the attachment a link points to, each group after a separator. A menu opened on
- * an embed of it has the commands of the embed after the first group.
+ * an embed of it has the commands of the embed after the first group. A menu opened in a note shown
+ * read-only has only the actions that leave the attachment unchanged.
*/
async function getAttachmentItems(
noteId: string,
@@ -161,15 +162,17 @@ async function getAttachmentItems(
embed: MenuEmbed | null
): Promise<MenuItem<CommandNames>[]> {
// Imported on demand: `attachment_actions` imports `link`, which imports this module.
- const [ attachment, { getAttachmentActionGroups } ] = await Promise.all([
+ const [ attachment, { getAttachmentActionGroups }, isReadOnly ] = await Promise.all([
froca.getAttachmentOfNote(noteId, attachmentId),
- import("../services/attachment_actions.js")
+ import("../services/attachment_actions.js"),
+ isInReadOnlyNote(getTarget(e))
]);
if (!attachment) {
return [];
}
- const [ firstGroup = [], ...otherGroups ] = getAttachmentActionGroups(attachment)
+ const actionGroups = getAttachmentActionGroups(attachment, { isReadOnly });
+ const [ firstGroup = [], ...otherGroups ] = actionGroups
.map((group) => group.map((action): MenuItem<CommandNames> => ({
title: action.title,
uiIcon: action.icon,
@@ -307,6 +310,19 @@ function getTarget(e: LinkMenuOrigin) {
return e.target instanceof Element ? e.target : null;
}
+/**
+ * Whether `element` is in a split pane whose note is shown read-only, by its `#readOnly` label or
+ * by its size.
+ */
+async function isInReadOnlyNote(element: Element | null) {
+ const ntxId = element instanceof HTMLElement ? getClosestNtxId(element) : null;
+ const noteContext = ntxId
+ ? appContext.tabManager.getNoteContexts().find((context) => context.ntxId === ntxId)
+ : null;
+
+ return (await noteContext?.isReadOnly()) ?? false;
+}
+
/** The text editor containing `element`, or `null` when there is none or it is read-only. */
async function getEditingTextEditor(element: Element | null) {
// Checked first: a note shown read-only has no editor, and asking for one waits for a timeout.
diff --git a/apps/client/src/services/attachment_actions.spec.ts b/apps/client/src/services/attachment_actions.spec.ts
index 6f70cd8449..162fd24af9 100644
--- a/apps/client/src/services/attachment_actions.spec.ts
+++ b/apps/client/src/services/attachment_actions.spec.ts
@@ -137,6 +137,18 @@ describe("getAttachmentActionGroups", () => {
expect(copyReference).toHaveBeenCalledOnce();
});
+ it("offers only the actions leaving the attachment unchanged for a read-only note", () => {
+ const image = attachmentOf("image", "image/png");
+ const groups = getAttachmentActionGroups(image, { isReadOnly: true });
+
+ expect(groups.map((group) => group.map((action) => action.title))).toEqual([ [
+ "attachments_actions.open_externally",
+ "attachments_actions.open_custom",
+ "attachments_actions.download",
+ "ocr.view_extracted_text"
+ ] ]);
+ });
+
it("renames, deletes and converts only once the user has agreed", async () => {
const actions = actionsOf(attachmentOf("file"));
mocks.dialog.prompt.mockResolvedValueOnce(" ").mockResolvedValueOnce("Renamed");
diff --git a/apps/client/src/services/attachment_actions.ts b/apps/client/src/services/attachment_actions.ts
index 51a9d19efe..21680e41c2 100644
--- a/apps/client/src/services/attachment_actions.ts
+++ b/apps/client/src/services/attachment_actions.ts
@@ -28,6 +28,8 @@ export interface AttachmentAction {
export interface AttachmentActionOptions {
/** Copies a reference to the attachment. Without it, the actions offer no copy. */
copyReference?: () => void | Promise<void>;
+ /** Leaves out the actions that change the attachment, for a note shown read-only. */
+ isReadOnly?: boolean;
}
/**
@@ -36,39 +38,54 @@ export interface AttachmentActionOptions {
*/
export function getAttachmentActionGroups(
attachment: FAttachment,
- { copyReference }: AttachmentActionOptions = {}
+ { copyReference, isReadOnly = false }: AttachmentActionOptions = {}
): AttachmentAction[][] {
+ const viewActions = getViewActions(attachment, copyReference);
+ return isReadOnly ? [ viewActions ] : [ viewActions, ...getEditActionGroups(attachment) ];
+}
+
+/** The actions that leave the attachment unchanged. */
+function getViewActions(
+ attachment: FAttachment,
+ copyReference: AttachmentActionOptions["copyReference"]
+): AttachmentAction[] {
+ const { attachmentId, mime } = attachment;
+
+ return [
+ getOpenExternallyAction(attachment),
+ {
+ title: t("attachments_actions.open_custom"),
+ tooltip: t("attachments_actions.open_custom_title"),
+ icon: "bx bx-customize",
+ disabledReason: utils.isElectron()
+ ? undefined
+ : t("attachments_actions.open_custom_client_only"),
+ run: () => open.openAttachmentCustom(attachmentId, mime)
+ },
+ getDownloadAction(attachment),
+ ...(copyReference ? [ {
+ title: t("attachments_actions.copy_link_to_clipboard"),
+ icon: "bx bx-copy",
+ run: copyReference
+ } ] : []),
+ ...(supportsOcr(attachment) ? [ {
+ title: t("ocr.view_extracted_text"),
+ icon: "bx bx-text",
+ run: () => {
+ appContext.triggerCommand("showOcrTextDialog", {
+ textUrl: `ocr/attachments/${attachmentId}/text`,
+ processUrl: `ocr/process-attachment/${attachmentId}`
+ });
+ }
+ } ] : [])
+ ];
+}
+
+/** The actions that change, delete or convert the attachment, in their groups. */
+function getEditActionGroups(attachment: FAttachment): AttachmentAction[][] {
const { attachmentId, mime } = attachment;
return [
- [
- getOpenExternallyAction(attachment),
- {
- title: t("attachments_actions.open_custom"),
- tooltip: t("attachments_actions.open_custom_title"),
- icon: "bx bx-customize",
- disabledReason: utils.isElectron()
- ? undefined
- : t("attachments_actions.open_custom_client_only"),
- run: () => open.openAttachmentCustom(attachmentId, mime)
- },
- getDownloadAction(attachment),
- ...(copyReference ? [ {
- title: t("attachments_actions.copy_link_to_clipboard"),
- icon: "bx bx-copy",
- run: copyReference
- } ] : []),
- ...(supportsOcr(attachment) ? [ {
- title: t("ocr.view_extracted_text"),
- icon: "bx bx-text",
- run: () => {
- appContext.triggerCommand("showOcrTextDialog", {
- textUrl: `ocr/attachments/${attachmentId}/text`,
- processUrl: `ocr/process-attachment/${attachmentId}`
- });
- }
- } ] : [])
- ],
[
{
title: t("attachments_actions.upload_new_revision"),