Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: add support for multiselecting attachments

Abdullah Atta1a00fc6e3094
diff --git a/apps/theme-builder/src/components/theme-builder/index.tsx b/apps/theme-builder/src/components/theme-builder/index.tsx
index 4b4df59e4..3dd469015 100644
--- a/apps/theme-builder/src/components/theme-builder/index.tsx
+++ b/apps/theme-builder/src/components/theme-builder/index.tsx
@@ -183,7 +183,7 @@ export default function ThemeBuilder() {
sx={{ py: "7px" }}
variant="secondary"
onClick={async () => {
- const file = await showFilePicker({
+ const [file] = await showFilePicker({
acceptedFileTypes: "application/json"
});
if (!file) return;
diff --git a/apps/web/src/components/editor/index.tsx b/apps/web/src/components/editor/index.tsx
index 903322ac4..146e4dd37 100644
--- a/apps/web/src/components/editor/index.tsx
+++ b/apps/web/src/components/editor/index.tsx
@@ -40,7 +40,7 @@ import Tiptap from "./tiptap";
import Header from "./header";
import { Attachment } from "../icons";
import { useEditorInstance } from "./context";
-import { attachFile, AttachmentProgress, insertAttachment } from "./picker";
+import { attachFile, AttachmentProgress, insertAttachments } from "./picker";
import { saveAttachment, downloadAttachment } from "../../common/attachments";
import { EV, EVENTS } from "@notesnook/core/dist/common";
import { db } from "../../common/db";
@@ -417,12 +417,13 @@ export function Editor(props: EditorProps) {
onPreviewDocument({ url: URL.createObjectURL(blob), hash });
}
}}
- onInsertAttachment={(type) => {
+ onInsertAttachment={async (type) => {
const mime = type === "file" ? "*/*" : "image/*";
- insertAttachment(mime).then((file) => {
- if (!file) return;
- editor.current?.attachFile(file);
- });
+ const attachments = await insertAttachments(mime);
+ if (!attachments) return;
+ for (const attachment of attachments) {
+ editor.current?.attachFile(attachment);
+ }
}}
onGetAttachmentData={(attachment) => {
return downloadAttachment(
diff --git a/apps/web/src/components/editor/picker.ts b/apps/web/src/components/editor/picker.ts
index 9b9cf923b..03fa1a974 100644
--- a/apps/web/src/components/editor/picker.ts
+++ b/apps/web/src/components/editor/picker.ts
@@ -30,18 +30,25 @@ import { Attachment } from "@notesnook/editor";
const FILE_SIZE_LIMIT = 500 * 1024 * 1024;
const IMAGE_SIZE_LIMIT = 50 * 1024 * 1024;
-export async function insertAttachment(type = "*/*") {
+export async function insertAttachments(type = "*/*") {
if (!isUserPremium()) {
await showBuyDialog();
return;
}
- const selectedFile = await showFilePicker({
- acceptedFileTypes: type || "*/*"
+ const files = await showFilePicker({
+ acceptedFileTypes: type || "*/*",
+ multiple: true
});
- if (!selectedFile) return;
+ if (!files) return;
- return await attachFile(selectedFile);
+ const attachments: Attachment[] = [];
+ for (const file of files) {
+ const attachment = await attachFile(file);
+ if (!attachment) continue;
+ attachments.push(attachment);
+ }
+ return attachments;
}
export async function attachFile(selectedFile: File) {
@@ -60,7 +67,7 @@ export async function reuploadAttachment(
type: string,
expectedFileHash: string
) {
- const selectedFile = await showFilePicker({
+ const [selectedFile] = await showFilePicker({
acceptedFileTypes: type || "*/*"
});
if (!selectedFile) return;
diff --git a/apps/web/src/dialogs/settings/components/themes-selector.tsx b/apps/web/src/dialogs/settings/components/themes-selector.tsx
index b867c0931..2336b116e 100644
--- a/apps/web/src/dialogs/settings/components/themes-selector.tsx
+++ b/apps/web/src/dialogs/settings/components/themes-selector.tsx
@@ -180,7 +180,7 @@ function ThemesList() {
<Button
variant="secondary"
onClick={async () => {
- const file = await showFilePicker({
+ const [file] = await showFilePicker({
acceptedFileTypes: "application/json"
});
if (!file) return;
diff --git a/apps/web/src/utils/file-picker.ts b/apps/web/src/utils/file-picker.ts
index 6b94e0dab..7fda8967f 100644
--- a/apps/web/src/utils/file-picker.ts
+++ b/apps/web/src/utils/file-picker.ts
@@ -19,25 +19,25 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { PAGE_VISIBILITY_CHANGE } from "./page-visibility";
-type FilePickerOptions = { acceptedFileTypes: string };
+type FilePickerOptions = { acceptedFileTypes: string; multiple?: boolean };
export function showFilePicker({
- acceptedFileTypes
-}: FilePickerOptions): Promise<File | undefined> {
+ acceptedFileTypes,
+ multiple
+}: FilePickerOptions): Promise<File[]> {
return new Promise((resolve) => {
PAGE_VISIBILITY_CHANGE.ignore = true;
const input = document.createElement("input");
input.setAttribute("type", "file");
+ input.setAttribute("multiple", `${multiple || false}`);
input.setAttribute("accept", acceptedFileTypes);
input.dispatchEvent(new MouseEvent("click"));
input.oncancel = async function () {
resolve(undefined);
};
input.onchange = async function () {
- if (!input.files) return resolve(undefined);
- const file = input.files[0];
- if (!file) return resolve(undefined);
- resolve(file);
+ if (!input.files) return resolve([]);
+ resolve(Array.from(input.files));
};
});
}