fix: Nested document menu item appears where it shouldnt (#1193)

This commit is contained in:
Tom Moor
2020-02-26 21:10:10 -08:00
committed by GitHub
parent 148affb52e
commit d06ec5ce0c
4 changed files with 33 additions and 11 deletions

View File

@@ -185,7 +185,7 @@ class DocumentMenu extends React.Component<Props> {
</DropdownMenuItem> </DropdownMenuItem>
</React.Fragment> </React.Fragment>
)} )}
{can.update && ( {can.createChildDocument && (
<DropdownMenuItem <DropdownMenuItem
onClick={this.handleNewChild} onClick={this.handleNewChild}
title="Create a nested document inside the current document" title="Create a nested document inside the current document"

View File

@@ -65,7 +65,7 @@ type Props = {
@observer @observer
class DocumentScene extends React.Component<Props> { class DocumentScene extends React.Component<Props> {
getEditorText: () => string; getEditorText: () => string = () => this.props.document.text;
@observable editorComponent = EditorImport; @observable editorComponent = EditorImport;
@observable isUploading: boolean = false; @observable isUploading: boolean = false;
@@ -80,6 +80,10 @@ class DocumentScene extends React.Component<Props> {
this.loadEditor(); this.loadEditor();
} }
componentDidMount() {
this.updateIsDirty();
}
@keydown('m') @keydown('m')
goToMove(ev) { goToMove(ev) {
ev.preventDefault(); ev.preventDefault();
@@ -179,14 +183,16 @@ class DocumentScene extends React.Component<Props> {
this.onSave({ done: false, autosave: true }); this.onSave({ done: false, autosave: true });
}, AUTOSAVE_DELAY); }, AUTOSAVE_DELAY);
updateIsDirty = debounce(() => { updateIsDirty = () => {
const { document } = this.props; const { document } = this.props;
const editorText = this.getEditorText().trim(); const editorText = this.getEditorText().trim();
// a single hash is a doc with just an empty title // a single hash is a doc with just an empty title
this.isEmpty = editorText === '#'; this.isEmpty = !editorText || editorText === '#';
this.isDirty = !!document && editorText !== document.text.trim(); this.isDirty = !!document && editorText !== document.text.trim();
}, IS_DIRTY_DELAY); };
updateIsDirtyDebounced = debounce(this.updateIsDirty, IS_DIRTY_DELAY);
onImageUploadStart = () => { onImageUploadStart = () => {
this.isUploading = true; this.isUploading = true;
@@ -198,7 +204,7 @@ class DocumentScene extends React.Component<Props> {
onChange = getEditorText => { onChange = getEditorText => {
this.getEditorText = getEditorText; this.getEditorText = getEditorText;
this.updateIsDirty(); this.updateIsDirtyDebounced();
this.autosave(); this.autosave();
}; };

View File

@@ -230,7 +230,7 @@ class Header extends React.Component<Props> {
</Action> </Action>
)} )}
{canEdit && {canEdit &&
!isDraft && ( can.createChildDocument && (
<Action> <Action>
<NewChildDocumentMenu <NewChildDocumentMenu
document={document} document={document}

View File

@@ -43,26 +43,42 @@ allow(User, ['star', 'unstar'], Document, (user, document) => {
}); });
allow(User, 'update', Document, (user, document) => { allow(User, 'update', Document, (user, document) => {
if (document.archivedAt) return false;
if (document.deletedAt) return false;
invariant( invariant(
document.collection, document.collection,
'collection is missing, did you forget to include in the query scope?' 'collection is missing, did you forget to include in the query scope?'
); );
if (cannot(user, 'update', document.collection)) return false; if (cannot(user, 'update', document.collection)) return false;
return user.teamId === document.teamId;
});
allow(User, 'createChildDocument', Document, (user, document) => {
if (document.archivedAt) return false; if (document.archivedAt) return false;
if (document.deletedAt) return false; if (document.archivedAt) return false;
if (!document.publishedAt) return false;
invariant(
document.collection,
'collection is missing, did you forget to include in the query scope?'
);
if (cannot(user, 'update', document.collection)) return false;
return user.teamId === document.teamId; return user.teamId === document.teamId;
}); });
allow(User, ['move', 'pin', 'unpin'], Document, (user, document) => { allow(User, ['move', 'pin', 'unpin'], Document, (user, document) => {
if (document.archivedAt) return false;
if (document.deletedAt) return false;
if (!document.publishedAt) return false;
invariant( invariant(
document.collection, document.collection,
'collection is missing, did you forget to include in the query scope?' 'collection is missing, did you forget to include in the query scope?'
); );
if (cannot(user, 'update', document.collection)) return false; if (cannot(user, 'update', document.collection)) return false;
if (document.archivedAt) return false;
if (document.deletedAt) return false;
if (!document.publishedAt) return false;
return user.teamId === document.teamId; return user.teamId === document.teamId;
}); });