| 1 |
var wp; |
| 2 |
(wp ||= {}).serverSideRender = (() => { |
| 3 |
var __create = Object.create; |
| 4 |
var __defProp = Object.defineProperty; |
| 5 |
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; |
| 6 |
var __getOwnPropNames = Object.getOwnPropertyNames; |
| 7 |
var __getProtoOf = Object.getPrototypeOf; |
| 8 |
var __hasOwnProp = Object.prototype.hasOwnProperty; |
| 9 |
var __commonJS = (cb, mod) => function __require() { |
| 10 |
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; |
| 11 |
}; |
| 12 |
var __export = (target, all) => { |
| 13 |
for (var name in all) |
| 14 |
__defProp(target, name, { get: all[name], enumerable: true }); |
| 15 |
}; |
| 16 |
var __copyProps = (to, from, except, desc) => { |
| 17 |
if (from && typeof from === "object" || typeof from === "function") { |
| 18 |
for (let key of __getOwnPropNames(from)) |
| 19 |
if (!__hasOwnProp.call(to, key) && key !== except) |
| 20 |
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); |
| 21 |
} |
| 22 |
return to; |
| 23 |
}; |
| 24 |
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( |
| 25 |
// If the importer is in node compatibility mode or this is not an ESM |
| 26 |
// file that has been converted to a CommonJS file using a Babel- |
| 27 |
// compatible transform (i.e. "__esModule" has not been set), then set |
| 28 |
// "default" to the CommonJS "module.exports" for node compatibility. |
| 29 |
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, |
| 30 |
mod |
| 31 |
)); |
| 32 |
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); |
| 33 |
|
| 34 |
// package-external:@wordpress/element |
| 35 |
var require_element = __commonJS({ |
| 36 |
"package-external:@wordpress/element"(exports, module) { |
| 37 |
module.exports = window.wp.element; |
| 38 |
} |
| 39 |
}); |
| 40 |
|
| 41 |
// package-external:@wordpress/i18n |
| 42 |
var require_i18n = __commonJS({ |
| 43 |
"package-external:@wordpress/i18n"(exports, module) { |
| 44 |
module.exports = window.wp.i18n; |
| 45 |
} |
| 46 |
}); |
| 47 |
|
| 48 |
// package-external:@wordpress/components |
| 49 |
var require_components = __commonJS({ |
| 50 |
"package-external:@wordpress/components"(exports, module) { |
| 51 |
module.exports = window.wp.components; |
| 52 |
} |
| 53 |
}); |
| 54 |
|
| 55 |
// package-external:@wordpress/data |
| 56 |
var require_data = __commonJS({ |
| 57 |
"package-external:@wordpress/data"(exports, module) { |
| 58 |
module.exports = window.wp.data; |
| 59 |
} |
| 60 |
}); |
| 61 |
|
| 62 |
// package-external:@wordpress/compose |
| 63 |
var require_compose = __commonJS({ |
| 64 |
"package-external:@wordpress/compose"(exports, module) { |
| 65 |
module.exports = window.wp.compose; |
| 66 |
} |
| 67 |
}); |
| 68 |
|
| 69 |
// package-external:@wordpress/api-fetch |
| 70 |
var require_api_fetch = __commonJS({ |
| 71 |
"package-external:@wordpress/api-fetch"(exports, module) { |
| 72 |
module.exports = window.wp.apiFetch; |
| 73 |
} |
| 74 |
}); |
| 75 |
|
| 76 |
// package-external:@wordpress/url |
| 77 |
var require_url = __commonJS({ |
| 78 |
"package-external:@wordpress/url"(exports, module) { |
| 79 |
module.exports = window.wp.url; |
| 80 |
} |
| 81 |
}); |
| 82 |
|
| 83 |
// package-external:@wordpress/blocks |
| 84 |
var require_blocks = __commonJS({ |
| 85 |
"package-external:@wordpress/blocks"(exports, module) { |
| 86 |
module.exports = window.wp.blocks; |
| 87 |
} |
| 88 |
}); |
| 89 |
|
| 90 |
// vendor-external:react/jsx-runtime |
| 91 |
var require_jsx_runtime = __commonJS({ |
| 92 |
"vendor-external:react/jsx-runtime"(exports, module) { |
| 93 |
module.exports = window.ReactJSXRuntime; |
| 94 |
} |
| 95 |
}); |
| 96 |
|
| 97 |
// packages/server-side-render/build-module/index.mjs |
| 98 |
var index_exports = {}; |
| 99 |
__export(index_exports, { |
| 100 |
ServerSideRender: () => ServerSideRenderWithPostId, |
| 101 |
default: () => index_default, |
| 102 |
useServerSideRender: () => useServerSideRender |
| 103 |
}); |
| 104 |
|
| 105 |
// packages/server-side-render/build-module/server-side-render.mjs |
| 106 |
var import_element2 = __toESM(require_element(), 1); |
| 107 |
var import_i18n = __toESM(require_i18n(), 1); |
| 108 |
var import_components = __toESM(require_components(), 1); |
| 109 |
var import_data = __toESM(require_data(), 1); |
| 110 |
|
| 111 |
// packages/server-side-render/build-module/hook.mjs |
| 112 |
var import_compose = __toESM(require_compose(), 1); |
| 113 |
var import_element = __toESM(require_element(), 1); |
| 114 |
var import_api_fetch = __toESM(require_api_fetch(), 1); |
| 115 |
var import_url = __toESM(require_url(), 1); |
| 116 |
var import_blocks = __toESM(require_blocks(), 1); |
| 117 |
function rendererPath(block, attributes = null, urlQueryArgs = {}) { |
| 118 |
return (0, import_url.addQueryArgs)(`/wp/v2/block-renderer/${block}`, { |
| 119 |
context: "edit", |
| 120 |
...null !== attributes ? { attributes } : {}, |
| 121 |
...urlQueryArgs |
| 122 |
}); |
| 123 |
} |
| 124 |
function removeBlockSupportAttributes(attributes) { |
| 125 |
const { |
| 126 |
backgroundColor, |
| 127 |
borderColor, |
| 128 |
fontFamily, |
| 129 |
fontSize, |
| 130 |
gradient, |
| 131 |
textColor, |
| 132 |
className, |
| 133 |
...restAttributes |
| 134 |
} = attributes; |
| 135 |
const { |
| 136 |
border, |
| 137 |
color, |
| 138 |
elements, |
| 139 |
shadow, |
| 140 |
spacing, |
| 141 |
typography, |
| 142 |
...restStyles |
| 143 |
} = attributes?.style || {}; |
| 144 |
return { |
| 145 |
...restAttributes, |
| 146 |
style: restStyles |
| 147 |
}; |
| 148 |
} |
| 149 |
function useServerSideRender(args) { |
| 150 |
const [response, setResponse] = (0, import_element.useState)({ status: "idle" }); |
| 151 |
const shouldDebounceRef = (0, import_element.useRef)(false); |
| 152 |
const { |
| 153 |
attributes, |
| 154 |
block, |
| 155 |
skipBlockSupportAttributes = false, |
| 156 |
httpMethod = "GET", |
| 157 |
urlQueryArgs |
| 158 |
} = args; |
| 159 |
let sanitizedAttributes = attributes && (0, import_blocks.__experimentalSanitizeBlockAttributes)(block, attributes); |
| 160 |
if (skipBlockSupportAttributes) { |
| 161 |
sanitizedAttributes = removeBlockSupportAttributes(sanitizedAttributes); |
| 162 |
} |
| 163 |
const isPostRequest = "POST" === httpMethod; |
| 164 |
const urlAttributes = isPostRequest ? null : sanitizedAttributes; |
| 165 |
const path = rendererPath(block, urlAttributes, urlQueryArgs); |
| 166 |
const body = isPostRequest ? JSON.stringify({ attributes: sanitizedAttributes ?? null }) : void 0; |
| 167 |
(0, import_element.useEffect)(() => { |
| 168 |
const controller = new AbortController(); |
| 169 |
const debouncedFetch = (0, import_compose.debounce)( |
| 170 |
function() { |
| 171 |
{ |
| 172 |
setResponse({ status: "loading" }); |
| 173 |
(0, import_api_fetch.default)({ |
| 174 |
path, |
| 175 |
method: isPostRequest ? "POST" : "GET", |
| 176 |
body, |
| 177 |
headers: isPostRequest ? { |
| 178 |
"Content-Type": "application/json" |
| 179 |
} : {}, |
| 180 |
signal: controller.signal |
| 181 |
}).then((res) => { |
| 182 |
setResponse({ |
| 183 |
status: "success", |
| 184 |
content: res ? res.rendered : "" |
| 185 |
}); |
| 186 |
}).catch((error) => { |
| 187 |
if (error.name === "AbortError") { |
| 188 |
return; |
| 189 |
} |
| 190 |
setResponse({ |
| 191 |
status: "error", |
| 192 |
error: error.message |
| 193 |
}); |
| 194 |
}).finally(() => { |
| 195 |
shouldDebounceRef.current = true; |
| 196 |
}); |
| 197 |
} |
| 198 |
}, |
| 199 |
shouldDebounceRef.current ? 500 : 0 |
| 200 |
); |
| 201 |
debouncedFetch(); |
| 202 |
return () => { |
| 203 |
controller.abort(); |
| 204 |
debouncedFetch.cancel(); |
| 205 |
}; |
| 206 |
}, [path, isPostRequest, body]); |
| 207 |
return response; |
| 208 |
} |
| 209 |
|
| 210 |
// packages/server-side-render/build-module/server-side-render.mjs |
| 211 |
var import_jsx_runtime = __toESM(require_jsx_runtime(), 1); |
| 212 |
var EMPTY_OBJECT = {}; |
| 213 |
function DefaultEmptyResponsePlaceholder({ className }) { |
| 214 |
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.Placeholder, { className, children: (0, import_i18n.__)("Block rendered as empty.") }); |
| 215 |
} |
| 216 |
function DefaultErrorResponsePlaceholder({ message, className }) { |
| 217 |
const errorMessage = (0, import_i18n.sprintf)( |
| 218 |
// translators: %s: error message describing the problem |
| 219 |
(0, import_i18n.__)("Error loading block: %s"), |
| 220 |
message |
| 221 |
); |
| 222 |
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.Placeholder, { className, children: errorMessage }); |
| 223 |
} |
| 224 |
function DefaultLoadingResponsePlaceholder({ children }) { |
| 225 |
const [showLoader, setShowLoader] = (0, import_element2.useState)(false); |
| 226 |
(0, import_element2.useEffect)(() => { |
| 227 |
const timeout = setTimeout(() => { |
| 228 |
setShowLoader(true); |
| 229 |
}, 1e3); |
| 230 |
return () => clearTimeout(timeout); |
| 231 |
}, []); |
| 232 |
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { style: { position: "relative" }, children: [ |
| 233 |
showLoader && /* @__PURE__ */ (0, import_jsx_runtime.jsx)( |
| 234 |
"div", |
| 235 |
{ |
| 236 |
style: { |
| 237 |
position: "absolute", |
| 238 |
top: "50%", |
| 239 |
left: "50%", |
| 240 |
marginTop: "-9px", |
| 241 |
marginLeft: "-9px" |
| 242 |
}, |
| 243 |
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.Spinner, {}) |
| 244 |
} |
| 245 |
), |
| 246 |
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: { opacity: showLoader ? "0.3" : 1 }, children }) |
| 247 |
] }); |
| 248 |
} |
| 249 |
function ServerSideRender(props) { |
| 250 |
const prevContentRef = (0, import_element2.useRef)(""); |
| 251 |
const { |
| 252 |
className, |
| 253 |
EmptyResponsePlaceholder = DefaultEmptyResponsePlaceholder, |
| 254 |
ErrorResponsePlaceholder = DefaultErrorResponsePlaceholder, |
| 255 |
LoadingResponsePlaceholder = DefaultLoadingResponsePlaceholder, |
| 256 |
...restProps |
| 257 |
} = props; |
| 258 |
const { content, status, error } = useServerSideRender(restProps); |
| 259 |
(0, import_element2.useEffect)(() => { |
| 260 |
if (content) { |
| 261 |
prevContentRef.current = content; |
| 262 |
} |
| 263 |
}, [content]); |
| 264 |
if (status === "loading") { |
| 265 |
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LoadingResponsePlaceholder, { ...props, children: !!prevContentRef.current && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_element2.RawHTML, { className, children: prevContentRef.current }) }); |
| 266 |
} |
| 267 |
if (status === "success" && !content) { |
| 268 |
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(EmptyResponsePlaceholder, { ...props }); |
| 269 |
} |
| 270 |
if (status === "error") { |
| 271 |
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ErrorResponsePlaceholder, { message: error, ...props }); |
| 272 |
} |
| 273 |
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_element2.RawHTML, { className, children: content }); |
| 274 |
} |
| 275 |
function ServerSideRenderWithPostId({ |
| 276 |
urlQueryArgs = EMPTY_OBJECT, |
| 277 |
...props |
| 278 |
}) { |
| 279 |
const currentPostId = (0, import_data.useSelect)((select) => { |
| 280 |
const postId = select("core/editor")?.getCurrentPostId(); |
| 281 |
return postId && typeof postId === "number" ? postId : null; |
| 282 |
}, []); |
| 283 |
const newUrlQueryArgs = (0, import_element2.useMemo)(() => { |
| 284 |
if (!currentPostId) { |
| 285 |
return urlQueryArgs; |
| 286 |
} |
| 287 |
return { |
| 288 |
post_id: currentPostId, |
| 289 |
...urlQueryArgs |
| 290 |
}; |
| 291 |
}, [currentPostId, urlQueryArgs]); |
| 292 |
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ServerSideRender, { urlQueryArgs: newUrlQueryArgs, ...props }); |
| 293 |
} |
| 294 |
|
| 295 |
// packages/server-side-render/build-module/index.mjs |
| 296 |
var ServerSideRenderCompat = ServerSideRenderWithPostId; |
| 297 |
ServerSideRenderCompat.ServerSideRender = ServerSideRenderWithPostId; |
| 298 |
ServerSideRenderCompat.useServerSideRender = useServerSideRender; |
| 299 |
var index_default = ServerSideRenderCompat; |
| 300 |
return __toCommonJS(index_exports); |
| 301 |
})(); |
| 302 |
if (typeof wp.serverSideRender === 'object' && wp.serverSideRender.default) { wp.serverSideRender = wp.serverSideRender.default; } |
| 303 |
//# sourceMappingURL=index.js.map |
| 304 |
|