UNPKG

@wordpress/block-editor

Version:
163 lines (161 loc) 4.6 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/block-editor/src/components/image-editor/use-save-image.js var use_save_image_exports = {}; __export(use_save_image_exports, { default: () => useSaveImage }); module.exports = __toCommonJS(use_save_image_exports); var import_data = require("@wordpress/data"); var import_element = require("@wordpress/element"); var import_i18n = require("@wordpress/i18n"); var import_notices = require("@wordpress/notices"); var import_dom = require("@wordpress/dom"); var import_store = require("../../store"); var import_private_keys = require("../../store/private-keys"); var messages = { crop: (0, import_i18n.__)("Image cropped."), rotate: (0, import_i18n.__)("Image rotated."), cropAndRotate: (0, import_i18n.__)("Image cropped and rotated.") }; function useSaveImage({ crop, rotation, url, id, onSaveImage, onFinishEditing }) { const { createErrorNotice, createSuccessNotice } = (0, import_data.useDispatch)(import_notices.store); const [isInProgress, setIsInProgress] = (0, import_element.useState)(false); const { editMediaEntity } = (0, import_data.useSelect)((select) => { const settings = select(import_store.store).getSettings(); return { editMediaEntity: settings?.[import_private_keys.mediaEditKey] }; }, []); const cancel = (0, import_element.useCallback)(() => { setIsInProgress(false); onFinishEditing(); }, [onFinishEditing]); const apply = (0, import_element.useCallback)(async () => { if (!editMediaEntity) { onFinishEditing(); createErrorNotice( (0, import_i18n.__)("Sorry, you are not allowed to edit images on this site."), { id: "image-editing-error", type: "snackbar" } ); return; } setIsInProgress(true); const modifiers = []; if (rotation > 0) { modifiers.push({ type: "rotate", args: { angle: rotation } }); } if (crop.width < 99.9 || crop.height < 99.9) { modifiers.push({ type: "crop", args: { left: crop.x, top: crop.y, width: crop.width, height: crop.height } }); } if (modifiers.length === 0) { setIsInProgress(false); onFinishEditing(); return; } const modifierType = modifiers.length === 1 ? modifiers[0].type : "cropAndRotate"; try { const savedImage = await editMediaEntity( id, { src: url, modifiers }, { throwOnError: true } ); if (savedImage) { onSaveImage({ id: savedImage.id, url: savedImage.source_url }); createSuccessNotice(messages[modifierType], { type: "snackbar", actions: [ { label: (0, import_i18n.__)("Undo"), onClick: () => { onSaveImage({ id, url }); } } ] }); } } catch (error) { createErrorNotice( (0, import_i18n.sprintf)( /* translators: %s: Error message. */ (0, import_i18n.__)("Could not edit image. %s"), (0, import_dom.__unstableStripHTML)(error.message) ), { id: "image-editing-error", type: "snackbar" } ); } finally { setIsInProgress(false); onFinishEditing(); } }, [ crop, rotation, id, url, onSaveImage, createErrorNotice, createSuccessNotice, onFinishEditing, editMediaEntity ]); return (0, import_element.useMemo)( () => ({ isInProgress, apply, cancel }), [isInProgress, apply, cancel] ); } //# sourceMappingURL=use-save-image.js.map