From b5a05be7262c2b21a2bb984f6f6d51198dc09679 Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Tue, 18 Jul 2017 23:29:20 -0700 Subject: [PATCH 1/2] Disable publish on empty documents --- frontend/models/Document.js | 20 +++++++++++++++++++- frontend/scenes/Document/Document.js | 5 ++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/frontend/models/Document.js b/frontend/models/Document.js index 2755820ea..db9bd6858 100644 --- a/frontend/models/Document.js +++ b/frontend/models/Document.js @@ -14,6 +14,8 @@ const parseHeader = text => { return firstLine.replace(/^#/, '').trim(); }; +const DEFAULT_TITLE = 'Untitled document'; + class Document { isSaving: boolean = false; hasPendingChanges: boolean = false; @@ -32,7 +34,7 @@ class Document { private: boolean = false; starred: boolean = false; text: string = ''; - title: string = 'Untitled document'; + title: string = ''; parentDocument: ?Document; updatedAt: string; updatedBy: User; @@ -70,6 +72,14 @@ class Document { return []; } + @computed get allowSave(): boolean { + // Check if the document title has been modified and user generated content exists + return ( + this.text.replace(new RegExp(`^\#$`), '').trim().length > 0 && + !this.isSaving + ); + } + /* Actions */ @action star = async () => { @@ -135,6 +145,14 @@ class Document { text: this.text, }); } else { + if (!this.title) { + this.title = DEFAULT_TITLE; + this.text = this.text.replace( + new RegExp(`^\# `), + `# ${DEFAULT_TITLE}` + ); + } + const data = { parentDocument: undefined, collection: this.collection.id, diff --git a/frontend/scenes/Document/Document.js b/frontend/scenes/Document/Document.js index 1b369a0b3..2df164ce0 100644 --- a/frontend/scenes/Document/Document.js +++ b/frontend/scenes/Document/Document.js @@ -70,6 +70,8 @@ type Props = { if (props.newDocument) { const newDocument = new Document({ collection: { id: props.match.params.id }, + title: '', + text: '', }); this.setState({ newDocument }); } else { @@ -102,6 +104,7 @@ type Props = { }; onSave = async (redirect: boolean = false) => { + if (!get(this.document, 'allowSave')) return; let document = this.document; if (!document) return; @@ -218,7 +221,7 @@ type Props = { ? : Edit} From 1e8378abef4099038092bf7c15c184224ad7186b Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Tue, 18 Jul 2017 23:45:01 -0700 Subject: [PATCH 2/2] Addressed comments --- frontend/models/Document.js | 11 ++++++----- frontend/scenes/Document/Document.js | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/frontend/models/Document.js b/frontend/models/Document.js index db9bd6858..c2068c1f9 100644 --- a/frontend/models/Document.js +++ b/frontend/models/Document.js @@ -72,12 +72,13 @@ class Document { return []; } - @computed get allowSave(): boolean { + @computed get isEmpty(): boolean { // Check if the document title has been modified and user generated content exists - return ( - this.text.replace(new RegExp(`^\#$`), '').trim().length > 0 && - !this.isSaving - ); + return this.text.replace(new RegExp(`^\#$`), '').trim().length === 0; + } + + @computed get allowSave(): boolean { + return !this.isEmpty && !this.isSaving; } /* Actions */ diff --git a/frontend/scenes/Document/Document.js b/frontend/scenes/Document/Document.js index 2df164ce0..e81507dbb 100644 --- a/frontend/scenes/Document/Document.js +++ b/frontend/scenes/Document/Document.js @@ -104,7 +104,7 @@ type Props = { }; onSave = async (redirect: boolean = false) => { - if (!get(this.document, 'allowSave')) return; + if (this.document && !this.document.allowSave) return; let document = this.document; if (!document) return; @@ -221,7 +221,7 @@ type Props = { ? : Edit}