@atlaskit/editor-common 82.14.0 → 83.0.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 (180) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/dist/cjs/hooks/useSharedPluginState.js +1 -4
  3. package/dist/cjs/i18n/cs.js +2 -1
  4. package/dist/cjs/i18n/da.js +2 -1
  5. package/dist/cjs/i18n/de.js +2 -1
  6. package/dist/cjs/i18n/en.js +1 -0
  7. package/dist/cjs/i18n/en_GB.js +1 -0
  8. package/dist/cjs/i18n/en_ZZ.js +1 -0
  9. package/dist/cjs/i18n/es.js +2 -1
  10. package/dist/cjs/i18n/fi.js +2 -1
  11. package/dist/cjs/i18n/fr.js +2 -1
  12. package/dist/cjs/i18n/hu.js +2 -1
  13. package/dist/cjs/i18n/it.js +2 -1
  14. package/dist/cjs/i18n/ja.js +2 -1
  15. package/dist/cjs/i18n/ko.js +2 -1
  16. package/dist/cjs/i18n/nb.js +2 -1
  17. package/dist/cjs/i18n/nl.js +2 -1
  18. package/dist/cjs/i18n/pl.js +2 -1
  19. package/dist/cjs/i18n/pt_BR.js +2 -1
  20. package/dist/cjs/i18n/ru.js +2 -1
  21. package/dist/cjs/i18n/sv.js +2 -1
  22. package/dist/cjs/i18n/th.js +7 -3
  23. package/dist/cjs/i18n/tr.js +4 -3
  24. package/dist/cjs/i18n/uk.js +2 -1
  25. package/dist/cjs/i18n/vi.js +2 -1
  26. package/dist/cjs/i18n/zh.js +3 -2
  27. package/dist/cjs/i18n/zh_TW.js +2 -1
  28. package/dist/cjs/monitoring/error.js +1 -1
  29. package/dist/cjs/preset/builder.js +491 -0
  30. package/dist/cjs/styles/shared/annotation.js +20 -18
  31. package/dist/cjs/types/index.js +6 -1
  32. package/dist/cjs/ui/DropList/index.js +1 -1
  33. package/dist/cjs/ui/Mention/mention-with-profilecard.js +3 -1
  34. package/dist/es2019/hooks/useSharedPluginState.js +1 -6
  35. package/dist/es2019/i18n/cs.js +2 -1
  36. package/dist/es2019/i18n/da.js +2 -1
  37. package/dist/es2019/i18n/de.js +2 -1
  38. package/dist/es2019/i18n/en.js +1 -0
  39. package/dist/es2019/i18n/en_GB.js +1 -0
  40. package/dist/es2019/i18n/en_ZZ.js +1 -0
  41. package/dist/es2019/i18n/es.js +2 -1
  42. package/dist/es2019/i18n/fi.js +2 -1
  43. package/dist/es2019/i18n/fr.js +2 -1
  44. package/dist/es2019/i18n/hu.js +2 -1
  45. package/dist/es2019/i18n/it.js +2 -1
  46. package/dist/es2019/i18n/ja.js +2 -1
  47. package/dist/es2019/i18n/ko.js +2 -1
  48. package/dist/es2019/i18n/nb.js +2 -1
  49. package/dist/es2019/i18n/nl.js +2 -1
  50. package/dist/es2019/i18n/pl.js +2 -1
  51. package/dist/es2019/i18n/pt_BR.js +2 -1
  52. package/dist/es2019/i18n/ru.js +2 -1
  53. package/dist/es2019/i18n/sv.js +2 -1
  54. package/dist/es2019/i18n/th.js +7 -3
  55. package/dist/es2019/i18n/tr.js +4 -3
  56. package/dist/es2019/i18n/uk.js +2 -1
  57. package/dist/es2019/i18n/vi.js +2 -1
  58. package/dist/es2019/i18n/zh.js +3 -2
  59. package/dist/es2019/i18n/zh_TW.js +2 -1
  60. package/dist/es2019/monitoring/error.js +1 -1
  61. package/dist/es2019/preset/builder.js +528 -0
  62. package/dist/es2019/preset/core-plugin/index.js +1 -0
  63. package/dist/es2019/styles/shared/annotation.js +54 -29
  64. package/dist/es2019/types/index.js +9 -1
  65. package/dist/es2019/ui/DropList/index.js +1 -1
  66. package/dist/es2019/ui/Mention/mention-with-profilecard.js +2 -0
  67. package/dist/esm/hooks/useSharedPluginState.js +1 -4
  68. package/dist/esm/i18n/cs.js +2 -1
  69. package/dist/esm/i18n/da.js +2 -1
  70. package/dist/esm/i18n/de.js +2 -1
  71. package/dist/esm/i18n/en.js +1 -0
  72. package/dist/esm/i18n/en_GB.js +1 -0
  73. package/dist/esm/i18n/en_ZZ.js +1 -0
  74. package/dist/esm/i18n/es.js +2 -1
  75. package/dist/esm/i18n/fi.js +2 -1
  76. package/dist/esm/i18n/fr.js +2 -1
  77. package/dist/esm/i18n/hu.js +2 -1
  78. package/dist/esm/i18n/it.js +2 -1
  79. package/dist/esm/i18n/ja.js +2 -1
  80. package/dist/esm/i18n/ko.js +2 -1
  81. package/dist/esm/i18n/nb.js +2 -1
  82. package/dist/esm/i18n/nl.js +2 -1
  83. package/dist/esm/i18n/pl.js +2 -1
  84. package/dist/esm/i18n/pt_BR.js +2 -1
  85. package/dist/esm/i18n/ru.js +2 -1
  86. package/dist/esm/i18n/sv.js +2 -1
  87. package/dist/esm/i18n/th.js +7 -3
  88. package/dist/esm/i18n/tr.js +4 -3
  89. package/dist/esm/i18n/uk.js +2 -1
  90. package/dist/esm/i18n/vi.js +2 -1
  91. package/dist/esm/i18n/zh.js +3 -2
  92. package/dist/esm/i18n/zh_TW.js +2 -1
  93. package/dist/esm/monitoring/error.js +1 -1
  94. package/dist/esm/preset/builder.js +528 -0
  95. package/dist/esm/preset/core-plugin/index.js +1 -0
  96. package/dist/esm/styles/shared/annotation.js +20 -18
  97. package/dist/esm/types/index.js +9 -1
  98. package/dist/esm/ui/DropList/index.js +1 -1
  99. package/dist/esm/ui/Mention/mention-with-profilecard.js +3 -1
  100. package/dist/types/hooks/useSharedPluginState.d.ts +5 -5
  101. package/dist/types/i18n/cs.d.ts +1 -0
  102. package/dist/types/i18n/da.d.ts +1 -0
  103. package/dist/types/i18n/de.d.ts +1 -0
  104. package/dist/types/i18n/en.d.ts +1 -0
  105. package/dist/types/i18n/en_GB.d.ts +1 -0
  106. package/dist/types/i18n/en_ZZ.d.ts +1 -0
  107. package/dist/types/i18n/es.d.ts +1 -0
  108. package/dist/types/i18n/fi.d.ts +1 -0
  109. package/dist/types/i18n/fr.d.ts +1 -0
  110. package/dist/types/i18n/hu.d.ts +1 -0
  111. package/dist/types/i18n/it.d.ts +1 -0
  112. package/dist/types/i18n/ja.d.ts +1 -0
  113. package/dist/types/i18n/ko.d.ts +1 -0
  114. package/dist/types/i18n/nb.d.ts +1 -0
  115. package/dist/types/i18n/nl.d.ts +1 -0
  116. package/dist/types/i18n/pl.d.ts +1 -0
  117. package/dist/types/i18n/pt_BR.d.ts +1 -0
  118. package/dist/types/i18n/ru.d.ts +1 -0
  119. package/dist/types/i18n/sv.d.ts +1 -0
  120. package/dist/types/i18n/th.d.ts +4 -0
  121. package/dist/types/i18n/tr.d.ts +1 -0
  122. package/dist/types/i18n/uk.d.ts +1 -0
  123. package/dist/types/i18n/vi.d.ts +1 -0
  124. package/dist/types/i18n/zh.d.ts +1 -0
  125. package/dist/types/i18n/zh_TW.d.ts +1 -0
  126. package/dist/types/preset/builder.d.ts +550 -11
  127. package/dist/types/preset/core-plugin/index.d.ts +1 -1
  128. package/dist/types/preset/core-plugin/requestDocument.d.ts +1 -2
  129. package/dist/types/preset/index.d.ts +1 -0
  130. package/dist/types/preset/plugin-injection-api.d.ts +9 -3
  131. package/dist/types/styles/shared/annotation.d.ts +17 -0
  132. package/dist/types/types/index.d.ts +8 -7
  133. package/dist/types/types/next-editor-plugin.d.ts +641 -98
  134. package/dist/types/ui/Mention/mention-with-profilecard.d.ts +2 -1
  135. package/dist/types-ts4.5/hooks/useSharedPluginState.d.ts +5 -5
  136. package/dist/types-ts4.5/i18n/cs.d.ts +1 -0
  137. package/dist/types-ts4.5/i18n/da.d.ts +1 -0
  138. package/dist/types-ts4.5/i18n/de.d.ts +1 -0
  139. package/dist/types-ts4.5/i18n/en.d.ts +1 -0
  140. package/dist/types-ts4.5/i18n/en_GB.d.ts +1 -0
  141. package/dist/types-ts4.5/i18n/en_ZZ.d.ts +1 -0
  142. package/dist/types-ts4.5/i18n/es.d.ts +1 -0
  143. package/dist/types-ts4.5/i18n/fi.d.ts +1 -0
  144. package/dist/types-ts4.5/i18n/fr.d.ts +1 -0
  145. package/dist/types-ts4.5/i18n/hu.d.ts +1 -0
  146. package/dist/types-ts4.5/i18n/it.d.ts +1 -0
  147. package/dist/types-ts4.5/i18n/ja.d.ts +1 -0
  148. package/dist/types-ts4.5/i18n/ko.d.ts +1 -0
  149. package/dist/types-ts4.5/i18n/nb.d.ts +1 -0
  150. package/dist/types-ts4.5/i18n/nl.d.ts +1 -0
  151. package/dist/types-ts4.5/i18n/pl.d.ts +1 -0
  152. package/dist/types-ts4.5/i18n/pt_BR.d.ts +1 -0
  153. package/dist/types-ts4.5/i18n/ru.d.ts +1 -0
  154. package/dist/types-ts4.5/i18n/sv.d.ts +1 -0
  155. package/dist/types-ts4.5/i18n/th.d.ts +4 -0
  156. package/dist/types-ts4.5/i18n/tr.d.ts +1 -0
  157. package/dist/types-ts4.5/i18n/uk.d.ts +1 -0
  158. package/dist/types-ts4.5/i18n/vi.d.ts +1 -0
  159. package/dist/types-ts4.5/i18n/zh.d.ts +1 -0
  160. package/dist/types-ts4.5/i18n/zh_TW.d.ts +1 -0
  161. package/dist/types-ts4.5/preset/builder.d.ts +577 -13
  162. package/dist/types-ts4.5/preset/core-plugin/index.d.ts +1 -1
  163. package/dist/types-ts4.5/preset/core-plugin/requestDocument.d.ts +1 -2
  164. package/dist/types-ts4.5/preset/index.d.ts +1 -0
  165. package/dist/types-ts4.5/preset/plugin-injection-api.d.ts +9 -8
  166. package/dist/types-ts4.5/styles/shared/annotation.d.ts +17 -0
  167. package/dist/types-ts4.5/types/index.d.ts +8 -7
  168. package/dist/types-ts4.5/types/next-editor-plugin.d.ts +673 -127
  169. package/dist/types-ts4.5/ui/Mention/mention-with-profilecard.d.ts +2 -1
  170. package/package.json +2 -2
  171. package/dist/cjs/preset/core-plugin/types.js +0 -5
  172. package/dist/cjs/types/editor-public-api.js +0 -5
  173. package/dist/es2019/preset/core-plugin/types.js +0 -1
  174. package/dist/es2019/types/editor-public-api.js +0 -1
  175. package/dist/esm/preset/core-plugin/types.js +0 -1
  176. package/dist/esm/types/editor-public-api.js +0 -1
  177. package/dist/types/preset/core-plugin/types.d.ts +0 -59
  178. package/dist/types/types/editor-public-api.d.ts +0 -3
  179. package/dist/types-ts4.5/preset/core-plugin/types.d.ts +0 -59
  180. package/dist/types-ts4.5/types/editor-public-api.d.ts +0 -4
