[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/js/dist/ -> annotations.js (source)

   1  (function() {
   2  "use strict";
   3  var wp;
   4  (wp ||= {}).annotations = (() => {
   5    var __create = Object.create;
   6    var __defProp = Object.defineProperty;
   7    var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
   8    var __getOwnPropNames = Object.getOwnPropertyNames;
   9    var __getProtoOf = Object.getPrototypeOf;
  10    var __hasOwnProp = Object.prototype.hasOwnProperty;
  11    var __commonJS = (cb, mod) => function __require() {
  12      return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
  13    };
  14    var __export = (target, all) => {
  15      for (var name2 in all)
  16        __defProp(target, name2, { get: all[name2], enumerable: true });
  17    };
  18    var __copyProps = (to, from, except, desc) => {
  19      if (from && typeof from === "object" || typeof from === "function") {
  20        for (let key of __getOwnPropNames(from))
  21          if (!__hasOwnProp.call(to, key) && key !== except)
  22            __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  23      }
  24      return to;
  25    };
  26    var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  27      // If the importer is in node compatibility mode or this is not an ESM
  28      // file that has been converted to a CommonJS file using a Babel-
  29      // compatible transform (i.e. "__esModule" has not been set), then set
  30      // "default" to the CommonJS "module.exports" for node compatibility.
  31      isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  32      mod
  33    ));
  34    var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  35  
  36    // package-external:@wordpress/rich-text
  37    var require_rich_text = __commonJS({
  38      "package-external:@wordpress/rich-text"(exports, module) {
  39        module.exports = window.wp.richText;
  40      }
  41    });
  42  
  43    // package-external:@wordpress/i18n
  44    var require_i18n = __commonJS({
  45      "package-external:@wordpress/i18n"(exports, module) {
  46        module.exports = window.wp.i18n;
  47      }
  48    });
  49  
  50    // package-external:@wordpress/hooks
  51    var require_hooks = __commonJS({
  52      "package-external:@wordpress/hooks"(exports, module) {
  53        module.exports = window.wp.hooks;
  54      }
  55    });
  56  
  57    // package-external:@wordpress/data
  58    var require_data = __commonJS({
  59      "package-external:@wordpress/data"(exports, module) {
  60        module.exports = window.wp.data;
  61      }
  62    });
  63  
  64    // packages/annotations/build-module/index.mjs
  65    var index_exports = {};
  66    __export(index_exports, {
  67      store: () => store
  68    });
  69  
  70    // packages/annotations/build-module/format/index.mjs
  71    var import_rich_text2 = __toESM(require_rich_text(), 1);
  72  
  73    // packages/annotations/build-module/format/annotation.mjs
  74    var import_i18n = __toESM(require_i18n(), 1);
  75    var import_rich_text = __toESM(require_rich_text(), 1);
  76  
  77    // packages/annotations/build-module/store/constants.mjs
  78    var STORE_NAME = "core/annotations";
  79  
  80    // packages/annotations/build-module/format/annotation.mjs
  81    var FORMAT_NAME = "core/annotation";
  82    var ANNOTATION_ATTRIBUTE_PREFIX = "annotation-text-";
  83    function applyAnnotations(record, annotations2 = []) {
  84      annotations2.forEach((annotation2) => {
  85        let { start, end } = annotation2;
  86        if (typeof start !== "number" || typeof end !== "number") {
  87          return;
  88        }
  89        if (start > record.text.length) {
  90          start = record.text.length;
  91        }
  92        if (end > record.text.length) {
  93          end = record.text.length;
  94        }
  95        const className = ANNOTATION_ATTRIBUTE_PREFIX + annotation2.source;
  96        const id = ANNOTATION_ATTRIBUTE_PREFIX + annotation2.id;
  97        record = (0, import_rich_text.applyFormat)(
  98          record,
  99          {
 100            type: FORMAT_NAME,
 101            attributes: {
 102              className,
 103              id
 104            }
 105          },
 106          start,
 107          end
 108        );
 109      });
 110      return record;
 111    }
 112    function retrieveAnnotationPositions(formats) {
 113      const positions = {};
 114      formats.forEach((characterFormats, i) => {
 115        characterFormats = characterFormats || [];
 116        characterFormats = characterFormats.filter(
 117          (format) => format.type === FORMAT_NAME
 118        );
 119        characterFormats.forEach((format) => {
 120          let { id } = format.attributes;
 121          id = id.replace(ANNOTATION_ATTRIBUTE_PREFIX, "");
 122          if (!positions.hasOwnProperty(id)) {
 123            positions[id] = {
 124              start: i
 125            };
 126          }
 127          positions[id].end = i + 1;
 128        });
 129      });
 130      return positions;
 131    }
 132    function updateAnnotationsWithPositions(annotations2, positions, {
 133      removeAnnotation,
 134      updateAnnotationRange
 135    }) {
 136      annotations2.forEach((currentAnnotation) => {
 137        const position = positions[currentAnnotation.id];
 138        if (!position) {
 139          removeAnnotation(currentAnnotation.id);
 140          return;
 141        }
 142        const { start, end } = currentAnnotation;
 143        if (typeof start === "number" && typeof end === "number" && (start !== position.start || end !== (position.end ?? position.start))) {
 144          updateAnnotationRange(
 145            currentAnnotation.id,
 146            position.start,
 147            position.end ?? position.start
 148          );
 149        }
 150      });
 151    }
 152    var annotation = {
 153      name: FORMAT_NAME,
 154      title: (0, import_i18n.__)("Annotation"),
 155      tagName: "mark",
 156      className: "annotation-text",
 157      attributes: {
 158        className: "class",
 159        id: "id"
 160      },
 161      interactive: false,
 162      object: false,
 163      edit: () => {
 164        return null;
 165      },
 166      __experimentalGetPropsForEditableTreePreparation: (select, { richTextIdentifier, blockClientId }) => {
 167        return {
 168          annotations: select(
 169            STORE_NAME
 170          ).__experimentalGetAnnotationsForRichText(
 171            blockClientId,
 172            richTextIdentifier
 173          )
 174        };
 175      },
 176      __experimentalCreatePrepareEditableTree: ({ annotations: annotations2 }) => {
 177        return (formats, text) => {
 178          if (annotations2.length === 0) {
 179            return formats;
 180          }
 181          let record = {
 182            formats,
 183            text,
 184            replacements: [],
 185            start: 0,
 186            end: 0
 187          };
 188          record = applyAnnotations(record, annotations2);
 189          return record.formats;
 190        };
 191      },
 192      __experimentalGetPropsForEditableTreeChangeHandler: (dispatch) => {
 193        return {
 194          removeAnnotation: dispatch(STORE_NAME).__experimentalRemoveAnnotation,
 195          updateAnnotationRange: dispatch(STORE_NAME).__experimentalUpdateAnnotationRange
 196        };
 197      },
 198      __experimentalCreateOnChangeEditableValue: (props) => {
 199        return (formats) => {
 200          const positions = retrieveAnnotationPositions(formats);
 201          const { removeAnnotation, updateAnnotationRange, annotations: annotations2 } = props;
 202          updateAnnotationsWithPositions(annotations2, positions, {
 203            removeAnnotation,
 204            updateAnnotationRange
 205          });
 206        };
 207      }
 208    };
 209  
 210    // packages/annotations/build-module/format/index.mjs
 211    var { name, ...settings } = annotation;
 212    (0, import_rich_text2.registerFormatType)(name, settings);
 213  
 214    // packages/annotations/build-module/block/index.mjs
 215    var import_hooks = __toESM(require_hooks(), 1);
 216    var import_data3 = __toESM(require_data(), 1);
 217  
 218    // packages/annotations/build-module/store/index.mjs
 219    var import_data2 = __toESM(require_data(), 1);
 220  
 221    // packages/annotations/build-module/store/reducer.mjs
 222    function filterWithReference(collection, predicate) {
 223      const filteredCollection = collection.filter(predicate);
 224      return collection.length === filteredCollection.length ? collection : filteredCollection;
 225    }
 226    var mapValues = (obj, callback) => Object.entries(obj).reduce((acc, [key, value]) => {
 227      if (value === void 0) {
 228        return acc;
 229      }
 230      return {
 231        ...acc,
 232        [key]: callback(value)
 233      };
 234    }, {});
 235    function isValidAnnotationRange(annotation2) {
 236      return Boolean(
 237        annotation2.range && typeof annotation2.range.start === "number" && typeof annotation2.range.end === "number" && annotation2.range.start <= annotation2.range.end
 238      );
 239    }
 240    function annotations(state = {}, action) {
 241      switch (action.type) {
 242        case "ANNOTATION_ADD":
 243          const blockClientId = action.blockClientId;
 244          const newAnnotation = {
 245            id: action.id,
 246            blockClientId,
 247            richTextIdentifier: action.richTextIdentifier,
 248            source: action.source,
 249            selector: action.selector,
 250            range: action.range
 251          };
 252          if (newAnnotation.selector === "range" && !isValidAnnotationRange(newAnnotation)) {
 253            return state;
 254          }
 255          const previousAnnotationsForBlock = state?.[blockClientId] ?? [];
 256          return {
 257            ...state,
 258            [blockClientId]: [
 259              ...previousAnnotationsForBlock,
 260              newAnnotation
 261            ]
 262          };
 263        case "ANNOTATION_REMOVE":
 264          return mapValues(state, (annotationsForBlock) => {
 265            return filterWithReference(
 266              annotationsForBlock,
 267              (annotation2) => {
 268                return annotation2.id !== action.annotationId;
 269              }
 270            );
 271          });
 272        case "ANNOTATION_UPDATE_RANGE":
 273          return mapValues(state, (annotationsForBlock) => {
 274            let hasChangedRange = false;
 275            const newAnnotations = annotationsForBlock.map(
 276              (annotation2) => {
 277                if (annotation2.id === action.annotationId) {
 278                  hasChangedRange = true;
 279                  return {
 280                    ...annotation2,
 281                    range: {
 282                      start: action.start,
 283                      end: action.end
 284                    }
 285                  };
 286                }
 287                return annotation2;
 288              }
 289            );
 290            return hasChangedRange ? newAnnotations : annotationsForBlock;
 291          });
 292        case "ANNOTATION_REMOVE_SOURCE":
 293          return mapValues(state, (annotationsForBlock) => {
 294            return filterWithReference(
 295              annotationsForBlock,
 296              (annotation2) => {
 297                return annotation2.source !== action.source;
 298              }
 299            );
 300          });
 301      }
 302      return state;
 303    }
 304    var reducer_default = annotations;
 305  
 306    // packages/annotations/build-module/store/selectors.mjs
 307    var selectors_exports = {};
 308    __export(selectors_exports, {
 309      __experimentalGetAllAnnotationsForBlock: () => __experimentalGetAllAnnotationsForBlock,
 310      __experimentalGetAnnotations: () => __experimentalGetAnnotations,
 311      __experimentalGetAnnotationsForBlock: () => __experimentalGetAnnotationsForBlock,
 312      __experimentalGetAnnotationsForRichText: () => __experimentalGetAnnotationsForRichText
 313    });
 314    var import_data = __toESM(require_data(), 1);
 315    var EMPTY_ARRAY = [];
 316    var __experimentalGetAnnotationsForBlock = (0, import_data.createSelector)(
 317      (state, blockClientId) => {
 318        return (state?.[blockClientId] ?? []).filter((annotation2) => {
 319          return annotation2.selector === "block";
 320        });
 321      },
 322      (state, blockClientId) => [
 323        state?.[blockClientId] ?? EMPTY_ARRAY
 324      ]
 325    );
 326    function __experimentalGetAllAnnotationsForBlock(state, blockClientId) {
 327      return state?.[blockClientId] ?? EMPTY_ARRAY;
 328    }
 329    var __experimentalGetAnnotationsForRichText = (0, import_data.createSelector)(
 330      (state, blockClientId, richTextIdentifier) => {
 331        return (state?.[blockClientId] ?? []).filter((annotation2) => {
 332          return annotation2.selector === "range" && richTextIdentifier === annotation2.richTextIdentifier;
 333        }).map((annotation2) => {
 334          const { range, ...other } = annotation2;
 335          return {
 336            ...range,
 337            ...other
 338          };
 339        });
 340      },
 341      (state, blockClientId) => [
 342        state?.[blockClientId] ?? EMPTY_ARRAY
 343      ]
 344    );
 345    function __experimentalGetAnnotations(state) {
 346      return Object.values(state).filter((arr) => Boolean(arr)).flat();
 347    }
 348  
 349    // packages/annotations/build-module/store/actions.mjs
 350    var actions_exports = {};
 351    __export(actions_exports, {
 352      __experimentalAddAnnotation: () => __experimentalAddAnnotation,
 353      __experimentalRemoveAnnotation: () => __experimentalRemoveAnnotation,
 354      __experimentalRemoveAnnotationsBySource: () => __experimentalRemoveAnnotationsBySource,
 355      __experimentalUpdateAnnotationRange: () => __experimentalUpdateAnnotationRange
 356    });
 357  
 358    // node_modules/uuid/dist/stringify.js
 359    var byteToHex = [];
 360    for (let i = 0; i < 256; ++i) {
 361      byteToHex.push((i + 256).toString(16).slice(1));
 362    }
 363    function unsafeStringify(arr, offset = 0) {
 364      return (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();
 365    }
 366  
 367    // node_modules/uuid/dist/rng.js
 368    var rnds8 = new Uint8Array(16);
 369    function rng() {
 370      return crypto.getRandomValues(rnds8);
 371    }
 372  
 373    // node_modules/uuid/dist/v4.js
 374    function v4(options, buf, offset) {
 375      if (!buf && !options && crypto.randomUUID) {
 376        return crypto.randomUUID();
 377      }
 378      return _v4(options, buf, offset);
 379    }
 380    function _v4(options, buf, offset) {
 381      options = options || {};
 382      const rnds = options.random ?? options.rng?.() ?? rng();
 383      if (rnds.length < 16) {
 384        throw new Error("Random bytes length must be >= 16");
 385      }
 386      rnds[6] = rnds[6] & 15 | 64;
 387      rnds[8] = rnds[8] & 63 | 128;
 388      if (buf) {
 389        offset = offset || 0;
 390        if (offset < 0 || offset + 16 > buf.length) {
 391          throw new RangeError(`UUID byte range $offset}:$offset + 15} is out of buffer bounds`);
 392        }
 393        for (let i = 0; i < 16; ++i) {
 394          buf[offset + i] = rnds[i];
 395        }
 396        return buf;
 397      }
 398      return unsafeStringify(rnds);
 399    }
 400    var v4_default = v4;
 401  
 402    // packages/annotations/build-module/store/actions.mjs
 403    function __experimentalAddAnnotation({
 404      blockClientId,
 405      richTextIdentifier = null,
 406      range = null,
 407      selector = "range",
 408      source = "default",
 409      id = v4_default()
 410    }) {
 411      const action = {
 412        type: "ANNOTATION_ADD",
 413        id,
 414        blockClientId,
 415        richTextIdentifier,
 416        source,
 417        selector
 418      };
 419      if (selector === "range" && range !== null) {
 420        action.range = range;
 421      }
 422      return action;
 423    }
 424    function __experimentalRemoveAnnotation(annotationId) {
 425      return {
 426        type: "ANNOTATION_REMOVE",
 427        annotationId
 428      };
 429    }
 430    function __experimentalUpdateAnnotationRange(annotationId, start, end) {
 431      return {
 432        type: "ANNOTATION_UPDATE_RANGE",
 433        annotationId,
 434        start,
 435        end
 436      };
 437    }
 438    function __experimentalRemoveAnnotationsBySource(source) {
 439      return {
 440        type: "ANNOTATION_REMOVE_SOURCE",
 441        source
 442      };
 443    }
 444  
 445    // packages/annotations/build-module/store/index.mjs
 446    var store = (0, import_data2.createReduxStore)(STORE_NAME, {
 447      reducer: reducer_default,
 448      selectors: selectors_exports,
 449      actions: actions_exports
 450    });
 451    (0, import_data2.register)(store);
 452  
 453    // packages/annotations/build-module/block/index.mjs
 454    var addAnnotationClassName = (OriginalComponent) => {
 455      return (0, import_data3.withSelect)((select, ownProps) => {
 456        const { clientId, className } = ownProps;
 457        const annotations2 = select(store).__experimentalGetAnnotationsForBlock(clientId);
 458        return {
 459          className: annotations2.map((annotation2) => {
 460            return "is-annotated-by-" + annotation2.source;
 461          }).concat(className || "").filter(Boolean).join(" ")
 462        };
 463      })(OriginalComponent);
 464    };
 465    (0, import_hooks.addFilter)(
 466      "editor.BlockListBlock",
 467      "core/annotations",
 468      addAnnotationClassName
 469    );
 470    return __toCommonJS(index_exports);
 471  })();
 472  (window.wp ||= {}).annotations = wp.annotations;
 473  })();


Generated : Mon Aug 24 08:20:24 2026 Cross-referenced by PHPXref