PluginProbe
Gutenberg / 12.1.0
Gutenberg v12.1.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 7.4.0 All 402 releases
gutenberg / build / hooks / index.js

index.js in Gutenberg 12.1.0, at build/hooks/index.js

642 lines 20.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /******/ (function() { // webpackBootstrap
2 /******/ "use strict";
3 /******/ // The require scope
4 /******/ var __webpack_require__ = {};
5 /******/
6 /************************************************************************/
7 /******/ /* webpack/runtime/define property getters */
8 /******/ !function() {
9 /******/ // define getter functions for harmony exports
10 /******/ __webpack_require__.d = function(exports, definition) {
11 /******/ for(var key in definition) {
12 /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
13 /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
14 /******/ }
15 /******/ }
16 /******/ };
17 /******/ }();
18 /******/
19 /******/ /* webpack/runtime/hasOwnProperty shorthand */
20 /******/ !function() {
21 /******/ __webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
22 /******/ }();
23 /******/
24 /******/ /* webpack/runtime/make namespace object */
25 /******/ !function() {
26 /******/ // define __esModule on exports
27 /******/ __webpack_require__.r = function(exports) {
28 /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
29 /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
30 /******/ }
31 /******/ Object.defineProperty(exports, '__esModule', { value: true });
32 /******/ };
33 /******/ }();
34 /******/
35 /************************************************************************/
36 var __webpack_exports__ = {};
37 // ESM COMPAT FLAG
38 __webpack_require__.r(__webpack_exports__);
39
40 // EXPORTS
41 __webpack_require__.d(__webpack_exports__, {
42 "actions": function() { return /* binding */ actions; },
43 "addAction": function() { return /* binding */ addAction; },
44 "addFilter": function() { return /* binding */ addFilter; },
45 "applyFilters": function() { return /* binding */ applyFilters; },
46 "createHooks": function() { return /* reexport */ build_module_createHooks; },
47 "currentAction": function() { return /* binding */ currentAction; },
48 "currentFilter": function() { return /* binding */ currentFilter; },
49 "defaultHooks": function() { return /* binding */ defaultHooks; },
50 "didAction": function() { return /* binding */ didAction; },
51 "didFilter": function() { return /* binding */ didFilter; },
52 "doAction": function() { return /* binding */ doAction; },
53 "doingAction": function() { return /* binding */ doingAction; },
54 "doingFilter": function() { return /* binding */ doingFilter; },
55 "filters": function() { return /* binding */ filters; },
56 "hasAction": function() { return /* binding */ hasAction; },
57 "hasFilter": function() { return /* binding */ hasFilter; },
58 "removeAction": function() { return /* binding */ removeAction; },
59 "removeAllActions": function() { return /* binding */ removeAllActions; },
60 "removeAllFilters": function() { return /* binding */ removeAllFilters; },
61 "removeFilter": function() { return /* binding */ removeFilter; }
62 });
63
64 ;// CONCATENATED MODULE: ./packages/hooks/build-module/validateNamespace.js
65 /**
66 * Validate a namespace string.
67 *
68 * @param {string} namespace The namespace to validate - should take the form
69 * `vendor/plugin/function`.
70 *
71 * @return {boolean} Whether the namespace is valid.
72 */
73 function validateNamespace(namespace) {
74 if ('string' !== typeof namespace || '' === namespace) {
75 // eslint-disable-next-line no-console
76 console.error('The namespace must be a non-empty string.');
77 return false;
78 }
79
80 if (!/^[a-zA-Z][a-zA-Z0-9_.\-\/]*$/.test(namespace)) {
81 // eslint-disable-next-line no-console
82 console.error('The namespace can only contain numbers, letters, dashes, periods, underscores and slashes.');
83 return false;
84 }
85
86 return true;
87 }
88
89 /* harmony default export */ var build_module_validateNamespace = (validateNamespace);
90 //# sourceMappingURL=validateNamespace.js.map
91 ;// CONCATENATED MODULE: ./packages/hooks/build-module/validateHookName.js
92 /**
93 * Validate a hookName string.
94 *
95 * @param {string} hookName The hook name to validate. Should be a non empty string containing
96 * only numbers, letters, dashes, periods and underscores. Also,
97 * the hook name cannot begin with `__`.
98 *
99 * @return {boolean} Whether the hook name is valid.
100 */
101 function validateHookName(hookName) {
102 if ('string' !== typeof hookName || '' === hookName) {
103 // eslint-disable-next-line no-console
104 console.error('The hook name must be a non-empty string.');
105 return false;
106 }
107
108 if (/^__/.test(hookName)) {
109 // eslint-disable-next-line no-console
110 console.error('The hook name cannot begin with `__`.');
111 return false;
112 }
113
114 if (!/^[a-zA-Z][a-zA-Z0-9_.-]*$/.test(hookName)) {
115 // eslint-disable-next-line no-console
116 console.error('The hook name can only contain numbers, letters, dashes, periods and underscores.');
117 return false;
118 }
119
120 return true;
121 }
122
123 /* harmony default export */ var build_module_validateHookName = (validateHookName);
124 //# sourceMappingURL=validateHookName.js.map
125 ;// CONCATENATED MODULE: ./packages/hooks/build-module/createAddHook.js
126 /**
127 * Internal dependencies
128 */
129
130
131 /**
132 * @callback AddHook
133 *
134 * Adds the hook to the appropriate hooks container.
135 *
136 * @param {string} hookName Name of hook to add
137 * @param {string} namespace The unique namespace identifying the callback in the form `vendor/plugin/function`.
138 * @param {import('.').Callback} callback Function to call when the hook is run
139 * @param {number} [priority=10] Priority of this hook
140 */
141
142 /**
143 * Returns a function which, when invoked, will add a hook.
144 *
145 * @param {import('.').Hooks} hooks Hooks instance.
146 * @param {import('.').StoreKey} storeKey
147 *
148 * @return {AddHook} Function that adds a new hook.
149 */
150
151 function createAddHook(hooks, storeKey) {
152 return function addHook(hookName, namespace, callback) {
153 let priority = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 10;
154 const hooksStore = hooks[storeKey];
155
156 if (!build_module_validateHookName(hookName)) {
157 return;
158 }
159
160 if (!build_module_validateNamespace(namespace)) {
161 return;
162 }
163
164 if ('function' !== typeof callback) {
165 // eslint-disable-next-line no-console
166 console.error('The hook callback must be a function.');
167 return;
168 } // Validate numeric priority
169
170
171 if ('number' !== typeof priority) {
172 // eslint-disable-next-line no-console
173 console.error('If specified, the hook priority must be a number.');
174 return;
175 }
176
177 const handler = {
178 callback,
179 priority,
180 namespace
181 };
182
183 if (hooksStore[hookName]) {
184 // Find the correct insert index of the new hook.
185 const handlers = hooksStore[hookName].handlers;
186 /** @type {number} */
187
188 let i;
189
190 for (i = handlers.length; i > 0; i--) {
191 if (priority >= handlers[i - 1].priority) {
192 break;
193 }
194 }
195
196 if (i === handlers.length) {
197 // If append, operate via direct assignment.
198 handlers[i] = handler;
199 } else {
200 // Otherwise, insert before index via splice.
201 handlers.splice(i, 0, handler);
202 } // We may also be currently executing this hook. If the callback
203 // we're adding would come after the current callback, there's no
204 // problem; otherwise we need to increase the execution index of
205 // any other runs by 1 to account for the added element.
206
207
208 hooksStore.__current.forEach(hookInfo => {
209 if (hookInfo.name === hookName && hookInfo.currentIndex >= i) {
210 hookInfo.currentIndex++;
211 }
212 });
213 } else {
214 // This is the first hook of its type.
215 hooksStore[hookName] = {
216 handlers: [handler],
217 runs: 0
218 };
219 }
220
221 if (hookName !== 'hookAdded') {
222 hooks.doAction('hookAdded', hookName, namespace, callback, priority);
223 }
224 };
225 }
226
227 /* harmony default export */ var build_module_createAddHook = (createAddHook);
228 //# sourceMappingURL=createAddHook.js.map
229 ;// CONCATENATED MODULE: ./packages/hooks/build-module/createRemoveHook.js
230 /**
231 * Internal dependencies
232 */
233
234
235 /**
236 * @callback RemoveHook
237 * Removes the specified callback (or all callbacks) from the hook with a given hookName
238 * and namespace.
239 *
240 * @param {string} hookName The name of the hook to modify.
241 * @param {string} namespace The unique namespace identifying the callback in the
242 * form `vendor/plugin/function`.
243 *
244 * @return {number | undefined} The number of callbacks removed.
245 */
246
247 /**
248 * Returns a function which, when invoked, will remove a specified hook or all
249 * hooks by the given name.
250 *
251 * @param {import('.').Hooks} hooks Hooks instance.
252 * @param {import('.').StoreKey} storeKey
253 * @param {boolean} [removeAll=false] Whether to remove all callbacks for a hookName,
254 * without regard to namespace. Used to create
255 * `removeAll*` functions.
256 *
257 * @return {RemoveHook} Function that removes hooks.
258 */
259
260 function createRemoveHook(hooks, storeKey) {
261 let removeAll = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
262 return function removeHook(hookName, namespace) {
263 const hooksStore = hooks[storeKey];
264
265 if (!build_module_validateHookName(hookName)) {
266 return;
267 }
268
269 if (!removeAll && !build_module_validateNamespace(namespace)) {
270 return;
271 } // Bail if no hooks exist by this name
272
273
274 if (!hooksStore[hookName]) {
275 return 0;
276 }
277
278 let handlersRemoved = 0;
279
280 if (removeAll) {
281 handlersRemoved = hooksStore[hookName].handlers.length;
282 hooksStore[hookName] = {
283 runs: hooksStore[hookName].runs,
284 handlers: []
285 };
286 } else {
287 // Try to find the specified callback to remove.
288 const handlers = hooksStore[hookName].handlers;
289
290 for (let i = handlers.length - 1; i >= 0; i--) {
291 if (handlers[i].namespace === namespace) {
292 handlers.splice(i, 1);
293 handlersRemoved++; // This callback may also be part of a hook that is
294 // currently executing. If the callback we're removing
295 // comes after the current callback, there's no problem;
296 // otherwise we need to decrease the execution index of any
297 // other runs by 1 to account for the removed element.
298
299 hooksStore.__current.forEach(hookInfo => {
300 if (hookInfo.name === hookName && hookInfo.currentIndex >= i) {
301 hookInfo.currentIndex--;
302 }
303 });
304 }
305 }
306 }
307
308 if (hookName !== 'hookRemoved') {
309 hooks.doAction('hookRemoved', hookName, namespace);
310 }
311
312 return handlersRemoved;
313 };
314 }
315
316 /* harmony default export */ var build_module_createRemoveHook = (createRemoveHook);
317 //# sourceMappingURL=createRemoveHook.js.map
318 ;// CONCATENATED MODULE: ./packages/hooks/build-module/createHasHook.js
319 /**
320 * @callback HasHook
321 *
322 * Returns whether any handlers are attached for the given hookName and optional namespace.
323 *
324 * @param {string} hookName The name of the hook to check for.
325 * @param {string} [namespace] Optional. The unique namespace identifying the callback
326 * in the form `vendor/plugin/function`.
327 *
328 * @return {boolean} Whether there are handlers that are attached to the given hook.
329 */
330
331 /**
332 * Returns a function which, when invoked, will return whether any handlers are
333 * attached to a particular hook.
334 *
335 * @param {import('.').Hooks} hooks Hooks instance.
336 * @param {import('.').StoreKey} storeKey
337 *
338 * @return {HasHook} Function that returns whether any handlers are
339 * attached to a particular hook and optional namespace.
340 */
341 function createHasHook(hooks, storeKey) {
342 return function hasHook(hookName, namespace) {
343 const hooksStore = hooks[storeKey]; // Use the namespace if provided.
344
345 if ('undefined' !== typeof namespace) {
346 return hookName in hooksStore && hooksStore[hookName].handlers.some(hook => hook.namespace === namespace);
347 }
348
349 return hookName in hooksStore;
350 };
351 }
352
353 /* harmony default export */ var build_module_createHasHook = (createHasHook);
354 //# sourceMappingURL=createHasHook.js.map
355 ;// CONCATENATED MODULE: ./packages/hooks/build-module/createRunHook.js
356 /**
357 * Returns a function which, when invoked, will execute all callbacks
358 * registered to a hook of the specified type, optionally returning the final
359 * value of the call chain.
360 *
361 * @param {import('.').Hooks} hooks Hooks instance.
362 * @param {import('.').StoreKey} storeKey
363 * @param {boolean} [returnFirstArg=false] Whether each hook callback is expected to
364 * return its first argument.
365 *
366 * @return {(hookName:string, ...args: unknown[]) => unknown} Function that runs hook callbacks.
367 */
368 function createRunHook(hooks, storeKey) {
369 let returnFirstArg = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
370 return function runHooks(hookName) {
371 const hooksStore = hooks[storeKey];
372
373 if (!hooksStore[hookName]) {
374 hooksStore[hookName] = {
375 handlers: [],
376 runs: 0
377 };
378 }
379
380 hooksStore[hookName].runs++;
381 const handlers = hooksStore[hookName].handlers; // The following code is stripped from production builds.
382
383 if (false) {}
384
385 for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
386 args[_key - 1] = arguments[_key];
387 }
388
389 if (!handlers || !handlers.length) {
390 return returnFirstArg ? args[0] : undefined;
391 }
392
393 const hookInfo = {
394 name: hookName,
395 currentIndex: 0
396 };
397
398 hooksStore.__current.push(hookInfo);
399
400 while (hookInfo.currentIndex < handlers.length) {
401 const handler = handlers[hookInfo.currentIndex];
402 const result = handler.callback.apply(null, args);
403
404 if (returnFirstArg) {
405 args[0] = result;
406 }
407
408 hookInfo.currentIndex++;
409 }
410
411 hooksStore.__current.pop();
412
413 if (returnFirstArg) {
414 return args[0];
415 }
416 };
417 }
418
419 /* harmony default export */ var build_module_createRunHook = (createRunHook);
420 //# sourceMappingURL=createRunHook.js.map
421 ;// CONCATENATED MODULE: ./packages/hooks/build-module/createCurrentHook.js
422 /**
423 * Returns a function which, when invoked, will return the name of the
424 * currently running hook, or `null` if no hook of the given type is currently
425 * running.
426 *
427 * @param {import('.').Hooks} hooks Hooks instance.
428 * @param {import('.').StoreKey} storeKey
429 *
430 * @return {() => string | null} Function that returns the current hook name or null.
431 */
432 function createCurrentHook(hooks, storeKey) {
433 return function currentHook() {
434 var _hooksStore$__current, _hooksStore$__current2;
435
436 const hooksStore = hooks[storeKey];
437 return (_hooksStore$__current = (_hooksStore$__current2 = hooksStore.__current[hooksStore.__current.length - 1]) === null || _hooksStore$__current2 === void 0 ? void 0 : _hooksStore$__current2.name) !== null && _hooksStore$__current !== void 0 ? _hooksStore$__current : null;
438 };
439 }
440
441 /* harmony default export */ var build_module_createCurrentHook = (createCurrentHook);
442 //# sourceMappingURL=createCurrentHook.js.map
443 ;// CONCATENATED MODULE: ./packages/hooks/build-module/createDoingHook.js
444 /**
445 * @callback DoingHook
446 * Returns whether a hook is currently being executed.
447 *
448 * @param {string} [hookName] The name of the hook to check for. If
449 * omitted, will check for any hook being executed.
450 *
451 * @return {boolean} Whether the hook is being executed.
452 */
453
454 /**
455 * Returns a function which, when invoked, will return whether a hook is
456 * currently being executed.
457 *
458 * @param {import('.').Hooks} hooks Hooks instance.
459 * @param {import('.').StoreKey} storeKey
460 *
461 * @return {DoingHook} Function that returns whether a hook is currently
462 * being executed.
463 */
464 function createDoingHook(hooks, storeKey) {
465 return function doingHook(hookName) {
466 const hooksStore = hooks[storeKey]; // If the hookName was not passed, check for any current hook.
467
468 if ('undefined' === typeof hookName) {
469 return 'undefined' !== typeof hooksStore.__current[0];
470 } // Return the __current hook.
471
472
473 return hooksStore.__current[0] ? hookName === hooksStore.__current[0].name : false;
474 };
475 }
476
477 /* harmony default export */ var build_module_createDoingHook = (createDoingHook);
478 //# sourceMappingURL=createDoingHook.js.map
479 ;// CONCATENATED MODULE: ./packages/hooks/build-module/createDidHook.js
480 /**
481 * Internal dependencies
482 */
483
484 /**
485 * @callback DidHook
486 *
487 * Returns the number of times an action has been fired.
488 *
489 * @param {string} hookName The hook name to check.
490 *
491 * @return {number | undefined} The number of times the hook has run.
492 */
493
494 /**
495 * Returns a function which, when invoked, will return the number of times a
496 * hook has been called.
497 *
498 * @param {import('.').Hooks} hooks Hooks instance.
499 * @param {import('.').StoreKey} storeKey
500 *
501 * @return {DidHook} Function that returns a hook's call count.
502 */
503
504 function createDidHook(hooks, storeKey) {
505 return function didHook(hookName) {
506 const hooksStore = hooks[storeKey];
507
508 if (!build_module_validateHookName(hookName)) {
509 return;
510 }
511
512 return hooksStore[hookName] && hooksStore[hookName].runs ? hooksStore[hookName].runs : 0;
513 };
514 }
515
516 /* harmony default export */ var build_module_createDidHook = (createDidHook);
517 //# sourceMappingURL=createDidHook.js.map
518 ;// CONCATENATED MODULE: ./packages/hooks/build-module/createHooks.js
519 /**
520 * Internal dependencies
521 */
522
523
524
525
526
527
528
529 /**
530 * Internal class for constructing hooks. Use `createHooks()` function
531 *
532 * Note, it is necessary to expose this class to make its type public.
533 *
534 * @private
535 */
536
537 class _Hooks {
538 constructor() {
539 /** @type {import('.').Store} actions */
540 this.actions = Object.create(null);
541 this.actions.__current = [];
542 /** @type {import('.').Store} filters */
543
544 this.filters = Object.create(null);
545 this.filters.__current = [];
546 this.addAction = build_module_createAddHook(this, 'actions');
547 this.addFilter = build_module_createAddHook(this, 'filters');
548 this.removeAction = build_module_createRemoveHook(this, 'actions');
549 this.removeFilter = build_module_createRemoveHook(this, 'filters');
550 this.hasAction = build_module_createHasHook(this, 'actions');
551 this.hasFilter = build_module_createHasHook(this, 'filters');
552 this.removeAllActions = build_module_createRemoveHook(this, 'actions', true);
553 this.removeAllFilters = build_module_createRemoveHook(this, 'filters', true);
554 this.doAction = build_module_createRunHook(this, 'actions');
555 this.applyFilters = build_module_createRunHook(this, 'filters', true);
556 this.currentAction = build_module_createCurrentHook(this, 'actions');
557 this.currentFilter = build_module_createCurrentHook(this, 'filters');
558 this.doingAction = build_module_createDoingHook(this, 'actions');
559 this.doingFilter = build_module_createDoingHook(this, 'filters');
560 this.didAction = build_module_createDidHook(this, 'actions');
561 this.didFilter = build_module_createDidHook(this, 'filters');
562 }
563
564 }
565 /** @typedef {_Hooks} Hooks */
566
567 /**
568 * Returns an instance of the hooks object.
569 *
570 * @return {Hooks} A Hooks instance.
571 */
572
573 function createHooks() {
574 return new _Hooks();
575 }
576
577 /* harmony default export */ var build_module_createHooks = (createHooks);
578 //# sourceMappingURL=createHooks.js.map
579 ;// CONCATENATED MODULE: ./packages/hooks/build-module/index.js
580 /**
581 * Internal dependencies
582 */
583
584 /** @typedef {(...args: any[])=>any} Callback */
585
586 /**
587 * @typedef Handler
588 * @property {Callback} callback The callback
589 * @property {string} namespace The namespace
590 * @property {number} priority The namespace
591 */
592
593 /**
594 * @typedef Hook
595 * @property {Handler[]} handlers Array of handlers
596 * @property {number} runs Run counter
597 */
598
599 /**
600 * @typedef Current
601 * @property {string} name Hook name
602 * @property {number} currentIndex The index
603 */
604
605 /**
606 * @typedef {Record<string, Hook> & {__current: Current[]}} Store
607 */
608
609 /**
610 * @typedef {'actions' | 'filters'} StoreKey
611 */
612
613 /**
614 * @typedef {import('./createHooks').Hooks} Hooks
615 */
616
617 const defaultHooks = build_module_createHooks();
618 const {
619 addAction,
620 addFilter,
621 removeAction,
622 removeFilter,
623 hasAction,
624 hasFilter,
625 removeAllActions,
626 removeAllFilters,
627 doAction,
628 applyFilters,
629 currentAction,
630 currentFilter,
631 doingAction,
632 doingFilter,
633 didAction,
634 didFilter,
635 actions,
636 filters
637 } = defaultHooks;
638
639 //# sourceMappingURL=index.js.map
640 (window.wp = window.wp || {}).hooks = __webpack_exports__;
641 /******/ })()
642 ;