1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-24 08:12:24 +02:00

Mobile: Resolves #9377: Don't attach empty drawings when a user exits without saving (#9386)

This commit is contained in:
Henry Heino 2023-11-27 11:14:04 -08:00 committed by GitHub
parent 02982464a6
commit f0a1b41794
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 25 deletions

View File

@ -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);
}

View File

@ -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({