@wordpress/annotations 3.43.0 → 3.44.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/build/block/index.cjs +8 -7
  3. package/build/block/index.cjs.map +3 -3
  4. package/build/format/annotation.cjs +25 -10
  5. package/build/format/annotation.cjs.map +3 -3
  6. package/build/format/index.cjs +3 -1
  7. package/build/format/index.cjs.map +3 -3
  8. package/build/index.cjs +2 -1
  9. package/build/index.cjs.map +2 -2
  10. package/build/store/actions.cjs +3 -2
  11. package/build/store/actions.cjs.map +3 -3
  12. package/build/store/constants.cjs +2 -1
  13. package/build/store/constants.cjs.map +3 -3
  14. package/build/store/index.cjs +2 -1
  15. package/build/store/index.cjs.map +3 -3
  16. package/build/store/reducer.cjs +13 -8
  17. package/build/store/reducer.cjs.map +3 -3
  18. package/build/store/selectors.cjs +9 -4
  19. package/build/store/selectors.cjs.map +3 -3
  20. package/build/types.cjs +19 -0
  21. package/build/types.cjs.map +7 -0
  22. package/build-module/block/index.mjs +6 -7
  23. package/build-module/block/index.mjs.map +3 -3
  24. package/build-module/format/annotation.mjs +24 -10
  25. package/build-module/format/annotation.mjs.map +3 -3
  26. package/build-module/format/index.mjs +1 -1
  27. package/build-module/format/index.mjs.map +3 -3
  28. package/build-module/index.mjs +1 -1
  29. package/build-module/index.mjs.map +1 -1
  30. package/build-module/store/actions.mjs +2 -2
  31. package/build-module/store/actions.mjs.map +3 -3
  32. package/build-module/store/constants.mjs +1 -1
  33. package/build-module/store/constants.mjs.map +3 -3
  34. package/build-module/store/index.mjs +1 -1
  35. package/build-module/store/index.mjs.map +3 -3
  36. package/build-module/store/reducer.mjs +12 -8
  37. package/build-module/store/reducer.mjs.map +3 -3
  38. package/build-module/store/selectors.mjs +8 -4
  39. package/build-module/store/selectors.mjs.map +3 -3
  40. package/build-module/types.mjs +1 -0
  41. package/build-module/types.mjs.map +7 -0
  42. package/build-types/block/index.d.ts +2 -0
  43. package/build-types/block/index.d.ts.map +1 -0
  44. package/build-types/format/annotation.d.ts +19 -0
  45. package/build-types/format/annotation.d.ts.map +1 -0
  46. package/build-types/format/index.d.ts +2 -0
  47. package/build-types/format/index.d.ts.map +1 -0
  48. package/build-types/index.d.ts +7 -0
  49. package/build-types/index.d.ts.map +1 -0
  50. package/build-types/store/actions.d.ts +48 -0
  51. package/build-types/store/actions.d.ts.map +1 -0
  52. package/build-types/store/constants.d.ts +5 -0
  53. package/build-types/store/constants.d.ts.map +1 -0
  54. package/build-types/store/index.d.ts +9 -0
  55. package/build-types/store/index.d.ts.map +1 -0
  56. package/build-types/store/reducer.d.ts +14 -0
  57. package/build-types/store/reducer.d.ts.map +1 -0
  58. package/build-types/store/selectors.d.ts +34 -0
  59. package/build-types/store/selectors.d.ts.map +1 -0
  60. package/build-types/types.d.ts +113 -0
  61. package/build-types/types.d.ts.map +1 -0
  62. package/package.json +8 -6
  63. package/src/block/index.ts +52 -0
  64. package/src/format/{annotation.js → annotation.ts} +69 -34
  65. package/src/format/{index.js → index.ts} +1 -1
  66. package/src/store/actions.ts +105 -0
  67. package/src/store/{constants.js → constants.ts} +0 -2
  68. package/src/store/{index.js → index.ts} +0 -2
  69. package/src/store/{reducer.js → reducer.ts} +58 -34
  70. package/src/store/{selectors.js → selectors.ts} +36 -22
  71. package/src/store/test/{reducer.js → reducer.ts} +17 -5
  72. package/src/types.ts +133 -0
  73. package/src/block/index.js +0 -40
  74. package/src/store/actions.js +0 -105
  75. /package/src/{index.js → index.ts} +0 -0