@@ -4,166 +4,712 @@
4
4
  * `Presets` - if the generics get too unwieldy, we may redesign how presets
5
5
  * are put together - but for now `Builder` & `Preset` aim to beinterchangeable.
6
6
  */
7
+ import type { JSONDocNode } from '@atlaskit/editor-json-transformer';
8
+ import type { Node, Schema } from '@atlaskit/editor-prosemirror/model';
7
9
  import type { EditorState } from '@atlaskit/editor-prosemirror/state';
8
- import type { CorePlugin } from '../preset/core-plugin/types';
10
+ import type { EditorView } from '@atlaskit/editor-prosemirror/view';
9
11
  import type { EditorCommand, EditorCommandWithMetadata } from './editor-command';
10
12
  import type { EditorPlugin } from './editor-plugin';
11
- type IsAny<T> = 0 extends 1 & T ? true : false;
12
- type PickSharedStatePropertyName<Metadata extends NextEditorPluginMetadata> = IsAny<Metadata> extends true ? never : ExtractSharedStateFromMetadata<Metadata> extends never ? never : 'getSharedState';
13
+ export interface Transformer<T> {
14
+ encode(node: Node): T;
15
+ parse(content: T): Node;
16
+ }
17
+ export type CorePlugin = NextEditorPlugin<'core', {
18
+ pluginConfiguration: {
19
+ getEditorView: () => EditorView | undefined;
20
+ };
21
+ actions: {
22
+ /**
23
+ * Dispatches an EditorCommand to ProseMirror
24
+ *
25
+ * @param command A function (EditorCommand | undefined) that takes an object containing a `Transaction` and returns a `Transaction` if it
26
+ * is successful or `null` if it shouldn't be dispatched.
27
+ * @returns (boolean) if the command was successful in dispatching
28
+ */
29
+ execute: (command: EditorCommand | undefined) => boolean;
30
+ /**
31
+ * Focuses the editor.
32
+ *
33
+ * Calls the focus method of the `EditorView` and scrolls the
34
+ * current selection into view.
35
+ *
36
+ * @returns (boolean) if the focus was successful
37
+ */
38
+ focus: () => boolean;
39
+ /**
40
+ * Blurs the editor.
41
+ *
42
+ * Calls blur on the editor DOM element.
43
+ *
44
+ * @returns (boolean) if the blur was successful
45
+ */
46
+ blur: () => boolean;
47
+ /**
48
+ * Request the editor document.
49
+ * The document will return when available. If called multiple times it will throttle and return the
50
+ * latest document when ready.
51
+ *
52
+ * A transformer can be created using `createTransformer`.
53
+ *
54
+ * @param onReceive Callback to handle the document. Document type based on the transformer.
55
+ * @param options Pass a transformer for the document to be transformed into a different format.
56
+ */
57
+ requestDocument<GenericTransformer extends Transformer<any> = Transformer<JSONDocNode>>(onReceive: (document: TransformerResult<GenericTransformer> | undefined) => void, options?: {
58
+ transformer?: GenericTransformer;
59
+ }): void;
60
+ /**
61
+ * Create a transformer
62
+ *
63
+ * @param schema Schema of the document
64
+ * @returns Transformer which can be used to request a document
65
+ */
66
+ createTransformer<Format>(cb: (schema: Schema) => Transformer<Format>): Transformer<Format> | undefined;
67
+ };
68
+ }>;
69
+ export type TransformerResult<GenericTransformer = Transformer<JSONDocNode>> = GenericTransformer extends Transformer<infer Content> ? Content : JSONDocNode;
70
+ export type InferTransformerResultCallback<T extends Transformer<any> | undefined> = (doc: T extends Transformer<infer U> ? TransformerResult<U> : TransformerResult<JSONDocNode>) => void;
71
+ export type DefaultTransformerResultCallback = (doc: TransformerResult<JSONDocNode> | undefined) => void;
72
+ /*********************
73
+ * *
74
+ * BASE TYPES *
75
+ * *
76
+ **********************/
77
+ type MaybeAction = ((...agrs: any) => any) | ((...agrs: any) => void);
78
+ type NextEditorPluginActions = Record<string, MaybeAction>;
79
+ type NextEditorEditorCommands = Record<string, EditorCommandWithMetadata | EditorCommand>;
80
+ type OptionalPrivateProperty = {
81
+ __optionalPluginType: true;
82
+ };
83
+ /**
84
+ * 🧱 Internal Type: Editor FE Platform
85
+ *
86
+ * Conditionally picks the property name 'getSharedState' if the Metadata contains a sharedState definition.
87
+ *
88
+ * This type checks if the generic Metadata type for a plugin has a shared state. If it does, it enables
89
+ * the inclusion of the 'getSharedState' method name in the plugin's API type. Otherwise, it results in `never`,
90
+ * indicating the absence of shared state functionality in the plugin.
91
+ *
92
+ * @returns 'getSharedState' if shared state is defined in Metadata, otherwise `never`.
93
+ */
13
94
  type WithSharedState<Metadata extends NextEditorPluginMetadata> = {
14
95
  [Property in keyof Pick<Metadata, 'sharedState'> as PickSharedStatePropertyName<Metadata>]: (editorState: EditorState | undefined) => ExtractSharedStateFromMetadata<Metadata>;
15
96
  };
