Merge pull request #171 from jorilallo/jori/prevent-saving-empty

Disable publish on empty documents (fixes #151)
This commit is contained in:
Jori Lallo
2017-07-18 23:58:29 -07:00
committed by GitHub
2 changed files with 24 additions and 2 deletions

View File

@@ -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,15 @@ class Document {
return [];
}
@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;
}
@computed get allowSave(): boolean {
return !this.isEmpty && !this.isSaving;
}
/* Actions */
@action star = async () => {
@@ -135,6 +146,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,

View File

@@ -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 (this.document && !this.document.allowSave) return;
let document = this.document;
if (!document) return;
@@ -218,7 +221,7 @@ type Props = {
? <SaveAction
showCheckmark={this.state.showAsSaved}
onClick={this.onSave.bind(this, true)}
disabled={get(this.document, 'isSaving')}
disabled={!(this.document && this.document.allowSave)}
isNew={!!isNew}
/>
: <a onClick={this.onClickEdit}>Edit</a>}