Skip to content

Build your personal knowledge base with Trilium Notes

feat(export): show a progress bar during the content-writing phase

Elian Doran9ed5151149b2
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 687bd123d1..3588e6b7dc 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -150,6 +150,7 @@
"export": "Export",
"choose_export_type": "Choose export type first please",
"export_in_progress": "Export in progress: {{progressCount}}",
+ "export_in_progress_with_total": "Exporting {{progressCount}} of {{totalCount}}",
"export_finished_successfully": "Export finished successfully.",
"format_pdf": "PDF - for printing or sharing purposes."
},
diff --git a/apps/client/src/widgets/dialogs/export.tsx b/apps/client/src/widgets/dialogs/export.tsx
index 9b373f277e..54ebdbbe69 100644
--- a/apps/client/src/widgets/dialogs/export.tsx
+++ b/apps/client/src/widgets/dialogs/export.tsx
@@ -157,7 +157,11 @@ ws.subscribeToMessages(async (message) => {
return {
id,
message,
- icon: "export"
+ icon: "export",
+ // This toast replaces the in-progress one (same id), and showPersistent merges fields rather
+ // than swapping the object — so clear the progress explicitly, otherwise the finished bar
+ // lingers at 100%.
+ progress: undefined
};
}
@@ -169,7 +173,7 @@ ws.subscribeToMessages(async (message) => {
toastService.closePersistent(message.taskId);
toastService.showError(message.message);
} else if (message.type === "taskProgressCount") {
- toastService.showPersistent(makeToast(message.taskId, t("export.export_in_progress", { progressCount: message.progressCount })));
+ toastService.showPersistent(makeProgressToast(message.taskId, message.progressCount, message.totalCount));
} else if (message.type === "taskSucceeded") {
const toast = makeToast(message.taskId, t("export.export_finished_successfully"));
toast.timeout = 5000;
@@ -177,3 +181,20 @@ ws.subscribeToMessages(async (message) => {
toastService.showPersistent(toast);
}
});
+
+/**
+ * Builds the persistent "export in progress" toast. The metadata-collection phase doesn't know the total
+ * up front, so it shows a bare running count; once the content-writing phase sets a total, the message
+ * switches to "Exporting X of N" with a progress bar.
+ */
+function makeProgressToast(taskId: string, progressCount: number, totalCount?: number): ToastOptionsWithRequiredId {
+ const hasTotal = typeof totalCount === "number" && totalCount > 0;
+ return {
+ id: taskId,
+ icon: "bx bx-loader-circle bx-spin",
+ message: hasTotal
+ ? t("export.export_in_progress_with_total", { progressCount, totalCount })
+ : t("export.export_in_progress", { progressCount }),
+ ...(hasTotal ? { progress: progressCount / totalCount } : {})
+ };
+}
diff --git a/packages/trilium-core/src/services/export/zip.ts b/packages/trilium-core/src/services/export/zip.ts
index dba9a9b6e6..fd5612068a 100644
--- a/packages/trilium-core/src/services/export/zip.ts
+++ b/packages/trilium-core/src/services/export/zip.ts
@@ -468,6 +468,11 @@ async function exportToZip(taskContext: TaskContext<"export">, branch: BBranch,
archive.append(metaFileJson, { name: "!!!meta.json" });
+ // The metadata pass above already drove the bare progress count. Reset it and seed the total with the
+ // number of notes about to be written so the content-writing pass renders a clean 0→100% progress bar.
+ taskContext.resetProgressCount();
+ taskContext.setTotalCount(countMetaNodes(rootMeta));
+
await saveNote(rootMeta, "");
provider.afterDone(rootMeta);
@@ -477,6 +482,14 @@ async function exportToZip(taskContext: TaskContext<"export">, branch: BBranch,
taskContext.taskSucceeded(null);
}
+/** Counts the notes in a metadata tree — i.e. the number of `saveNote()` calls the content-writing pass will make. */
+function countMetaNodes(meta: NoteMeta): number {
+ let count = 1;
+ for (const child of meta.children || []) {
+ count += countMetaNodes(child);
+ }
+ return count;
+}
async function exportToZipFile(noteId: string, format: ExportFormat, zipFilePath: string, zipExportOptions?: AdvancedExportOptions) {
const { destination, waitForFinish } = getZipProvider().createFileStream(zipFilePath);
diff --git a/packages/trilium-core/src/services/task_context.ts b/packages/trilium-core/src/services/task_context.ts
index 09528b29ae..1ef9f4c4e6 100644
--- a/packages/trilium-core/src/services/task_context.ts
+++ b/packages/trilium-core/src/services/task_context.ts
@@ -50,6 +50,15 @@ class TaskContext<T extends TaskType> {
this.totalCount = totalCount;
}
+ /**
+ * Resets the running progress count back to zero. Useful for multi-phase tasks (e.g. zip export,
+ * which first walks the tree to build metadata and then walks it again to write content) so a later
+ * phase can drive a 0→100% progress bar from scratch instead of continuing the earlier phase's count.
+ */
+ resetProgressCount() {
+ this.progressCount = 0;
+ }
+
increaseProgressCount() {
this.progressCount++;