16
- type PickActionsPropertyName<Metadata extends NextEditorPluginMetadata> = IsAny<Metadata> extends true ? never : ExtractActionsFromMetadata<Metadata> extends never ? never : 'actions';
17
- type PickCommandsPropertyName<Metadata extends NextEditorPluginMetadata> = IsAny<Metadata> extends true ? never : ExtractCommandsFromMetadata<Metadata> extends never ? never : 'commands';
97
+ /**
98
+ * 🧱 Internal Type: Editor FE Platform
99
+ *
100
+ * Conditionally picks the property name 'actions' if the Metadata contains action definitions.
101
+ *
102
+ * This type evaluates whether the given Metadata for a plugin defines any actions. If actions are present,
103
+ * the 'actions' property name is included in the plugin's API type, enabling action handling. If there are no
104
+ * actions, it results in `never`.
105
+ *
106
+ * @returns 'actions' if actions are defined in Metadata, otherwise `never`.
107
+ */
18
108
  type WithActions<Metadata extends NextEditorPluginMetadata> = {
19
109
  [Property in keyof Pick<Metadata, 'actions'> as PickActionsPropertyName<Metadata>]: ExtractActionsFromMetadata<Metadata>;
20
110
  };
111
+ /**
112
+ * 🧱 Internal Type: Editor FE Platform
113
+ *
114
+ * Conditionally picks the property name 'commands' if the Metadata contains command definitions.
115
+ *
116
+ * This type assesses whether the provided Metadata for a plugin includes commands. If commands exist,
117
+ * it allows for the inclusion of the 'commands' property name in the plugin's API type, facilitating command
118
+ * execution. If no commands are defined, the result is `never`.
119
+ *
120
+ * @returns 'commands' if commands are defined in Metadata, otherwise `never`.
121
+ */
21
122
  type WithCommands<Metadata extends NextEditorPluginMetadata> = {
22
123
  [Property in keyof Pick<Metadata, 'commands'> as PickCommandsPropertyName<Metadata>]: ExtractCommandsFromMetadata<Metadata>;
23
124
  };