@@ -0,0 +1,113 @@
1
+ /**
2
+ * Represents a range in text content.
3
+ */
4
+ export interface AnnotationRange {
5
+ /** The offset where the annotation should start. */
6
+ start: number;
7
+ /** The offset where the annotation should end. */
8
+ end: number;
9
+ }
10
+ /**
11
+ * Represents an annotation selector type.
12
+ */
13
+ export type AnnotationSelector = 'range' | 'block';
14
+ /**
15
+ * Base annotation interface.
16
+ */
17
+ export interface Annotation {
18
+ /** Unique identifier for the annotation. */
19
+ id: string;
20
+ /** The block client ID this annotation applies to. */
21
+ blockClientId: string;
22
+ /** The source that created this annotation. */
23
+ source: string;
24
+ /** The type of selector used for this annotation. */
25
+ selector: AnnotationSelector;
26
+ /** Rich text identifier for range annotations. */
27
+ richTextIdentifier?: string | null;
28
+ /** Range for range-based annotations. */
29
+ range?: AnnotationRange | null;
30
+ /** Start position for annotations returned from selectors. */
31
+ start?: number;
32
+ /** End position for annotations returned from selectors. */
33
+ end?: number;
34
+ }
35
+ /**
36
+ * Parameters for adding an annotation.
37
+ */
38
+ export interface AddAnnotationParameters {
39
+ /** The blockClientId to add the annotation to. */
40
+ blockClientId: string;
41
+ /** Identifier for the RichText instance the annotation applies to. */
42
+ richTextIdentifier?: string | null;
43
+ /** The range at which to apply this annotation. */
44
+ range?: AnnotationRange | null;
45
+ /** The way to apply this annotation. */
46
+ selector?: AnnotationSelector;
47
+ /** The source that added the annotation. */
48
+ source?: string;
49
+ /** The ID the annotation should have. Generates a UUID by default. */
50
+ id?: string;
51
+ }
52
+ /**
53
+ * Store state interface.
54
+ */
55
+ export type AnnotationsState = Partial<Record<string, Annotation[]>>;
56
+ /**
57
+ * Action types.
58
+ */
59
+ export type AnnotationAction = {
60
+ type: 'ANNOTATION_ADD';
61
+ id: string;
62
+ blockClientId: string;
63
+ richTextIdentifier: string | null;
64
+ source: string;
65
+ selector: AnnotationSelector;
66
+ range?: AnnotationRange;
67
+ } | {
68
+ type: 'ANNOTATION_REMOVE';
69
+ annotationId: string;
70
+ } | {
71
+ type: 'ANNOTATION_UPDATE_RANGE';
72
+ annotationId: string;
73
+ start: number;
74
+ end: number;
75
+ } | {
76
+ type: 'ANNOTATION_REMOVE_SOURCE';
77
+ source: string;
78
+ };
79
+ /**
80
+ * Format registration interface for annotations.
81
+ */
82
+ export interface AnnotationFormat {
83
+ name: string;
84
+ title: string;
85
+ tagName: string;
86
+ className: string;
87
+ attributes: {
88
+ className: string;
89
+ id: string;
90
+ };
91
+ interactive: boolean;
92
+ object: boolean;
93
+ edit: () => null;
94
+ __experimentalGetPropsForEditableTreePreparation: (select: any, props: {
95
+ richTextIdentifier: string;
96
+ blockClientId: string;
97
+ }) => {
98
+ annotations: Annotation[];
99
+ };
100
+ __experimentalCreatePrepareEditableTree: (props: {
101
+ annotations: Annotation[];
102
+ }) => (formats: any[], text: string) => any[];
103
+ __experimentalGetPropsForEditableTreeChangeHandler: (dispatch: any) => {
104
+ removeAnnotation: (annotationId: string) => void;
105
+ updateAnnotationRange: (annotationId: string, start: number, end: number) => void;
106
+ };
107
+ __experimentalCreateOnChangeEditableValue: (props: {
108
+ removeAnnotation: (annotationId: string) => void;
109
+ updateAnnotationRange: (annotationId: string, start: number, end: number) => void;
110
+ annotations: Annotation[];
111
+ }) => (formats: any[]) => void;
112
+ }
113
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/annotations",
3
- "version": "3.43.0",
3
+ "version": "3.44.0",
4
4
  "description": "Annotate content in the Gutenberg editor.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -33,6 +33,7 @@
