From 28616530b886303fcc9ba1e7975af08aa7727857 Mon Sep 17 00:00:00 2001 From: Ammar Ahmed Date: Fri, 24 Jun 2022 11:51:29 +0500 Subject: [PATCH] add back handler in editor --- .../src/screens/editor/tiptap/commands.ts | 18 ++++++++++++------ .../screens/editor/tiptap/useEditorEvents.ts | 5 +++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/apps/mobile/src/screens/editor/tiptap/commands.ts b/apps/mobile/src/screens/editor/tiptap/commands.ts index b1906bede..7c21fd3ff 100644 --- a/apps/mobile/src/screens/editor/tiptap/commands.ts +++ b/apps/mobile/src/screens/editor/tiptap/commands.ts @@ -51,9 +51,9 @@ class Commands { this.ref = ref; } - async doAsync(job: string) { + async doAsync(job: string) { if (!this.ref) return false; - return await call(this.ref, fn(job)); + return call(this.ref, fn(job)) as Promise; } focus = async () => { @@ -132,22 +132,28 @@ statusBar.current.set({date:"",saved:""}); }; insertAttachment = async (attachment: Attachment) => { - this.doAsync(`editor && editor.commands.insertAttachment(${JSON.stringify(attachment)})`); + await this.doAsync(`editor && editor.commands.insertAttachment(${JSON.stringify(attachment)})`); }; setAttachmentProgress = async (attachmentProgress: AttachmentProgress) => { - this.doAsync( + await this.doAsync( `editor && editor.commands.setAttachmentProgress(${JSON.stringify(attachmentProgress)})` ); }; insertImage = async (image: ImageAttributes) => { console.log('image data', image); - this.doAsync(`editor && editor.commands.insertImage(${JSON.stringify(image)})`); + await this.doAsync(`editor && editor.commands.insertImage(${JSON.stringify(image)})`); }; updateImage = async (image: ImageAttributes) => { - this.doAsync(`editor && editor.commands.updateImage(${JSON.stringify(image)})`); + await this.doAsync(`editor && editor.commands.updateImage(${JSON.stringify(image)})`); + }; + + handleBack = async () => { + return this.doAsync( + `response = window.dispatchEvent(new Event("handleBackPress",{cancelable:true}));` + ); }; //todo add replace image function } diff --git a/apps/mobile/src/screens/editor/tiptap/useEditorEvents.ts b/apps/mobile/src/screens/editor/tiptap/useEditorEvents.ts index a61dc3a75..ddbf13e1d 100644 --- a/apps/mobile/src/screens/editor/tiptap/useEditorEvents.ts +++ b/apps/mobile/src/screens/editor/tiptap/useEditorEvents.ts @@ -130,6 +130,11 @@ export const useEditorEvents = (editor: useEditorType) => { }, [currentEditingNote, fullscreen, isPremium, readonly, editor.sessionId, editor.loading]); const onBackPress = useCallback(async () => { + const editorHandledBack = await editor.commands.handleBack(); + if (editorHandledBack) { + logger.info('editor handled back event'); + return; + } setTimeout(async () => { if (deviceMode !== 'mobile' && fullscreen) { if (fullscreen) {