Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): drag-and-drop file picker for the import dialog

Elian Doran379850a45da7
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 5c4b87088e..ffa87a8fc4 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -230,6 +230,12 @@
"in-progress-with-total": "Importing {{progress}} of {{total}}",
"successful": "Import finished successfully."
},
+ "file_upload": {
+ "drop_or_browse_single": "Drag and drop a file here, or click to browse",
+ "drop_or_browse_multiple": "Drag and drop files here, or click to browse",
+ "selected_one": "{{count}} file selected",
+ "selected_other": "{{count}} files selected"
+ },
"import_provider": {
"choose_provider": "Choose a service above to import from."
},
diff --git a/apps/client/src/widgets/dialogs/import/anytype.tsx b/apps/client/src/widgets/dialogs/import/anytype.tsx
index edf9d93fed..b8998d0edd 100644
--- a/apps/client/src/widgets/dialogs/import/anytype.tsx
+++ b/apps/client/src/widgets/dialogs/import/anytype.tsx
@@ -3,7 +3,7 @@ import { useCallback, useEffect, useRef, useState } from "preact/hooks";
import { t } from "../../../services/i18n.js";
import importService from "../../../services/import.js";
import Button from "../../react/Button.js";
-import FormFileUpload from "../../react/FormFileUpload.js";
+import FileDropZone from "../../react/FileDropZone.js";
import FormGroup from "../../react/FormGroup.js";
import iconUrl from "./icons/anytype.svg?url";
import type { ImportProvider, ImportProviderPanelProps } from "./types.js";
@@ -48,7 +48,7 @@ function AnytypePanel({ parentNoteId, closeDialog, setFooter }: ImportProviderPa
<div className="anytype-panel">
<p>{t("anytype_import.description_long")}</p>
<FormGroup name="anytype-file" label={t("anytype_import.choose_file")}>
- <FormFileUpload onChange={onChange} />
+ <FileDropZone onChange={onChange} />
</FormGroup>
</div>
);
diff --git a/apps/client/src/widgets/dialogs/import/evernote.tsx b/apps/client/src/widgets/dialogs/import/evernote.tsx
index f9a83f50f1..38a080746f 100644
--- a/apps/client/src/widgets/dialogs/import/evernote.tsx
+++ b/apps/client/src/widgets/dialogs/import/evernote.tsx
@@ -3,7 +3,7 @@ import { useCallback, useEffect, useRef, useState } from "preact/hooks";
import { t } from "../../../services/i18n.js";
import importService from "../../../services/import.js";
import Button from "../../react/Button.js";
-import FormFileUpload from "../../react/FormFileUpload.js";
+import FileDropZone from "../../react/FileDropZone.js";
import FormGroup from "../../react/FormGroup.js";
import iconUrl from "./icons/evernote.svg?url";
import type { ImportProvider, ImportProviderPanelProps } from "./types.js";
@@ -49,7 +49,7 @@ function EvernotePanel({ parentNoteId, closeDialog, setFooter }: ImportProviderP
<div className="evernote-panel">
<p>{t("evernote_import.description_long")}</p>
<FormGroup name="evernote-file" label={t("evernote_import.choose_file")}>
- <FormFileUpload multiple onChange={onChange} />
+ <FileDropZone multiple onChange={onChange} />
</FormGroup>
</div>
);
diff --git a/apps/client/src/widgets/dialogs/import/files.tsx b/apps/client/src/widgets/dialogs/import/files.tsx
index ca523369e2..beba1100da 100644
--- a/apps/client/src/widgets/dialogs/import/files.tsx
+++ b/apps/client/src/widgets/dialogs/import/files.tsx
@@ -4,8 +4,8 @@ import { t } from "../../../services/i18n.js";
import importService, { type UploadFilesOptions } from "../../../services/import.js";
import tree from "../../../services/tree.js";
import Button from "../../react/Button.js";
+import FileDropZone from "../../react/FileDropZone.js";
import FormCheckbox from "../../react/FormCheckbox.js";
-import FormFileUpload from "../../react/FormFileUpload.js";
import FormGroup, { FormMultiGroup } from "../../react/FormGroup.js";
import { useTriliumOptionBool } from "../../react/hooks.js";
import RawHtml from "../../react/RawHtml.js";
@@ -69,7 +69,7 @@ function FilesPanel({ parentNoteId, closeDialog, setFooter }: ImportProviderPane
{t("import.importZipRecommendation")}
</>
}>
- <FormFileUpload multiple onChange={setFiles} />
+ <FileDropZone multiple onChange={setFiles} />
</FormGroup>
<FormMultiGroup label={t("import.options")}>
diff --git a/apps/client/src/widgets/dialogs/import/keep.tsx b/apps/client/src/widgets/dialogs/import/keep.tsx
index 8eba5f7884..0e3ba625f6 100644
--- a/apps/client/src/widgets/dialogs/import/keep.tsx
+++ b/apps/client/src/widgets/dialogs/import/keep.tsx
@@ -3,7 +3,7 @@ import { useCallback, useEffect, useRef, useState } from "preact/hooks";
import { t } from "../../../services/i18n.js";
import importService from "../../../services/import.js";
import Button from "../../react/Button.js";
-import FormFileUpload from "../../react/FormFileUpload.js";
+import FileDropZone from "../../react/FileDropZone.js";
import FormGroup from "../../react/FormGroup.js";
import iconUrl from "./icons/keep.svg?url";
import type { ImportProvider, ImportProviderPanelProps } from "./types.js";
@@ -48,7 +48,7 @@ function KeepPanel({ parentNoteId, closeDialog, setFooter }: ImportProviderPanel
<div className="keep-panel">
<p>{t("keep_import.description_long")}</p>
<FormGroup name="keep-file" label={t("keep_import.choose_file")}>
- <FormFileUpload onChange={onChange} />
+ <FileDropZone onChange={onChange} />
</FormGroup>
</div>
);
diff --git a/apps/client/src/widgets/dialogs/import/notion.tsx b/apps/client/src/widgets/dialogs/import/notion.tsx
index ccc20fd4d3..f374be3031 100644
--- a/apps/client/src/widgets/dialogs/import/notion.tsx
+++ b/apps/client/src/widgets/dialogs/import/notion.tsx
@@ -3,7 +3,7 @@ import { useCallback, useEffect, useRef, useState } from "preact/hooks";
import { t } from "../../../services/i18n.js";
import importService from "../../../services/import.js";
import Button from "../../react/Button.js";
-import FormFileUpload from "../../react/FormFileUpload.js";
+import FileDropZone from "../../react/FileDropZone.js";
import FormGroup from "../../react/FormGroup.js";
import iconUrl from "./icons/notion.svg?url";
import type { ImportProvider, ImportProviderPanelProps } from "./types.js";
@@ -48,7 +48,7 @@ function NotionPanel({ parentNoteId, closeDialog, setFooter }: ImportProviderPan
<div className="notion-panel">
<p>{t("notion_import.description_long")}</p>
<FormGroup name="notion-file" label={t("notion_import.choose_file")}>
- <FormFileUpload onChange={onChange} />
+ <FileDropZone onChange={onChange} />
</FormGroup>
</div>
);
diff --git a/apps/client/src/widgets/react/FileDropZone.css b/apps/client/src/widgets/react/FileDropZone.css
new file mode 100644
index 0000000000..e990195832
--- /dev/null
+++ b/apps/client/src/widgets/react/FileDropZone.css
@@ -0,0 +1,88 @@
+.file-drop-zone {
+ position: relative;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ width: 100%;
+ min-height: 8em;
+ padding: 1.5em;
+ border: 2px dashed var(--main-border-color);
+ border-radius: 8px;
+ background: var(--accented-background-color);
+ cursor: pointer;
+ text-align: center;
+ transition: border-color 0.15s ease, background-color 0.15s ease;
+}
+
+.file-drop-zone:hover,
+.file-drop-zone:focus-within {
+ border-color: var(--main-text-color);
+}
+
+.file-drop-zone.dragging {
+ border-color: var(--main-text-color);
+ background: var(--active-item-background-color);
+}
+
+/* Visually hidden but kept focusable so the zone gets a focus ring via :focus-within. */
+.file-drop-zone-input {
+ position: absolute;
+ width: 1px;
+ height: 1px;
+ padding: 0;
+ margin: -1px;
+ border: 0;
+ overflow: hidden;
+ clip: rect(0, 0, 0, 0);
+ white-space: nowrap;
+}
+
+.file-drop-zone-prompt {
+ display: flex;
+ flex-direction: column;
+ align-items: center;
+ gap: 0.5em;
+ color: var(--muted-text-color);
+}
+
+.file-drop-zone-icon {
+ font-size: 2.5em;
+}
+
+.file-drop-zone-selection {
+ display: flex;
+ flex-direction: column;
+ gap: 0.5em;
+ width: 100%;
+}
+
+.file-drop-zone-count {
+ color: var(--muted-text-color);
+ font-size: 0.9em;
+}
+
+.file-drop-zone-files {
+ list-style: none;
+ margin: 0;
+ padding: 0;
+ display: flex;
+ flex-direction: column;
+ gap: 0.35em;
+ width: 100%;
+ max-height: 12em;
+ overflow-y: auto;
+ text-align: left;
+}
+
+.file-drop-zone-files li {
+ display: flex;
+ align-items: center;
+ gap: 0.5em;
+ min-width: 0;
+}
+
+.file-drop-zone-filename {
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+}
diff --git a/apps/client/src/widgets/react/FileDropZone.tsx b/apps/client/src/widgets/react/FileDropZone.tsx
new file mode 100644
index 0000000000..c04224a021
--- /dev/null
+++ b/apps/client/src/widgets/react/FileDropZone.tsx
@@ -0,0 +1,94 @@
+import "./FileDropZone.css";
+
+import { useCallback, useEffect, useRef, useState } from "preact/hooks";
+
+import { t } from "../../services/i18n";
+
+export interface FileDropZoneProps {
+ name?: string;
+ onChange: (files: FileList | null) => void;
+ multiple?: boolean;
+ /** Optional `accept` attribute forwarded to the underlying file input (e.g. `".zip"`). */
+ accept?: string;
+}
+
+/**
+ * A styled drop region for selecting files: drag-and-drop onto it or click it to open the native file
+ * picker. Selected files are listed inside. Drop-in replacement for {@link FormFileUpload} — same
+ * `onChange(files)` / `multiple` contract.
+ */
+export default function FileDropZone({ name, onChange, multiple, accept }: FileDropZoneProps) {
+ const inputRef = useRef<HTMLInputElement>(null);
+ const [files, setFiles] = useState<File[]>([]);
+ const [dragging, setDragging] = useState(false);
+ // Tracks dragenter/dragleave depth so moving the cursor over child elements doesn't flicker the
+ // highlight (dragleave fires when crossing between nested children, not just on leaving the zone).
+ const dragDepth = useRef(0);
+
+ // Reset any stale selection carried over from a previous mount of this widget.
+ useEffect(() => { onChange(null); }, []);
+
+ const update = useCallback((list: FileList | null) => {
+ const selected = list && list.length ? (multiple ? Array.from(list) : [list[0]]) : [];
+ setFiles(selected);
+ onChange(selected.length ? list : null);
+ }, [multiple, onChange]);
+
+ const onDragEnter = useCallback((e: DragEvent) => {
+ e.preventDefault();
+ dragDepth.current++;
+ setDragging(true);
+ }, []);
+
+ const onDragLeave = useCallback(() => {
+ dragDepth.current = Math.max(0, dragDepth.current - 1);
+ if (dragDepth.current === 0) {
+ setDragging(false);
+ }
+ }, []);
+
+ const onDrop = useCallback((e: DragEvent) => {
+ e.preventDefault();
+ dragDepth.current = 0;
+ setDragging(false);
+ const dropped = e.dataTransfer?.files;
+ if (dropped && dropped.length) {
+ update(dropped);
+ }
+ }, [update]);
+
+ return (
+ <label
+ className={`file-drop-zone ${dragging ? "dragging" : ""} ${files.length ? "has-files" : ""}`}
+ onDragEnter={onDragEnter}
+ onDragOver={(e) => e.preventDefault()}
+ onDragLeave={onDragLeave}
+ onDrop={onDrop}
+ >
+ <input
+ ref={inputRef}
+ name={name}
+ type="file"
+ className="file-drop-zone-input"
+ multiple={multiple}
+ accept={accept}
+ onChange={(e) => update((e.target as HTMLInputElement).files)}
+ />
+ {files.length ? (
+ <div className="file-drop-zone-selection">
+ {files.length > 1 && <div className="file-drop-zone-count">{t("file_upload.selected", { count: files.length })}</div>}
+ <ul className="file-drop-zone-files">
+ {files.map((file, index) => (
+ <li key={index}><span className="bx bx-file-blank" /> <span className="file-drop-zone-filename">{file.name}</span></li>
+ ))}
+ </ul>
+ </div>
+ ) : (
+ <div className="file-drop-zone-prompt">
+ <span className="bx bx-cloud-upload file-drop-zone-icon" />
+ <span>{multiple ? t("file_upload.drop_or_browse_multiple") : t("file_upload.drop_or_browse_single")}</span>
+ </div>
+ )}
+ </label>
+ );
+}