33
33
  "module": "build-module/index.mjs",
34
34
  "exports": {
35
35
  ".": {
36
+ "types": "./build-types/index.d.ts",
36
37
  "import": "./build-module/index.mjs",
37
38
  "require": "./build/index.cjs"
38
39
  },
@@ -40,11 +41,12 @@
40
41
  },
41
42
  "react-native": "src/index",
42
43
  "wpScript": true,
44
+ "types": "build-types/index.d.ts",
43
45
  "dependencies": {
44
- "@wordpress/data": "^10.43.0",
45
- "@wordpress/hooks": "^4.43.0",
46
- "@wordpress/i18n": "^6.16.0",
47
- "@wordpress/rich-text": "^7.43.0",
46
+ "@wordpress/data": "^10.44.0",
47
+ "@wordpress/hooks": "^4.44.0",
48
+ "@wordpress/i18n": "^6.17.0",
49
+ "@wordpress/rich-text": "^7.44.0",
48
50
  "uuid": "^9.0.1"
49
51
  },
50
52
  "peerDependencies": {
@@ -53,5 +55,5 @@
53
55
  "publishConfig": {
54
56
  "access": "public"
55
57
  },
56
- "gitHead": "2cea90674d11aa521ec3f71652fb3a6a4c383969"
58
+ "gitHead": "b862d8c84121a47bbeff882f6c87e61681ce2e0d"
57
59
  }
@@ -0,0 +1,52 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import type { ComponentType } from 'react';
5
+
6
+ /**
7
+ * WordPress dependencies
8
+ */
9
+ import { addFilter } from '@wordpress/hooks';
10
+ import { withSelect } from '@wordpress/data';
11
+
12
+ /**
13
+ * Internal dependencies
14
+ */
15
+ import { store } from '../store';
16
+ import type { Annotation } from '../types';
17
+
18
+ interface BlockListBlockProps {
19
+ clientId: string;
20
+ className?: string;
21
+ [ key: string ]: unknown;
22
+ }
23
+
24
+ /**
25
+ * Adds annotation className to the block-list-block component.
26
+ *
27
+ * @param OriginalComponent The original BlockListBlock component.
28
+ * @return The enhanced component.
29
+ */
30
+ const addAnnotationClassName = ( OriginalComponent: ComponentType< any > ) => {
31
+ return withSelect( ( select, ownProps ) => {
32
+ const { clientId, className } = ownProps as BlockListBlockProps;
33
+ const annotations: Annotation[] =
34
+ select( store ).__experimentalGetAnnotationsForBlock( clientId );
35
+
36
+ return {
37
+ className: annotations
38
+ .map( ( annotation ) => {
39
+ return 'is-annotated-by-' + annotation.source;
40
+ } )
41
+ .concat( className || '' )
42
+ .filter( Boolean )
43
+ .join( ' ' ),
44
+ };
45
+ } )( OriginalComponent );
46
+ };
47
+
48
+ addFilter(
49
+ 'editor.BlockListBlock',
50
+ 'core/annotations',
51
+ addAnnotationClassName
52
+ );
@@ -3,26 +3,36 @@
3
3
  */
4
4
  import { __ } from '@wordpress/i18n';
5
5
  import { applyFormat, removeFormat } from '@wordpress/rich-text';
6
+ import type { RichTextValue } from '@wordpress/rich-text';
6
7
 
