UNPKG

@wordpress/editor

Version:
168 lines (166 loc) 6.21 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/editor/src/components/post-publish-button/index.js var post_publish_button_exports = {}; __export(post_publish_button_exports, { PostPublishButton: () => PostPublishButton, default: () => post_publish_button_default }); module.exports = __toCommonJS(post_publish_button_exports); var import_components = require("@wordpress/components"); var import_data = require("@wordpress/data"); var import_label = __toESM(require("./label.cjs")); var import_store = require("../../store/index.cjs"); var import_jsx_runtime = require("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 } = (0, import_data.useSelect)((select) => { const store = select(import_store.store); 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 } = (0, import_data.useDispatch)(import_store.store); 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__ */ (0, import_jsx_runtime.jsx)( import_components.Button, { ...componentProps, className: `${componentProps.className} editor-post-publish-button__button`, size: "compact", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_label.default, {}) } ); } var post_publish_button_default = PostPublishButton; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { PostPublishButton }); //# sourceMappingURL=index.cjs.map