125
+ /****************************************************
126
+ * *
127
+ * METADATA PROPERTIES EXTRACTION TYPES *
128
+ * *
129
+ ****************************************************/
130
+ /**
131
+ * 🧱 Internal Type: Editor FE Platform
132
+ *
133
+ * Conditionally picks the property name 'getSharedState' if the Metadata contains a sharedState definition.
134
+ *
135
+ * This type checks if the generic Metadata type for a plugin has a shared state. If it does, it enables
136
+ * the inclusion of the 'getSharedState' method name in the plugin's API type. Otherwise, it results in `never`,
137
+ * indicating the absence of shared state functionality in the plugin.
138
+ *
139
+ * @template Metadata The metadata type to check for shared state.
140
+ * @returns 'getSharedState' if shared state is defined in Metadata, otherwise `never`.
141
+ */
142
+ type PickSharedStatePropertyName<Metadata extends NextEditorPluginMetadata> = IsAny<Metadata> extends true ? never : ExtractSharedStateFromMetadata<Metadata> extends never ? never : 'getSharedState';
143
+ /**
144
+ * 🧱 Internal Type: Editor FE Platform
145
+ *
146
+ * Conditionally picks the property name 'actions' if the Metadata contains action definitions.
147
+ *
148
+ * This type evaluates whether the given Metadata for a plugin defines any actions. If actions are present,
149
+ * the 'actions' property name is included in the plugin's API type, enabling action handling. If there are no
150
+ * actions, it results in `never`.
151
+ *
152
+ * @returns 'actions' if actions are defined in Metadata, otherwise `never`.
153
+ */
154
+ type PickActionsPropertyName<Metadata extends NextEditorPluginMetadata> = IsAny<Metadata> extends true ? never : ExtractActionsFromMetadata<Metadata> extends never ? never : 'actions';
155
+ /**
156
+ * 🧱 Internal Type: Editor FE Platform
157
+ *
158
+ * Conditionally picks the property name 'commands' if the Metadata contains command definitions.
159
+ *
160
+ * This type assesses whether the provided Metadata for a plugin includes commands. If commands exist,
161
+ * it allows for the inclusion of the 'commands' property name in the plugin's API type, facilitating command
162
+ * execution. If no commands are defined, the result is `never`.
163
+ *
164
+ * @returns 'commands' if commands are defined in Metadata, otherwise `never`.
165
+ */
166
+ type PickCommandsPropertyName<Metadata extends NextEditorPluginMetadata> = IsAny<Metadata> extends true ? never : ExtractCommandsFromMetadata<Metadata> extends never ? never : 'commands';
167
+ /**
168
+ * 🧱 Internal Type: Editor FE Platform
169
+ *
170
+ * Extracts the dependencies from the plugin Metadata, if any are defined.
171
+ *
172
+ * This type checks the Metadata for a 'dependencies' key and ensures it is a list of DependencyPlugin types.
173
+ * If dependencies are present, they are returned, excluding any potential `undefined` values. If no dependencies
174
+ * are defined, an empty array is returned.
175
+ *
176
+ * @returns An array of DependencyPlugin types if defined, otherwise an empty array.
177
+ */
178
+ type ExtractPluginDependenciesFromMetadata<Metadata> = 'dependencies' extends keyof Metadata ? Metadata['dependencies'] extends DependencyPlugin[] ? Exclude<Metadata['dependencies'], undefined> : [
179
+ ] : [
180
+ ];
181
+ /**
182
+ * 🧱 Internal Type: Editor FE Platform
183
+ *
184
+ * Extracts the shared state definition from the plugin Metadata, if present.
185
+ *
186
+ * This type looks for a 'sharedState' key within the Metadata. If found, it returns the associated type of the
187
+ * shared state. If the 'sharedState' key is not present, the result is `never`.
188
+ *
189
+ * @returns The shared state type if defined, otherwise `never`.
190
+ */
191
+ type ExtractSharedStateFromMetadata<Metadata> = 'sharedState' extends keyof Metadata ? Metadata['sharedState'] : never;
192
+ /**
193
+ * 🧱 Internal Type: Editor FE Platform
194
+ *
195
+ * Extracts the actions definition from the plugin Metadata, if available.
196
+ *
197
+ * This type queries the Metadata for an 'actions' key. If such a key exists, it returns the type of the
198
+ * defined actions. If there are no actions defined in the Metadata, the result is `never`.
199
+ *
200
+ * @returns The actions type if defined, otherwise `never`.
201
+ */
202
+ type ExtractActionsFromMetadata<Metadata> = 'actions' extends keyof Metadata ? Metadata['actions'] : never;
203
+ /**
204
+ * 🧱 Internal Type: Editor FE Platform
205
+ *
206
+ * Extracts the commands definition from the plugin Metadata, if it exists.
207
+ *
208
+ * By checking for a 'commands' key within the Metadata, this type determines if any commands are defined.
209
+ * If commands are present, their type is returned. If not, the result is `never`.
210
+ *
211
+ * @returns The commands type if defined, otherwise `never`.
212
+ */
213
+ type ExtractCommandsFromMetadata<Metadata> = 'commands' extends keyof Metadata ? Metadata['commands'] : never;
214
+ /**
215
+ * 🧱 Internal Type: Editor FE Platform
216
+ *
217
+ * Extracts the plugin name from a NextEditorPlugin type.
218
+ *
219
+ * This type attempts to derive the plugin name from a given NextEditorPlugin type by checking if the plugin
220
+ * is a function returning `DefaultEditorPlugin` with a specific name. If the name can be inferred, it is returned;
221
+ * otherwise, the result is `never`.
222
+ *
223
+ * @returns The inferred plugin name if possible, otherwise `never`.
224
+ */
225
+ type ExtractPluginName<Plugin> = Plugin extends NextEditorPlugin<any, any> ? Plugin extends (...args: any) => DefaultEditorPlugin<infer PluginName, any> ? PluginName : never : never;
226
+ /********************************
227
+ * *
228
+ * TYPE INFER *
229
+ * *
230
+ *********************************/
231
+ /**
232
+ * 🧱 Internal Type: Editor FE Platform
233
+ *
234
+ * Utility type to check if a given type T is 'any'.
235
+ *
236
+ * This type exploits TypeScript's behavior to determine if a type is 'any'. It leverages the fact that
237
+ * 'any' is the only type assignable to both '1 & T' and '0'. If T is 'any', the type resolves to true;
238
+ * otherwise, it resolves to false.
239
+ *
240
+ * @returns `true` if T is 'any', otherwise `false`.
241
+ */
242
+ type IsAny<T> = 0 extends 1 & T ? true : false;
243
+ /******************************
244
+ * *
245
+ * EDITOR API MAPPED TUPLES *
246
+ * *
247
+ ******************************/
248
+ /**
249
+ * 🧱 Internal Type: Editor FE Platform
250
+ *
251
+ * Extracts and lists all required plugins from a tuple list of plugins, excluding any that are marked as optional.
252
+ *
253
+ * This recursive type iterates through a tuple of plugins, filtering out any plugins that are either undefined or
254
+ * explicitly marked as optional (using `OptionalPlugin`). The result is a new tuple containing only the required
255
+ * plugins. This is useful for determining the set of plugins that must be present for a certain feature or
256
+ * configuration to work.
257
+ *
258
+ * @returns A tuple of required plugins, with optional plugins removed.
259
+ *
260
+ * @example
261
+ * ```typescript
262
+ * type MyRequiredPlugins = ExtractRequiredPlugins<[OptionalPlugin<MyPlugin>, MyPlugin2]>;
263
+ *
264
+ * // Type: [MyPlugin2].
265
+ * ```
266
+ */
267
+ type ExtractRequiredPlugins<TuplePluginList extends [
268
+ ...any[]
269
+ ]> = TuplePluginList extends [
270
+ infer First,
271
+ ...infer Rest
272
+ ] ? undefined extends First ? ExtractRequiredPlugins<Rest> : First extends OptionalPlugin<NextEditorPlugin<any, any>> ? ExtractRequiredPlugins<Rest> : First extends NextEditorPlugin<any, any> ? [
273
+ First,
274
+ ...ExtractRequiredPlugins<[
275
+ ...Rest
276
+ ]>
277
+ ] : ExtractRequiredPlugins<Rest> : [
278
+ ];
279
+ /**
280
+ * 🧱 Internal Type: Editor FE Platform
281
+ *
282
+ * Extracts and lists all optional plugins from a tuple list of plugins, including any that are explicitly marked as
283
+ * optional or are undefined.
284
+ *
285
+ * This type recursively scans through a tuple of plugins, collecting plugins that are either marked as `undefined`
286
+ * or wrapped with `OptionalPlugin`. The result is a new tuple consisting solely of these optional plugins, effectively
287
+ * separating them from the required ones. This type is instrumental in configurations where optional dependencies
288
+ * need to be distinguished from required ones for conditional loading or initialization.
289
+ *
290
+ * @returns A tuple of optional plugins, excluding required ones.
291
+ *
292
+ * @example
293
+ * ```typescript
294
+ * type MyOptionalPlugins = ExtractOptionalPlugins<[OptionalPlugin<MyPlugin>, MyPlugin2]>;
295
+ *
296
+ * // Type: [MyPlugin].
297
+ * ```
298
+ */
299
+ type ExtractOptionalPlugins<TuplePluginList extends [
300
+ ...any[]
301
+ ]> = TuplePluginList extends [
302
+ infer First,
303
+ ...infer Rest
304
+ ] ? undefined extends First ? [
305
+ First,
306
+ ...ExtractOptionalPlugins<[
307
+ ...Rest
308
+ ]>
309
+ ] : First extends OptionalPlugin<infer Plugin> ? [
310
+ Plugin,
311
+ ...ExtractOptionalPlugins<[
312
+ ...Rest
313
+ ]>
314
+ ] : ExtractOptionalPlugins<[
315
+ ...Rest
316
+ ]> : [
317
+ ];
318
+ /**
319
+ * 🧱 Internal Type: Editor FE Platform
320
+ *
321
+ * Extracts the shared state type from a plugin or its metadata.
322
+ *
323
+ * This type conditionally checks whether the input is a NextEditorPlugin instance or directly NextEditorPluginMetadata.
324
+ * If the input is a plugin instance, it attempts to infer the Metadata type from the plugin's constructor function signature,
325
+ * specifically looking for a shared state definition. If the input is directly plugin metadata, it extracts the shared state
326
+ * definition directly from the metadata.
327
+ *
328
+ * @returns The type of the shared state if defined, otherwise `never`.
329
+ */
330
+ export type ExtractPluginSharedState<PluginOrMetadata> = PluginOrMetadata extends NextEditorPlugin<any, any> ? PluginOrMetadata extends (props: {
331
+ config: any;
332
+ api: any;
333
+ }) => DefaultEditorPlugin<any, infer Metadata> ? ExtractSharedStateFromMetadata<Metadata> : never : PluginOrMetadata extends NextEditorPluginMetadata ? ExtractSharedStateFromMetadata<PluginOrMetadata> : never;
334
+ /**
335
+ * 🧱 Internal Type: Editor FE Platform
336
+ *
337
+ * Extracts the actions definition from a plugin or its metadata.
338
+ *
339
+ * Similar to `ExtractPluginSharedState`, this type discerns whether it is dealing with a NextEditorPlugin instance
340
+ * or its metadata. It then attempts to extract or infer the actions defined within. For a plugin instance, it looks
341
+ * into the plugin's constructor function signature to infer the Metadata and subsequently extracts any actions defined.
342
+ * If dealing with plugin metadata directly, it extracts the actions from there.
343
+ *
344
+ * @returns The actions definition if available, otherwise `never`.
345
+ */
346
+ export type ExtractPluginActions<PluginOrMetadata> = PluginOrMetadata extends NextEditorPlugin<any, any> ? PluginOrMetadata extends (props: {
347
+ config: any;
348
+ api: any;
349
+ }) => DefaultEditorPlugin<any, infer Metadata> ? ExtractActionsFromMetadata<Metadata> : never : PluginOrMetadata extends NextEditorPluginMetadata ? ExtractActionsFromMetadata<PluginOrMetadata> : never;
350
+ /*****************************
351
+ * *
352
+ * MAPPED TYPES *
353
+ * *
354
+ ******************************/
355
+ /**
356
+ * 🧱 Internal Type: Editor FE Platform
357
+ *
358
+ * Constructs a unified API object from a list of plugins, combining the optional injection APIs of each.
359
+ *
360
+ * This type recursively processes a list of plugins, extracting the name and metadata from each to construct
361
+ * an `EditorOptionalInjectionAPI` for it. These APIs are then combined into a single object, allowing for
362
+ * a comprehensive API interface that includes all provided plugins.
363
+ *
364
+ * @returns An object representing the combined API of all plugins in the list.
365
+ *
366
+ * @example
367
+ * ```typescript
368
+ * type MyEditorAPI = PublicPluginAPIFromPlugins<[PluginA, PluginB]>;
369
+ * ```
370
+ */
371
+ type PublicPluginAPIFromPlugins<PluginList extends NextEditorPlugin<any, any>[]> = PluginList extends [
372
+ infer Head,
373
+ ...infer Tail
374
+ ] ? Head extends NextEditorPlugin<infer Name, infer Metadata> ? EditorOptionalInjectionAPI<Name, Metadata> & PublicPluginAPIFromPlugins<Tail extends NextEditorPlugin<any, any>[] ? Tail : [
375
+ ]> : {} : {};
376
+ /**
377
+ * 🧱 Internal Type: Editor FE Platform
378
+ *
379
+ * Generates an object type mapping each plugin's name to its dependencies API, ensuring all dependencies are required.
380
+ *
381
+ * This type iterates over a list of plugins, extracting each plugin's name and associating it with its respective
382
+ * dependencies API (`PluginDependenciesAPI`). The resulting object type requires all listed plugins to have their
383
+ * dependencies satisfied, making this useful for constructing contexts where all plugin dependencies are mandatory.
384
+ *
385
+ * @returns An object type with a required dependencies API for each plugin.
386
+ */
387
+ type RequiredPluginDependenciesAPI<PluginList extends NextEditorPlugin<any, any>[]> = {
388
+ [Plugin in PluginList[number] as `${ExtractPluginName<Plugin>}`]: PluginDependenciesAPI<Plugin>;
389
+ };
390
+ /**
391
+ * 🧱 Internal Type: Editor FE Platform
392
+ *
393
+ * Creates an object type mapping each plugin's name to its dependencies API, marking all dependencies as optional.
394
+ *
395
+ * Similar to `RequiredPluginDependenciesAPI`, but marks each plugin's dependencies API as optional. This is particularly
396
+ * useful in scenarios where plugins may have optional functionality that should not enforce the presence of certain
397
+ * dependencies.
398
+ *
399
+ * @returns An object type with an optional dependencies API for each plugin.
400
+ */
401
+ type OptionalPluginDependenciesAPI<PluginList extends NextEditorPlugin<any, any>[]> = {
402
+ [Plugin in PluginList[number] as `${ExtractPluginName<Plugin>}`]+?: PluginDependenciesAPI<Plugin> | undefined;
403
+ };
404
+ /**
405
+ * 🧱 Internal Type: Editor FE Platform
406
+ *
407
+ * Constructs a dependencies API object for a plugin itself, identified by its name, using its metadata.
408
+ *
409
+ * This type maps the plugin's name to its own `BasePluginDependenciesAPI` based on its metadata. It's useful for
410
+ * creating a self-reference within the plugin's API, allowing the plugin to access its own shared state, actions,
411
+ * and commands.
412
+ *
413
+ * @returns An object with the plugin's own dependencies API.
414
+ */
415
+ type SelfPluginDependenciesAPI<Name extends string, Metadata extends NextEditorPluginMetadata> = {
416
+ [Plugin in Name]: BasePluginDependenciesAPI<Metadata>;
417
+ };
418
+ /**
419
+ * 🧱 Internal Type: Editor FE Platform
420
+ *
421
+ * Similar to `SelfPluginDependenciesAPI`, but marks the plugin's own dependencies API as optional.
422
+ */
423
+ type SelfOptionalPluginDependenciesAPI<Name extends string, Metadata extends NextEditorPluginMetadata> = {
424
+ [Plugin in Name]?: BasePluginDependenciesAPI<Metadata> | undefined;
425
+ };
426
+ /*************************
427
+ * *
428
+ * PUBLIC TYPES *
429
+ * *
430
+ *************************/
431
+ /**
432
+ * 🧱 Internal Type: Editor FE Platform
433
+ *
434
+ * ⚠️⚠️⚠️ Any breaking change here will have a massive impact across all Editor Plugin packages ⚠️⚠️⚠️
435
+ *
436
+ * The runtime implementation for NextEditorPlugin.
437
+ *
438
+ * All types prefixed `With` are being used in the EditorInjectionAPI.
439
+ * Some infer operations required this intersection approach.
440
+ *
441
+ * @example
442
+ * For instance, when a `sharedState` is declared in NextEditorPluginMetadata
443
+ * then the plugin needs to implement a `getSharedState` function.
444
+ *
445
+ * @see WithSharedState
446
+ * @see WithActions
447
+ * @see WithCommands
448
+ */
24
449
  export type DefaultEditorPlugin<Name extends string, Metadata extends NextEditorPluginMetadata> = EditorPlugin & WithSharedState<Metadata> & WithActions<Metadata> & WithCommands<Metadata> & {
25
450
  name: Name;
26
451
  };
