PluginProbe
Gutenberg / 16.6.0
Gutenberg v16.6.0
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 / hooks / index.js

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

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