UNPKG

@wordpress/editor

Version:
133 lines (132 loc) 4.42 kB
// packages/editor/src/components/post-publish-button/index.js import { Button } from "@wordpress/components"; import { useDispatch, useSelect } from "@wordpress/data"; import PublishButtonLabel from "./label.mjs"; import { store as editorStore } from "../../store/index.mjs"; import { jsx } from "react/jsx-runtime"; var noop = () => { }; function PostPublishButton({ forceIsDirty, isOpen, isToggle, onSubmit = noop, onToggle, setEntitiesSavedStatesCallback }) { const { hasPublishAction, isBeingScheduled, isPostSavingLocked, isPublishable, isPublished, isSaveable, isSaving, isAutoSaving, visibility, hasNonPostEntityChanges, isSavingNonPostEntityChanges, postStatus, postStatusHasChanged, postType, postId } = useSelect((select) => { const store = select(editorStore); return { isSaving: store.isSavingPost(), isAutoSaving: store.isAutosavingPost(), isBeingScheduled: store.isEditedPostBeingScheduled(), visibility: store.getEditedPostVisibility(), isSaveable: store.isEditedPostSaveable(), isPostSavingLocked: store.isPostSavingLocked(), isPublishable: store.isEditedPostPublishable(), isPublished: store.isCurrentPostPublished(), hasPublishAction: !!store.getCurrentPost()?._links?.["wp:action-publish"], postType: store.getCurrentPostType(), postId: store.getCurrentPostId(), postStatus: store.getEditedPostAttribute("status"), postStatusHasChanged: store.getPostEdits()?.status, hasNonPostEntityChanges: store.hasNonPostEntityChanges(), isSavingNonPostEntityChanges: store.isSavingNonPostEntityChanges() }; }, []); const { editPost, savePost } = useDispatch(editorStore); const savePostStatus = (status) => { editPost({ status }, { undoIgnore: true }); savePost(); }; const createOnClick = (callback) => (...args) => { if (hasNonPostEntityChanges && setEntitiesSavedStatesCallback) { const onClose = (savedEntities) => { if (savedEntities && savedEntities.some( (elt) => elt.kind === "postType" && elt.name === postType && elt.key === postId )) { callback(...args); } }; setEntitiesSavedStatesCallback(() => onClose); return noop; } return callback(...args); }; const isButtonDisabled = isPostSavingLocked || // Disable while a non-post entity (e.g. a newly created term) is mid-save. isSavingNonPostEntityChanges || (isSaving || !isSaveable || !isPublishable && !forceIsDirty) && !hasNonPostEntityChanges; const isToggleDisabled = isPostSavingLocked || isSavingNonPostEntityChanges || (isPublished || isSaving || !isSaveable || !isPublishable && !forceIsDirty) && !hasNonPostEntityChanges; let publishStatus = "publish"; if (postStatusHasChanged) { publishStatus = postStatus; } else if (!hasPublishAction) { publishStatus = "pending"; } else if (visibility === "private") { publishStatus = "private"; } else if (isBeingScheduled) { publishStatus = "future"; } const onClickButton = () => { if (isButtonDisabled) { return; } onSubmit(); savePostStatus(publishStatus); }; const onClickToggle = () => { if (isToggleDisabled) { return; } onToggle(); }; const buttonProps = { "aria-disabled": isButtonDisabled, className: "editor-post-publish-button", isBusy: !isAutoSaving && isSaving, variant: "primary", onClick: createOnClick(onClickButton), "aria-haspopup": hasNonPostEntityChanges ? "dialog" : void 0 }; const toggleProps = { "aria-disabled": isToggleDisabled, "aria-expanded": isOpen, className: "editor-post-publish-panel__toggle", isBusy: isSaving && isPublished, variant: "primary", size: "compact", onClick: createOnClick(onClickToggle), "aria-haspopup": hasNonPostEntityChanges ? "dialog" : void 0 }; const componentProps = isToggle ? toggleProps : buttonProps; return /* @__PURE__ */ jsx( Button, { ...componentProps, className: `${componentProps.className} editor-post-publish-button__button`, size: "compact", children: /* @__PURE__ */ jsx(PublishButtonLabel, {}) } ); } var post_publish_button_default = PostPublishButton; export { PostPublishButton, post_publish_button_default as default }; //# sourceMappingURL=index.mjs.map