Skip to content

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

web: get rid of all attachment loading logic

Abdullah Attab6fd0df715c9
diff --git a/apps/web/src/common/attachments.ts b/apps/web/src/common/attachments.ts
index 3191200d2..de8234abf 100644
--- a/apps/web/src/common/attachments.ts
+++ b/apps/web/src/common/attachments.ts
@@ -20,11 +20,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { lazify } from "../utils/lazify";
import { db } from "./db";
-async function download(hash: string) {
+async function download(hash: string, groupId?: string) {
const attachment = db.attachments?.attachment(hash);
if (!attachment) return;
const downloadResult = await db.fs?.downloadFile(
- attachment.metadata.hash,
+ groupId || attachment.metadata.hash,
attachment.metadata.hash,
attachment.chunkSize,
attachment.metadata
@@ -61,8 +61,12 @@ type OutputTypeToReturnType = {
export async function downloadAttachment<
TType extends "blob" | "base64" | "text",
TOutputType = OutputTypeToReturnType[TType]
->(hash: string, type: TType): Promise<TOutputType | undefined> {
- const response = await download(hash);
+>(
+ hash: string,
+ type: TType,
+ groupId?: string
+): Promise<TOutputType | undefined> {
+ const response = await download(hash, groupId);
if (!response) return;
const { attachment, key } = response;
diff --git a/apps/web/src/components/editor/index.tsx b/apps/web/src/components/editor/index.tsx
index 8a191043a..a5b1e339e 100644
--- a/apps/web/src/components/editor/index.tsx
+++ b/apps/web/src/components/editor/index.tsx
@@ -135,26 +135,7 @@ export default function EditorManager({
if (result) item.data = result.data;
else EV.publish(EVENTS.vaultLocked);
}
- const oldHashes = editorInstance.current.getMediaHashes();
-
editorInstance.current.updateContent(item.data as string);
-
- const newHashes = editorInstance.current.getMediaHashes();
- const hashesToLoad = newHashes.filter(
- (hash, index) => hash !== oldHashes[index]
- );
-
- if (appstore.get().isSyncing()) {
- db.eventManager.subscribe(
- EVENTS.syncCompleted,
- async () => {
- await db.attachments?.downloadMedia(id, hashesToLoad);
- },
- true
- );
- } else {
- await db.attachments?.downloadMedia(id, hashesToLoad);
- }
} else if (isNote) {
if (!locked && item.locked) return EV.publish(EVENTS.vaultLocked);
@@ -180,18 +161,6 @@ export default function EditorManager({
setTimestamp(Date.now());
}, []);
- const loadMedia = useCallback(async () => {
- if (previewSession.current) {
- await db.content?.downloadMedia(
- noteId,
- previewSession.current.content,
- true
- );
- } else if (noteId && editorstore.get().session.content) {
- await db.attachments?.downloadMedia(noteId);
- }
- }, [noteId]);
-
useEffect(() => {
if (!isNewSession) return;
@@ -237,6 +206,7 @@ export default function EditorManager({
/>
)}
<Editor
+ id={noteId}
nonce={timestamp}
content={() =>
previewSession.current?.content?.data ||
@@ -247,7 +217,6 @@ export default function EditorManager({
options={{
readonly: isReadonly || isPreviewSession,
onRequestFocus: () => toggleProperties(false),
- onLoadMedia: loadMedia,
focusMode: isFocusMode,
isMobile: isMobile || isTablet
}}
@@ -351,9 +320,9 @@ type EditorOptions = {
readonly?: boolean;
focusMode?: boolean;
onRequestFocus?: () => void;
- onLoadMedia?: () => void;
};
type EditorProps = {
+ id: string | number;
content: () => string | undefined;
nonce?: number;
options?: EditorOptions;
@@ -361,8 +330,9 @@ type EditorProps = {
onPreviewDocument?: (preview: DocumentPreview) => void;
};
export function Editor(props: EditorProps) {
- const { content, nonce, options, onContentChange, onPreviewDocument } = props;
- const { readonly, headless, onLoadMedia, isMobile } = options || {
+ const { id, content, nonce, options, onContentChange, onPreviewDocument } =
+ props;
+ const { readonly, headless, isMobile } = options || {
headless: false,
readonly: false,
focusMode: false,
@@ -375,40 +345,16 @@ export function Editor(props: EditorProps) {
useEffect(() => {
const event = AppEventManager.subscribe(
AppEvents.UPDATE_ATTACHMENT_PROGRESS,
- ({ hash, loaded, total, type }: AttachmentProgress) => {
+ ({ hash, loaded, total }: AttachmentProgress) => {
editor.current?.sendAttachmentProgress(
hash,
- type,
Math.round((loaded / total) * 100)
);
}
);
- const mediaAttachmentDownloadedEvent = EV.subscribe(
- EVENTS.mediaAttachmentDownloaded,
- ({
- groupId,
- hash,
- attachmentType,
- src
- }: {
- groupId?: string;
- attachmentType: "image" | "webclip" | "generic";
- hash: string;
- src: string;
- }) => {
- if (groupId?.startsWith("monograph")) return;
- if (attachmentType === "image") {
- editor.current?.loadImage(hash, src);
- } else if (attachmentType === "webclip") {
- editor.current?.loadWebClip(hash, src);
- }
- }
- );
-
return () => {
event.unsubscribe();
- mediaAttachmentDownloadedEvent.unsubscribe();
};
}, [editor]);
@@ -424,7 +370,6 @@ export function Editor(props: EditorProps) {
corsHost: Config.get("corsProxy", "https://cors.notesnook.com")
}}
onLoad={() => {
- if (onLoadMedia) onLoadMedia();
if (nonce && nonce > 0) setIsLoading(false);
}}
onContentChange={onContentChange}
@@ -437,8 +382,11 @@ export function Editor(props: EditorProps) {
const container = document.getElementById("dialogContainer");
if (!(container instanceof HTMLElement)) return;
- const dataurl =
- data.bloburl || (await downloadAttachment(hash, "base64"));
+ const dataurl = await downloadAttachment(
+ hash,
+ "base64",
+ id.toString()
+ );
if (!dataurl)
return showToast("error", "This image cannot be previewed.");
@@ -455,7 +403,7 @@ export function Editor(props: EditorProps) {
);
} else if (attachment && onPreviewDocument) {
onPreviewDocument({ hash });
- const blob = await downloadAttachment(hash, "blob");
+ const blob = await downloadAttachment(hash, "blob", id.toString());
if (!blob) return;
onPreviewDocument({ url: URL.createObjectURL(blob), hash });
}
@@ -467,6 +415,14 @@ export function Editor(props: EditorProps) {
editor.current?.attachFile(file);
});
}}
+ onGetAttachmentData={(attachment) => {
+ console.log("ID", id);
+ return downloadAttachment(
+ attachment.hash,
+ attachment.type === "web-clip" ? "text" : "base64",
+ id.toString()
+ );
+ }}
onAttachFile={async (file) => {
const result = await attachFile(file);
if (!result) return;
diff --git a/apps/web/src/components/editor/picker.ts b/apps/web/src/components/editor/picker.ts
index d209adb71..5bb64ad81 100644
--- a/apps/web/src/components/editor/picker.ts
+++ b/apps/web/src/components/editor/picker.ts
@@ -25,6 +25,7 @@ import { TaskManager } from "../../common/task-manager";
import { isUserPremium } from "../../hooks/use-is-user-premium";
import { showToast } from "../../utils/toast";
import { showFilePicker } from "../../utils/file-picker";
+import { Attachment } from "@notesnook/editor";
const FILE_SIZE_LIMIT = 500 * 1024 * 1024;
const IMAGE_SIZE_LIMIT = 50 * 1024 * 1024;
@@ -80,33 +81,54 @@ export async function reuploadAttachment(
}
/**
- * @param {File} selectedFile
+ * @param {File} file
* @returns
*/
-async function pickFile(selectedFile: File, options?: AddAttachmentOptions) {
+async function pickFile(
+ file: File,
+ options?: AddAttachmentOptions
+): Promise<Attachment | undefined> {
try {
- if (selectedFile.size > FILE_SIZE_LIMIT)
+ if (file.size > FILE_SIZE_LIMIT)
throw new Error("File too big. You cannot add files over 500 MB.");
- if (!selectedFile) return;
+ if (!file) return;
- return await addAttachment(selectedFile, undefined, options);
+ const hash = await addAttachment(file, options);
+ return {
+ type: "file",
+ filename: file.name,
+ hash,
+ mime: file.type,
+ size: file.size
+ };
} catch (e) {
showToast("error", `${(e as Error).message}`);
}
}
/**
- * @param {File} selectedImage
+ * @param {File} file
* @returns
*/
-async function pickImage(selectedImage: File, options?: AddAttachmentOptions) {
+async function pickImage(
+ file: File,
+ options?: AddAttachmentOptions
+): Promise<Attachment | undefined> {
try {
- if (selectedImage.size > IMAGE_SIZE_LIMIT)
+ if (file.size > IMAGE_SIZE_LIMIT)
throw new Error("Image too big. You cannot add images over 50 MB.");
- if (!selectedImage) return;
+ if (!file) return;
- const dataurl = await toDataURL(selectedImage);
- return await addAttachment(selectedImage, dataurl, options);
+ const hash = await addAttachment(file, options);
+ const dimensions = await getImageDimensions(file);
+ return {
+ type: "image",
+ filename: file.name,
+ hash,
+ mime: file.type,
+ size: file.size,
+ ...dimensions
+ };
} catch (e) {
showToast("error", (e as Error).message);
}
@@ -118,12 +140,6 @@ async function getEncryptionKey(): Promise<SerializedKey> {
return key;
}
-async function toDataURL(file: File): Promise<string> {
- const buffer = await file.arrayBuffer();
- const base64 = Buffer.from(buffer).toString("base64");
- return `data:${file.type};base64,${base64}`;
-}
-
export type AttachmentProgress = {
hash: string;
type: "encrypt" | "download" | "upload";
@@ -131,15 +147,6 @@ export type AttachmentProgress = {
loaded: number;
};
-export type Attachment = {
- hash: string;
- filename: string;
- mime: string;
- size: number;
- dataurl?: string;
- bloburl?: string;
-};
-
type AddAttachmentOptions = {
expectedFileHash?: string;
showProgress?: boolean;
@@ -148,9 +155,8 @@ type AddAttachmentOptions = {
async function addAttachment(
file: File,
- dataurl: string | undefined,
options: AddAttachmentOptions = {}
-): Promise<Attachment> {
+): Promise<string> {
const { default: FS } = await import("../../interfaces/fs");
const { expectedFileHash, showProgress = true } = options;
let forceWrite = options.forceWrite;
@@ -187,13 +193,7 @@ async function addAttachment(
});
}
- return {
- hash: hash,
- filename: file.name,
- mime: file.type,
- size: file.size,
- dataurl
- };
+ return hash;
};
const result = showProgress
@@ -204,7 +204,10 @@ async function addAttachment(
return result;
}
-function withProgress<T>(file: File, action: () => Promise<T>): Promise<T> {
+function withProgress<T>(
+ file: File,
+ action: () => Promise<T>
+): Promise<T | Error> {
return TaskManager.startTask({
type: "modal",
title: "Encrypting attachment",
@@ -226,3 +229,19 @@ function withProgress<T>(file: File, action: () => Promise<T>): Promise<T> {
}
});
}
+
+function getImageDimensions(file: File) {
+ return new Promise<{ width: number; height: number } | undefined>(
+ (resolve) => {
+ const img = new Image();
+ img.onload = () => {
+ const { naturalWidth: width, naturalHeight: height } = img;
+ resolve({ width, height });
+ };
+ img.onerror = () => {
+ resolve(undefined);
+ };
+ img.src = URL.createObjectURL(file);
+ }
+ );
+}
diff --git a/apps/web/src/components/editor/tiptap.tsx b/apps/web/src/components/editor/tiptap.tsx
index 64797724a..e79771f62 100644
--- a/apps/web/src/components/editor/tiptap.tsx
+++ b/apps/web/src/components/editor/tiptap.tsx
@@ -34,11 +34,11 @@ import {
getTotalWords,
countWords,
getFontById,
- TiptapOptions
+ TiptapOptions,
+ Attachment
} from "@notesnook/editor";
import { Box, Flex } from "@theme-ui/components";
import { PropsWithChildren, useEffect, useMemo, useRef, useState } from "react";
-import { Attachment } from "./picker";
import { IEditor } from "./types";
import {
useConfigureEditor,
@@ -57,7 +57,6 @@ import { store as editorstore } from "../../stores/editor-store";
import { ScopedThemeProvider } from "../theme-provider";
import { writeText } from "clipboard-polyfill";
import { useStore as useThemeStore } from "../../stores/theme-store";
-import { toBlobURL } from "@notesnook/editor/dist/utils/downloader";
type OnChangeHandler = (
id: string | undefined,
@@ -72,6 +71,7 @@ type TipTapProps = {
onInsertAttachment?: (type: AttachmentType) => void;
onDownloadAttachment?: (attachment: Attachment) => void;
onPreviewAttachment?: (attachment: Attachment) => void;
+ onGetAttachmentData?: (attachment: Attachment) => Promise<string | undefined>;
onAttachFile?: (file: File) => void;
onFocus?: () => void;
content?: () => string | undefined;
@@ -117,6 +117,7 @@ function TipTap(props: TipTapProps) {
onInsertAttachment,
onDownloadAttachment,
onPreviewAttachment,
+ onGetAttachmentData,
onAttachFile,
onContentChange,
onFocus = () => {},
@@ -282,7 +283,8 @@ function TipTap(props: TipTapProps) {
onOpenLink: (url) => {
window.open(url, "_blank");
return true;
- }
+ },
+ getAttachmentData: onGetAttachmentData
};
}, [readonly, nonce, doubleSpacedLines, dateFormat, timeFormat]);
@@ -420,14 +422,6 @@ function toIEditor(editor: Editor): IEditor {
}),
undo: () => editor.current?.commands.undo(),
redo: () => editor.current?.commands.redo(),
- getMediaHashes: () => {
- if (!editor.current) return [];
- const hashes: string[] = [];
- editor.current.state.doc.descendants((n) => {
- if (typeof n.attrs.hash === "string") hashes.push(n.attrs.hash);
- });
- return hashes;
- },
updateContent: (content) => {
const { from, to } = editor.state.selection;
editor.current
@@ -443,27 +437,18 @@ function toIEditor(editor: Editor): IEditor {
})
.run();
},
- attachFile: (file: Attachment) => {
- if (file.dataurl) {
- editor.current?.commands.insertImage({
- ...file,
- bloburl: toBlobURL(file.dataurl, file.hash)
- });
- } else editor.current?.commands.insertAttachment(file);
- },
- loadWebClip: (hash, src) =>
- editor.current?.commands.updateWebClip({ hash }, { src }),
- loadImage: (hash, dataurl) =>
- editor.current?.commands.updateImage(
- { hash },
- { hash, bloburl: toBlobURL(dataurl, hash), preventUpdate: true }
- ),
- sendAttachmentProgress: (hash, type, progress) =>
- editor.current?.commands.setAttachmentProgress({
- hash,
- type,
- progress
- })
+ attachFile: (file: Attachment) =>
+ file.type === "image"
+ ? editor.current?.commands.insertImage(file)
+ : editor.current?.commands.insertAttachment(file),
+ sendAttachmentProgress: (hash, progress) =>
+ editor.current?.commands.updateAttachment(
+ {
+ hash,
+ progress
+ },
+ { query: (a) => a.hash === hash, preventUpdate: true }
+ )
};
}
diff --git a/apps/web/src/components/editor/types.ts b/apps/web/src/components/editor/types.ts
index 8821d80ea..9a07d3dae 100644
--- a/apps/web/src/components/editor/types.ts
+++ b/apps/web/src/components/editor/types.ts
@@ -17,7 +17,7 @@ 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 { Attachment } from "./picker";
+import { Attachment } from "@notesnook/editor";
export type NoteStatistics = {
words: {
@@ -33,14 +33,7 @@ export interface IEditor {
}) => void;
undo: () => void;
redo: () => void;
- getMediaHashes: () => string[];
updateContent: (content: string) => void;
attachFile: (file: Attachment) => void;
- loadWebClip: (hash: string, html: string) => void;
- loadImage: (hash: string, src: string) => void;
- sendAttachmentProgress: (
- hash: string,
- type: "download" | "upload" | "encrypt",
- progress: number
- ) => void;
+ sendAttachmentProgress: (hash: string, progress: number) => void;
}