UNPKG

@wordpress/core-data

Version:
318 lines (316 loc) 9.54 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/core-data/src/private-actions.js var private_actions_exports = {}; __export(private_actions_exports, { __unstableNotifySyncUndoManagerChange: () => __unstableNotifySyncUndoManagerChange, editMediaEntity: () => editMediaEntity, receiveEditorAssets: () => receiveEditorAssets, receiveEditorSettings: () => receiveEditorSettings, receiveRegisteredPostMeta: () => receiveRegisteredPostMeta, receiveViewConfig: () => receiveViewConfig, saveDirtyEntities: () => saveDirtyEntities, setCollaborationSupported: () => setCollaborationSupported, setSyncConnectionStatus: () => setSyncConnectionStatus }); module.exports = __toCommonJS(private_actions_exports); var import_api_fetch = __toESM(require("@wordpress/api-fetch")); var import_notices = require("@wordpress/notices"); var import_block_editor = require("@wordpress/block-editor"); var import_html_entities = require("@wordpress/html-entities"); var import_i18n = require("@wordpress/i18n"); var import_name = require("./name.cjs"); var import_sync = require("./sync.cjs"); function receiveRegisteredPostMeta(postType, registeredPostMeta) { return { type: "RECEIVE_REGISTERED_POST_META", postType, registeredPostMeta }; } var editMediaEntity = (recordId, edits = {}, { __unstableFetch = import_api_fetch.default, throwOnError = false } = {}) => async ({ dispatch, resolveSelect }) => { if (!recordId) { return; } const kind = "postType"; const name = "attachment"; const configs = await resolveSelect.getEntitiesConfig(kind); const entityConfig = configs.find( (config) => config.kind === kind && config.name === name ); if (!entityConfig) { return; } const lock = await dispatch.__unstableAcquireStoreLock( import_name.STORE_NAME, ["entities", "records", kind, name, recordId], { exclusive: true } ); let updatedRecord; let error; let hasError = false; try { dispatch({ type: "SAVE_ENTITY_RECORD_START", kind, name, recordId }); try { const path = `${entityConfig.baseURL}/${recordId}/edit`; const newRecord = await __unstableFetch({ path, method: "POST", data: { ...edits } }); if (newRecord) { dispatch.receiveEntityRecords( kind, name, newRecord, void 0, true, void 0, void 0 ); updatedRecord = newRecord; } } catch (e) { error = e; hasError = true; } dispatch({ type: "SAVE_ENTITY_RECORD_FINISH", kind, name, recordId, error }); if (hasError && throwOnError) { throw error; } return updatedRecord; } finally { dispatch.__unstableReleaseStoreLock(lock); } }; function receiveEditorSettings(settings) { return { type: "RECEIVE_EDITOR_SETTINGS", settings }; } function receiveEditorAssets(assets) { return { type: "RECEIVE_EDITOR_ASSETS", assets }; } var setCollaborationSupported = (supported) => ({ dispatch }) => { dispatch({ type: "SET_COLLABORATION_SUPPORTED", supported }); if (!supported && (0, import_sync.hasSyncManager)()) { (0, import_sync.getSyncManager)().unloadAll(); dispatch.__unstableNotifySyncUndoManagerChange({ hasUndo: false, hasRedo: false }); } }; function receiveViewConfig(kind, name, config) { return { type: "RECEIVE_VIEW_CONFIG", kind, name, config }; } function __unstableNotifySyncUndoManagerChange(state) { return { type: "SYNC_UNDO_MANAGER_CHANGE", ...state }; } function setSyncConnectionStatus(kind, name, key, status) { if (!status) { return { type: "CLEAR_SYNC_CONNECTION_STATUS", kind, name, key }; } return { type: "SET_SYNC_CONNECTION_STATUS", kind, name, key, status }; } var saveDirtyEntities = ({ onSave, dirtyEntityRecords = [], entitiesToSkip = [], close, successNoticeContent } = {}) => ({ registry }) => { const PUBLISH_ON_SAVE_ENTITIES = [ { kind: "postType", name: "wp_navigation" } ]; const saveNoticeId = "site-editor-save-success"; const homeUrl = registry.select(import_name.STORE_NAME).getEntityRecord("root", "__unstableBase")?.home; registry.dispatch(import_notices.store).removeNotice(saveNoticeId); const entitiesToSave = dirtyEntityRecords.filter( ({ kind, name, key, property }) => { return !entitiesToSkip.some( (elt) => elt.kind === kind && elt.name === name && elt.key === key && elt.property === property ); } ); close?.(entitiesToSave); const siteItemsToSave = []; const pendingSavedRecords = []; entitiesToSave.forEach(({ kind, name, key, property }) => { if ("root" === kind && "site" === name) { siteItemsToSave.push(property); } else { if (PUBLISH_ON_SAVE_ENTITIES.some( (typeToPublish) => typeToPublish.kind === kind && typeToPublish.name === name )) { registry.dispatch(import_name.STORE_NAME).editEntityRecord(kind, name, key, { status: "publish" }); } pendingSavedRecords.push( registry.dispatch(import_name.STORE_NAME).saveEditedEntityRecord(kind, name, key, { throwOnError: true }).catch(ensureError) ); } }); if (siteItemsToSave.length) { pendingSavedRecords.push( registry.dispatch(import_name.STORE_NAME).__experimentalSaveSpecifiedEntityEdits( "root", "site", void 0, siteItemsToSave, { throwOnError: true } ).catch(ensureError) ); } registry.dispatch(import_block_editor.store).__unstableMarkLastChangeAsPersistent(); return Promise.all(pendingSavedRecords).then(async (values) => { if (onSave) { await onSave(); } return values; }).then((values) => { const errors = values.filter((v) => v instanceof Error); if (errors.length) { const firstMessage = errors.find( (e) => e.message )?.message; registry.dispatch(import_notices.store).createErrorNotice( (0, import_html_entities.decodeEntities)( firstMessage || (0, import_i18n.__)("Saving failed.") ), { type: "snackbar", id: saveNoticeId } ); } else { registry.dispatch(import_notices.store).createSuccessNotice( successNoticeContent || (0, import_i18n.__)("Site updated."), { type: "snackbar", id: saveNoticeId, actions: [ { label: (0, import_i18n.__)("View site"), url: homeUrl, openInNewTab: true } ] } ); } }).catch( (error) => registry.dispatch(import_notices.store).createErrorNotice( (0, import_html_entities.decodeEntities)( error?.message || (0, import_i18n.__)("Saving failed.") ), { type: "snackbar", id: saveNoticeId } ) ); function ensureError(error) { if (error instanceof Error) { return error; } let message; if (!error) { } else if (typeof error.message === "string") { message = error.message; } else if (typeof error === "string") { message = error; } else if ( // Only consider own method, lest we erroneously end up calling // `Object#toString` at the end of the prototype chain, thereby // returning `"[object Object]"`. Object.hasOwn(error, "toString") && typeof error.toString === "function" ) { const result = error.toString(); if (typeof result === "string") { message = result; } } return new Error(message, { cause: error }); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { __unstableNotifySyncUndoManagerChange, editMediaEntity, receiveEditorAssets, receiveEditorSettings, receiveRegisteredPostMeta, receiveViewConfig, saveDirtyEntities, setCollaborationSupported, setSyncConnectionStatus }); //# sourceMappingURL=private-actions.cjs.map