Skip to content

Build your personal knowledge base with Trilium Notes

feat(client): badge marking an office document as a preview

Elian Dorand954d34a0cc7
diff --git a/apps/client/src/stylesheets/theme-next-dark.css b/apps/client/src/stylesheets/theme-next-dark.css
index 1c7364a01a..37622220e9 100644
--- a/apps/client/src/stylesheets/theme-next-dark.css
+++ b/apps/client/src/stylesheets/theme-next-dark.css
@@ -221,6 +221,7 @@
--badge-temporaraily-editable-background-color: #297331;
--badge-read-only-background-color: #af4340;
+ --badge-office-preview-background-color: #3f5470;
--badge-share-background-color: #4d4d4d;
--badge-snippet-background-color: #4d4d4d;
--badge-clipped-note-background-color: #295773;
diff --git a/apps/client/src/stylesheets/theme-next-light.css b/apps/client/src/stylesheets/theme-next-light.css
index f04ce136fe..af6430cc16 100644
--- a/apps/client/src/stylesheets/theme-next-light.css
+++ b/apps/client/src/stylesheets/theme-next-light.css
@@ -213,6 +213,7 @@
--badge-temporaraily-editable-background-color: #35a64c;
--badge-read-only-background-color: #c8302c;
+ --badge-office-preview-background-color: #566d87;
--badge-share-background-color: #6b6b6b;
--badge-snippet-background-color: #6b6b6b;
--badge-clipped-note-background-color: #2284c0;
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index f8493e80a9..29ace2cbe8 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -3518,6 +3518,8 @@
"read_only_auto_description": "This note was set automatically to read-only mode for performance reasons. This automatic limit is adjustable from settings.\n\nClick to edit it temporarily.",
"read_only_temporarily_disabled": "Temporarily editable",
"read_only_temporarily_disabled_description": "This note is currently editable, but it is normally read-only. The note will go back to being read-only as soon as you navigate to another note.\n\nClick to re-enable read-only mode.",
+ "office_preview": "Preview",
+ "office_preview_description": "This document is shown as a read-only preview, so it cannot be edited here.\n\nTo change it, download the file, edit it in an external application and upload it back as a new revision.",
"shared_publicly": "Shared publicly",
"shared_locally": "Shared locally",
"shared_copy_to_clipboard": "Copy link to clipboard",
diff --git a/apps/client/src/widgets/layout/NoteBadges.css b/apps/client/src/widgets/layout/NoteBadges.css
index 80f96a90f7..aa8d8d6f96 100644
--- a/apps/client/src/widgets/layout/NoteBadges.css
+++ b/apps/client/src/widgets/layout/NoteBadges.css
@@ -18,6 +18,7 @@
.ext-badge {
&.temporarily-editable-badge {--color: var(--badge-temporaraily-editable-background-color)}
&.read-only-badge {--color: var(--badge-read-only-background-color);}
+ &.office-preview-badge {--color: var(--badge-office-preview-background-color);}
&.share-badge {--color: var(--badge-share-background-color);}
&.clipped-note-badge {--color: var(--badge-clipped-note-background-color);}
&.doc-url-badge {--color: var(--badge-doc-url-background-color);}
diff --git a/apps/client/src/widgets/layout/NoteBadges.spec.tsx b/apps/client/src/widgets/layout/NoteBadges.spec.tsx
new file mode 100644
index 0000000000..50d6f7758f
--- /dev/null
+++ b/apps/client/src/widgets/layout/NoteBadges.spec.tsx
@@ -0,0 +1,54 @@
+import { describe, expect, it, vi } from "vitest";
+
+import FNote from "../../entities/fnote";
+import { ViewScope } from "../../services/link";
+
+// The badge reads the note being shown and the view it is shown in, neither of which a rendered
+// widget brings with it.
+const shownNote = vi.hoisted(() => ({ current: null as FNote | null, viewScope: undefined as ViewScope | undefined }));
+vi.mock("../react/hooks", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("../react/hooks")>()),
+ useNoteContext: () => ({
+ note: shownNote.current,
+ viewScope: shownNote.viewScope
+ })
+}));
+
+// i18next is not initialized under test, where t() answers with an empty string; echoing the key
+// back keeps the assertions about which wording the badge picks.
+vi.mock("../../services/i18n", () => ({
+ t: (key: string) => key
+}));
+
+import { buildNote } from "../../test/easy-froca";
+import { renderInto } from "../../test/render";
+import { OfficePreviewBadge } from "./NoteBadges";
+
+const DOCX_MIME = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
+
+describe("OfficePreviewBadge", () => {
+ it("marks a file note that the office preview renders", () => {
+ const badge = renderBadge({ type: "file", mime: DOCX_MIME });
+
+ expect(badge?.textContent).toBe("breadcrumb_badges.office_preview");
+ expect(badge?.querySelector(".bx-show")).not.toBeNull();
+ });
+
+ it("stays away from files without an office preview, and from office MIME types on other note types", () => {
+ expect(renderBadge({ type: "file", mime: "application/pdf" })).toBeNull();
+ // A text note keeps its own editor, whatever MIME it carries.
+ expect(renderBadge({ type: "text", mime: DOCX_MIME })).toBeNull();
+ });
+
+ it("stays away from views that show something other than the preview", () => {
+ expect(renderBadge({ type: "file", mime: DOCX_MIME }, { viewMode: "attachments" })).toBeNull();
+ // A context without a view mode is on the note itself, which is where the preview renders.
+ expect(renderBadge({ type: "file", mime: DOCX_MIME }, undefined)).not.toBeNull();
+ });
+
+ function renderBadge(noteDef: { type: "file" | "text", mime: string }, viewScope: ViewScope | undefined = { viewMode: "default" }) {
+ shownNote.current = buildNote({ title: "Document", ...noteDef });
+ shownNote.viewScope = viewScope;
+ return renderInto(<OfficePreviewBadge />).querySelector(".office-preview-badge");
+ }
+});
diff --git a/apps/client/src/widgets/layout/NoteBadges.tsx b/apps/client/src/widgets/layout/NoteBadges.tsx
index 2f59f81b64..78e21eccac 100644
--- a/apps/client/src/widgets/layout/NoteBadges.tsx
+++ b/apps/client/src/widgets/layout/NoteBadges.tsx
@@ -1,5 +1,6 @@
import "./NoteBadges.css";
+import { isOfficeMimeType } from "@triliumnext/commons";
import { clsx } from "clsx";
import { useEffect, useState } from "preact/hooks";
@@ -8,7 +9,7 @@ import { t } from "../../services/i18n";
import { goToLinkExt } from "../../services/link";
import { Badge, BadgeWithDropdown } from "../react/Badge";
import { FormDropdownDivider, FormListItem } from "../react/FormList";
-import { useGetContextDataFrom, useIsNoteReadOnly, useNoteContext, useNoteLabel, useNoteLabelBoolean } from "../react/hooks";
+import { useGetContextDataFrom, useIsNoteReadOnly, useNoteContext, useNoteLabel, useNoteLabelBoolean, useNoteProperty } from "../react/hooks";
import { useShareState } from "../ribbon/BasicPropertiesTab";
import { useShareInfo } from "../shared_info";
import { ActiveContentBadges } from "./ActiveContentBadges";
@@ -19,6 +20,7 @@ export default function NoteBadges() {
<div className="note-badges">
<SaveStatusBadge />
<ReadOnlyBadge />
+ <OfficePreviewBadge />
<ShareBadge />
<ClippedNoteBadge />
<ExecuteBadge />
@@ -52,6 +54,31 @@ function ReadOnlyBadge() {
}
}
+/**
+ * Marks a file note that `OfficePreview` renders as HTML (DOCX/XLSX/PPTX, ODT/ODS/ODP, RTF and
+ * EPUB). The preview is a rendering of the document, not an editor, so the badge tells the reader
+ * why nothing can be typed into it. Unlike `ReadOnlyBadge` there is nothing to unlock.
+ */
+export function OfficePreviewBadge() {
+ const { note, viewScope } = useNoteContext();
+ const type = useNoteProperty(note, "type");
+ const mime = useNoteProperty(note, "mime");
+
+ const isPreviewShown = !viewScope?.viewMode || viewScope.viewMode === "default";
+ if (type !== "file" || !isOfficeMimeType(mime) || !isPreviewShown) {
+ return;
+ }
+
+ return (
+ <Badge
+ className="office-preview-badge"
+ icon="bx bx-show"
+ text={t("breadcrumb_badges.office_preview")}
+ tooltip={t("breadcrumb_badges.office_preview_description")}
+ />
+ );
+}
+
function ShareBadge() {
const { note } = useNoteContext();
const [ , switchShareState ] = useShareState(note);