7
- const FORMAT_NAME = 'core/annotation';
8
-
9
- const ANNOTATION_ATTRIBUTE_PREFIX = 'annotation-text-';
10
8
  /**
11
9
  * Internal dependencies
12
10
  */
13
11
  import { STORE_NAME } from '../store/constants';
12
+ import type { Annotation, AnnotationFormat } from '../types';
13
+
14
+ const FORMAT_NAME = 'core/annotation';
15
+
16
+ const ANNOTATION_ATTRIBUTE_PREFIX = 'annotation-text-';
14
17
 
15
18
  /**
16
19
  * Applies given annotations to the given record.
17
20
  *
18
- * @param {Object} record The record to apply annotations to.
19
- * @param {Array} annotations The annotation to apply.
20
- * @return {Object} A record with the annotations applied.
21
+ * @param record The record to apply annotations to.
22
+ * @param annotations The annotation to apply.
23
+ * @return A record with the annotations applied.
21
24
  */
22
- export function applyAnnotations( record, annotations = [] ) {
25
+ export function applyAnnotations(
26
+ record: RichTextValue,
27
+ annotations: Annotation[] = []
28
+ ): RichTextValue {
23
29
  annotations.forEach( ( annotation ) => {
24
30
  let { start, end } = annotation;
25
31
 
32
+ if ( typeof start !== 'number' || typeof end !== 'number' ) {
33
+ return;
34
+ }
35
+
26
36
  if ( start > record.text.length ) {
27
37
  start = record.text.length;
28
38
  }
@@ -42,7 +52,7 @@ export function applyAnnotations( record, annotations = [] ) {
42
52
  className,
43
53
  id,
44
54
  },
45
- },
55
+ } as any,
46
56
  start,
47
57
  end
48
58
  );
@@ -54,21 +64,23 @@ export function applyAnnotations( record, annotations = [] ) {
54
64
  /**
55
65
  * Removes annotations from the given record.
56
66
  *
57
- * @param {Object} record Record to remove annotations from.
58
- * @return {Object} The cleaned record.
67
+ * @param record Record to remove annotations from.
68
+ * @return The cleaned record.
59
69
  */
60
- export function removeAnnotations( record ) {
70
+ export function removeAnnotations( record: RichTextValue ): RichTextValue {
61
71
  return removeFormat( record, 'core/annotation', 0, record.text.length );
62
72
  }
63
73
 
64
74
  /**
65
75
  * Retrieves the positions of annotations inside an array of formats.
66
76
  *
67
- * @param {Array} formats Formats with annotations in there.
68
- * @return {Object} ID keyed positions of annotations.
77
+ * @param formats Formats with annotations in there.
78
+ * @return ID keyed positions of annotations.
69
79
  */
70
- function retrieveAnnotationPositions( formats ) {
71
- const positions = {};
80
+ function retrieveAnnotationPositions(
81
+ formats: any[][]
82
+ ): Record< string, { start: number; end?: number } > {
83
+ const positions: Record< string, { start: number; end?: number } > = {};
72
84
 
73
85
  formats.forEach( ( characterFormats, i ) => {
74
86
  characterFormats = characterFormats || [];
@@ -98,17 +110,27 @@ function retrieveAnnotationPositions( formats ) {
98
110
  /**
99
111
  * Updates annotations in the state based on positions retrieved from RichText.
100
112
  *
101
- * @param {Array} annotations The annotations that are currently applied.
102
- * @param {Array} positions The current positions of the given annotations.
103
- * @param {Object} actions
104
- * @param {Function} actions.removeAnnotation Function to remove an annotation from the state.
105
- * @param {Function} actions.updateAnnotationRange Function to update an annotation range in the state.
113
+ * @param annotations The annotations that are currently applied.
114
+ * @param positions The current positions of the given annotations.
115
+ * @param actions
116
+ * @param actions.removeAnnotation Function to remove an annotation from the state.
117
+ * @param actions.updateAnnotationRange Function to update an annotation range in the state.
106
118
  */
107
119
  function updateAnnotationsWithPositions(
108
- annotations,
109
- positions,
110
- { removeAnnotation, updateAnnotationRange }
111
- ) {
120
+ annotations: Annotation[],
121
+ positions: Record< string, { start: number; end?: number } >,
122
+ {
123
+ removeAnnotation,
124
+ updateAnnotationRange,
125
+ }: {
126
+ removeAnnotation: ( annotationId: string ) => void;
127
+ updateAnnotationRange: (
128
+ annotationId: string,
129
+ start: number,
130
+ end: number
131
+ ) => void;
132
+ }
133
+ ): void {
112
134
  annotations.forEach( ( currentAnnotation ) => {
113
135
  const position = positions[ currentAnnotation.id ];
114
136
  // If we cannot find an annotation, delete it.
@@ -120,17 +142,22 @@ function updateAnnotationsWithPositions(
120
142
  }
121
143
 
122
144
  const { start, end } = currentAnnotation;
123
- if ( start !== position.start || end !== position.end ) {
145
+ if (
146
+ typeof start === 'number' &&
147
+ typeof end === 'number' &&
148
+ ( start !== position.start ||
149
+ end !== ( position.end ?? position.start ) )
150
+ ) {
124
151
  updateAnnotationRange(
125
152
  currentAnnotation.id,
126
153
  position.start,
127
- position.end
154
+ position.end ?? position.start
128
155
  );
129
156
  }
130
157
  } );
131
158
  }
132
159
 
133
- export const annotation = {
160
+ export const annotation: AnnotationFormat = {
134
161
  name: FORMAT_NAME,
135
162
  title: __( 'Annotation' ),
136
163
  tagName: 'mark',
@@ -139,13 +166,15 @@ export const annotation = {
139
166
  className: 'class',
140
167
  id: 'id',
141
168
  },
142
- edit() {
169
+ interactive: false,
170
+ object: false,
171
+ edit: () => {
143
172
  return null;
144
173
  },
145
- __experimentalGetPropsForEditableTreePreparation(
174
+ __experimentalGetPropsForEditableTreePreparation: (
146
175
  select,
147
176
  { richTextIdentifier, blockClientId }
148
- ) {
177
+ ) => {
149
178
  return {
150
179
  annotations: select(
151
180
  STORE_NAME
@@ -155,18 +184,24 @@ export const annotation = {
155
184
  ),
156
185
  };
157
186
  },
158
- __experimentalCreatePrepareEditableTree( { annotations } ) {
187
+ __experimentalCreatePrepareEditableTree: ( { annotations } ) => {
159
188
  return ( formats, text ) => {
160
189
  if ( annotations.length === 0 ) {
161
190
  return formats;
162
191
  }
163
192
 
164
- let record = { formats, text };
193
+ let record: RichTextValue = {
194
+ formats,
195
+ text,
196
+ replacements: [],
197
+ start: 0,
198
+ end: 0,
199
+ };
165
200
  record = applyAnnotations( record, annotations );
166
201
  return record.formats;
167
202
  };
168
203
  },
169
- __experimentalGetPropsForEditableTreeChangeHandler( dispatch ) {
204
+ __experimentalGetPropsForEditableTreeChangeHandler: ( dispatch ) => {
170
205
  return {
171
206
  removeAnnotation:
172
207
  dispatch( STORE_NAME ).__experimentalRemoveAnnotation,
@@ -174,7 +209,7 @@ export const annotation = {
174
209
  dispatch( STORE_NAME ).__experimentalUpdateAnnotationRange,
175
210
  };
176
211
  },
177
- __experimentalCreateOnChangeEditableValue( props ) {
212
+ __experimentalCreateOnChangeEditableValue: ( props ) => {
178
213
  return ( formats ) => {
179
214
  const positions = retrieveAnnotationPositions( formats );
180
215
  const { removeAnnotation, updateAnnotationRange, annotations } =
@@ -10,4 +10,4 @@ import { annotation } from './annotation';
10
10
 
11
11
  const { name, ...settings } = annotation;
12
12
 
13
- registerFormatType( name, settings );
13
+ registerFormatType( name, settings as any );
@@ -0,0 +1,105 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import { v4 as uuid } from 'uuid';
5
+
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import type { AddAnnotationParameters, AnnotationAction } from '../types';
10
+
11
+ /**
12
+ * Adds an annotation to a block.
13
+ *
14
+ * The `block` attribute refers to a block ID that needs to be annotated.
15
+ * `isBlockAnnotation` controls whether or not the annotation is a block
16
+ * annotation. The `source` is the source of the annotation, this will be used
17
+ * to identify groups of annotations.
18
+ *
19
+ * The `range` property is only relevant if the selector is 'range'.
20
+ *
21
+ * @param annotation The annotation to add.
22
+ * @param annotation.blockClientId The blockClientId to add the annotation to.
23
+ * @param annotation.richTextIdentifier Identifier for the RichText instance the annotation applies to.
24
+ * @param annotation.range The range at which to apply this annotation.
25
+ * @param annotation.selector The way to apply this annotation.
26
+ * @param annotation.source The source that added the annotation.
27
+ * @param annotation.id The ID the annotation should have. Generates a UUID by default.
28
+ * @return Action object.
29
+ */
30
+ export function __experimentalAddAnnotation( {
31
+ blockClientId,
32
+ richTextIdentifier = null,
33
+ range = null,
34
+ selector = 'range',
35
+ source = 'default',
36
+ id = uuid(),
37
+ }: AddAnnotationParameters ): AnnotationAction {
38
+ const action: AnnotationAction = {
39
+ type: 'ANNOTATION_ADD',
40
+ id,
41
+ blockClientId,
42
+ richTextIdentifier,
43
+ source,
44
+ selector,
45
+ };
46
+
47
+ if ( selector === 'range' && range !== null ) {
48
+ (
49
+ action as Extract< AnnotationAction, { type: 'ANNOTATION_ADD' } >
50
+ ).range = range;
51
+ }
52
+
53
+ return action;
54
+ }
55
+
56
+ /**
57
+ * Removes an annotation with a specific ID.
58
+ *
59
+ * @param annotationId The annotation to remove.
60
+ * @return Action object.
61
+ */
62
+ export function __experimentalRemoveAnnotation(
63
+ annotationId: string
64
+ ): AnnotationAction {
65
+ return {
66
+ type: 'ANNOTATION_REMOVE',
67
+ annotationId,
68
+ };
69
+ }
70
+
71
+ /**
72
+ * Updates the range of an annotation.
73
+ *
74
+ * @param annotationId ID of the annotation to update.
75
+ * @param start The start of the new range.
76
+ * @param end The end of the new range.
77
+ * @return Action object.
78
+ */
79
+ export function __experimentalUpdateAnnotationRange(
80
+ annotationId: string,
81
+ start: number,
82
+ end: number
83
+ ): AnnotationAction {
84
+ return {
85
+ type: 'ANNOTATION_UPDATE_RANGE',
86
+ annotationId,
87
+ start,
88
+ end,
89
+ };
90
+ }
91
+
92
+ /**
93
+ * Removes all annotations of a specific source.
94
+ *
95
+ * @param source The source to remove.
96
+ * @return Action object.
97
+ */
98
+ export function __experimentalRemoveAnnotationsBySource(
99
+ source: string
100
+ ): AnnotationAction {
101
+ return {
102
+ type: 'ANNOTATION_REMOVE_SOURCE',
103
+ source,
104
+ };
105
+ }
@@ -1,6 +1,4 @@
1
1
  /**
2
2
  * The identifier for the data store.
3
- *
4
- * @type {string}
5
3
  */
6
4
  export const STORE_NAME = 'core/annotations';
@@ -19,8 +19,6 @@ import { STORE_NAME } from './constants';
19
19
  * Store definition for the annotations namespace.
20
20
  *
21
21
  * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore
22
- *
23
- * @type {Object}
24
22
  */
25
23
  export const store = createReduxStore( STORE_NAME, {
26
24
  reducer,