Skip to content

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

web: add image compression

Abdullah Atta2cee5020dd1f
diff --git a/apps/web/src/common/dialog-controller.tsx b/apps/web/src/common/dialog-controller.tsx
index e2af22e24..505cb481a 100644
--- a/apps/web/src/common/dialog-controller.tsx
+++ b/apps/web/src/common/dialog-controller.tsx
@@ -487,6 +487,12 @@ export function showEditProfilePictureDialog(profile?: Profile) {
));
}
+export function showImagePickerDialog(images: File[]): Promise<false | File[]> {
+ return showDialog("ImagePickerDialog", (Dialog, perform) => (
+ <Dialog onClose={(res) => perform(res)} images={images} />
+ ));
+}
+
export function showSettings() {
return showDialog("SettingsDialog", (Dialog, perform) => (
<Dialog onClose={(res: boolean) => perform(res)} />
diff --git a/apps/web/src/components/editor/index.tsx b/apps/web/src/components/editor/index.tsx
index 146e4dd37..ff6ab22d6 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, insertAttachments } from "./picker";
+import { attachFiles, AttachmentProgress, insertAttachments } from "./picker";
import { saveAttachment, downloadAttachment } from "../../common/attachments";
import { EV, EVENTS } from "@notesnook/core/dist/common";
import { db } from "../../common/db";
@@ -432,10 +432,12 @@ export function Editor(props: EditorProps) {
id?.toString()
);
}}
- onAttachFile={async (file) => {
- const result = await attachFile(file);
+ onAttachFiles={async (files) => {
+ const result = await attachFiles(files);
if (!result) return;
- editor.current?.attachFile(result);
+ result.forEach((attachment) =>
+ editor.current?.attachFile(attachment)
+ );
}}
/>
</EditorChrome>
@@ -651,11 +653,9 @@ function DropZone(props: DropZoneProps) {
if (!editor || !e.dataTransfer.files?.length) return;
e.preventDefault();
- for (const file of e.dataTransfer.files) {
- const result = await attachFile(file);
- if (!result) continue;
- editor.current?.attachFile(result);
- }
+ (await attachFiles(Array.from(e.dataTransfer.files)))?.forEach(
+ (attachment) => editor.current?.attachFile(attachment)
+ );
}}
>
<Flex
diff --git a/apps/web/src/components/editor/picker.ts b/apps/web/src/components/editor/picker.ts
index 03fa1a974..b9e61a686 100644
--- a/apps/web/src/components/editor/picker.ts
+++ b/apps/web/src/components/editor/picker.ts
@@ -20,7 +20,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { SerializedKey } from "@notesnook/crypto/dist/src/types";
import { AppEventManager, AppEvents } from "../../common/app-events";
import { db } from "../../common/db";
-import { showBuyDialog } from "../../common/dialog-controller";
+import {
+ showBuyDialog,
+ showImagePickerDialog
+} from "../../common/dialog-controller";
import { TaskManager } from "../../common/task-manager";
import { isUserPremium } from "../../hooks/use-is-user-premium";
import { showToast } from "../../utils/toast";
@@ -41,26 +44,29 @@ export async function insertAttachments(type = "*/*") {
multiple: true
});
if (!files) return;
-
- const attachments: Attachment[] = [];
- for (const file of files) {
- const attachment = await attachFile(file);
- if (!attachment) continue;
- attachments.push(attachment);
- }
- return attachments;
+ return await attachFiles(files);
}
-export async function attachFile(selectedFile: File) {
+export async function attachFiles(files: File[]) {
if (!isUserPremium()) {
await showBuyDialog();
return;
}
- if (selectedFile.type.startsWith("image/")) {
- return await pickImage(selectedFile);
- } else {
- return await pickFile(selectedFile);
+
+ const images =
+ (await showImagePickerDialog(
+ files.filter((f) => f.type.startsWith("image/"))
+ )) || [];
+ const documents = files.filter((f) => !f.type.startsWith("image/"));
+ const attachments: Attachment[] = [];
+ for (const file of [...images, ...documents]) {
+ const attachment = file.type.startsWith("image/")
+ ? await pickImage(file)
+ : await pickFile(file);
+ if (!attachment) continue;
+ attachments.push(attachment);
}
+ return attachments;
}
export async function reuploadAttachment(
@@ -109,6 +115,7 @@ async function pickFile(
size: file.size
};
} catch (e) {
+ console.error(e);
showToast("error", `${(e as Error).message}`);
}
}
@@ -192,14 +199,13 @@ async function addAttachment(
if (!output) throw new Error("Could not encrypt file.");
if (forceWrite && exists) await db.attachments.reset(hash);
+
await db.attachments.add({
...output,
- metadata: {
- hash,
- hashType,
- filename: exists?.filename || file.name,
- type: exists?.type || file.type
- },
+ hash,
+ hashType,
+ filename: exists?.filename || file.name,
+ mimeType: exists?.type || file.type,
key
});
}
diff --git a/apps/web/src/components/editor/tiptap.tsx b/apps/web/src/components/editor/tiptap.tsx
index d650ab220..710f0b1b6 100644
--- a/apps/web/src/components/editor/tiptap.tsx
+++ b/apps/web/src/components/editor/tiptap.tsx
@@ -73,7 +73,7 @@ type TipTapProps = {
onDownloadAttachment?: (attachment: Attachment) => void;
onPreviewAttachment?: (attachment: Attachment) => void;
onGetAttachmentData?: (attachment: Attachment) => Promise<string | undefined>;
- onAttachFile?: (file: File) => void;
+ onAttachFiles?: (files: File[]) => void;
onFocus?: () => void;
content?: () => string | undefined;
toolbarContainerId?: string;
@@ -120,7 +120,7 @@ function TipTap(props: TipTapProps) {
onDownloadAttachment,
onPreviewAttachment,
onGetAttachmentData,
- onAttachFile,
+ onAttachFiles,
onContentChange,
onFocus = () => {},
content,
@@ -173,12 +173,10 @@ function TipTap(props: TipTapProps) {
// files if there is text.
// TODO: give user an actionable hint to allow them to select what they
// want to do in such cases.
- if (!hasText && event.clipboardData?.files?.length && onAttachFile) {
+ if (!hasText && event.clipboardData?.files?.length && onAttachFiles) {
event.preventDefault();
event.stopPropagation();
- for (const file of event.clipboardData.files) {
- onAttachFile(file);
- }
+ onAttachFiles(Array.from(event.clipboardData.files));
return true;
}
}
diff --git a/apps/web/src/dialogs/image-picker-dialog.tsx b/apps/web/src/dialogs/image-picker-dialog.tsx
new file mode 100644
index 000000000..b502f4b13
--- /dev/null
+++ b/apps/web/src/dialogs/image-picker-dialog.tsx
@@ -0,0 +1,146 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import { useEffect, useState } from "react";
+import { Perform } from "../common/dialog-controller";
+import Dialog from "../components/dialog";
+import { ScrollContainer } from "@notesnook/ui";
+import { Flex, Image, Label, Text } from "@theme-ui/components";
+import { formatBytes } from "@notesnook/common";
+import { compressImage } from "../utils/image-compressor";
+
+export type ImagePickerDialogProps = {
+ onClose: Perform<false | File[]>;
+ images: File[];
+};
+class FileWithURI extends File {
+ uri: string;
+ constructor(
+ fileBits: BlobPart[],
+ fileName: string,
+ options?: FilePropertyBag
+ ) {
+ super(fileBits, fileName, options);
+ this.uri = URL.createObjectURL(this);
+ }
+}
+
+export default function ImagePickerDialog(props: ImagePickerDialogProps) {
+ const [images, setImages] = useState<FileWithURI[]>([]);
+ const [selectedIndex, setSelectedIndex] = useState(0);
+ const [compress, setCompress] = useState(true);
+ const selectedImage = images[selectedIndex];
+
+ useEffect(() => {
+ (async function () {
+ const images: FileWithURI[] = [];
+ for (const image of props.images) {
+ const compressed = compress
+ ? await compressImage(image, {
+ maxWidth: (naturalWidth) => Math.min(1920, naturalWidth * 0.7),
+ width: (naturalWidth) => naturalWidth,
+ height: (_, naturalHeight) => naturalHeight,
+ resize: "contain",
+ quality: 0.7
+ })
+ : image;
+ images.push(
+ new FileWithURI([compressed], image.name, {
+ lastModified: image.lastModified,
+ type: image.type
+ })
+ );
+ }
+ setImages(images);
+ })();
+ }, [props.images, compress]);
+
+ useEffect(() => {
+ return () => {
+ images.forEach((i) => URL.revokeObjectURL(i.uri));
+ };
+ }, [images]);
+
+ return (
+ <Dialog
+ isOpen={true}
+ onClose={() => props.onClose(false)}
+ positiveButton={{
+ text: "Insert",
+ onClick: () => props.onClose(images)
+ }}
+ negativeButton={{ text: "Cancel", onClick: () => props.onClose(false) }}
+ >
+ {selectedImage && (
+ <Flex sx={{ flexDirection: "column", alignItems: "center", mt: 4 }}>
+ <Image
+ src={selectedImage.uri}
+ sx={{
+ maxHeight: 250,
+ objectFit: "contain",
+ alignSelf: "center",
+ borderRadius: "default"
+ }}
+ />
+ <Text variant="subBody" sx={{ my: 2 }}>
+ {selectedImage.name} ({formatBytes(selectedImage.size)})
+ </Text>
+ </Flex>
+ )}
+ {images.length > 1 ? (
+ <ScrollContainer style={{ display: "flex" }}>
+ {images.map((image, index) => (
+ <Image
+ key={image.name + image.size}
+ src={image.uri}
+ sx={{
+ flexShrink: 0,
+ height: "55px",
+ width: "55px",
+ objectFit: "contain",
+ border:
+ selectedIndex === index
+ ? "2px solid var(--accent)"
+ : "2px solid transparent",
+ borderRadius: "default"
+ }}
+ onClick={() => setSelectedIndex(index)}
+ />
+ ))}
+ </ScrollContainer>
+ ) : null}
+
+ <Label variant="text.body" sx={{ mt: 2 }}>
+ <input
+ type="checkbox"
+ style={{
+ accentColor: "var(--accent)",
+ marginRight: 1,
+ width: 14,
+ height: 14
+ }}
+ defaultChecked={compress}
+ checked={compress}
+ onChange={() => setCompress((s) => !s)}
+ />
+ <span style={{ marginLeft: 5 }}>Enable compression (recommended)</span>
+ </Label>
+ </Dialog>
+ );
+}
diff --git a/apps/web/src/dialogs/index.ts b/apps/web/src/dialogs/index.ts
index 8d6219d6b..ebe7ae70a 100644
--- a/apps/web/src/dialogs/index.ts
+++ b/apps/web/src/dialogs/index.ts
@@ -56,7 +56,10 @@ const BackupPasswordDialog = React.lazy(
() => import("./backup-password-dialog")
);
const CreateColorDialog = React.lazy(() => import("./create-color-dialog"));
-const EditProfilePictureDialog = React.lazy(() => import("./edit-profile-picture-dialog"));
+const EditProfilePictureDialog = React.lazy(
+ () => import("./edit-profile-picture-dialog")
+);
+const ImagePickerDialog = React.lazy(() => import("./image-picker-dialog"));
export const Dialogs = {
AddNotebookDialog,
@@ -87,5 +90,6 @@ export const Dialogs = {
ThemeDetailsDialog,
BackupPasswordDialog,
CreateColorDialog,
- EditProfilePictureDialog
+ EditProfilePictureDialog,
+ ImagePickerDialog
};
diff --git a/apps/web/src/utils/image-compressor.ts b/apps/web/src/utils/image-compressor.ts
new file mode 100644
index 000000000..2a6b26b47
--- /dev/null
+++ b/apps/web/src/utils/image-compressor.ts
@@ -0,0 +1,318 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+type DeriveDimension = (naturalWidth: number, naturalHeight: number) => number;
+
+interface CompressorOptions {
+ resize?: "none" | "cover" | "contain";
+ maxWidth: DeriveDimension | number;
+ maxHeight: DeriveDimension | number;
+ minWidth: DeriveDimension | number;
+ minHeight: DeriveDimension | number;
+ width: DeriveDimension | number;
+ height: DeriveDimension | number;
+ mimeType?: "auto" | string;
+ quality?: number;
+ beforeDraw?: (
+ context: CanvasRenderingContext2D,
+ canvas: HTMLCanvasElement
+ ) => void;
+ afterDraw?: (
+ context: CanvasRenderingContext2D,
+ canvas: HTMLCanvasElement
+ ) => void;
+}
+
+const DEFAULTS: CompressorOptions = {
+ maxWidth: Infinity,
+ maxHeight: Infinity,
+ minWidth: 0,
+ minHeight: 0,
+ width: 0,
+ height: 0,
+ resize: "none",
+ quality: 0.8,
+ mimeType: "auto",
+ beforeDraw: undefined,
+ afterDraw: undefined
+};
+
+const COMPRESSIBLE_TYPES = [
+ "image/jpeg",
+ "image/png",
+ "image/webp",
+ "image/jpg"
+];
+
+export function compressImage(file: File, options: Partial<CompressorOptions>) {
+ if (!COMPRESSIBLE_TYPES.includes(file.type)) return Promise.resolve(file);
+ return new Promise<Blob>((resolve, reject) => {
+ const image = new Image();
+ image.onload = async () => {
+ const compressed = await drawToCanvas({
+ file,
+ image,
+ options: { ...DEFAULTS, ...options },
+ naturalHeight: image.naturalHeight,
+ naturalWidth: image.naturalWidth
+ });
+ resolve(compressed);
+ URL.revokeObjectURL(image.src);
+ };
+ image.onabort = () => {
+ reject(new Error("Aborted to load the image."));
+ };
+ image.onerror = () => {
+ reject(new Error("Failed to load the image."));
+ };
+ image.src = URL.createObjectURL(file);
+ });
+}
+
+function drawToCanvas({
+ file,
+ image,
+ options,
+ naturalWidth,
+ naturalHeight,
+ rotate = 0,
+ scaleX = 1,
+ scaleY = 1
+}: {
+ options: CompressorOptions;
+ file: File;
+ image: HTMLImageElement;
+ naturalWidth: number;
+ naturalHeight: number;
+ rotate?: number;
+ scaleX?: number;
+ scaleY?: number;
+}) {
+ const canvas = document.createElement("canvas");
+ const context = canvas.getContext("2d");
+ if (!context) throw new Error("Failed to get 2d context.");
+
+ const derive = dimensionDeriver(image);
+ const is90DegreesRotated = Math.abs(rotate) % 180 === 90;
+ const resizable =
+ (options.resize === "contain" || options.resize === "cover") &&
+ isPositiveNumber(derive(options.width)) &&
+ isPositiveNumber(derive(options.height));
+ let maxWidth = Math.max(derive(options.maxWidth), 0) || Infinity;
+ let maxHeight = Math.max(derive(options.maxHeight), 0) || Infinity;
+ let minWidth = Math.max(derive(options.minWidth), 0) || 0;
+ let minHeight = Math.max(derive(options.minHeight), 0) || 0;
+ let aspectRatio = naturalWidth / naturalHeight;
+ let width = derive(options.width);
+ let height = derive(options.height);
+
+ if (is90DegreesRotated) {
+ [maxWidth, maxHeight] = [maxHeight, maxWidth];
+ [minWidth, minHeight] = [minHeight, minWidth];
+ [width, height] = [height, width];
+ }
+
+ if (resizable) {
+ aspectRatio = width / height;
+ }
+
+ ({ width: maxWidth, height: maxHeight } = getAdjustedSizes(
+ {
+ aspectRatio,
+ width: maxWidth,
+ height: maxHeight
+ },
+ "contain"
+ ));
+ ({ width: minWidth, height: minHeight } = getAdjustedSizes(
+ {
+ aspectRatio,
+ width: minWidth,
+ height: minHeight
+ },
+ "cover"
+ ));
+
+ if (resizable) {
+ ({ width, height } = getAdjustedSizes(
+ {
+ aspectRatio,
+ width,
+ height
+ },
+ options.resize
+ ));
+ } else {
+ ({ width = naturalWidth, height = naturalHeight } = getAdjustedSizes({
+ aspectRatio,
+ width,
+ height
+ }));
+ }
+
+ width = Math.floor(
+ normalizeDecimalNumber(Math.min(Math.max(width, minWidth), maxWidth))
+ );
+ height = Math.floor(
+ normalizeDecimalNumber(Math.min(Math.max(height, minHeight), maxHeight))
+ );
+
+ const destX = -width / 2;
+ const destY = -height / 2;
+ const destWidth = width;
+ const destHeight = height;
+ let srcX = 0;
+ let srcY = 0;
+ let srcWidth = naturalWidth;
+ let srcHeight = naturalHeight;
+
+ if (resizable) {
+ ({ width: srcWidth, height: srcHeight } = getAdjustedSizes(
+ {
+ aspectRatio,
+ width: naturalWidth,
+ height: naturalHeight
+ },
+ options.resize || "none"
+ ));
+ srcX = (naturalWidth - srcWidth) / 2;
+ srcY = (naturalHeight - srcHeight) / 2;
+ }
+
+ if (is90DegreesRotated) {
+ [width, height] = [height, width];
+ }
+
+ canvas.width = width;
+ canvas.height = height;
+
+ if (!options.mimeType || !isImageType(options.mimeType)) {
+ options.mimeType = file.type;
+ }
+
+ let fillStyle = "transparent";
+
+ const isJPEGImage = options.mimeType === "image/jpeg";
+ if (isJPEGImage) {
+ fillStyle = "#fff";
+ }
+
+ // Override the default fill color (#000, black)
+ context.fillStyle = fillStyle;
+ context.fillRect(0, 0, width, height);
+
+ if (options.beforeDraw) {
+ options.beforeDraw(context, canvas);
+ }
+
+ context.save();
+ context.translate(width / 2, height / 2);
+ context.rotate((rotate * Math.PI) / 180);
+ context.scale(scaleX, scaleY);
+ if (resizable)
+ context.drawImage(
+ image,
+ srcX,
+ srcY,
+ srcWidth,
+ srcHeight,
+ destX,
+ destY,
+ destWidth,
+ destHeight
+ );
+ else context.drawImage(image, destX, destY, destWidth, destHeight);
+ context.restore();
+
+ if (options.afterDraw) {
+ options.afterDraw(context, canvas);
+ }
+
+ return new Promise<Blob>((resolve) => {
+ canvas.toBlob(
+ (blob) => resolve(blob || file),
+ options.mimeType,
+ options.quality
+ );
+ });
+}
+
+const REGEXP_DECIMALS = /\.\d*(?:0|9){12}\d*$/;
+/**
+ * Normalize decimal number.
+ * Check out {@link https://0.30000000000000004.com/}
+ */
+function normalizeDecimalNumber(value: number, times = 100000000000) {
+ return REGEXP_DECIMALS.test(value.toString())
+ ? Math.round(value * times) / times
+ : value;
+}
+
+/**
+ * Get the max sizes in a rectangle under the given aspect ratio.
+ * @param data - The original sizes.
+ * @param type - The adjust type.
+ * @returns The result sizes.
+ */
+export function getAdjustedSizes(
+ {
+ aspectRatio,
+ height,
+ width
+ }: { aspectRatio: number; height: number; width: number },
+ type: "none" | "contain" | "cover" = "none"
+) {
+ const isValidWidth = isPositiveNumber(width);
+ const isValidHeight = isPositiveNumber(height);
+
+ if (isValidWidth && isValidHeight) {
+ const adjustedWidth = height * aspectRatio;
+
+ if (
+ ((type === "contain" || type === "none") && adjustedWidth > width) ||
+ (type === "cover" && adjustedWidth < width)
+ ) {
+ height = width / aspectRatio;
+ } else {
+ width = height * aspectRatio;
+ }
+ } else if (isValidWidth) {
+ height = width / aspectRatio;
+ } else if (isValidHeight) {
+ width = height * aspectRatio;
+ }
+
+ return {
+ width,
+ height
+ };
+}
+
+const isPositiveNumber = (value: number) => value > 0 && value < Infinity;
+
+function isImageType(mimeType: string): boolean {
+ return mimeType.startsWith("image/");
+}
+
+function dimensionDeriver(image: HTMLImageElement) {
+ return (dimension: DeriveDimension | number) =>
+ typeof dimension === "number"
+ ? dimension
+ : dimension(image.naturalWidth, image.naturalHeight);
+}