fix: Cleanup totally empty drafts on leave (#3310)

* fix: Cleanup totally empty drafts on leave

* cleanup

* fix: Add check the doc has never been saved after creation when auto-deleting
This commit is contained in:
Tom Moor
2022-04-03 11:51:38 -07:00
committed by GitHub
parent 5cd002bb88
commit 41579eb4bf
2 changed files with 20 additions and 1 deletions

View File

@@ -172,6 +172,11 @@ export default class Document extends BaseModel {
return !this.publishedAt;
}
@computed
get hasEmptyTitle(): boolean {
return this.title === "";
}
@computed
get titleWithDefault(): string {
return this.title || "Untitled";

View File

@@ -138,6 +138,19 @@ class DocumentScene extends React.Component<Props> {
}
}
componentWillUnmount() {
if (
this.isEmpty &&
this.props.document.createdBy.id === this.props.auth.user?.id &&
this.props.document.isDraft &&
this.props.document.isActive &&
this.props.document.hasEmptyTitle &&
this.props.document.isPersistedOnce
) {
this.props.document.delete();
}
}
replaceDocument = (template: Document | Revision) => {
const editorRef = this.editor.current;
@@ -341,7 +354,8 @@ class DocumentScene extends React.Component<Props> {
this.isEditorDirty = editorText !== document.text.trim();
// a single hash is a doc with just an empty title
this.isEmpty = (!editorText || editorText === "#") && !this.title;
this.isEmpty =
(!editorText || editorText === "#" || editorText === "\\") && !this.title;
};
updateIsDirtyDebounced = debounce(this.updateIsDirty, 500);