Skip to content

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

mobile: fix loading images in realtime sync

ammarahm-edf56cbfc1421b
diff --git a/apps/mobile/app/screens/editor/index.tsx b/apps/mobile/app/screens/editor/index.tsx
index a852b31a6..0d83e1dd1 100755
--- a/apps/mobile/app/screens/editor/index.tsx
+++ b/apps/mobile/app/screens/editor/index.tsx
@@ -107,6 +107,8 @@ const Editor = React.memo(
attachmentType: string;
}) => {
if (groupId !== editor.note.current?.id) return;
+ console.log("marking image as loaded", hash);
+ editorController.current.markImageLoaded(hash);
if (attachmentType === "webclip") {
editor.commands.updateWebclip({
hash: hash,
diff --git a/apps/mobile/app/screens/editor/tiptap/use-editor.ts b/apps/mobile/app/screens/editor/tiptap/use-editor.ts
index 7d2840986..5714d6342 100644
--- a/apps/mobile/app/screens/editor/tiptap/use-editor.ts
+++ b/apps/mobile/app/screens/editor/tiptap/use-editor.ts
@@ -17,6 +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 { EVENTS } from "@notesnook/core/common";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import WebView from "react-native-webview";
import { db } from "../../../common/database";
@@ -29,6 +30,7 @@ import {
openVault
} from "../../../services/event-manager";
import Navigation from "../../../services/navigation";
+import SettingsService from "../../../services/settings";
import { TipManager } from "../../../services/tip-manager";
import { useEditorStore } from "../../../stores/use-editor-store";
import { useNoteStore } from "../../../stores/use-notes-store";
@@ -50,8 +52,6 @@ import {
makeSessionId,
post
} from "./utils";
-import { EVENTS } from "@notesnook/core/common";
-import SettingsService from "../../../services/settings";
export const useEditor = (
editorId = "",
@@ -76,6 +76,8 @@ export const useEditor = (
const saveCount = useRef(0);
const lastContentChangeTime = useRef<number>(0);
const lock = useRef(false);
+ const attachedImages = useRef<{ [name: string]: any }>({});
+ const loadedImages = useRef<{ [name: string]: any }>({});
const postMessage = useCallback(
async <T>(type: string, data: T) =>
@@ -154,7 +156,9 @@ export const useEditor = (
async (resetState = true) => {
currentNote.current?.id && db.fs.cancel(currentNote.current.id);
currentNote.current = null;
+ attachedImages.current = [];
currentContent.current = null;
+ clearTimeout(timers.current["loading-images"]);
sessionHistoryId.current = undefined;
saveCount.current = 0;
useEditorStore.getState().setReadonly(false);
@@ -290,6 +294,52 @@ export const useEditor = (
}
}, []);
+ const getImagesToLoad = () => {
+ if (!currentNote.current?.id) return [];
+ const currentImages = [
+ ...(db.attachments?.ofNote(currentNote.current?.id, "images") || []),
+ ...(db.attachments?.ofNote(currentNote.current?.id, "webclips") || [])
+ ];
+ if (!currentImages || currentImages?.length === 0) return [];
+ const imagesToLoad: any[] = [];
+ for (const image of currentImages) {
+ if (!loadedImages.current[image.metadata.hash]) {
+ loadedImages.current[image.metadata.hash] = false;
+ imagesToLoad.push(image);
+ }
+ attachedImages.current[image.metadata.hash] = image;
+ }
+ return imagesToLoad;
+ };
+
+ const markImageLoaded = (hash: string) => {
+ const attachment = attachedImages.current[hash];
+ if (attachment) {
+ loadedImages.current[hash] = true;
+ }
+ };
+
+ const loadImages = useCallback(() => {
+ if (!currentNote.current?.id) return;
+ const timerId = "loading-images";
+ clearTimeout(timers.current[timerId]);
+ timers.current[timerId] = setTimeout(() => {
+ if (!currentNote.current?.id) return;
+ if (currentNote.current?.content?.isPreview) {
+ db.content?.downloadMedia(
+ currentNote.current?.id,
+ currentNote.current.content,
+ true
+ );
+ } else {
+ const images = getImagesToLoad();
+ if (images.length > 0) {
+ db.attachments?.downloadMedia(currentNote.current?.id, images);
+ }
+ }
+ }, 300);
+ }, []);
+
const loadNote = useCallback(
async (
item: Omit<NoteType, "type"> & {
@@ -314,6 +364,7 @@ export const useEditor = (
if (!item.forced && currentNote.current?.id === item.id) return;
isDefaultEditor && editorState.setCurrentlyEditingNote(item.id);
overlay(true, item);
+ attachedImages.current = [];
currentNote.current && (await reset(false));
await loadContent(item as NoteType);
lastContentChangeTime.current = item.dateEdited;
@@ -333,25 +384,17 @@ export const useEditor = (
loadImages();
}
},
- [commands, isDefaultEditor, loadContent, overlay, postMessage, reset]
+ [
+ commands,
+ isDefaultEditor,
+ loadContent,
+ loadImages,
+ overlay,
+ postMessage,
+ reset
+ ]
);
- const loadImages = () => {
- if (!currentNote.current?.id) return;
- setTimeout(() => {
- if (!currentNote.current?.id) return;
- if (currentNote.current?.content?.isPreview) {
- db.content?.downloadMedia(
- currentNote.current?.id,
- currentNote.current.content,
- true
- );
- } else {
- db.attachments?.downloadMedia(currentNote.current?.id);
- }
- }, 300);
- };
-
const lockNoteWithVault = useCallback((note: NoteType) => {
eSendEvent(eClearEditor);
openVault({
@@ -378,6 +421,7 @@ export const useEditor = (
return;
lock.current = true;
+
if (data.type === "tiptap") {
if (!currentNote.current.locked && isContentEncrypted) {
lockNoteWithVault(note);
@@ -408,16 +452,19 @@ export const useEditor = (
}
await commands.setStatus(timeConverter(note.dateEdited), "Saved");
}
- db.eventManager.subscribe(
- EVENTS.syncCompleted,
- async () => {
- loadImages();
- },
- true
- );
lock.current = false;
+ if (data.type === "tiptap") {
+ loadImages();
+ db.eventManager.subscribe(
+ EVENTS.syncCompleted,
+ () => {
+ loadImages();
+ },
+ true
+ );
+ }
},
- [commands, postMessage, lockNoteWithVault]
+ [loadImages, lockNoteWithVault, postMessage, commands]
);
useEffect(() => {
@@ -560,6 +607,7 @@ export const useEditor = (
onReady,
saveContent,
onContentChanged,
- editorId: editorId
+ editorId: editorId,
+ markImageLoaded
};
};
diff --git a/apps/mobile/app/stores/use-user-store.ts b/apps/mobile/app/stores/use-user-store.ts
index 8111dc00a..c082a856d 100644
--- a/apps/mobile/app/stores/use-user-store.ts
+++ b/apps/mobile/app/stores/use-user-store.ts
@@ -47,8 +47,10 @@ export const useUserStore = create<UserStore>((set) => ({
verifyUser: false,
setUser: (user) => set({ user: user }),
setPremium: (premium) => set({ premium: premium }),
- setSyncing: (syncing, status = SyncStatus.Passed) =>
- set({ syncing: syncing, lastSyncStatus: status }),
+ setSyncing: (syncing, status = SyncStatus.Passed) => {
+ console.log("set syncing", syncing, status);
+ set({ syncing: syncing, lastSyncStatus: status });
+ },
setLastSynced: (lastSynced) => set({ lastSynced: lastSynced }),
setVerifyUser: (verified) => set({ verifyUser: verified }),
lastSyncStatus: SyncStatus.Never
diff --git a/packages/core/collections/attachments.js b/packages/core/collections/attachments.js
index 54bff0e0d..efb28fcce 100644
--- a/packages/core/collections/attachments.js
+++ b/packages/core/collections/attachments.js
@@ -294,10 +294,12 @@ export default class Attachments extends Collection {
return { key, metadata };
}
- async downloadMedia(noteId) {
- const attachments = this.media.filter((attachment) =>
- hasItem(attachment.noteIds, noteId)
- );
+ async downloadMedia(noteId, attachments) {
+ if (!attachments) {
+ attachments = this.media.filter((attachment) =>
+ hasItem(attachment.noteIds, noteId)
+ );
+ }
try {
for (let i = 0; i < attachments.length; i++) {
const attachment = attachments[i];
diff --git a/packages/editor-mobile/src/components/styles.module.css b/packages/editor-mobile/src/components/styles.module.css
index 4bb3b2162..eec05a88f 100644
--- a/packages/editor-mobile/src/components/styles.module.css
+++ b/packages/editor-mobile/src/components/styles.module.css
@@ -19,3 +19,11 @@
.titleBar::placeholder {
color: var(--nn_placeholder);
}
+
+.container {
+ -ms-overflow-style: none;
+ scrollbar-width: none;
+}
+.container::-webkit-scrollbar {
+ display: none;
+}
diff --git a/packages/editor-mobile/src/components/tags.tsx b/packages/editor-mobile/src/components/tags.tsx
index 0378ce731..db9a01132 100644
--- a/packages/editor-mobile/src/components/tags.tsx
+++ b/packages/editor-mobile/src/components/tags.tsx
@@ -39,6 +39,7 @@ export default function Tags(): JSX.Element {
return (
<div
+ className={styles.container}
style={{
padding: "0px 12px",
display: "flex",