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

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

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