PluginProbe
Gutenberg / 17.6.6
Gutenberg v17.6.6
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / build / server-side-render / index.js

index.js in Gutenberg 17.6.6, at build/server-side-render/index.js

443 lines 14.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /******/ (() => { // webpackBootstrap
2 /******/ "use strict";
3 /******/ var __webpack_modules__ = ({
4
5 /***/ 5619:
6 /***/ ((module) => {
7
8
9
10 // do not edit .js files directly - edit src/index.jst
11
12
13 var envHasBigInt64Array = typeof BigInt64Array !== 'undefined';
14
15
16 module.exports = function equal(a, b) {
17 if (a === b) return true;
18
19 if (a && b && typeof a == 'object' && typeof b == 'object') {
20 if (a.constructor !== b.constructor) return false;
21
22 var length, i, keys;
23 if (Array.isArray(a)) {
24 length = a.length;
25 if (length != b.length) return false;
26 for (i = length; i-- !== 0;)
27 if (!equal(a[i], b[i])) return false;
28 return true;
29 }
30
31
32 if ((a instanceof Map) && (b instanceof Map)) {
33 if (a.size !== b.size) return false;
34 for (i of a.entries())
35 if (!b.has(i[0])) return false;
36 for (i of a.entries())
37 if (!equal(i[1], b.get(i[0]))) return false;
38 return true;
39 }
40
41 if ((a instanceof Set) && (b instanceof Set)) {
42 if (a.size !== b.size) return false;
43 for (i of a.entries())
44 if (!b.has(i[0])) return false;
45 return true;
46 }
47
48 if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
49 length = a.length;
50 if (length != b.length) return false;
51 for (i = length; i-- !== 0;)
52 if (a[i] !== b[i]) return false;
53 return true;
54 }
55
56
57 if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags;
58 if (a.valueOf !== Object.prototype.valueOf) return a.valueOf() === b.valueOf();
59 if (a.toString !== Object.prototype.toString) return a.toString() === b.toString();
60
61 keys = Object.keys(a);
62 length = keys.length;
63 if (length !== Object.keys(b).length) return false;
64
65 for (i = length; i-- !== 0;)
66 if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
67
68 for (i = length; i-- !== 0;) {
69 var key = keys[i];
70
71 if (!equal(a[key], b[key])) return false;
72 }
73
74 return true;
75 }
76
77 // true if both NaN, false otherwise
78 return a!==a && b!==b;
79 };
80
81
82 /***/ })
83
84 /******/ });
85 /************************************************************************/
86 /******/ // The module cache
87 /******/ var __webpack_module_cache__ = {};
88 /******/
89 /******/ // The require function
90 /******/ function __webpack_require__(moduleId) {
91 /******/ // Check if module is in cache
92 /******/ var cachedModule = __webpack_module_cache__[moduleId];
93 /******/ if (cachedModule !== undefined) {
94 /******/ return cachedModule.exports;
95 /******/ }
96 /******/ // Create a new module (and put it into the cache)
97 /******/ var module = __webpack_module_cache__[moduleId] = {
98 /******/ // no module.id needed
99 /******/ // no module.loaded needed
100 /******/ exports: {}
101 /******/ };
102 /******/
103 /******/ // Execute the module function
104 /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
105 /******/
106 /******/ // Return the exports of the module
107 /******/ return module.exports;
108 /******/ }
109 /******/
110 /************************************************************************/
111 /******/ /* webpack/runtime/compat get default export */
112 /******/ (() => {
113 /******/ // getDefaultExport function for compatibility with non-harmony modules
114 /******/ __webpack_require__.n = (module) => {
115 /******/ var getter = module && module.__esModule ?
116 /******/ () => (module['default']) :
117 /******/ () => (module);
118 /******/ __webpack_require__.d(getter, { a: getter });
119 /******/ return getter;
120 /******/ };
121 /******/ })();
122 /******/
123 /******/ /* webpack/runtime/define property getters */
124 /******/ (() => {
125 /******/ // define getter functions for harmony exports
126 /******/ __webpack_require__.d = (exports, definition) => {
127 /******/ for(var key in definition) {
128 /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
129 /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
130 /******/ }
131 /******/ }
132 /******/ };
133 /******/ })();
134 /******/
135 /******/ /* webpack/runtime/hasOwnProperty shorthand */
136 /******/ (() => {
137 /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
138 /******/ })();
139 /******/
140 /************************************************************************/
141 var __webpack_exports__ = {};
142 // This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
143 (() => {
144
145 // EXPORTS
146 __webpack_require__.d(__webpack_exports__, {
147 "default": () => (/* binding */ build_module)
148 });
149
150 ;// CONCATENATED MODULE: external "React"
151 const external_React_namespaceObject = window["React"];
152 ;// CONCATENATED MODULE: external ["wp","element"]
153 const external_wp_element_namespaceObject = window["wp"]["element"];
154 ;// CONCATENATED MODULE: external ["wp","data"]
155 const external_wp_data_namespaceObject = window["wp"]["data"];
156 // EXTERNAL MODULE: ./node_modules/fast-deep-equal/es6/index.js
157 var es6 = __webpack_require__(5619);
158 var es6_default = /*#__PURE__*/__webpack_require__.n(es6);
159 ;// CONCATENATED MODULE: external ["wp","compose"]
160 const external_wp_compose_namespaceObject = window["wp"]["compose"];
161 ;// CONCATENATED MODULE: external ["wp","i18n"]
162 const external_wp_i18n_namespaceObject = window["wp"]["i18n"];
163 ;// CONCATENATED MODULE: external ["wp","apiFetch"]
164 const external_wp_apiFetch_namespaceObject = window["wp"]["apiFetch"];
165 var external_wp_apiFetch_default = /*#__PURE__*/__webpack_require__.n(external_wp_apiFetch_namespaceObject);
166 ;// CONCATENATED MODULE: external ["wp","url"]
167 const external_wp_url_namespaceObject = window["wp"]["url"];
168 ;// CONCATENATED MODULE: external ["wp","components"]
169 const external_wp_components_namespaceObject = window["wp"]["components"];
170 ;// CONCATENATED MODULE: external ["wp","blocks"]
171 const external_wp_blocks_namespaceObject = window["wp"]["blocks"];
172 ;// CONCATENATED MODULE: ./packages/server-side-render/build-module/server-side-render.js
173
174 /**
175 * External dependencies
176 */
177
178
179 /**
180 * WordPress dependencies
181 */
182
183
184
185
186
187
188
189 const EMPTY_OBJECT = {};
190 function rendererPath(block, attributes = null, urlQueryArgs = {}) {
191 return (0,external_wp_url_namespaceObject.addQueryArgs)(`/wp/v2/block-renderer/${block}`, {
192 context: 'edit',
193 ...(null !== attributes ? {
194 attributes
195 } : {}),
196 ...urlQueryArgs
197 });
198 }
199 function removeBlockSupportAttributes(attributes) {
200 const {
201 backgroundColor,
202 borderColor,
203 fontFamily,
204 fontSize,
205 gradient,
206 textColor,
207 className,
208 ...restAttributes
209 } = attributes;
210 const {
211 border,
212 color,
213 elements,
214 spacing,
215 typography,
216 ...restStyles
217 } = attributes?.style || EMPTY_OBJECT;
218 return {
219 ...restAttributes,
220 style: restStyles
221 };
222 }
223 function DefaultEmptyResponsePlaceholder({
224 className
225 }) {
226 return (0,external_React_namespaceObject.createElement)(external_wp_components_namespaceObject.Placeholder, {
227 className: className
228 }, (0,external_wp_i18n_namespaceObject.__)('Block rendered as empty.'));
229 }
230 function DefaultErrorResponsePlaceholder({
231 response,
232 className
233 }) {
234 const errorMessage = (0,external_wp_i18n_namespaceObject.sprintf)(
235 // translators: %s: error message describing the problem
236 (0,external_wp_i18n_namespaceObject.__)('Error loading block: %s'), response.errorMsg);
237 return (0,external_React_namespaceObject.createElement)(external_wp_components_namespaceObject.Placeholder, {
238 className: className
239 }, errorMessage);
240 }
241 function DefaultLoadingResponsePlaceholder({
242 children,
243 showLoader
244 }) {
245 return (0,external_React_namespaceObject.createElement)("div", {
246 style: {
247 position: 'relative'
248 }
249 }, showLoader && (0,external_React_namespaceObject.createElement)("div", {
250 style: {
251 position: 'absolute',
252 top: '50%',
253 left: '50%',
254 marginTop: '-9px',
255 marginLeft: '-9px'
256 }
257 }, (0,external_React_namespaceObject.createElement)(external_wp_components_namespaceObject.Spinner, null)), (0,external_React_namespaceObject.createElement)("div", {
258 style: {
259 opacity: showLoader ? '0.3' : 1
260 }
261 }, children));
262 }
263 function ServerSideRender(props) {
264 const {
265 attributes,
266 block,
267 className,
268 httpMethod = 'GET',
269 urlQueryArgs,
270 skipBlockSupportAttributes = false,
271 EmptyResponsePlaceholder = DefaultEmptyResponsePlaceholder,
272 ErrorResponsePlaceholder = DefaultErrorResponsePlaceholder,
273 LoadingResponsePlaceholder = DefaultLoadingResponsePlaceholder
274 } = props;
275 const isMountedRef = (0,external_wp_element_namespaceObject.useRef)(true);
276 const [showLoader, setShowLoader] = (0,external_wp_element_namespaceObject.useState)(false);
277 const fetchRequestRef = (0,external_wp_element_namespaceObject.useRef)();
278 const [response, setResponse] = (0,external_wp_element_namespaceObject.useState)(null);
279 const prevProps = (0,external_wp_compose_namespaceObject.usePrevious)(props);
280 const [isLoading, setIsLoading] = (0,external_wp_element_namespaceObject.useState)(false);
281 function fetchData() {
282 var _sanitizedAttributes, _sanitizedAttributes2;
283 if (!isMountedRef.current) {
284 return;
285 }
286 setIsLoading(true);
287 let sanitizedAttributes = attributes && (0,external_wp_blocks_namespaceObject.__experimentalSanitizeBlockAttributes)(block, attributes);
288 if (skipBlockSupportAttributes) {
289 sanitizedAttributes = removeBlockSupportAttributes(sanitizedAttributes);
290 }
291
292 // If httpMethod is 'POST', send the attributes in the request body instead of the URL.
293 // This allows sending a larger attributes object than in a GET request, where the attributes are in the URL.
294 const isPostRequest = 'POST' === httpMethod;
295 const urlAttributes = isPostRequest ? null : (_sanitizedAttributes = sanitizedAttributes) !== null && _sanitizedAttributes !== void 0 ? _sanitizedAttributes : null;
296 const path = rendererPath(block, urlAttributes, urlQueryArgs);
297 const data = isPostRequest ? {
298 attributes: (_sanitizedAttributes2 = sanitizedAttributes) !== null && _sanitizedAttributes2 !== void 0 ? _sanitizedAttributes2 : null
299 } : null;
300
301 // Store the latest fetch request so that when we process it, we can
302 // check if it is the current request, to avoid race conditions on slow networks.
303 const fetchRequest = fetchRequestRef.current = external_wp_apiFetch_default()({
304 path,
305 data,
306 method: isPostRequest ? 'POST' : 'GET'
307 }).then(fetchResponse => {
308 if (isMountedRef.current && fetchRequest === fetchRequestRef.current && fetchResponse) {
309 setResponse(fetchResponse.rendered);
310 }
311 }).catch(error => {
312 if (isMountedRef.current && fetchRequest === fetchRequestRef.current) {
313 setResponse({
314 error: true,
315 errorMsg: error.message
316 });
317 }
318 }).finally(() => {
319 if (isMountedRef.current && fetchRequest === fetchRequestRef.current) {
320 setIsLoading(false);
321 }
322 });
323 return fetchRequest;
324 }
325 const debouncedFetchData = (0,external_wp_compose_namespaceObject.useDebounce)(fetchData, 500);
326
327 // When the component unmounts, set isMountedRef to false. This will
328 // let the async fetch callbacks know when to stop.
329 (0,external_wp_element_namespaceObject.useEffect)(() => () => {
330 isMountedRef.current = false;
331 }, []);
332 (0,external_wp_element_namespaceObject.useEffect)(() => {
333 // Don't debounce the first fetch. This ensures that the first render
334 // shows data as soon as possible.
335 if (prevProps === undefined) {
336 fetchData();
337 } else if (!es6_default()(prevProps, props)) {
338 debouncedFetchData();
339 }
340 });
341
342 /**
343 * Effect to handle showing the loading placeholder.
344 * Show it only if there is no previous response or
345 * the request takes more than one second.
346 */
347 (0,external_wp_element_namespaceObject.useEffect)(() => {
348 if (!isLoading) {
349 return;
350 }
351 const timeout = setTimeout(() => {
352 setShowLoader(true);
353 }, 1000);
354 return () => clearTimeout(timeout);
355 }, [isLoading]);
356 const hasResponse = !!response;
357 const hasEmptyResponse = response === '';
358 const hasError = response?.error;
359 if (isLoading) {
360 return (0,external_React_namespaceObject.createElement)(LoadingResponsePlaceholder, {
361 ...props,
362 showLoader: showLoader
363 }, hasResponse && (0,external_React_namespaceObject.createElement)(external_wp_element_namespaceObject.RawHTML, {
364 className: className
365 }, response));
366 }
367 if (hasEmptyResponse || !hasResponse) {
368 return (0,external_React_namespaceObject.createElement)(EmptyResponsePlaceholder, {
369 ...props
370 });
371 }
372 if (hasError) {
373 return (0,external_React_namespaceObject.createElement)(ErrorResponsePlaceholder, {
374 response: response,
375 ...props
376 });
377 }
378 return (0,external_React_namespaceObject.createElement)(external_wp_element_namespaceObject.RawHTML, {
379 className: className
380 }, response);
381 }
382
383 ;// CONCATENATED MODULE: ./packages/server-side-render/build-module/index.js
384
385 /**
386 * WordPress dependencies
387 */
388
389
390
391 /**
392 * Internal dependencies
393 */
394
395
396 /**
397 * Constants
398 */
399 const build_module_EMPTY_OBJECT = {};
400 const ExportedServerSideRender = (0,external_wp_data_namespaceObject.withSelect)(select => {
401 // FIXME: @wordpress/server-side-render should not depend on @wordpress/editor.
402 // It is used by blocks that can be loaded into a *non-post* block editor.
403 // eslint-disable-next-line @wordpress/data-no-store-string-literals
404 const coreEditorSelect = select('core/editor');
405 if (coreEditorSelect) {
406 const currentPostId = coreEditorSelect.getCurrentPostId();
407 // For templates and template parts we use a custom ID format.
408 // Since they aren't real posts, we don't want to use their ID
409 // for server-side rendering. Since they use a string based ID,
410 // we can assume real post IDs are numbers.
411 if (currentPostId && typeof currentPostId === 'number') {
412 return {
413 currentPostId
414 };
415 }
416 }
417 return build_module_EMPTY_OBJECT;
418 })(({
419 urlQueryArgs = build_module_EMPTY_OBJECT,
420 currentPostId,
421 ...props
422 }) => {
423 const newUrlQueryArgs = (0,external_wp_element_namespaceObject.useMemo)(() => {
424 if (!currentPostId) {
425 return urlQueryArgs;
426 }
427 return {
428 post_id: currentPostId,
429 ...urlQueryArgs
430 };
431 }, [currentPostId, urlQueryArgs]);
432 return (0,external_React_namespaceObject.createElement)(ServerSideRender, {
433 urlQueryArgs: newUrlQueryArgs,
434 ...props
435 });
436 });
437 /* harmony default export */ const build_module = (ExportedServerSideRender);
438
439 })();
440
441 (window.wp = window.wp || {}).serverSideRender = __webpack_exports__["default"];
442 /******/ })()
443 ;