27
- type MaybeAction = ((...agrs: any) => any) | ((...agrs: any) => void);
28
- type NextEditorPluginActions = Record<string, MaybeAction>;
29
- type NextEditorEditorCommands = Record<string, EditorCommandWithMetadata | EditorCommand>;
452
+ /**
453
+ * 🧱 Internal Type: Editor FE Platform
454
+ *
455
+ * ⚠️⚠️⚠️ Any breaking change here will have a massive impact across all Editor Plugin packages ⚠️⚠️⚠️
456
+ *
457
+ * If you are not from Editor Platform FE, you shouldn't be using or changing this.
458
+ */
30
459
  export interface NextEditorPluginMetadata {
460
+ /**
461
+ * The real implementation will be infered by BasePluginDependenciesAPI and other
462
+ * internal types.
463
+ */
31
464
  readonly sharedState?: any;
465
+ /**
466
+ * The real implementation will be infered by Preset internal types and @see NextEditorPluginFunctionOptionalConfigDefinition
467
+ */
32
468
  readonly pluginConfiguration?: any;
469
+ /**
470
+ * This is used only on compile time. There is no runtime implementation for the dependencies
471
+ * @see DependencyPlugin
472
+ */
33
473
  readonly dependencies?: DependencyPlugin[];
474
+ /**
475
+ * @see NextEditorPluginActions
476
+ */
34
477
  readonly actions?: NextEditorPluginActions;
478
+ /**
479
+ *
480
+ * @see NextEditorEditorCommands
481
+ */
35
482
  readonly commands?: NextEditorEditorCommands;
36
483
  }
37
484
  /**
38
- * `PluginInjectionAPI` doesn't actually use `dependencies`,
39
- * we should use it as PluginInjectionAPI< ... >['dependencies']
40
- * it is only here because otherwise typescript blows up
41
- * trying to compare `PluginInjectionAPI` to `PublicPluginAPI`
485
+ * 🧱 Internal Type: Editor FE Platform
486
+ *
487
+ * ⚠️⚠️⚠️ Any breaking change here will have a massive impact across all Editor Plugin packages ⚠️⚠️⚠️
42
488
  */
