Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): show a progress bar for ENEX imports

Elian Doran3fd222bf3ea6
diff --git a/packages/trilium-core/src/services/import/enex.spec.ts b/packages/trilium-core/src/services/import/enex.spec.ts
index 3ab09c3e4b..6e11fcf9db 100644
--- a/packages/trilium-core/src/services/import/enex.spec.ts
+++ b/packages/trilium-core/src/services/import/enex.spec.ts
@@ -1,7 +1,7 @@
import fs from "fs";
import { default as path, dirname } from "path";
import { fileURLToPath } from "url";
-import { beforeAll, describe, expect, it } from "vitest";
+import { beforeAll, describe, expect, it, vi } from "vitest";
import becca from "../../becca/becca.js";
import type BNote from "../../becca/entities/bnote.js";
@@ -17,7 +17,7 @@ async function testImport(fileName: string) {
const sample = fs.readFileSync(path.join(scriptDir, "samples", fileName));
const taskContext = TaskContext.getInstance("import-enex", "importNotes", {});
- return new Promise<{ importedNote: BNote; rootNote: BNote }>((resolve, reject) => {
+ return new Promise<{ importedNote: BNote; rootNote: BNote; taskContext: TaskContext<"importNotes"> }>((resolve, reject) => {
getContext().init(async () => {
const rootNote = becca.getNote("root");
if (!rootNote) {
@@ -32,7 +32,8 @@ async function testImport(fileName: string) {
}, rootNote as BNote);
resolve({
importedNote,
- rootNote
+ rootNote,
+ taskContext
});
});
});
@@ -89,6 +90,21 @@ describe("importEnex", () => {
expect(test3!.getAttachmentsByRole("file")).toHaveLength(0);
});
+ it("reports the note count as the task total so the client shows a progress bar", async () => {
+ // setTotalCount drives the denominator of the progress bar; spy on the prototype before importing
+ // since the call happens inside importEnex. It should match the number of imported notes.
+ const setTotalCount = vi.spyOn(TaskContext.prototype, "setTotalCount");
+ try {
+ const { importedNote } = await testImport("File with attachments.enex");
+
+ const noteCount = importedNote.getChildNotes().length;
+ expect(noteCount).toBeGreaterThan(0);
+ expect(setTotalCount).toHaveBeenCalledWith(noteCount);
+ } finally {
+ setTotalCount.mockRestore();
+ }
+ });
+
it("converts Evernote's rich blocks (code, math, mermaid, tasks, callouts, toggles, checkboxes)", async () => {
const { importedNote } = await testImport("Formatting.enex");
diff --git a/packages/trilium-core/src/services/import/enex.ts b/packages/trilium-core/src/services/import/enex.ts
index 73d388fc45..ac0d3fc246 100644
--- a/packages/trilium-core/src/services/import/enex.ts
+++ b/packages/trilium-core/src/services/import/enex.ts
@@ -390,6 +390,15 @@ async function importEnex(taskContext: TaskContext<"importNotes">, file: File, p
const content = typeof file.buffer === "string" ? file.buffer : new TextDecoder().decode(file.buffer);
+ // saveNote() increments progress once per note; count the note-opening tags up front so the client can
+ // render a progress bar instead of a bare count. The file is already in memory, so a cheap regex over
+ // the raw text gives the denominator without a second parse. `[\s>]` keeps it from matching
+ // `<note-attributes>` or `<en-note>`.
+ const totalNotes = (content.match(/<note[\s>]/g) ?? []).length;
+ if (totalNotes > 0) {
+ taskContext.setTotalCount(totalNotes);
+ }
+
const CHUNK_SIZE = 64 * 1024;
for (let i = 0; i < content.length; i += CHUNK_SIZE) {
parser.write(content.slice(i, i + CHUNK_SIZE));