Merge branch 'thenanyu-refactor-doc-dirty-logic'
This commit is contained in:
@@ -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 });
|
||||
|
||||
@@ -79,6 +79,7 @@ class DocumentScene extends React.Component<Props> {
|
||||
@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<Props> {
|
||||
}
|
||||
|
||||
this.isDirty = false;
|
||||
this.isEmpty = false;
|
||||
|
||||
const document = this.document;
|
||||
|
||||
@@ -226,17 +228,19 @@ class DocumentScene extends React.Component<Props> {
|
||||
let document = this.document;
|
||||
if (!document) return;
|
||||
|
||||
// prevent saves when we are 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 (single hash is empty doc)
|
||||
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,11 @@ class DocumentScene extends React.Component<Props> {
|
||||
|
||||
updateIsDirty = debounce(() => {
|
||||
const document = this.document;
|
||||
const editorText = this.getEditorText().trim();
|
||||
|
||||
this.isDirty =
|
||||
!!document && this.getEditorText().trim() !== document.text.trim();
|
||||
// a single hash is a doc with just an empty title
|
||||
this.isEmpty = editorText === '#';
|
||||
this.isDirty = !!document && editorText !== document.text.trim();
|
||||
}, IS_DIRTY_DELAY);
|
||||
|
||||
onImageUploadStart = () => {
|
||||
@@ -377,7 +383,10 @@ class DocumentScene extends React.Component<Props> {
|
||||
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}
|
||||
/>
|
||||
|
||||
@@ -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<Props> {
|
||||
isPublishing,
|
||||
isSaving,
|
||||
savingIsDisabled,
|
||||
publishingIsDisabled,
|
||||
auth,
|
||||
} = this.props;
|
||||
const canShareDocuments =
|
||||
@@ -171,7 +173,7 @@ class Header extends React.Component<Props> {
|
||||
<Button
|
||||
onClick={this.handlePublish}
|
||||
title="Publish document"
|
||||
disabled={savingIsDisabled}
|
||||
disabled={publishingIsDisabled}
|
||||
small
|
||||
>
|
||||
{isPublishing ? 'Publishing…' : 'Publish'}
|
||||
|
||||
@@ -522,7 +522,6 @@ router.post('documents.create', auth(), async ctx => {
|
||||
index,
|
||||
} = ctx.body;
|
||||
ctx.assertUuid(collectionId, 'collectionId must be an uuid');
|
||||
ctx.assertPresent(title, 'title is required');
|
||||
ctx.assertPresent(text, 'text is required');
|
||||
if (parentDocumentId) {
|
||||
ctx.assertUuid(parentDocumentId, 'parentDocumentId must be an uuid');
|
||||
|
||||
@@ -284,11 +284,7 @@ export default function Api() {
|
||||
}
|
||||
required
|
||||
/>
|
||||
<Argument
|
||||
id="title"
|
||||
description="Title for the document"
|
||||
required
|
||||
/>
|
||||
<Argument id="title" description="Title for the document" />
|
||||
<Argument
|
||||
id="text"
|
||||
description="Content of the document in Markdow"
|
||||
|
||||
Reference in New Issue
Block a user