From dea6085a11e1b0067131d6c5127e5ea05841c365 Mon Sep 17 00:00:00 2001 From: thenanyu Date: Sat, 6 Jul 2019 21:45:50 -0700 Subject: [PATCH] refactor document dirty and empty logic --- app/models/Document.js | 11 ----------- app/scenes/Document/Document.js | 25 +++++++++++++++++------- app/scenes/Document/components/Header.js | 4 +++- server/api/documents.js | 1 - 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/app/models/Document.js b/app/models/Document.js index 55c10296..271f6ab9 100644 --- a/app/models/Document.js +++ b/app/models/Document.js @@ -72,17 +72,6 @@ export default class Document extends BaseModel { return !this.publishedAt; } - @computed - get isEmpty(): boolean { - // Check if the document title has been modified and user generated content exists - return this.text.replace(/^#/, '').trim().length === 0; - } - - @computed - get allowSave(): boolean { - return !this.isEmpty && !this.isSaving; - } - @action share = async () => { const res = await client.post('/shares.create', { documentId: this.id }); diff --git a/app/scenes/Document/Document.js b/app/scenes/Document/Document.js index fc0c90fc..502eddbe 100644 --- a/app/scenes/Document/Document.js +++ b/app/scenes/Document/Document.js @@ -79,6 +79,7 @@ class DocumentScene extends React.Component { @observable isSaving: boolean = false; @observable isPublishing: boolean = false; @observable isDirty: boolean = false; + @observable isEmpty: boolean = true; @observable notFound: boolean = false; @observable moveModalOpen: boolean = false; @@ -166,6 +167,7 @@ class DocumentScene extends React.Component { } this.isDirty = false; + this.isEmpty = false; const document = this.document; @@ -226,17 +228,19 @@ class DocumentScene extends React.Component { let document = this.document; if (!document) return; + // prevent saves when we're already saving + if (document.isSaving ) return; + // get the latest version of the editor text value const text = this.getEditorText ? this.getEditorText() : document.text; + // prevent save before anything has been written + if (text.trim() === "#") return; + // prevent autosave if nothing has changed if (options.autosave && document.text.trim() === text.trim()) return; document.text = text; - if (!document.allowSave) return; - - // prevent autosave before anything has been written - if (options.autosave && !document.title && !document.id) return; let isNew = !document.id; this.isSaving = true; @@ -261,9 +265,15 @@ class DocumentScene extends React.Component { updateIsDirty = debounce(() => { const document = this.document; + const editorText = this.getEditorText().trim() + + this.isEmpty = editorText === "#"; + this.isDirty = !!document && editorText !== document.text.trim() + + console.log({editorText}) + console.log({doctext: document.text.trim()}) + - this.isDirty = - !!document && this.getEditorText().trim() !== document.text.trim(); }, IS_DIRTY_DELAY); onImageUploadStart = () => { @@ -377,7 +387,8 @@ class DocumentScene extends React.Component { isEditing={this.isEditing} isSaving={this.isSaving} isPublishing={this.isPublishing} - savingIsDisabled={!document.allowSave} + publishingIsDisabled={document.isSaving || this.isPublishing || this.isEmpty} + savingIsDisabled={document.isSaving || this.isEmpty} onDiscard={this.onDiscard} onSave={this.onSave} /> diff --git a/app/scenes/Document/components/Header.js b/app/scenes/Document/components/Header.js index c42efc5d..7b7871d9 100644 --- a/app/scenes/Document/components/Header.js +++ b/app/scenes/Document/components/Header.js @@ -30,6 +30,7 @@ type Props = { isEditing: boolean, isSaving: boolean, isPublishing: boolean, + publishingIsDisabled: boolean, savingIsDisabled: boolean, onDiscard: () => *, onSave: ({ @@ -99,6 +100,7 @@ class Header extends React.Component { isPublishing, isSaving, savingIsDisabled, + publishingIsDisabled, auth, } = this.props; const canShareDocuments = @@ -171,7 +173,7 @@ class Header extends React.Component {