From f0a1b417944b06c4b77ba404a089ca0ea7c0a3a8 Mon Sep 17 00:00:00 2001 From: Henry Heino <46334387+personalizedrefrigerator@users.noreply.github.com> Date: Mon, 27 Nov 2023 11:14:04 -0800 Subject: [PATCH] Mobile: Resolves #9377: Don't attach empty drawings when a user exits without saving (#9386) --- .../ImageEditor/js-draw/createJsDrawEditor.ts | 4 -- .../app-mobile/components/screens/Note.tsx | 66 +++++++++++++------ 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/packages/app-mobile/components/NoteEditor/ImageEditor/js-draw/createJsDrawEditor.ts b/packages/app-mobile/components/NoteEditor/ImageEditor/js-draw/createJsDrawEditor.ts index e96851f21c..530cdcb17c 100644 --- a/packages/app-mobile/components/NoteEditor/ImageEditor/js-draw/createJsDrawEditor.ts +++ b/packages/app-mobile/components/NoteEditor/ImageEditor/js-draw/createJsDrawEditor.ts @@ -156,10 +156,6 @@ export const createJsDrawEditor = ( // Load from a template if no initial data if (svgData === '') { await applyTemplateToEditor(editor, templateData); - - // The editor expects to be saved initially (without - // unsaved changes). Save now. - saveNow(); } else { await editor.loadFromSVG(svgData); } diff --git a/packages/app-mobile/components/screens/Note.tsx b/packages/app-mobile/components/screens/Note.tsx index ca7d705c27..61061a4bea 100644 --- a/packages/app-mobile/components/screens/Note.tsx +++ b/packages/app-mobile/components/screens/Note.tsx @@ -749,7 +749,11 @@ class NoteScreenComponent extends BaseScreenComponent { if (this.useEditorBeta()) { // The beta editor needs to be explicitly informed of changes // to the note's body - this.editorRef.current.insertText(newText); + if (this.editorRef.current) { + this.editorRef.current.insertText(newText); + } else { + logger.error(`Tried to attach resource ${resource.id} to the note when the editor is not visible!`); + } } } else { newNote.body += `\n${resourceTag}`; @@ -814,31 +818,34 @@ class NoteScreenComponent extends BaseScreenComponent { }, 'image'); } - private drawPicture_onPress = async () => { - // Create a new empty drawing and attach it now. - const resource = await this.attachNewDrawing(''); - await this.editDrawing(resource); - }; - private async updateDrawing(svgData: string) { let resource: ResourceEntity|null = this.state.imageEditorResource; if (!resource) { - throw new Error('No resource is loaded in the editor'); + resource = await this.attachNewDrawing(svgData); + + // Set resouce and file path to allow + // 1. subsequent saves to update the resource + // 2. the editor to load from the resource's filepath (can happen + // if the webview is reloaded). + this.setState({ + imageEditorResourceFilepath: Resource.fullPath(resource), + imageEditorResource: resource, + }); + } else { + logger.info('Saving drawing to resource', resource.id); + + const tempFilePath = join(Setting.value('tempDir'), uuid.createNano()); + await shim.fsDriver().writeFile(tempFilePath, svgData, 'utf8'); + + resource = await Resource.updateResourceBlobContent( + resource.id, + tempFilePath, + ); + await shim.fsDriver().remove(tempFilePath); + + await this.refreshResource(resource); } - - logger.info('Saving drawing to resource', resource.id); - - const tempFilePath = join(Setting.value('tempDir'), uuid.createNano()); - await shim.fsDriver().writeFile(tempFilePath, svgData, 'utf8'); - - resource = await Resource.updateResourceBlobContent( - resource.id, - tempFilePath, - ); - await shim.fsDriver().remove(tempFilePath); - - await this.refreshResource(resource); } private onSaveDrawing = async (svgData: string) => { @@ -849,6 +856,23 @@ class NoteScreenComponent extends BaseScreenComponent { this.setState({ showImageEditor: false }); }; + private drawPicture_onPress = async () => { + if (this.state.mode === 'edit') { + // Create a new empty drawing and attach it now, before the image editor is opened. + // With the present structure of Note.tsx, the we can't use this.editorRef while + // the image editor is open, and thus can't attach drawings at the cursor locaiton. + const resource = await this.attachNewDrawing(''); + await this.editDrawing(resource); + } else { + logger.info('Showing image editor...'); + this.setState({ + showImageEditor: true, + imageEditorResourceFilepath: null, + imageEditorResource: null, + }); + } + }; + private async editDrawing(item: BaseItem) { const filePath = Resource.fullPath(item); this.setState({