Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): recognize a vault only for drag-and-drop

Elian Doranb66fd047acf4
diff --git a/apps/client/src/services/import.ts b/apps/client/src/services/import.ts
index c7ff965c61..307805b649 100644
--- a/apps/client/src/services/import.ts
+++ b/apps/client/src/services/import.ts
@@ -10,7 +10,10 @@ import ws from "./ws.js";
type BooleanLike = boolean | "true" | "false";
export interface UploadFilesOptions {
- /** Routes the upload to a specific importer (e.g. "notion"), overriding extension-based detection. */
+ /**
+ * Routes the upload to a specific importer (e.g. "notion"), overriding extension-based detection.
+ * `"auto"` instead lets the importer recognize the archive itself, which only drag-and-drop asks for.
+ */
format?: string;
safeImport?: BooleanLike;
shrinkImages: BooleanLike;
diff --git a/apps/client/src/widgets/note_tree.spec.ts b/apps/client/src/widgets/note_tree.spec.ts
index 2cfc5f26af..adde8f056a 100644
--- a/apps/client/src/widgets/note_tree.spec.ts
+++ b/apps/client/src/widgets/note_tree.spec.ts
@@ -6,6 +6,8 @@ import noteCreateService from "../services/note_create.js";
import treeService from "../services/tree.js";
import NoteTreeWidget, { publishDropMarkerShift } from "./note_tree.js";
+vi.mock("../services/import.js", () => ({ uploadFiles: vi.fn() }));
+
describe("fancytree scrollIntoView patch", () => {
it("resolves instead of crashing for a node without rendered markup (#10407)", async () => {
const $tree = $("<div>").appendTo(document.body);
@@ -146,3 +148,40 @@ describe("the drop marker's distance from the row boundary", () => {
expect(shift()).toBe("8px");
});
});
+
+describe("files dropped on the tree", () => {
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ /** Drives the fancytree drop handler the widget registers, with nothing live behind the tree. */
+ async function dropFiles(files: File[]) {
+ const widget = new NoteTreeWidget();
+ let treeOptions: Fancytree.FancytreeOptions | undefined;
+ vi.spyOn(widget, "prepareRootNode").mockReturnValue({ key: "root" } as never);
+ vi.spyOn($.fn, "fancytree").mockImplementation(function (this: JQuery, opts: unknown) {
+ treeOptions = opts as Fancytree.FancytreeOptions;
+ return this;
+ } as never);
+ vi.spyOn($.ui.fancytree, "getTree").mockReturnValue({} as never);
+
+ widget.doRender();
+ // Let doRender's init promise chain settle against the stubs above.
+ await new Promise((resolve) => setTimeout(resolve));
+ widget.initFancyTree();
+
+ const dnd = treeOptions?.dnd5 as { dragDrop(node: unknown, data: unknown): Promise<void> };
+ await dnd.dragDrop(
+ { data: { noteId: "target", noteType: "text" } },
+ { hitMode: "over", dataTransfer: { files, getData: () => "" } }
+ );
+ }
+
+ it("tags the upload \"auto\", so an archive the importer recognizes is not imported as a plain zip", async () => {
+ const file = new File(["zip bytes"], "MyVault.zip");
+ await dropFiles([file]);
+
+ const { uploadFiles } = await import("../services/import.js");
+ expect(uploadFiles).toHaveBeenCalledWith("notes", "target", [file], expect.objectContaining({ format: "auto" }));
+ });
+});
diff --git a/apps/client/src/widgets/note_tree.ts b/apps/client/src/widgets/note_tree.ts
index d90d5131f7..1e0fc41bd9 100644
--- a/apps/client/src/widgets/note_tree.ts
+++ b/apps/client/src/widgets/note_tree.ts
@@ -587,6 +587,9 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
const importService = await import("../services/import.js");
importService.uploadFiles("notes", node.data.noteId, files, {
+ // Nothing was chosen here the way the import dialog makes a user choose, so
+ // let the importer recognize an archive it knows (an Obsidian vault).
+ format: "auto",
safeImport: true,
shrinkImages: true,
textImportedAsText: true,
diff --git a/packages/trilium-core/src/services/import/dispatch.spec.ts b/packages/trilium-core/src/services/import/dispatch.spec.ts
index 86af611047..9f20b901f8 100644
--- a/packages/trilium-core/src/services/import/dispatch.spec.ts
+++ b/packages/trilium-core/src/services/import/dispatch.spec.ts
@@ -115,7 +115,7 @@ describe("importFile (dispatch)", () => {
it("routes a .zip to the generic zip importer when archives are exploded", async () => {
const file = makeFile({ originalname: "backup.zip", path: "/tmp/b.zip" });
await importFile(taskContext, file, parentNote, makeOptions());
- expect(stubbed.importZip).toHaveBeenCalledWith(taskContext, { path: "/tmp/b.zip" }, parentNote, { onObsidianVault: expect.any(Function) });
+ expect(stubbed.importZip).toHaveBeenCalledWith(taskContext, { path: "/tmp/b.zip" }, parentNote);
});
it("imports a .zip as a single file when archives are not exploded", async () => {
@@ -151,15 +151,21 @@ describe("importFile (dispatch)", () => {
});
});
- describe("Obsidian detection (an untagged .zip, e.g. dropped on the note tree)", () => {
+ describe("Obsidian detection (only for the \"auto\" tag a drag-and-drop sends)", () => {
it("hands the zip importer an Obsidian fallback that imports the vault in place of the archive", async () => {
const file = makeFile({ originalname: "MyVault.zip", path: "/tmp/v.zip" });
- await importFile(taskContext, file, parentNote, makeOptions());
+ await importFile(taskContext, file, parentNote, makeOptions(), "auto");
// The zip importer's scan pass decides whether to call this; recognition is covered in zip.spec.ts.
const opts = stubbed.importZip.mock.calls[0]?.[3];
await opts?.onObsidianVault?.();
expect(stubbed.importObsidian).toHaveBeenCalledWith(taskContext, { path: "/tmp/v.zip" }, parentNote, "MyVault.zip");
});
+
+ it("offers no fallback for a .zip chosen in the import dialog", async () => {
+ // The dialog's Files tile sends no format: the user picked a plain archive import and gets one.
+ await importFile(taskContext, makeFile({ originalname: "MyVault.zip", path: "/tmp/v.zip" }), parentNote, makeOptions());
+ expect(stubbed.importZip).toHaveBeenCalledWith(taskContext, { path: "/tmp/v.zip" }, parentNote);
+ });
});
});
diff --git a/packages/trilium-core/src/services/import/dispatch.ts b/packages/trilium-core/src/services/import/dispatch.ts
index 54db284599..12c16b0b5e 100644
--- a/packages/trilium-core/src/services/import/dispatch.ts
+++ b/packages/trilium-core/src/services/import/dispatch.ts
@@ -80,13 +80,17 @@ async function routeToImporter(taskContext: TaskContext<"importNotes">, file: Fi
// Trilium export by extension alone; the Obsidian import dialog tags the upload to route it here.
return await obsidianImportService.importObsidian(taskContext, zipSource, parentNote, file.originalname);
} else if (extension === ".zip" && options.explodeArchives && (file.path || typeof file.buffer !== "string")) {
- // Nothing tags a vault dropped straight onto the note tree, so the zip importer's scan pass hands
- // one back here instead of importing it: the generic importer would strip it of wikilinks, callouts
- // and frontmatter properties. That scan already reads every entry name for `!!!meta.json`, so
- // recognizing a vault adds no pass over the archive.
- return await zipImportService.importZip(taskContext, zipSource, parentNote, {
- onObsidianVault: () => obsidianImportService.importObsidian(taskContext, zipSource, parentNote, file.originalname)
- });
+ if (format === "auto") {
+ // What a drag-and-drop onto the note tree sends: no importer was picked, so the zip importer's
+ // scan pass hands an Obsidian vault back here instead of importing it as a plain archive, which
+ // would strip it of wikilinks, callouts and frontmatter properties. That scan already reads
+ // every entry name for `!!!meta.json`, so recognizing a vault adds no pass over the archive.
+ // A `.zip` chosen in the import dialog is a decision already made, and is left alone.
+ return await zipImportService.importZip(taskContext, zipSource, parentNote, {
+ onObsidianVault: () => obsidianImportService.importObsidian(taskContext, zipSource, parentNote, file.originalname)
+ });
+ }
+ return await zipImportService.importZip(taskContext, zipSource, parentNote);
} else if (extension === ".opml" && options.explodeArchives) {
return await opmlImportService.importOpml(taskContext, file.buffer, parentNote);
} else if (extension === ".enex" && options.explodeArchives) {