Skip to content

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

refactor: abstract away the att. handling logic

thecodrra127af1456f7
diff --git a/apps/web/src/components/editor/index.tsx b/apps/web/src/components/editor/index.tsx
index fb4fdb354..f9be0c658 100644
--- a/apps/web/src/components/editor/index.tsx
+++ b/apps/web/src/components/editor/index.tsx
@@ -12,9 +12,15 @@ import Tiptap from "./tiptap";
import Header from "./header";
import { Attachment } from "../icons";
import { useEditorInstance } from "./context";
-import { attachFile, insertAttachment } from "./plugins/picker";
+import {
+ attachFile,
+ AttachmentProgress,
+ insertAttachment,
+} from "./plugins/picker";
import { DropEvent } from "react-dropzone";
import { downloadAttachment } from "../../common/attachments";
+import { EV, EVENTS } from "notes-core/common";
+import { db } from "../../common/db";
function updateWordCount(counter?: CharacterCounter) {
AppEventManager.publish(
@@ -60,16 +66,21 @@ export default function EditorManager({
useEffect(() => {
if (!isNewSession) return;
- editorstore.newSession(nonce);
- setContent("");
+ (async function () {
+ await editorstore.newSession(nonce);
+ setContent("");
+ })();
}, [isNewSession, nonce]);
useEffect(() => {
+ if (!noteId) return;
+
(async function () {
const content = await editorstore.get().getSessionContent();
setContent(content?.data);
+ if (noteId && content) await db.attachments?.downloadImages(noteId);
})();
- }, [isPreviewMode]);
+ }, [noteId, isPreviewMode]);
useEffect(() => {
if (!isOldSession) return;
@@ -79,6 +90,10 @@ export default function EditorManager({
let content = await editorstore.get().getSessionContent();
setContent(content?.data);
+ // editorstore.set(
+ // (state: any) => (state.session.state = SESSION_STATES.stale)
+ // );
+ if (noteId && content) await db.attachments?.downloadImages(noteId);
})();
}, [noteId, isOldSession]);
@@ -151,9 +166,42 @@ type EditorProps = {
};
function Editor({ content, readonly, focusMode, onRequestFocus }: EditorProps) {
const editor = useEditorInstance();
+
useEffect(() => {
- console.log("Rerendering editor.");
- }, []);
+ if (!editor) return;
+
+ const event = AppEventManager.subscribe(
+ AppEvents.UPDATE_ATTACHMENT_PROGRESS,
+ ({ hash, loaded, total, type }: AttachmentProgress) => {
+ editor.sendAttachmentProgress(
+ hash,
+ type,
+ Math.round((loaded / total) * 100)
+ );
+ }
+ );
+
+ const mediaAttachmentDownloadedEvent = EV.subscribe(
+ EVENTS.mediaAttachmentDownloaded,
+ ({
+ groupId,
+ hash,
+ src,
+ }: {
+ groupId?: string;
+ hash: string;
+ src: string;
+ }) => {
+ if (groupId?.startsWith("monograph")) return;
+ editor.loadImage(hash, src);
+ }
+ );
+
+ return () => {
+ event.unsubscribe();
+ mediaAttachmentDownloadedEvent.unsubscribe();
+ };
+ }, [editor]);
return (
<>
diff --git a/apps/web/src/components/editor/tiptap.tsx b/apps/web/src/components/editor/tiptap.tsx
index 8d3bd6dbb..1e3c5baee 100644
--- a/apps/web/src/components/editor/tiptap.tsx
+++ b/apps/web/src/components/editor/tiptap.tsx
@@ -77,7 +77,7 @@ function TipTap(props: TipTapProps) {
return true;
},
},
- [content, readonly, theme, onInsertAttachment, onDownloadAttachment]
+ [content, readonly, theme]
);
useEffect(() => {
@@ -90,44 +90,6 @@ function TipTap(props: TipTapProps) {
if (isSearching && !isEditorSearching) toggleSearch();
}, [toggleSearch, editor?.storage.searchreplace?.isSearching]);
- useEffect(() => {
- if (!editor) return;
-
- const event = AppEventManager.subscribe(
- AppEvents.UPDATE_ATTACHMENT_PROGRESS,
- (progress: AttachmentProgress) => {
- setTimeout(() =>
- editor.commands.setAttachmentProgress({
- hash: progress.hash,
- type: progress.type,
- progress: Math.round((progress.loaded / progress.total) * 100),
- })
- );
- }
- );
-
- const mediaAttachmentDownloadedEvent = EV.subscribe(
- EVENTS.mediaAttachmentDownloaded,
- ({
- groupId,
- hash,
- src,
- }: {
- groupId?: string;
- hash: string;
- src: string;
- }) => {
- console.log(src, hash);
- if (groupId?.startsWith("monograph")) return;
- setTimeout(() => editor?.commands?.updateImage({ hash, src }));
- }
- );
- return () => {
- event.unsubscribe();
- mediaAttachmentDownloadedEvent.unsubscribe();
- };
- }, [editor]);
-
return (
<Flex sx={{ flex: 1, flexDirection: "column" }}>
<Portal containerId={toolbarContainerId}>
@@ -184,5 +146,12 @@ function toIEditor(editor: Editor): IEditor {
editor.commands.insertImage({ ...file, src: file.dataurl });
} else editor.commands.insertAttachment(file);
},
+ loadImage: (hash, src) => editor.commands.updateImage({ hash, src }),
+ sendAttachmentProgress: (hash, type, progress) =>
+ editor.commands.setAttachmentProgress({
+ hash,
+ type: type as any,
+ progress,
+ }),
};
}
diff --git a/apps/web/src/components/editor/titlebox.js b/apps/web/src/components/editor/titlebox.js
index 960225adf..0ea60b45a 100644
--- a/apps/web/src/components/editor/titlebox.js
+++ b/apps/web/src/components/editor/titlebox.js
@@ -32,11 +32,6 @@ function TitleBox(props) {
fontFamily: "heading",
fontSize: ["1.625em", "1.625em", "2.625em"],
fontWeight: "heading",
- border: "none",
- resize: "none",
- }}
- onKeyDown={(e) => {
- if (e.key === "Enter") e.preventDefault();
}}
onChange={(e) => {
setTitle(e.target.value);
diff --git a/apps/web/src/components/editor/types.ts b/apps/web/src/components/editor/types.ts
index 9512e39a5..3e05ef680 100644
--- a/apps/web/src/components/editor/types.ts
+++ b/apps/web/src/components/editor/types.ts
@@ -10,4 +10,10 @@ export interface IEditor {
undo: () => void;
redo: () => void;
attachFile: (file: Attachment) => void;
+ loadImage: (hash: string, src: string) => void;
+ sendAttachmentProgress: (
+ hash: string,
+ type: string,
+ progress: number
+ ) => void;
}