1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +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 Laurent Cozic
parent 86b470359d
commit 0a75480a32
2 changed files with 45 additions and 25 deletions

View File

@ -157,10 +157,6 @@ export const createJsDrawEditor = (
// Load from a template if no initial data // Load from a template if no initial data
if (svgData === '') { if (svgData === '') {
await applyTemplateToEditor(editor, templateData); await applyTemplateToEditor(editor, templateData);
// The editor expects to be saved initially (without
// unsaved changes). Save now.
saveNow();
} else { } else {
await editor.loadFromSVG(svgData); await editor.loadFromSVG(svgData);
} }

View File

@ -799,7 +799,11 @@ class NoteScreenComponent extends BaseScreenComponent {
if (this.useEditorBeta()) { if (this.useEditorBeta()) {
// The beta editor needs to be explicitly informed of changes // The beta editor needs to be explicitly informed of changes
// to the note's body // 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 { } else {
newNote.body += `\n${resourceTag}`; newNote.body += `\n${resourceTag}`;
@ -864,31 +868,34 @@ class NoteScreenComponent extends BaseScreenComponent {
}, 'image'); }, '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) { private async updateDrawing(svgData: string) {
let resource: ResourceEntity|null = this.state.imageEditorResource; let resource: ResourceEntity|null = this.state.imageEditorResource;
if (!resource) { 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) => { private onSaveDrawing = async (svgData: string) => {
@ -899,6 +906,23 @@ class NoteScreenComponent extends BaseScreenComponent {
this.setState({ showImageEditor: false }); 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) { private async editDrawing(item: BaseItem) {
const filePath = Resource.fullPath(item); const filePath = Resource.fullPath(item);
this.setState({ this.setState({