UNPKG

@wordpress/server-side-render

Version:

The component used with WordPress to server-side render a preview of dynamic blocks to display in the editor.

139 lines (138 loc) 4.76 kB
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); var hook_exports = {}; __export(hook_exports, { removeBlockSupportAttributes: () => removeBlockSupportAttributes, rendererPath: () => rendererPath, useServerSideRender: () => useServerSideRender }); module.exports = __toCommonJS(hook_exports); var import_compose = require("@wordpress/compose"); var import_element = require("@wordpress/element"); var import_api_fetch = __toESM(require("@wordpress/api-fetch")); var import_url = require("@wordpress/url"); var import_blocks = require("@wordpress/blocks"); function rendererPath(block, attributes = null, urlQueryArgs = {}) { return (0, import_url.addQueryArgs)(`/wp/v2/block-renderer/${block}`, { context: "edit", ...null !== attributes ? { attributes } : {}, ...urlQueryArgs }); } function removeBlockSupportAttributes(attributes) { const { backgroundColor, borderColor, fontFamily, fontSize, gradient, textColor, className, ...restAttributes } = attributes; const { border, color, elements, shadow, spacing, typography, ...restStyles } = attributes?.style || {}; return { ...restAttributes, style: restStyles }; } function useServerSideRender(args) { const [response, setResponse] = (0, import_element.useState)({ status: "idle" }); const shouldDebounceRef = (0, import_element.useRef)(false); const { attributes, block, skipBlockSupportAttributes = false, httpMethod = "GET", urlQueryArgs } = args; let sanitizedAttributes = attributes && (0, import_blocks.__experimentalSanitizeBlockAttributes)(block, attributes); if (skipBlockSupportAttributes) { sanitizedAttributes = removeBlockSupportAttributes(sanitizedAttributes); } const isPostRequest = "POST" === httpMethod; const urlAttributes = isPostRequest ? null : sanitizedAttributes; const path = rendererPath(block, urlAttributes, urlQueryArgs); const body = isPostRequest ? JSON.stringify({ attributes: sanitizedAttributes ?? null }) : void 0; (0, import_element.useEffect)(() => { const controller = new AbortController(); const debouncedFetch = (0, import_compose.debounce)( function() { { setResponse({ status: "loading" }); (0, import_api_fetch.default)({ path, method: isPostRequest ? "POST" : "GET", body, headers: isPostRequest ? { "Content-Type": "application/json" } : {}, signal: controller.signal }).then((res) => { setResponse({ status: "success", content: res ? res.rendered : "" }); }).catch((error) => { if (error.name === "AbortError") { return; } setResponse({ status: "error", error: error.message }); }).finally(() => { shouldDebounceRef.current = true; }); } }, shouldDebounceRef.current ? 500 : 0 ); debouncedFetch(); return () => { controller.abort(); debouncedFetch.cancel(); }; }, [path, isPostRequest, body]); return response; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { removeBlockSupportAttributes, rendererPath, useServerSideRender }); //# sourceMappingURL=hook.js.map