From c140c64346549f3c6f1860711d16b6b12b206c15 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 16 Aug 2018 21:19:31 -0700 Subject: [PATCH] Use new RME callback to reduce amount of document serialization Account for new RME autoFocus prop in 5.0 --- app/scenes/Document/Document.js | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/app/scenes/Document/Document.js b/app/scenes/Document/Document.js index 977fa2e49..1686205cb 100644 --- a/app/scenes/Document/Document.js +++ b/app/scenes/Document/Document.js @@ -61,6 +61,7 @@ type Props = { class DocumentScene extends React.Component { savedTimeout: TimeoutID; viewTimeout: TimeoutID; + getEditorText: () => string; @observable editorComponent; @observable editCache: ?string; @@ -163,7 +164,19 @@ class DocumentScene extends React.Component { options: { done?: boolean, publish?: boolean, autosave?: boolean } = {} ) => { let document = this.document; - if (!document || !document.allowSave) return; + if (!document) return; + + // get the latest version of the editor text value + const text = this.getEditorText(); + + // prevent autosave if nothing has changed + if (options.autosave && document.text.trim() === text.trim()) return; + + document.updateData({ text }, true); + if (!document.allowSave) return; + + // prevent autosave before anything has been written + if (options.autosave && !document.title && !document.id) return; let isNew = !document.id; this.editCache = null; @@ -182,7 +195,7 @@ class DocumentScene extends React.Component { } }; - autosave = debounce(async () => { + autosave = debounce(() => { this.onSave({ done: false, autosave: true }); }, AUTOSAVE_INTERVAL); @@ -194,18 +207,10 @@ class DocumentScene extends React.Component { this.isUploading = false; }; - onChange = debounce(getText => { - let document = this.document; - if (!document) return; - - const text = getText(); - if (document.text.trim() === text.trim()) return; - document.updateData({ text }, true); - - // prevent autosave before anything has been written - if (!document.title && !document.id) return; + onChange = getEditorText => { + this.getEditorText = getEditorText; this.autosave(); - }, 250); + }; onDiscard = () => { let url; @@ -342,6 +347,7 @@ class DocumentScene extends React.Component { onCancel={this.onDiscard} onShowToast={this.onShowToast} readOnly={!this.isEditing} + autoFocus={!document.text} toc />