43
- export type PluginInjectionAPI<Name extends string, Metadata extends NextEditorPluginMetadata> = {
44
- dependencies: PublicPluginAPI<[
45
- NextEditorPlugin<Name, Metadata>,
46
- ...ExtractPluginDependenciesFromMetadata<Metadata>
47
- ]>;
48
- };
49
- export type PluginInjectionAPIWithDependencies<Plugins extends NextEditorPlugin<any, any>[]> = PublicPluginAPI<Plugins>;
50
489
  export type NextEditorPluginFunctionOptionalConfigDefinition<Name extends string, Metadata extends NextEditorPluginMetadata, Configuration = undefined> = (props: {
51
490
  config: Configuration;
52
- api?: PluginInjectionAPI<Name, Metadata>['dependencies'];
491
+ api?: {
492
+ [Plugin in Name]: BasePluginDependenciesAPI<Metadata>;
493
+ } & RequiredPluginDependenciesAPI<ExtractRequiredPlugins<[
494
+ CorePlugin,
495
+ ...ExtractPluginDependenciesFromMetadata<Metadata>
496
+ ]>> & OptionalPluginDependenciesAPI<ExtractOptionalPlugins<ExtractPluginDependenciesFromMetadata<Metadata>>>;
53
497
  }) => DefaultEditorPlugin<Name, Metadata>;
54
- type OptionalPrivateProperty = {
55
- __optionalPluginType: true;
56
- };
498
+ /**
499
+ * 📢 Public Type API: Helps Editor Plugin developers to defined optional dependencies for their plugins.
500
+ *
501
+ * You shouldn't be using this in any other place outside the `NextEditorPlugin`.
502
+ *
503
+ * @example
504
+ * ```typescript
505
+ * const toolbarPlugin: NextEditorPlugin<'toolbar', {
506
+ * dependencies: [
507
+ * OptionalPlugin<TablePlugin>,
508
+ * ListsPlugin,
509
+ * ]
510
+ * }>
511
+ * ```
512
+ *
513
+ * Later on, this will be used by
514
+ *
515
+ * @see ExtractInjectionAPI - Optional dependencies will need can be undefined on runtime. So, a null-check is required
516
+ */
57
517
  export type OptionalPlugin<EditorPlugin extends NextEditorPlugin<any, any>> = EditorPlugin & OptionalPrivateProperty;
58
- type DependencyPlugin = OptionalPlugin<NextEditorPlugin<any, any>> | NextEditorPlugin<any, any>;
518
+ /**
519
+ * 🧱 Internal Type: Editor FE Platform
520
+ *
521
+ * @see NextEditorPluginMetadata
522
+ */
523
+ export type DependencyPlugin = OptionalPlugin<NextEditorPlugin<any, any>> | NextEditorPlugin<any, any>;
524
+ /**
525
+ * 📢 Public Type API
526
+ *
527
+ * The base type for the entire Editor Plugin Ecosystem.
528
+ *
529
+ * The first parameter is the only one required,
530
+ * it used to force the plugin to have a runtime string:
531
+ *
532
+ * ```typescript
533
+ * const plugin: NextEditorPlugin<'hello'> = () => ({
534
+ * // This string should match the name declared in the `NextEditorPlugin`
535
+ * name: 'hello',
536
+ * })
537
+ *
538
+ * // Later, this name is how other plugins will be able access your plugin API:
539
+ * // api?.hello?.actions
540
+ * ```
541
+ *
542
+ * @see NextEditorPluginMetadata for the second parameter
543
+ */
59
544
  export type NextEditorPlugin<Name extends string, Metadata extends NextEditorPluginMetadata = {}> = Metadata extends NextEditorPluginMetadata ? 'pluginConfiguration' extends keyof Metadata ? NextEditorPluginFunctionOptionalConfigDefinition<Name, Metadata, Metadata['pluginConfiguration']> : NextEditorPluginFunctionOptionalConfigDefinition<Name, Metadata> : never;
60
- type FilterOptionalPlugins<T extends DependencyPlugin[]> = T extends [
61
- infer Head,
62
- ...infer Tail
63
- ] ? Tail extends DependencyPlugin[] ? Head extends OptionalPlugin<NextEditorPlugin<any, any>> ? FilterOptionalPlugins<Tail> : [
64
- Head,
65
- ...FilterOptionalPlugins<Tail>
66
- ] : T : T;
67
- type ExtractPluginDependenciesFromMetadataWithoutOptionals<Metadata extends NextEditorPluginMetadata> = Metadata['dependencies'] extends DependencyPlugin[] ? FilterOptionalPlugins<Metadata['dependencies']> : [
68
- ];
69
- type ExtractPluginDependenciesFromMetadata<Metadata> = 'dependencies' extends keyof Metadata ? Metadata['dependencies'] extends DependencyPlugin[] ? Exclude<Metadata['dependencies'], undefined> : [
70
- ] : [
71
- ];
72
- type ExtractSharedStateFromMetadata<Metadata> = 'sharedState' extends keyof Metadata ? Metadata['sharedState'] : never;
73
- type ExtractActionsFromMetadata<Metadata> = 'actions' extends keyof Metadata ? Metadata['actions'] : never;
74
- type ExtractCommandsFromMetadata<Metadata> = 'commands' extends keyof Metadata ? Metadata['commands'] : never;
75
- type ExtractPluginConfigurationFromMetadata<Metadata> = 'pluginConfiguration' extends keyof Metadata ? Metadata['pluginConfiguration'] : never;
76
- export type ExtractPluginDependencies<Plugin> = Plugin extends NextEditorPlugin<any, any> ? Plugin extends (props: {
77
- config: any;
78
- api: any;
79
- }) => DefaultEditorPlugin<any, infer Metadata> ? ExtractPluginDependenciesFromMetadataWithoutOptionals<Metadata> : never : never;
80
- type ExtractPluginConfiguration<Plugin> = Plugin extends NextEditorPlugin<any, any> ? Plugin extends (props: {
81
- config: any;
82
- api: any;
83
- }) => DefaultEditorPlugin<any, infer Metadata> ? ExtractPluginConfigurationFromMetadata<Metadata> : never : never;
84
- export type ExtractPluginSharedState<Plugin> = Plugin extends NextEditorPlugin<any, any> ? Plugin extends (props: {
85
- config: any;
86
- api: any;
87
- }) => DefaultEditorPlugin<any, infer Metadata> ? ExtractSharedStateFromMetadata<Metadata> : never : never;
88
- export type ExtractPluginActions<Plugin> = Plugin extends NextEditorPlugin<any, any> ? Plugin extends (props: {
89
- config: any;
90
- api: any;
91
- }) => DefaultEditorPlugin<any, infer Metadata> ? ExtractActionsFromMetadata<Metadata> : never : never;
92
- type ExtractPluginName<Plugin> = Plugin extends NextEditorPlugin<any, any> ? Plugin extends (...args: any) => DefaultEditorPlugin<infer PluginName, any> ? PluginName : never : never;
93
545
  type Unsubscribe = () => void;
