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