Skip to content

Build your personal knowledge base with Trilium Notes

chore(spreadsheet): move export to match existing conventions

Elian Doran6b1eb9fa8e40
diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.ts
index 257d0b7893..23bf57aa52 100644
--- a/apps/client/src/components/app_context.ts
+++ b/apps/client/src/components/app_context.ts
@@ -492,6 +492,7 @@ type EventMappings = {
};
exportSvg: { ntxId: string | null | undefined; };
exportPng: { ntxId: string | null | undefined; };
+ exportXlsx: { ntxId: string | null | undefined; };
geoMapCreateChildNote: {
ntxId: string | null | undefined; // TODO: deduplicate ntxId
};
diff --git a/apps/client/src/widgets/FloatingButtonsDefinitions.tsx b/apps/client/src/widgets/FloatingButtonsDefinitions.tsx
index 0b11699a98..419f51db4a 100644
--- a/apps/client/src/widgets/FloatingButtonsDefinitions.tsx
+++ b/apps/client/src/widgets/FloatingButtonsDefinitions.tsx
@@ -61,6 +61,7 @@ export const DESKTOP_FLOATING_BUTTONS: FloatingButtonsList = [
RelationMapButtons,
CopyImageReferenceButton,
ExportImageButtons,
+ ExportSpreadsheetButton,
InAppHelpButton,
Backlinks
];
@@ -328,6 +329,17 @@ function ExportImageButtons({ note, triggerEvent, isDefaultViewMode }: FloatingB
);
}
+function ExportSpreadsheetButton({ note, triggerEvent, isDefaultViewMode }: FloatingButtonContext) {
+ const isEnabled = note?.type === "spreadsheet" && note?.isContentAvailable() && isDefaultViewMode;
+ return isEnabled && (
+ <FloatingButton
+ icon="bx bxs-spreadsheet"
+ text={t("spreadsheet.export-xlsx")}
+ onClick={() => triggerEvent("exportXlsx")}
+ />
+ );
+}
+
function InAppHelpButton({ note }: FloatingButtonContext) {
const helpUrl = getHelpUrlForNote(note);
const isEnabled = note.type !== "book" && !!helpUrl;
diff --git a/apps/client/src/widgets/ribbon/NoteActions.tsx b/apps/client/src/widgets/ribbon/NoteActions.tsx
index eec68f5f58..f950bb0d9c 100644
--- a/apps/client/src/widgets/ribbon/NoteActions.tsx
+++ b/apps/client/src/widgets/ribbon/NoteActions.tsx
@@ -82,6 +82,7 @@ export function NoteContextMenu({ note, noteContext, itemsAtStart, itemsNearNote
|| (note.noteId === "_backendLog");
const isInOptionsOrHelp = note?.noteId.startsWith("_options") || note?.noteId.startsWith("_help");
const isExportableToImage = ["mermaid", "mindMap"].includes(noteType);
+ const isExportableToXlsx = noteType === "spreadsheet";
const isContentAvailable = note.isContentAvailable();
const isPrintable = isContentAvailable && (
["text", "code", "spreadsheet"].includes(noteType) ||
@@ -151,6 +152,12 @@ export function NoteContextMenu({ note, noteContext, itemsAtStart, itemsNearNote
defaultType: "single"
})} />
{isExportableToImage && isNormalViewMode && isContentAvailable && <ExportAsImage ntxId={noteContext.ntxId} parentComponent={parentComponent} />}
+ {isExportableToXlsx && isNormalViewMode && isContentAvailable && (
+ <FormListItem
+ icon="bx bxs-spreadsheet"
+ onClick={() => parentComponent?.triggerEvent("exportXlsx", { ntxId: noteContext.ntxId })}
+ >{t("spreadsheet.export-xlsx")}</FormListItem>
+ )}
<CommandItem command="printActiveNote" icon="bx bx-printer" disabled={!isPrintable}
text={isElectron ? t("note_actions.print_or_export_to_pdf") : t("note_actions.print_note")}
/>
diff --git a/apps/client/src/widgets/type_widgets/spreadsheet/Spreadsheet.tsx b/apps/client/src/widgets/type_widgets/spreadsheet/Spreadsheet.tsx
index 14e790d9bc..300141ae57 100644
--- a/apps/client/src/widgets/type_widgets/spreadsheet/Spreadsheet.tsx
+++ b/apps/client/src/widgets/type_widgets/spreadsheet/Spreadsheet.tsx
@@ -76,7 +76,7 @@ function SpreadsheetEditor({ note, noteContext, readOnly }: TypeWidgetProps & {
useReleaseFillShortcuts(apiRef);
useDarkMode(apiRef);
usePersistence(note, noteContext, apiRef, containerRef);
- useSpreadsheetExport(apiRef, note);
+ useSpreadsheetExport(apiRef, note, noteContext);
useSearchIntegration(apiRef, noteContext);
useDismissDialogsOnNoteSwitch(apiRef);
useFixRadixPortals();
diff --git a/apps/client/src/widgets/type_widgets/spreadsheet/export.tsx b/apps/client/src/widgets/type_widgets/spreadsheet/export.tsx
index 961d794dd8..b323ceabae 100644
--- a/apps/client/src/widgets/type_widgets/spreadsheet/export.tsx
+++ b/apps/client/src/widgets/type_widgets/spreadsheet/export.tsx
@@ -1,66 +1,33 @@
import { FUniver } from "@univerjs/presets";
-import { MutableRef, useEffect, useRef } from "preact/hooks";
+import { MutableRef } from "preact/hooks";
+import NoteContext from "../../../components/note_context";
import FNote from "../../../entities/fnote";
import { t } from "../../../services/i18n";
import toast from "../../../services/toast";
import utils from "../../../services/utils";
+import { useTriliumEvent } from "../../react/hooks";
const XLSX_MIME = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
-const ICON_ID = "trilium-export-xlsx-icon";
-const MENU_ID = "trilium.spreadsheet.export-xlsx";
/**
- * Adds an "Export to Excel" entry to the Univer toolbar (and right-click menu) that
- * serializes the live workbook and downloads it as an `.xlsx` file. The conversion runs
- * client-side via the `exceljs`-backed exporter in `@triliumnext/commons`, dynamically
- * imported so exceljs is only fetched when the user actually exports (and never enters the
- * standalone/core bundles).
- *
- * Note: Univer hides its toolbar and context menu in read-only mode, so this entry is only
- * reachable while the note is editable.
+ * Exports the spreadsheet to an `.xlsx` file when the `exportXlsx` event fires for this note
+ * context. The event is raised from the note actions menu and the floating buttons (the same
+ * surfaces the PNG/SVG exports use), so it works regardless of Univer's own toolbar (which is
+ * hidden in read-only mode). The conversion runs client-side via the `exceljs`-backed exporter
+ * in `@triliumnext/commons`, dynamically imported so exceljs is only fetched on export (and
+ * never enters the standalone/core bundles).
*/
-export default function useSpreadsheetExport(apiRef: MutableRef<FUniver | undefined>, note: FNote) {
- // The Univer instance is reused across notes, so keep the latest note in a ref and have
- // the action (registered once) read it at click time rather than capturing a stale note.
- const noteRef = useRef(note);
- noteRef.current = note;
-
- useEffect(() => {
- const univerAPI = apiRef.current;
- if (!univerAPI) return;
-
- try {
- // Toolbar buttons need a registered icon component to render at all.
- univerAPI.registerComponent(ICON_ID, ExportIcon);
-
- const menu = univerAPI.createMenu({
- id: MENU_ID,
- icon: ICON_ID,
- title: t("spreadsheet.export-xlsx"),
- tooltip: t("spreadsheet.export-xlsx"),
- action: () => void exportToXlsx(univerAPI, noteRef.current)
- });
- // appendTo takes a single position string; call once per location (an array is
- // interpreted as one nested path, not several positions).
- menu.appendTo("ribbon.start.others");
- menu.appendTo("contextMenu.others");
- } catch (e) {
- console.error("Failed to register spreadsheet export menu", e);
- }
- }, [ apiRef ]);
+export default function useSpreadsheetExport(apiRef: MutableRef<FUniver | undefined>, note: FNote, noteContext: NoteContext | null | undefined) {
+ useTriliumEvent("exportXlsx", ({ ntxId }) => {
+ if (ntxId !== noteContext?.ntxId) return;
+ void exportToXlsx(apiRef.current, note);
+ });
}
-function ExportIcon() {
- return (
- <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
- <path d="M8 1.5v7.5m0 0L5.2 6.2M8 9l2.8-2.8" stroke="currentColor" strokeWidth={1.3} strokeLinecap="round" strokeLinejoin="round" />
- <path d="M2.75 10.5v1.75A1.25 1.25 0 0 0 4 13.5h8a1.25 1.25 0 0 0 1.25-1.25V10.5" stroke="currentColor" strokeWidth={1.3} strokeLinecap="round" strokeLinejoin="round" />
- </svg>
- );
-}
+async function exportToXlsx(univerAPI: FUniver | undefined, note: FNote) {
+ if (!univerAPI) return;
-async function exportToXlsx(univerAPI: FUniver, note: FNote) {
try {
const workbook = univerAPI.getActiveWorkbook();
if (!workbook) return;