PluginProbe
Gutenberg / 23.5.2
Gutenberg v23.5.2
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 / scripts / annotations / index.js

index.js in Gutenberg 23.5.2, at build/scripts/annotations/index.js

472 lines 15.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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/stringify.js
358 var byteToHex = [];
359 for (let i = 0; i < 256; ++i) {
360 byteToHex.push((i + 256).toString(16).slice(1));
361 }
362 function unsafeStringify(arr, offset = 0) {
363 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();
364 }
365
366 // node_modules/uuid/dist/rng.js
367 var rnds8 = new Uint8Array(16);
368 function rng() {
369 return crypto.getRandomValues(rnds8);
370 }
371
372 // node_modules/uuid/dist/v4.js
373 function v4(options, buf, offset) {
374 if (!buf && !options && crypto.randomUUID) {
375 return crypto.randomUUID();
376 }
377 return _v4(options, buf, offset);
378 }
379 function _v4(options, buf, offset) {
380 options = options || {};
381 const rnds = options.random ?? options.rng?.() ?? rng();
382 if (rnds.length < 16) {
383 throw new Error("Random bytes length must be >= 16");
384 }
385 rnds[6] = rnds[6] & 15 | 64;
386 rnds[8] = rnds[8] & 63 | 128;
387 if (buf) {
388 offset = offset || 0;
389 if (offset < 0 || offset + 16 > buf.length) {
390 throw new RangeError(`UUID byte range ${offset}:${offset + 15} is out of buffer bounds`);
391 }
392 for (let i = 0; i < 16; ++i) {
393 buf[offset + i] = rnds[i];
394 }
395 return buf;
396 }
397 return unsafeStringify(rnds);
398 }
399 var v4_default = v4;
400
401 // packages/annotations/build-module/store/actions.mjs
402 function __experimentalAddAnnotation({
403 blockClientId,
404 richTextIdentifier = null,
405 range = null,
406 selector = "range",
407 source = "default",
408 id = v4_default()
409 }) {
410 const action = {
411 type: "ANNOTATION_ADD",
412 id,
413 blockClientId,
414 richTextIdentifier,
415 source,
416 selector
417 };
418 if (selector === "range" && range !== null) {
419 action.range = range;
420 }
421 return action;
422 }
423 function __experimentalRemoveAnnotation(annotationId) {
424 return {
425 type: "ANNOTATION_REMOVE",
426 annotationId
427 };
428 }
429 function __experimentalUpdateAnnotationRange(annotationId, start, end) {
430 return {
431 type: "ANNOTATION_UPDATE_RANGE",
432 annotationId,
433 start,
434 end
435 };
436 }
437 function __experimentalRemoveAnnotationsBySource(source) {
438 return {
439 type: "ANNOTATION_REMOVE_SOURCE",
440 source
441 };
442 }
443
444 // packages/annotations/build-module/store/index.mjs
445 var store = (0, import_data2.createReduxStore)(STORE_NAME, {
446 reducer: reducer_default,
447 selectors: selectors_exports,
448 actions: actions_exports
449 });
450 (0, import_data2.register)(store);
451
452 // packages/annotations/build-module/block/index.mjs
453 var addAnnotationClassName = (OriginalComponent) => {
454 return (0, import_data3.withSelect)((select, ownProps) => {
455 const { clientId, className } = ownProps;
456 const annotations2 = select(store).__experimentalGetAnnotationsForBlock(clientId);
457 return {
458 className: annotations2.map((annotation2) => {
459 return "is-annotated-by-" + annotation2.source;
460 }).concat(className || "").filter(Boolean).join(" ")
461 };
462 })(OriginalComponent);
463 };
464 (0, import_hooks.addFilter)(
465 "editor.BlockListBlock",
466 "core/annotations",
467 addAnnotationClassName
468 );
469 return __toCommonJS(index_exports);
470 })();
471 //# sourceMappingURL=index.js.map
472