// @flow import * as React from "react"; import { Redirect } from "react-router-dom"; import { observable } from "mobx"; import { inject, observer } from "mobx-react"; import Document from "models/Document"; import UiStore from "stores/UiStore"; import AuthStore from "stores/AuthStore"; import CollectionStore from "stores/CollectionsStore"; import PoliciesStore from "stores/PoliciesStore"; import { documentUrl, documentMoveUrl, documentEditUrl, documentHistoryUrl, newDocumentUrl, } from "utils/routeHelpers"; import { DropdownMenu, DropdownMenuItem } from "components/DropdownMenu"; type Props = { ui: UiStore, auth: AuthStore, position?: "left" | "right" | "center", document: Document, collections: CollectionStore, policies: PoliciesStore, className: string, isRevision?: boolean, showPrint?: boolean, showToggleEmbeds?: boolean, showPin?: boolean, onOpen?: () => void, onClose?: () => void, }; @observer class DocumentMenu extends React.Component { @observable redirectTo: ?string; componentDidUpdate() { this.redirectTo = undefined; } handleNewChild = (ev: SyntheticEvent<>) => { const { document } = this.props; this.redirectTo = newDocumentUrl(document.collectionId, document.id); }; handleDelete = (ev: SyntheticEvent<>) => { const { document } = this.props; this.props.ui.setActiveModal("document-delete", { document }); }; handleDocumentHistory = () => { if (this.props.isRevision) { this.redirectTo = documentUrl(this.props.document); } else { this.redirectTo = documentHistoryUrl(this.props.document); } }; handleMove = (ev: SyntheticEvent<>) => { this.redirectTo = documentMoveUrl(this.props.document); }; handleEdit = (ev: SyntheticEvent<>) => { this.redirectTo = documentEditUrl(this.props.document); }; handleDuplicate = async (ev: SyntheticEvent<>) => { const duped = await this.props.document.duplicate(); // when duplicating, go straight to the duplicated document content this.redirectTo = duped.url; this.props.ui.showToast("Document duplicated"); }; handleArchive = async (ev: SyntheticEvent<>) => { await this.props.document.archive(); this.props.ui.showToast("Document archived"); }; handleRestore = async (ev: SyntheticEvent<>) => { await this.props.document.restore(); this.props.ui.showToast("Document restored"); }; handlePin = (ev: SyntheticEvent<>) => { this.props.document.pin(); }; handleUnpin = (ev: SyntheticEvent<>) => { this.props.document.unpin(); }; handleStar = (ev: SyntheticEvent<>) => { ev.stopPropagation(); this.props.document.star(); }; handleUnstar = (ev: SyntheticEvent<>) => { ev.stopPropagation(); this.props.document.unstar(); }; handleExport = (ev: SyntheticEvent<>) => { this.props.document.download(); }; handleShareLink = async (ev: SyntheticEvent<>) => { const { document } = this.props; if (!document.shareUrl) await document.share(); this.props.ui.setActiveModal("document-share", { document }); }; render() { if (this.redirectTo) return ; const { policies, document, position, className, showToggleEmbeds, showPrint, showPin, auth, onOpen, onClose, } = this.props; const can = policies.abilities(document.id); const canShareDocuments = can.share && auth.team && auth.team.sharing; const canViewHistory = can.read && !can.restore; return ( {(can.unarchive || can.restore) && ( Restore )} {showPin && (document.pinned ? can.unpin && ( Unpin ) : can.pin && ( Pin to collection ))} {document.isStarred ? can.unstar && ( Unstar ) : can.star && ( Star )} {canShareDocuments && ( Share link… )} {showToggleEmbeds && ( {document.embedsDisabled ? ( Enable embeds ) : ( Disable embeds )} )} {canViewHistory && (
Document history
)} {can.createChildDocument && ( New nested document )} {can.update && ( Edit )} {can.update && ( Duplicate )} {can.archive && ( Archive )} {can.delete && ( Delete… )} {can.move && ( Move… )}
{can.download && ( Download )} {showPrint && ( Print )}
); } } export default inject("ui", "auth", "collections", "policies")(DocumentMenu);