94
- export type PluginDependenciesAPI<Plugin extends NextEditorPlugin<any, any>> = {
546
+ /**
547
+ * 🧱 Internal Type: Editor FE Platform
548
+ *
549
+ */
550
+ export type BasePluginDependenciesAPI<Metadata extends NextEditorPluginMetadata> = {
95
551
  sharedState: {
96
- currentState: () => ExtractPluginSharedState<Plugin> | undefined;
552
+ currentState: () => ExtractPluginSharedState<Metadata> | undefined;
97
553
  onChange: (sub: (props: {
98
- nextSharedState: ExtractPluginSharedState<Plugin>;
99
- prevSharedState: ExtractPluginSharedState<Plugin>;
554
+ nextSharedState: ExtractPluginSharedState<Metadata>;
555
+ prevSharedState: ExtractPluginSharedState<Metadata>;
100
556
  }) => void) => Unsubscribe;
101
557
  };
102
- actions: ExtractPluginActions<Plugin>;
103
- commands: Plugin extends NextEditorPlugin<any, infer Metadata> ? ExtractCommandsFromMetadata<Metadata> : never;
104
- };
105
- export type CreatePluginDependenciesAPI<PluginList extends NextEditorPlugin<any, any>[]> = {
106
- [Plugin in PluginList[number] as `${ExtractPluginName<Plugin>}`]: Plugin extends OptionalPlugin<NextEditorPlugin<any, any>> ? PluginDependenciesAPI<Plugin> | undefined : Plugin extends NextEditorPlugin<'core', any> ? PluginDependenciesAPI<Plugin> : Plugin extends NextEditorPlugin<any, any> ? PluginDependenciesAPI<Plugin> | undefined : never;
107
- };
108
- export type PluginWithConfiguration<Plugin> = undefined extends ExtractPluginConfiguration<Plugin> ? [
109
- Plugin,
110
- ExtractPluginConfiguration<Plugin>?
111
- ] : [
112
- Plugin,
113
- ExtractPluginConfiguration<Plugin>
114
- ];
115
- export type MaybePluginName<T extends string> = [
116
- T
117
- ];
118
- export type PresetPlugin = PluginWithConfiguration<any> | NextEditorPlugin<any, any>;
119
- export type MaybePlugin<T extends PresetPlugin> = T | undefined;
120
- export type AllEditorPresetPluginTypes = PresetPlugin | MaybePlugin<NextEditorPlugin<any, any>>;
121
- type ExtractNextEditorPlugin<Plugin> = Plugin extends PluginWithConfiguration<any> ? Plugin[0] : never;
122
- export type VerifyPluginDependencies<Plugin, PluginsStack extends AllEditorPresetPluginTypes[]> = ExtractPluginDependencies<Plugin> extends [
123
- ] ? Plugin extends PluginWithConfiguration<any> | NextEditorPlugin<any, any> ? Plugin : never :
124
- /**
125
- * case 1: We're looking for its dependent plugins indexed on `AllEditorPresetPluginTypes`
126
- */
127
- ExtractPluginDependencies<Plugin>[number] extends (ExtractPluginDependencies<Plugin>[number] & PluginsStack[number])
128
- /**
129
- * case 2:
130
- * Otherwise check whether the dependent-plugin, is hidden inside a tuple,
131
- * unwrapping `Plugins` via `ExtractNextEditorPlugin`
132
- */
133
- | (ExtractPluginDependencies<Plugin>[number] & ExtractNextEditorPlugin<PluginsStack[number]>) ? Plugin : never;
134
- type FilterExistingPlugins<T extends DependencyPlugin[], PluginsStack extends AllEditorPresetPluginTypes[]> = T extends [
135
- infer CurrentPluginDependency,
136
- ...infer RemainingPluginDependencies
137
- ] ? RemainingPluginDependencies extends DependencyPlugin[] ? CurrentPluginDependency extends PluginsStack[number] ? FilterExistingPlugins<RemainingPluginDependencies, PluginsStack> : [
138
- CurrentPluginDependency,
139
- ...FilterExistingPlugins<RemainingPluginDependencies, PluginsStack>
140
- ] : T : T;
141
- type DependencyErrorMessage<Message extends string> = {
142
- errorMessage: Message;
143
- };
144
- type ExtractRequiredDependencies<Plugin, PluginsStack extends AllEditorPresetPluginTypes[]> = Plugin extends NextEditorPlugin<infer PluginName, infer Metadata> ? Metadata['dependencies'] extends undefined ? DependencyErrorMessage<'No found dependencies'> : Metadata['dependencies'] extends DependencyPlugin[] ? FilterOptionalPlugins<FilterExistingPlugins<Metadata['dependencies'], PluginsStack>>[number] extends NextEditorPlugin<infer Name, any> ? Name : DependencyErrorMessage<`Invalid dependency for ${PluginName}`> : DependencyErrorMessage<`Invalid dependencies for ${PluginName}`> : DependencyErrorMessage<'Plugin is not NextEditorPlugin'>;
145
- type GetDependencyErrorMessage<Plugin, StackPlugins extends AllEditorPresetPluginTypes[]> = ExtractRequiredDependencies<Plugin, StackPlugins> extends string ? DependencyErrorMessage<`Missing dependency: ${ExtractRequiredDependencies<Plugin, StackPlugins>}Plugin`> : ExtractRequiredDependencies<Plugin, StackPlugins>;
146
- export type CheckDuplicatePlugin<Plugin, StackPlugins extends AllEditorPresetPluginTypes[]> = Plugin extends NextEditorPlugin<infer PluginName, any> ? Plugin extends StackPlugins[number] ? unknown extends StackPlugins[number] ? unknown : DependencyErrorMessage<`Duplicate plugin: ${PluginName}`> : unknown : unknown;
147
- /**
148
- * Used to check if a plugin being added can be added to a Preset/builder
149
- */
150
- export type SafePresetCheck<Plugin, StackPlugins extends AllEditorPresetPluginTypes[]> = Plugin extends Plugin & VerifyPluginDependencies<Plugin, StackPlugins> ? Plugin extends NextEditorPlugin<any, any> ? CheckDuplicatePlugin<Plugin, StackPlugins> & CheckBasicPlugin<Plugin> : never : GetDependencyErrorMessage<Plugin, StackPlugins>;
151
- type CheckTupleRequirements<Plugin, Config, ArrayType> = unknown extends Config ? Plugin | ArrayType : undefined extends Config ? Plugin | ArrayType : [
152
- Config
153
- ] extends [
154
- never
155
- ] ? Plugin : ArrayType;
156
- type CheckBasicPlugin<Plugin> = Plugin extends (args: any, api: any) => EditorPlugin ? CheckTupleRequirements<Plugin, ExtractPluginConfiguration<Plugin>, PluginWithConfiguration<Plugin>> : never;
157
- export type ExtractPluginNameFromAllBuilderPlugins<Plugin extends PresetPlugin> = Plugin extends Array<any> ? Plugin extends [
158
- infer MPlugin,
159
- ...any
160
- ] ? MPlugin extends NextEditorPlugin<any, any> ? ExtractPluginName<MPlugin> : never : never : Plugin extends NextEditorPlugin<any, any> ? ExtractPluginName<Plugin> : never;
161
- export type ExtractInjectionAPI<Plugin> = Plugin extends NextEditorPlugin<infer Name, infer Metadata> ? PluginInjectionAPI<Name, Metadata>['dependencies'] : never;
162
- export type PublicPluginAPI<PluginList extends NextEditorPlugin<any, any>[]> = CreatePluginDependenciesAPI<[
163
- ...PluginList,
164
- CorePlugin
165
- ]>;
166
- export type ExtractNextEditorPlugins<Plugins extends AllEditorPresetPluginTypes[]> = {
167
- [PluginNumber in keyof Plugins]: Plugins[PluginNumber] extends MaybePlugin<NextEditorPlugin<infer Name, infer Metadata>> ? NextEditorPlugin<Name, Metadata> : Plugins[PluginNumber] extends MaybePlugin<NextEditorPlugin<infer Name, infer Metadata>> ? NextEditorPlugin<Name, Metadata> : never;
558
+ actions: ExtractPluginActions<Metadata>;
559
+ commands: ExtractCommandsFromMetadata<Metadata>;
168
560
  };
561
+ /**
562
+ * 🧱 Internal Type: Editor FE Platform
563
+ *
564
+ * Type that defines a Editor API based on a NextEditorPlugin.
565
+ *
566
+ *
567
+ * If you are not from Editor Platform FE, you shouldn't be using or changing this.
568
+ */
569
+ export type PluginDependenciesAPI<Plugin extends NextEditorPlugin<any, any>> = Plugin extends NextEditorPlugin<any, infer Metadata> ? BasePluginDependenciesAPI<Metadata> : never;
570
+ /**
571
+ * 🧱 Internal Type: Editor FE Platform
572
+ *
573
+ *
574
+ * Type to enable thats create a API based ona single plugin:
575
+ *
576
+ * - The plugin itself will become a required property
577
+ * - Its optional dependencies will become a non-required property
578
+ * - Its required dependencies will become a required property
579
+ * - Core will be a required property
580
+ */
581
+ export type EditorInjectionAPI<Name extends string, Metadata extends NextEditorPluginMetadata> = {
582
+ core: PluginDependenciesAPI<CorePlugin>;
583
+ } & SelfPluginDependenciesAPI<Name, Metadata> & RequiredPluginDependenciesAPI<ExtractRequiredPlugins<ExtractPluginDependenciesFromMetadata<Metadata>>> & OptionalPluginDependenciesAPI<ExtractOptionalPlugins<ExtractPluginDependenciesFromMetadata<Metadata>>>;
584
+ /**
585
+ * 🧱 Internal Type: Editor FE Platform
586
+ *
587
+ * Helper type to enable thats create a fully optional API based ona single plugin:
588
+ *
589
+ * - The plugin itself will become a non-required property
590
+ * - Its optional dependencies will become a non-required property
591
+ * - Its required dependencies will become a non-required property
592
+ * - Core will be a required property
593
+ */
594
+ export type EditorOptionalInjectionAPI<Name extends string, Metadata extends NextEditorPluginMetadata> = {
595
+ core: PluginDependenciesAPI<CorePlugin>;
596
+ } & SelfOptionalPluginDependenciesAPI<Name, Metadata> & OptionalPluginDependenciesAPI<ExtractRequiredPlugins<ExtractPluginDependenciesFromMetadata<Metadata>>> & OptionalPluginDependenciesAPI<ExtractOptionalPlugins<ExtractPluginDependenciesFromMetadata<Metadata>>>;
597
+ /**
598
+ * 📢 Public Type API: Helps Editor Plugin developers to build the EditorInjectAPI type for their internal plugin development.
599
+ *
600
+ * You should use this API if you are:
601
+ * - Working inside a EditorPlugin package, e.g.: `@atlaskit/editor-table-plugin`.
602
+ * - Trying to use it to receive an API from the NextEditorPlugin argument.
603
+ *
604
+ * You should not use this API if you are:
605
+ *
606
+ * - Using it outside an EditorPlugin package
607
+ * - Trying to use it to receive an API directly from EditorPresetBuilder
608
+ *
609
+ * This type will used the base plugin dependencies to build the type.
610
+ * ```typescript
611
+ *
612
+ * type PluginDog = NextEditorPlugin<'dog'>;
613
+ * type PluginBark = NextEditorPlugin<'bark', { dependencies: [PluginDog], actions: { doBark: () => void }}>;
614
+ *
615
+ * type MyPlugin = NextEditorPlugin<'myPlugin', { dependencies: [PluginBark] }>;
616
+ * type MyAPI = ExtractInjectionAPI<MyPlugin>;
617
+ *
618
+ * const somePluginInternalFunction = (api?: MyAPI) => {
619
+ * api?.bark.actions.doBark();
620
+ * }
621
+ * ```
622
+ *
623
+ * If the dependency is marked as OptionalPlugin then the API will return it as optional property too:
624
+ *
625
+ *```typescript
626
+ *
627
+ * type PluginDog = NextEditorPlugin<'dog'>;
628
+ * type PluginBark = NextEditorPlugin<'bark', { dependencies: [PluginDog], actions: { doBark: () => void }}>;
629
+ *
630
+ * type MyPlugin = NextEditorPlugin<'myPlugin', { dependencies: [OptionalPlugin<PluginBark>] }>;
631
+ * type MyAPI = ExtractInjectionAPI<MyPlugin>;
632
+ *
633
+ * const somePluginInternalFunction = (api?: MyAPI) => {
634
+ * // Now, you will need to do a null check
635
+ * api?.bark?.actions.doBark();
636
+ * }
637
+ * ```
638
+ *
639
+ * A `NextEditorPlugin` will receive an `api` argument that match with this helper type:
640
+ * ```typescript
641
+ *
642
+ * type PluginDog = NextEditorPlugin<'dog'>;
643
+ *
644
+ * const dogPlugin: PluginDog = ({ api }) => {
645
+ * type ReceivedAPI = typeof api;
646
+ * type BuiltAPI = ExtractInjectionAPI<PluginDog>;
647
+ *
648
+ * type AssertTrue = BuiltAPI extends ReceivedAPI ? true : false;
649
+ * return {
650
+ * name: 'dog';
651
+ * }
652
+ * };
653
+ * ```
654
+ *
655
+ * However, this is not compatible with the EditorPresetBuilder API
656
+ * due to possibility some plugins maybe be not be available on runtime.
657
+ *
658
+ * ```typescript
659
+ *
660
+ * type PluginDog = NextEditorPlugin<'dog'>;
661
+ *
662
+ * const dogPlugin: PluginDog = ({ api }) => ({ name: 'dog' });
663
+ * const preset = new EditorPresetBuilder()
664
+ * .maybeAdd(dogPlugin, false);
665
+ *
666
+ * type BuiltAPI = ExtractInjectionAPI<typeof dogPlugin>;
667
+ * type PresetAPI = ExtractPresetAPI<typeof preset>;
668
+ *
669
+ *
670
+ * type AssertFalse = BuiltAPI extends PresetAPI ? true : false;
671
+ * // If you need to match the API from a Preset, you should use `PublicPluginAPI`
672
+ *
673
+ * ```
674
+ *
675
+ * P.S.: This type will implicitly inject the CorePlugin as required attribute.
676
+ */
677
+ export type ExtractInjectionAPI<Plugin extends NextEditorPlugin<any, any>> = Plugin extends NextEditorPlugin<infer Name, infer Metadata> ? EditorInjectionAPI<Name, Metadata> : never;
678
+ /**
679
+ * 📢 Public Type API: Helps Product developers to build the EditorInjectAPI type for external use in the product codebase.
680
+ *
681
+ * You may use more than one plugin:
682
+ * ```typescript
683
+ * PublicPluginAPI<[PluginDog, PluginCat]>
684
+ * ```
685
+ *
686
+ * You should use this API if you are:
687
+ * - Working inside a non EditorPlugin, e.g.: `@atlaskit/chat-ai-mate`.
688
+ * - Trying to use it to receive an API directly from EditorPresetBuilder
689
+ *
690
+ * - Working
691
+ *
692
+ * The API type will consider all plugins as optional. That means,
693
+ * you will need to do the null safe check when using the type implementation,
694
+ *
695
+ * ```typescript
696
+ *
697
+ * type MyCustomEditorPlugin = NextEditorPlugin<'custom', { actions: { something: () => boolean } }>;
698
+ * const myPlugin: MyCustomEditorPlugin = () => ({ name: 'custom', actions: { something: () => true }});
699
+ *
700
+ * type Props = {
701
+ * api?: PublicPluginAPI<[MyCustomEditorPlugin]>
702
+ * }
703
+ *
704
+ * // There is no way to guarantee the API will have the plugin requested.
705
+ * // So, this will force developers to do the safe check.
706
+ * function useMyHook({ api }: Props) {
707
+ * api?.custom?.actions.something();
708
+ * return null;
709
+ * }
710
+ * ```
711
+ *
712
+ * P.S.: This type will implicitly inject the CorePlugin as required attribute.
713
+ */
714
+ export type PublicPluginAPI<MaybePlugin extends NextEditorPlugin<any, any>[] | NextEditorPlugin<any, any>> = MaybePlugin extends NextEditorPlugin<infer Name, infer Metadata> ? EditorOptionalInjectionAPI<Name, Metadata> : PublicPluginAPIFromPlugins<MaybePlugin extends NextEditorPlugin<any, any>[] ? MaybePlugin : never>;
169
715
  export {};