PluginProbe
Gutenberg / 16.2.0
Gutenberg v16.2.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 / annotations / index.js

index.js in Gutenberg 16.2.0, at build/annotations/index.js

1,040 lines 30.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 /******/ // 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 "store": () => (/* reexport */ store)
43 });
44
45 // NAMESPACE OBJECT: ./packages/annotations/build-module/store/selectors.js
46 var selectors_namespaceObject = {};
47 __webpack_require__.r(selectors_namespaceObject);
48 __webpack_require__.d(selectors_namespaceObject, {
49 "__experimentalGetAllAnnotationsForBlock": () => (__experimentalGetAllAnnotationsForBlock),
50 "__experimentalGetAnnotations": () => (__experimentalGetAnnotations),
51 "__experimentalGetAnnotationsForBlock": () => (__experimentalGetAnnotationsForBlock),
52 "__experimentalGetAnnotationsForRichText": () => (__experimentalGetAnnotationsForRichText)
53 });
54
55 // NAMESPACE OBJECT: ./packages/annotations/build-module/store/actions.js
56 var actions_namespaceObject = {};
57 __webpack_require__.r(actions_namespaceObject);
58 __webpack_require__.d(actions_namespaceObject, {
59 "__experimentalAddAnnotation": () => (__experimentalAddAnnotation),
60 "__experimentalRemoveAnnotation": () => (__experimentalRemoveAnnotation),
61 "__experimentalRemoveAnnotationsBySource": () => (__experimentalRemoveAnnotationsBySource),
62 "__experimentalUpdateAnnotationRange": () => (__experimentalUpdateAnnotationRange)
63 });
64
65 ;// CONCATENATED MODULE: external ["wp","richText"]
66 const external_wp_richText_namespaceObject = window["wp"]["richText"];
67 ;// CONCATENATED MODULE: external ["wp","i18n"]
68 const external_wp_i18n_namespaceObject = window["wp"]["i18n"];
69 ;// CONCATENATED MODULE: ./packages/annotations/build-module/store/constants.js
70 /**
71 * The identifier for the data store.
72 *
73 * @type {string}
74 */
75 const STORE_NAME = 'core/annotations';
76
77 ;// CONCATENATED MODULE: ./packages/annotations/build-module/format/annotation.js
78 /**
79 * WordPress dependencies
80 */
81
82
83 const FORMAT_NAME = 'core/annotation';
84 const ANNOTATION_ATTRIBUTE_PREFIX = 'annotation-text-';
85 /**
86 * Internal dependencies
87 */
88
89
90 /**
91 * Applies given annotations to the given record.
92 *
93 * @param {Object} record The record to apply annotations to.
94 * @param {Array} annotations The annotation to apply.
95 * @return {Object} A record with the annotations applied.
96 */
97
98 function applyAnnotations(record, annotations = []) {
99 annotations.forEach(annotation => {
100 let {
101 start,
102 end
103 } = annotation;
104
105 if (start > record.text.length) {
106 start = record.text.length;
107 }
108
109 if (end > record.text.length) {
110 end = record.text.length;
111 }
112
113 const className = ANNOTATION_ATTRIBUTE_PREFIX + annotation.source;
114 const id = ANNOTATION_ATTRIBUTE_PREFIX + annotation.id;
115 record = (0,external_wp_richText_namespaceObject.applyFormat)(record, {
116 type: FORMAT_NAME,
117 attributes: {
118 className,
119 id
120 }
121 }, start, end);
122 });
123 return record;
124 }
125 /**
126 * Removes annotations from the given record.
127 *
128 * @param {Object} record Record to remove annotations from.
129 * @return {Object} The cleaned record.
130 */
131
132 function removeAnnotations(record) {
133 return removeFormat(record, 'core/annotation', 0, record.text.length);
134 }
135 /**
136 * Retrieves the positions of annotations inside an array of formats.
137 *
138 * @param {Array} formats Formats with annotations in there.
139 * @return {Object} ID keyed positions of annotations.
140 */
141
142 function retrieveAnnotationPositions(formats) {
143 const positions = {};
144 formats.forEach((characterFormats, i) => {
145 characterFormats = characterFormats || [];
146 characterFormats = characterFormats.filter(format => format.type === FORMAT_NAME);
147 characterFormats.forEach(format => {
148 let {
149 id
150 } = format.attributes;
151 id = id.replace(ANNOTATION_ATTRIBUTE_PREFIX, '');
152
153 if (!positions.hasOwnProperty(id)) {
154 positions[id] = {
155 start: i
156 };
157 } // Annotations refer to positions between characters.
158 // Formats refer to the character themselves.
159 // So we need to adjust for that here.
160
161
162 positions[id].end = i + 1;
163 });
164 });
165 return positions;
166 }
167 /**
168 * Updates annotations in the state based on positions retrieved from RichText.
169 *
170 * @param {Array} annotations The annotations that are currently applied.
171 * @param {Array} positions The current positions of the given annotations.
172 * @param {Object} actions
173 * @param {Function} actions.removeAnnotation Function to remove an annotation from the state.
174 * @param {Function} actions.updateAnnotationRange Function to update an annotation range in the state.
175 */
176
177
178 function updateAnnotationsWithPositions(annotations, positions, {
179 removeAnnotation,
180 updateAnnotationRange
181 }) {
182 annotations.forEach(currentAnnotation => {
183 const position = positions[currentAnnotation.id]; // If we cannot find an annotation, delete it.
184
185 if (!position) {
186 // Apparently the annotation has been removed, so remove it from the state:
187 // Remove...
188 removeAnnotation(currentAnnotation.id);
189 return;
190 }
191
192 const {
193 start,
194 end
195 } = currentAnnotation;
196
197 if (start !== position.start || end !== position.end) {
198 updateAnnotationRange(currentAnnotation.id, position.start, position.end);
199 }
200 });
201 }
202
203 const annotation = {
204 name: FORMAT_NAME,
205 title: (0,external_wp_i18n_namespaceObject.__)('Annotation'),
206 tagName: 'mark',
207 className: 'annotation-text',
208 attributes: {
209 className: 'class',
210 id: 'id'
211 },
212
213 edit() {
214 return null;
215 },
216
217 __experimentalGetPropsForEditableTreePreparation(select, {
218 richTextIdentifier,
219 blockClientId
220 }) {
221 return {
222 annotations: select(STORE_NAME).__experimentalGetAnnotationsForRichText(blockClientId, richTextIdentifier)
223 };
224 },
225
226 __experimentalCreatePrepareEditableTree({
227 annotations
228 }) {
229 return (formats, text) => {
230 if (annotations.length === 0) {
231 return formats;
232 }
233
234 let record = {
235 formats,
236 text
237 };
238 record = applyAnnotations(record, annotations);
239 return record.formats;
240 };
241 },
242
243 __experimentalGetPropsForEditableTreeChangeHandler(dispatch) {
244 return {
245 removeAnnotation: dispatch(STORE_NAME).__experimentalRemoveAnnotation,
246 updateAnnotationRange: dispatch(STORE_NAME).__experimentalUpdateAnnotationRange
247 };
248 },
249
250 __experimentalCreateOnChangeEditableValue(props) {
251 return formats => {
252 const positions = retrieveAnnotationPositions(formats);
253 const {
254 removeAnnotation,
255 updateAnnotationRange,
256 annotations
257 } = props;
258 updateAnnotationsWithPositions(annotations, positions, {
259 removeAnnotation,
260 updateAnnotationRange
261 });
262 };
263 }
264
265 };
266
267 ;// CONCATENATED MODULE: ./packages/annotations/build-module/format/index.js
268 /**
269 * WordPress dependencies
270 */
271
272 /**
273 * Internal dependencies
274 */
275
276
277 const {
278 name: format_name,
279 ...settings
280 } = annotation;
281 (0,external_wp_richText_namespaceObject.registerFormatType)(format_name, settings);
282
283 ;// CONCATENATED MODULE: external ["wp","hooks"]
284 const external_wp_hooks_namespaceObject = window["wp"]["hooks"];
285 ;// CONCATENATED MODULE: external ["wp","data"]
286 const external_wp_data_namespaceObject = window["wp"]["data"];
287 ;// CONCATENATED MODULE: ./packages/annotations/build-module/block/index.js
288 /**
289 * WordPress dependencies
290 */
291
292
293 /**
294 * Internal dependencies
295 */
296
297
298 /**
299 * Adds annotation className to the block-list-block component.
300 *
301 * @param {Object} OriginalComponent The original BlockListBlock component.
302 * @return {Object} The enhanced component.
303 */
304
305 const addAnnotationClassName = OriginalComponent => {
306 return (0,external_wp_data_namespaceObject.withSelect)((select, {
307 clientId,
308 className
309 }) => {
310 const annotations = select(STORE_NAME).__experimentalGetAnnotationsForBlock(clientId);
311
312 return {
313 className: annotations.map(annotation => {
314 return 'is-annotated-by-' + annotation.source;
315 }).concat(className).filter(Boolean).join(' ')
316 };
317 })(OriginalComponent);
318 };
319
320 (0,external_wp_hooks_namespaceObject.addFilter)('editor.BlockListBlock', 'core/annotations', addAnnotationClassName);
321
322 ;// CONCATENATED MODULE: ./packages/annotations/build-module/store/reducer.js
323 /**
324 * Filters an array based on the predicate, but keeps the reference the same if
325 * the array hasn't changed.
326 *
327 * @param {Array} collection The collection to filter.
328 * @param {Function} predicate Function that determines if the item should stay
329 * in the array.
330 * @return {Array} Filtered array.
331 */
332 function filterWithReference(collection, predicate) {
333 const filteredCollection = collection.filter(predicate);
334 return collection.length === filteredCollection.length ? collection : filteredCollection;
335 }
336 /**
337 * Creates a new object with the same keys, but with `callback()` called as
338 * a transformer function on each of the values.
339 *
340 * @param {Object} obj The object to transform.
341 * @param {Function} callback The function to transform each object value.
342 * @return {Array} Transformed object.
343 */
344
345
346 const mapValues = (obj, callback) => Object.entries(obj).reduce((acc, [key, value]) => ({ ...acc,
347 [key]: callback(value)
348 }), {});
349 /**
350 * Verifies whether the given annotations is a valid annotation.
351 *
352 * @param {Object} annotation The annotation to verify.
353 * @return {boolean} Whether the given annotation is valid.
354 */
355
356
357 function isValidAnnotationRange(annotation) {
358 return typeof annotation.start === 'number' && typeof annotation.end === 'number' && annotation.start <= annotation.end;
359 }
360 /**
361 * Reducer managing annotations.
362 *
363 * @param {Object} state The annotations currently shown in the editor.
364 * @param {Object} action Dispatched action.
365 *
366 * @return {Array} Updated state.
367 */
368
369
370 function annotations(state = {}, action) {
371 var _state$blockClientId;
372
373 switch (action.type) {
374 case 'ANNOTATION_ADD':
375 const blockClientId = action.blockClientId;
376 const newAnnotation = {
377 id: action.id,
378 blockClientId,
379 richTextIdentifier: action.richTextIdentifier,
380 source: action.source,
381 selector: action.selector,
382 range: action.range
383 };
384
385 if (newAnnotation.selector === 'range' && !isValidAnnotationRange(newAnnotation.range)) {
386 return state;
387 }
388
389 const previousAnnotationsForBlock = (_state$blockClientId = state?.[blockClientId]) !== null && _state$blockClientId !== void 0 ? _state$blockClientId : [];
390 return { ...state,
391 [blockClientId]: [...previousAnnotationsForBlock, newAnnotation]
392 };
393
394 case 'ANNOTATION_REMOVE':
395 return mapValues(state, annotationsForBlock => {
396 return filterWithReference(annotationsForBlock, annotation => {
397 return annotation.id !== action.annotationId;
398 });
399 });
400
401 case 'ANNOTATION_UPDATE_RANGE':
402 return mapValues(state, annotationsForBlock => {
403 let hasChangedRange = false;
404 const newAnnotations = annotationsForBlock.map(annotation => {
405 if (annotation.id === action.annotationId) {
406 hasChangedRange = true;
407 return { ...annotation,
408 range: {
409 start: action.start,
410 end: action.end
411 }
412 };
413 }
414
415 return annotation;
416 });
417 return hasChangedRange ? newAnnotations : annotationsForBlock;
418 });
419
420 case 'ANNOTATION_REMOVE_SOURCE':
421 return mapValues(state, annotationsForBlock => {
422 return filterWithReference(annotationsForBlock, annotation => {
423 return annotation.source !== action.source;
424 });
425 });
426 }
427
428 return state;
429 }
430 /* harmony default export */ const reducer = (annotations);
431
432 ;// CONCATENATED MODULE: ./node_modules/rememo/rememo.js
433
434
435 /** @typedef {(...args: any[]) => *[]} GetDependants */
436
437 /** @typedef {() => void} Clear */
438
439 /**
440 * @typedef {{
441 * getDependants: GetDependants,
442 * clear: Clear
443 * }} EnhancedSelector
444 */
445
446 /**
447 * Internal cache entry.
448 *
449 * @typedef CacheNode
450 *
451 * @property {?CacheNode|undefined} [prev] Previous node.
452 * @property {?CacheNode|undefined} [next] Next node.
453 * @property {*[]} args Function arguments for cache entry.
454 * @property {*} val Function result.
455 */
456
457 /**
458 * @typedef Cache
459 *
460 * @property {Clear} clear Function to clear cache.
461 * @property {boolean} [isUniqueByDependants] Whether dependants are valid in
462 * considering cache uniqueness. A cache is unique if dependents are all arrays
463 * or objects.
464 * @property {CacheNode?} [head] Cache head.
465 * @property {*[]} [lastDependants] Dependants from previous invocation.
466 */
467
468 /**
469 * Arbitrary value used as key for referencing cache object in WeakMap tree.
470 *
471 * @type {{}}
472 */
473 var LEAF_KEY = {};
474
475 /**
476 * Returns the first argument as the sole entry in an array.
477 *
478 * @template T
479 *
480 * @param {T} value Value to return.
481 *
482 * @return {[T]} Value returned as entry in array.
483 */
484 function arrayOf(value) {
485 return [value];
486 }
487
488 /**
489 * Returns true if the value passed is object-like, or false otherwise. A value
490 * is object-like if it can support property assignment, e.g. object or array.
491 *
492 * @param {*} value Value to test.
493 *
494 * @return {boolean} Whether value is object-like.
495 */
496 function isObjectLike(value) {
497 return !!value && 'object' === typeof value;
498 }
499
500 /**
501 * Creates and returns a new cache object.
502 *
503 * @return {Cache} Cache object.
504 */
505 function createCache() {
506 /** @type {Cache} */
507 var cache = {
508 clear: function () {
509 cache.head = null;
510 },
511 };
512
513 return cache;
514 }
515
516 /**
517 * Returns true if entries within the two arrays are strictly equal by
518 * reference from a starting index.
519 *
520 * @param {*[]} a First array.
521 * @param {*[]} b Second array.
522 * @param {number} fromIndex Index from which to start comparison.
523 *
524 * @return {boolean} Whether arrays are shallowly equal.
525 */
526 function isShallowEqual(a, b, fromIndex) {
527 var i;
528
529 if (a.length !== b.length) {
530 return false;
531 }
532
533 for (i = fromIndex; i < a.length; i++) {
534 if (a[i] !== b[i]) {
535 return false;
536 }
537 }
538
539 return true;
540 }
541
542 /**
543 * Returns a memoized selector function. The getDependants function argument is
544 * called before the memoized selector and is expected to return an immutable
545 * reference or array of references on which the selector depends for computing
546 * its own return value. The memoize cache is preserved only as long as those
547 * dependant references remain the same. If getDependants returns a different
548 * reference(s), the cache is cleared and the selector value regenerated.
549 *
550 * @template {(...args: *[]) => *} S
551 *
552 * @param {S} selector Selector function.
553 * @param {GetDependants=} getDependants Dependant getter returning an array of
554 * references used in cache bust consideration.
555 */
556 /* harmony default export */ function rememo(selector, getDependants) {
557 /** @type {WeakMap<*,*>} */
558 var rootCache;
559
560 /** @type {GetDependants} */
561 var normalizedGetDependants = getDependants ? getDependants : arrayOf;
562
563 /**
564 * Returns the cache for a given dependants array. When possible, a WeakMap
565 * will be used to create a unique cache for each set of dependants. This
566 * is feasible due to the nature of WeakMap in allowing garbage collection
567 * to occur on entries where the key object is no longer referenced. Since
568 * WeakMap requires the key to be an object, this is only possible when the
569 * dependant is object-like. The root cache is created as a hierarchy where
570 * each top-level key is the first entry in a dependants set, the value a
571 * WeakMap where each key is the next dependant, and so on. This continues
572 * so long as the dependants are object-like. If no dependants are object-
573 * like, then the cache is shared across all invocations.
574 *
575 * @see isObjectLike
576 *
577 * @param {*[]} dependants Selector dependants.
578 *
579 * @return {Cache} Cache object.
580 */
581 function getCache(dependants) {
582 var caches = rootCache,
583 isUniqueByDependants = true,
584 i,
585 dependant,
586 map,
587 cache;
588
589 for (i = 0; i < dependants.length; i++) {
590 dependant = dependants[i];
591
592 // Can only compose WeakMap from object-like key.
593 if (!isObjectLike(dependant)) {
594 isUniqueByDependants = false;
595 break;
596 }
597
598 // Does current segment of cache already have a WeakMap?
599 if (caches.has(dependant)) {
600 // Traverse into nested WeakMap.
601 caches = caches.get(dependant);
602 } else {
603 // Create, set, and traverse into a new one.
604 map = new WeakMap();
605 caches.set(dependant, map);
606 caches = map;
607 }
608 }
609
610 // We use an arbitrary (but consistent) object as key for the last item
611 // in the WeakMap to serve as our running cache.
612 if (!caches.has(LEAF_KEY)) {
613 cache = createCache();
614 cache.isUniqueByDependants = isUniqueByDependants;
615 caches.set(LEAF_KEY, cache);
616 }
617
618 return caches.get(LEAF_KEY);
619 }
620
621 /**
622 * Resets root memoization cache.
623 */
624 function clear() {
625 rootCache = new WeakMap();
626 }
627
628 /* eslint-disable jsdoc/check-param-names */
629 /**
630 * The augmented selector call, considering first whether dependants have
631 * changed before passing it to underlying memoize function.
632 *
633 * @param {*} source Source object for derivation.
634 * @param {...*} extraArgs Additional arguments to pass to selector.
635 *
636 * @return {*} Selector result.
637 */
638 /* eslint-enable jsdoc/check-param-names */
639 function callSelector(/* source, ...extraArgs */) {
640 var len = arguments.length,
641 cache,
642 node,
643 i,
644 args,
645 dependants;
646
647 // Create copy of arguments (avoid leaking deoptimization).
648 args = new Array(len);
649 for (i = 0; i < len; i++) {
650 args[i] = arguments[i];
651 }
652
653 dependants = normalizedGetDependants.apply(null, args);
654 cache = getCache(dependants);
655
656 // If not guaranteed uniqueness by dependants (primitive type), shallow
657 // compare against last dependants and, if references have changed,
658 // destroy cache to recalculate result.
659 if (!cache.isUniqueByDependants) {
660 if (
661 cache.lastDependants &&
662 !isShallowEqual(dependants, cache.lastDependants, 0)
663 ) {
664 cache.clear();
665 }
666
667 cache.lastDependants = dependants;
668 }
669
670 node = cache.head;
671 while (node) {
672 // Check whether node arguments match arguments
673 if (!isShallowEqual(node.args, args, 1)) {
674 node = node.next;
675 continue;
676 }
677
678 // At this point we can assume we've found a match
679
680 // Surface matched node to head if not already
681 if (node !== cache.head) {
682 // Adjust siblings to point to each other.
683 /** @type {CacheNode} */ (node.prev).next = node.next;
684 if (node.next) {
685 node.next.prev = node.prev;
686 }
687
688 node.next = cache.head;
689 node.prev = null;
690 /** @type {CacheNode} */ (cache.head).prev = node;
691 cache.head = node;
692 }
693
694 // Return immediately
695 return node.val;
696 }
697
698 // No cached value found. Continue to insertion phase:
699
700 node = /** @type {CacheNode} */ ({
701 // Generate the result from original function
702 val: selector.apply(null, args),
703 });
704
705 // Avoid including the source object in the cache.
706 args[0] = null;
707 node.args = args;
708
709 // Don't need to check whether node is already head, since it would
710 // have been returned above already if it was
711
712 // Shift existing head down list
713 if (cache.head) {
714 cache.head.prev = node;
715 node.next = cache.head;
716 }
717
718 cache.head = node;
719
720 return node.val;
721 }
722
723 callSelector.getDependants = normalizedGetDependants;
724 callSelector.clear = clear;
725 clear();
726
727 return /** @type {S & EnhancedSelector} */ (callSelector);
728 }
729
730 ;// CONCATENATED MODULE: ./packages/annotations/build-module/store/selectors.js
731 /**
732 * External dependencies
733 */
734
735 /**
736 * Shared reference to an empty array for cases where it is important to avoid
737 * returning a new array reference on every invocation, as in a connected or
738 * other pure component which performs `shouldComponentUpdate` check on props.
739 * This should be used as a last resort, since the normalized data should be
740 * maintained by the reducer result in state.
741 *
742 * @type {Array}
743 */
744
745 const EMPTY_ARRAY = [];
746 /**
747 * Returns the annotations for a specific client ID.
748 *
749 * @param {Object} state Editor state.
750 * @param {string} clientId The ID of the block to get the annotations for.
751 *
752 * @return {Array} The annotations applicable to this block.
753 */
754
755 const __experimentalGetAnnotationsForBlock = rememo((state, blockClientId) => {
756 var _state$blockClientId;
757
758 return ((_state$blockClientId = state?.[blockClientId]) !== null && _state$blockClientId !== void 0 ? _state$blockClientId : []).filter(annotation => {
759 return annotation.selector === 'block';
760 });
761 }, (state, blockClientId) => {
762 var _state$blockClientId2;
763
764 return [(_state$blockClientId2 = state?.[blockClientId]) !== null && _state$blockClientId2 !== void 0 ? _state$blockClientId2 : EMPTY_ARRAY];
765 });
766 function __experimentalGetAllAnnotationsForBlock(state, blockClientId) {
767 var _state$blockClientId3;
768
769 return (_state$blockClientId3 = state?.[blockClientId]) !== null && _state$blockClientId3 !== void 0 ? _state$blockClientId3 : EMPTY_ARRAY;
770 }
771 /**
772 * Returns the annotations that apply to the given RichText instance.
773 *
774 * Both a blockClientId and a richTextIdentifier are required. This is because
775 * a block might have multiple `RichText` components. This does mean that every
776 * block needs to implement annotations itself.
777 *
778 * @param {Object} state Editor state.
779 * @param {string} blockClientId The client ID for the block.
780 * @param {string} richTextIdentifier Unique identifier that identifies the given RichText.
781 * @return {Array} All the annotations relevant for the `RichText`.
782 */
783
784 const __experimentalGetAnnotationsForRichText = rememo((state, blockClientId, richTextIdentifier) => {
785 var _state$blockClientId4;
786
787 return ((_state$blockClientId4 = state?.[blockClientId]) !== null && _state$blockClientId4 !== void 0 ? _state$blockClientId4 : []).filter(annotation => {
788 return annotation.selector === 'range' && richTextIdentifier === annotation.richTextIdentifier;
789 }).map(annotation => {
790 const {
791 range,
792 ...other
793 } = annotation;
794 return { ...range,
795 ...other
796 };
797 });
798 }, (state, blockClientId) => {
799 var _state$blockClientId5;
800
801 return [(_state$blockClientId5 = state?.[blockClientId]) !== null && _state$blockClientId5 !== void 0 ? _state$blockClientId5 : EMPTY_ARRAY];
802 });
803 /**
804 * Returns all annotations in the editor state.
805 *
806 * @param {Object} state Editor state.
807 * @return {Array} All annotations currently applied.
808 */
809
810 function __experimentalGetAnnotations(state) {
811 return Object.values(state).flat();
812 }
813
814 ;// CONCATENATED MODULE: ./node_modules/uuid/dist/esm-browser/rng.js
815 // Unique ID creation requires a high quality random # generator. In the browser we therefore
816 // require the crypto API and do not support built-in fallback to lower quality random number
817 // generators (like Math.random()).
818 // getRandomValues needs to be invoked in a context where "this" is a Crypto implementation. Also,
819 // find the complete implementation of crypto (msCrypto) on IE11.
820 var getRandomValues = typeof crypto !== 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto) || typeof msCrypto !== 'undefined' && typeof msCrypto.getRandomValues === 'function' && msCrypto.getRandomValues.bind(msCrypto);
821 var rnds8 = new Uint8Array(16);
822 function rng() {
823 if (!getRandomValues) {
824 throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
825 }
826
827 return getRandomValues(rnds8);
828 }
829 ;// CONCATENATED MODULE: ./node_modules/uuid/dist/esm-browser/regex.js
830 /* harmony default export */ const regex = (/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i);
831 ;// CONCATENATED MODULE: ./node_modules/uuid/dist/esm-browser/validate.js
832
833
834 function validate(uuid) {
835 return typeof uuid === 'string' && regex.test(uuid);
836 }
837
838 /* harmony default export */ const esm_browser_validate = (validate);
839 ;// CONCATENATED MODULE: ./node_modules/uuid/dist/esm-browser/stringify.js
840
841 /**
842 * Convert array of 16 byte values to UUID string format of the form:
843 * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
844 */
845
846 var byteToHex = [];
847
848 for (var i = 0; i < 256; ++i) {
849 byteToHex.push((i + 0x100).toString(16).substr(1));
850 }
851
852 function stringify(arr) {
853 var offset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
854 // Note: Be careful editing this code! It's been tuned for performance
855 // and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
856 var uuid = (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + '-' + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + '-' + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + '-' + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + '-' + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase(); // Consistency check for valid UUID. If this throws, it's likely due to one
857 // of the following:
858 // - One or more input array values don't map to a hex octet (leading to
859 // "undefined" in the uuid)
860 // - Invalid input values for the RFC `version` or `variant` fields
861
862 if (!esm_browser_validate(uuid)) {
863 throw TypeError('Stringified UUID is invalid');
864 }
865
866 return uuid;
867 }
868
869 /* harmony default export */ const esm_browser_stringify = (stringify);
870 ;// CONCATENATED MODULE: ./node_modules/uuid/dist/esm-browser/v4.js
871
872
873
874 function v4(options, buf, offset) {
875 options = options || {};
876 var rnds = options.random || (options.rng || rng)(); // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
877
878 rnds[6] = rnds[6] & 0x0f | 0x40;
879 rnds[8] = rnds[8] & 0x3f | 0x80; // Copy bytes to buffer, if provided
880
881 if (buf) {
882 offset = offset || 0;
883
884 for (var i = 0; i < 16; ++i) {
885 buf[offset + i] = rnds[i];
886 }
887
888 return buf;
889 }
890
891 return esm_browser_stringify(rnds);
892 }
893
894 /* harmony default export */ const esm_browser_v4 = (v4);
895 ;// CONCATENATED MODULE: ./packages/annotations/build-module/store/actions.js
896 /**
897 * External dependencies
898 */
899
900 /**
901 * @typedef WPAnnotationRange
902 *
903 * @property {number} start The offset where the annotation should start.
904 * @property {number} end The offset where the annotation should end.
905 */
906
907 /**
908 * Adds an annotation to a block.
909 *
910 * The `block` attribute refers to a block ID that needs to be annotated.
911 * `isBlockAnnotation` controls whether or not the annotation is a block
912 * annotation. The `source` is the source of the annotation, this will be used
913 * to identity groups of annotations.
914 *
915 * The `range` property is only relevant if the selector is 'range'.
916 *
917 * @param {Object} annotation The annotation to add.
918 * @param {string} annotation.blockClientId The blockClientId to add the annotation to.
919 * @param {string} annotation.richTextIdentifier Identifier for the RichText instance the annotation applies to.
920 * @param {WPAnnotationRange} annotation.range The range at which to apply this annotation.
921 * @param {string} [annotation.selector="range"] The way to apply this annotation.
922 * @param {string} [annotation.source="default"] The source that added the annotation.
923 * @param {string} [annotation.id] The ID the annotation should have. Generates a UUID by default.
924 *
925 * @return {Object} Action object.
926 */
927
928 function __experimentalAddAnnotation({
929 blockClientId,
930 richTextIdentifier = null,
931 range = null,
932 selector = 'range',
933 source = 'default',
934 id = esm_browser_v4()
935 }) {
936 const action = {
937 type: 'ANNOTATION_ADD',
938 id,
939 blockClientId,
940 richTextIdentifier,
941 source,
942 selector
943 };
944
945 if (selector === 'range') {
946 action.range = range;
947 }
948
949 return action;
950 }
951 /**
952 * Removes an annotation with a specific ID.
953 *
954 * @param {string} annotationId The annotation to remove.
955 *
956 * @return {Object} Action object.
957 */
958
959 function __experimentalRemoveAnnotation(annotationId) {
960 return {
961 type: 'ANNOTATION_REMOVE',
962 annotationId
963 };
964 }
965 /**
966 * Updates the range of an annotation.
967 *
968 * @param {string} annotationId ID of the annotation to update.
969 * @param {number} start The start of the new range.
970 * @param {number} end The end of the new range.
971 *
972 * @return {Object} Action object.
973 */
974
975 function __experimentalUpdateAnnotationRange(annotationId, start, end) {
976 return {
977 type: 'ANNOTATION_UPDATE_RANGE',
978 annotationId,
979 start,
980 end
981 };
982 }
983 /**
984 * Removes all annotations of a specific source.
985 *
986 * @param {string} source The source to remove.
987 *
988 * @return {Object} Action object.
989 */
990
991 function __experimentalRemoveAnnotationsBySource(source) {
992 return {
993 type: 'ANNOTATION_REMOVE_SOURCE',
994 source
995 };
996 }
997
998 ;// CONCATENATED MODULE: ./packages/annotations/build-module/store/index.js
999 /**
1000 * WordPress dependencies
1001 */
1002
1003 /**
1004 * Internal dependencies
1005 */
1006
1007
1008
1009
1010 /**
1011 * Module Constants
1012 */
1013
1014
1015 /**
1016 * Store definition for the annotations namespace.
1017 *
1018 * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore
1019 *
1020 * @type {Object}
1021 */
1022
1023 const store = (0,external_wp_data_namespaceObject.createReduxStore)(STORE_NAME, {
1024 reducer: reducer,
1025 selectors: selectors_namespaceObject,
1026 actions: actions_namespaceObject
1027 });
1028 (0,external_wp_data_namespaceObject.register)(store);
1029
1030 ;// CONCATENATED MODULE: ./packages/annotations/build-module/index.js
1031 /**
1032 * Internal dependencies
1033 */
1034
1035
1036
1037
1038 (window.wp = window.wp || {}).annotations = __webpack_exports__;
1039 /******/ })()
1040 ;