@ckeditor/ckeditor5-widget 0.0.0-internal-20241017.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.

Potentially problematic release.


This version of @ckeditor/ckeditor5-widget might be problematic. Click here for more details.

Files changed (259) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/LICENSE.md +21 -0
  3. package/README.md +22 -0
  4. package/dist/augmentation.d.ts +17 -0
  5. package/dist/highlightstack.d.ts +78 -0
  6. package/dist/index-content.css +4 -0
  7. package/dist/index-editor.css +183 -0
  8. package/dist/index.css +257 -0
  9. package/dist/index.css.map +1 -0
  10. package/dist/index.d.ts +17 -0
  11. package/dist/index.js +3054 -0
  12. package/dist/index.js.map +1 -0
  13. package/dist/translations/ar.d.ts +8 -0
  14. package/dist/translations/ar.js +5 -0
  15. package/dist/translations/ar.umd.js +11 -0
  16. package/dist/translations/az.d.ts +8 -0
  17. package/dist/translations/az.js +5 -0
  18. package/dist/translations/az.umd.js +11 -0
  19. package/dist/translations/bg.d.ts +8 -0
  20. package/dist/translations/bg.js +5 -0
  21. package/dist/translations/bg.umd.js +11 -0
  22. package/dist/translations/bn.d.ts +8 -0
  23. package/dist/translations/bn.js +5 -0
  24. package/dist/translations/bn.umd.js +11 -0
  25. package/dist/translations/ca.d.ts +8 -0
  26. package/dist/translations/ca.js +5 -0
  27. package/dist/translations/ca.umd.js +11 -0
  28. package/dist/translations/cs.d.ts +8 -0
  29. package/dist/translations/cs.js +5 -0
  30. package/dist/translations/cs.umd.js +11 -0
  31. package/dist/translations/da.d.ts +8 -0
  32. package/dist/translations/da.js +5 -0
  33. package/dist/translations/da.umd.js +11 -0
  34. package/dist/translations/de-ch.d.ts +8 -0
  35. package/dist/translations/de-ch.js +5 -0
  36. package/dist/translations/de-ch.umd.js +11 -0
  37. package/dist/translations/de.d.ts +8 -0
  38. package/dist/translations/de.js +5 -0
  39. package/dist/translations/de.umd.js +11 -0
  40. package/dist/translations/el.d.ts +8 -0
  41. package/dist/translations/el.js +5 -0
  42. package/dist/translations/el.umd.js +11 -0
  43. package/dist/translations/en-au.d.ts +8 -0
  44. package/dist/translations/en-au.js +5 -0
  45. package/dist/translations/en-au.umd.js +11 -0
  46. package/dist/translations/en.d.ts +8 -0
  47. package/dist/translations/en.js +5 -0
  48. package/dist/translations/en.umd.js +11 -0
  49. package/dist/translations/es.d.ts +8 -0
  50. package/dist/translations/es.js +5 -0
  51. package/dist/translations/es.umd.js +11 -0
  52. package/dist/translations/et.d.ts +8 -0
  53. package/dist/translations/et.js +5 -0
  54. package/dist/translations/et.umd.js +11 -0
  55. package/dist/translations/fa.d.ts +8 -0
  56. package/dist/translations/fa.js +5 -0
  57. package/dist/translations/fa.umd.js +11 -0
  58. package/dist/translations/fi.d.ts +8 -0
  59. package/dist/translations/fi.js +5 -0
  60. package/dist/translations/fi.umd.js +11 -0
  61. package/dist/translations/fr.d.ts +8 -0
  62. package/dist/translations/fr.js +5 -0
  63. package/dist/translations/fr.umd.js +11 -0
  64. package/dist/translations/gl.d.ts +8 -0
  65. package/dist/translations/gl.js +5 -0
  66. package/dist/translations/gl.umd.js +11 -0
  67. package/dist/translations/he.d.ts +8 -0
  68. package/dist/translations/he.js +5 -0
  69. package/dist/translations/he.umd.js +11 -0
  70. package/dist/translations/hi.d.ts +8 -0
  71. package/dist/translations/hi.js +5 -0
  72. package/dist/translations/hi.umd.js +11 -0
  73. package/dist/translations/hr.d.ts +8 -0
  74. package/dist/translations/hr.js +5 -0
  75. package/dist/translations/hr.umd.js +11 -0
  76. package/dist/translations/hu.d.ts +8 -0
  77. package/dist/translations/hu.js +5 -0
  78. package/dist/translations/hu.umd.js +11 -0
  79. package/dist/translations/id.d.ts +8 -0
  80. package/dist/translations/id.js +5 -0
  81. package/dist/translations/id.umd.js +11 -0
  82. package/dist/translations/it.d.ts +8 -0
  83. package/dist/translations/it.js +5 -0
  84. package/dist/translations/it.umd.js +11 -0
  85. package/dist/translations/ja.d.ts +8 -0
  86. package/dist/translations/ja.js +5 -0
  87. package/dist/translations/ja.umd.js +11 -0
  88. package/dist/translations/ko.d.ts +8 -0
  89. package/dist/translations/ko.js +5 -0
  90. package/dist/translations/ko.umd.js +11 -0
  91. package/dist/translations/ku.d.ts +8 -0
  92. package/dist/translations/ku.js +5 -0
  93. package/dist/translations/ku.umd.js +11 -0
  94. package/dist/translations/lt.d.ts +8 -0
  95. package/dist/translations/lt.js +5 -0
  96. package/dist/translations/lt.umd.js +11 -0
  97. package/dist/translations/lv.d.ts +8 -0
  98. package/dist/translations/lv.js +5 -0
  99. package/dist/translations/lv.umd.js +11 -0
  100. package/dist/translations/ms.d.ts +8 -0
  101. package/dist/translations/ms.js +5 -0
  102. package/dist/translations/ms.umd.js +11 -0
  103. package/dist/translations/nl.d.ts +8 -0
  104. package/dist/translations/nl.js +5 -0
  105. package/dist/translations/nl.umd.js +11 -0
  106. package/dist/translations/no.d.ts +8 -0
  107. package/dist/translations/no.js +5 -0
  108. package/dist/translations/no.umd.js +11 -0
  109. package/dist/translations/pl.d.ts +8 -0
  110. package/dist/translations/pl.js +5 -0
  111. package/dist/translations/pl.umd.js +11 -0
  112. package/dist/translations/pt-br.d.ts +8 -0
  113. package/dist/translations/pt-br.js +5 -0
  114. package/dist/translations/pt-br.umd.js +11 -0
  115. package/dist/translations/pt.d.ts +8 -0
  116. package/dist/translations/pt.js +5 -0
  117. package/dist/translations/pt.umd.js +11 -0
  118. package/dist/translations/ro.d.ts +8 -0
  119. package/dist/translations/ro.js +5 -0
  120. package/dist/translations/ro.umd.js +11 -0
  121. package/dist/translations/ru.d.ts +8 -0
  122. package/dist/translations/ru.js +5 -0
  123. package/dist/translations/ru.umd.js +11 -0
  124. package/dist/translations/sk.d.ts +8 -0
  125. package/dist/translations/sk.js +5 -0
  126. package/dist/translations/sk.umd.js +11 -0
  127. package/dist/translations/sq.d.ts +8 -0
  128. package/dist/translations/sq.js +5 -0
  129. package/dist/translations/sq.umd.js +11 -0
  130. package/dist/translations/sr-latn.d.ts +8 -0
  131. package/dist/translations/sr-latn.js +5 -0
  132. package/dist/translations/sr-latn.umd.js +11 -0
  133. package/dist/translations/sr.d.ts +8 -0
  134. package/dist/translations/sr.js +5 -0
  135. package/dist/translations/sr.umd.js +11 -0
  136. package/dist/translations/sv.d.ts +8 -0
  137. package/dist/translations/sv.js +5 -0
  138. package/dist/translations/sv.umd.js +11 -0
  139. package/dist/translations/th.d.ts +8 -0
  140. package/dist/translations/th.js +5 -0
  141. package/dist/translations/th.umd.js +11 -0
  142. package/dist/translations/tk.d.ts +8 -0
  143. package/dist/translations/tk.js +5 -0
  144. package/dist/translations/tk.umd.js +11 -0
  145. package/dist/translations/tr.d.ts +8 -0
  146. package/dist/translations/tr.js +5 -0
  147. package/dist/translations/tr.umd.js +11 -0
  148. package/dist/translations/uk.d.ts +8 -0
  149. package/dist/translations/uk.js +5 -0
  150. package/dist/translations/uk.umd.js +11 -0
  151. package/dist/translations/ur.d.ts +8 -0
  152. package/dist/translations/ur.js +5 -0
  153. package/dist/translations/ur.umd.js +11 -0
  154. package/dist/translations/uz.d.ts +8 -0
  155. package/dist/translations/uz.js +5 -0
  156. package/dist/translations/uz.umd.js +11 -0
  157. package/dist/translations/vi.d.ts +8 -0
  158. package/dist/translations/vi.js +5 -0
  159. package/dist/translations/vi.umd.js +11 -0
  160. package/dist/translations/zh-cn.d.ts +8 -0
  161. package/dist/translations/zh-cn.js +5 -0
  162. package/dist/translations/zh-cn.umd.js +11 -0
  163. package/dist/translations/zh.d.ts +8 -0
  164. package/dist/translations/zh.js +5 -0
  165. package/dist/translations/zh.umd.js +11 -0
  166. package/dist/utils.d.ts +219 -0
  167. package/dist/verticalnavigation.d.ts +19 -0
  168. package/dist/widget.d.ts +111 -0
  169. package/dist/widgetresize/resizer.d.ts +181 -0
  170. package/dist/widgetresize/resizerstate.d.ts +129 -0
  171. package/dist/widgetresize/sizeview.d.ts +59 -0
  172. package/dist/widgetresize.d.ts +133 -0
  173. package/dist/widgettoolbarrepository.d.ts +102 -0
  174. package/dist/widgettypearound/utils.d.ts +42 -0
  175. package/dist/widgettypearound/widgettypearound.d.ts +237 -0
  176. package/lang/contexts.json +12 -0
  177. package/lang/translations/ar.po +58 -0
  178. package/lang/translations/az.po +58 -0
  179. package/lang/translations/bg.po +58 -0
  180. package/lang/translations/bn.po +58 -0
  181. package/lang/translations/ca.po +58 -0
  182. package/lang/translations/cs.po +58 -0
  183. package/lang/translations/da.po +58 -0
  184. package/lang/translations/de-ch.po +58 -0
  185. package/lang/translations/de.po +58 -0
  186. package/lang/translations/el.po +58 -0
  187. package/lang/translations/en-au.po +58 -0
  188. package/lang/translations/en.po +58 -0
  189. package/lang/translations/es.po +58 -0
  190. package/lang/translations/et.po +58 -0
  191. package/lang/translations/fa.po +58 -0
  192. package/lang/translations/fi.po +58 -0
  193. package/lang/translations/fr.po +58 -0
  194. package/lang/translations/gl.po +58 -0
  195. package/lang/translations/he.po +58 -0
  196. package/lang/translations/hi.po +58 -0
  197. package/lang/translations/hr.po +58 -0
  198. package/lang/translations/hu.po +58 -0
  199. package/lang/translations/id.po +58 -0
  200. package/lang/translations/it.po +58 -0
  201. package/lang/translations/ja.po +58 -0
  202. package/lang/translations/ko.po +58 -0
  203. package/lang/translations/ku.po +58 -0
  204. package/lang/translations/lt.po +58 -0
  205. package/lang/translations/lv.po +58 -0
  206. package/lang/translations/ms.po +58 -0
  207. package/lang/translations/nl.po +58 -0
  208. package/lang/translations/no.po +58 -0
  209. package/lang/translations/pl.po +58 -0
  210. package/lang/translations/pt-br.po +58 -0
  211. package/lang/translations/pt.po +58 -0
  212. package/lang/translations/ro.po +58 -0
  213. package/lang/translations/ru.po +58 -0
  214. package/lang/translations/sk.po +58 -0
  215. package/lang/translations/sq.po +58 -0
  216. package/lang/translations/sr-latn.po +58 -0
  217. package/lang/translations/sr.po +58 -0
  218. package/lang/translations/sv.po +58 -0
  219. package/lang/translations/th.po +58 -0
  220. package/lang/translations/tk.po +58 -0
  221. package/lang/translations/tr.po +58 -0
  222. package/lang/translations/uk.po +58 -0
  223. package/lang/translations/ur.po +58 -0
  224. package/lang/translations/uz.po +58 -0
  225. package/lang/translations/vi.po +58 -0
  226. package/lang/translations/zh-cn.po +58 -0
  227. package/lang/translations/zh.po +58 -0
  228. package/package.json +42 -0
  229. package/src/augmentation.d.ts +13 -0
  230. package/src/augmentation.js +5 -0
  231. package/src/highlightstack.d.ts +74 -0
  232. package/src/highlightstack.js +129 -0
  233. package/src/index.d.ts +13 -0
  234. package/src/index.js +13 -0
  235. package/src/utils.d.ts +215 -0
  236. package/src/utils.js +396 -0
  237. package/src/verticalnavigation.d.ts +15 -0
  238. package/src/verticalnavigation.js +196 -0
  239. package/src/widget.d.ts +107 -0
  240. package/src/widget.js +603 -0
  241. package/src/widgetresize/resizer.d.ts +177 -0
  242. package/src/widgetresize/resizer.js +372 -0
  243. package/src/widgetresize/resizerstate.d.ts +125 -0
  244. package/src/widgetresize/resizerstate.js +129 -0
  245. package/src/widgetresize/sizeview.d.ts +55 -0
  246. package/src/widgetresize/sizeview.js +63 -0
  247. package/src/widgetresize.d.ts +129 -0
  248. package/src/widgetresize.js +194 -0
  249. package/src/widgettoolbarrepository.d.ts +98 -0
  250. package/src/widgettoolbarrepository.js +274 -0
  251. package/src/widgettypearound/utils.d.ts +38 -0
  252. package/src/widgettypearound/utils.js +52 -0
  253. package/src/widgettypearound/widgettypearound.d.ts +233 -0
  254. package/src/widgettypearound/widgettypearound.js +779 -0
  255. package/theme/icons/drag-handle.svg +1 -0
  256. package/theme/icons/return-arrow.svg +1 -0
  257. package/theme/widget.css +91 -0
  258. package/theme/widgetresize.css +43 -0
  259. package/theme/widgettypearound.css +113 -0
@@ -0,0 +1,274 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module widget/widgettoolbarrepository
7
+ */
8
+ import { Plugin } from '@ckeditor/ckeditor5-core';
9
+ import { BalloonPanelView, ContextualBalloon, ToolbarView } from '@ckeditor/ckeditor5-ui';
10
+ import { CKEditorError, logWarning } from '@ckeditor/ckeditor5-utils';
11
+ import { isWidget } from './utils.js';
12
+ /**
13
+ * Widget toolbar repository plugin. A central point for registering widget toolbars. This plugin handles the whole
14
+ * toolbar rendering process and exposes a concise API.
15
+ *
16
+ * To add a toolbar for your widget use the {@link ~WidgetToolbarRepository#register `WidgetToolbarRepository#register()`} method.
17
+ *
18
+ * The following example comes from the {@link module:image/imagetoolbar~ImageToolbar} plugin:
19
+ *
20
+ * ```ts
21
+ * class ImageToolbar extends Plugin {
22
+ * static get requires() {
23
+ * return [ WidgetToolbarRepository ];
24
+ * }
25
+ *
26
+ * afterInit() {
27
+ * const editor = this.editor;
28
+ * const widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
29
+ *
30
+ * widgetToolbarRepository.register( 'image', {
31
+ * items: editor.config.get( 'image.toolbar' ),
32
+ * getRelatedElement: getClosestSelectedImageWidget
33
+ * } );
34
+ * }
35
+ * }
36
+ * ```
37
+ */
38
+ export default class WidgetToolbarRepository extends Plugin {
39
+ constructor() {
40
+ super(...arguments);
41
+ /**
42
+ * A map of toolbar definitions.
43
+ */
44
+ this._toolbarDefinitions = new Map();
45
+ }
46
+ /**
47
+ * @inheritDoc
48
+ */
49
+ static get requires() {
50
+ return [ContextualBalloon];
51
+ }
52
+ /**
53
+ * @inheritDoc
54
+ */
55
+ static get pluginName() {
56
+ return 'WidgetToolbarRepository';
57
+ }
58
+ /**
59
+ * @inheritDoc
60
+ */
61
+ static get isOfficialPlugin() {
62
+ return true;
63
+ }
64
+ /**
65
+ * @inheritDoc
66
+ */
67
+ init() {
68
+ const editor = this.editor;
69
+ // Disables the default balloon toolbar for all widgets.
70
+ if (editor.plugins.has('BalloonToolbar')) {
71
+ const balloonToolbar = editor.plugins.get('BalloonToolbar');
72
+ this.listenTo(balloonToolbar, 'show', evt => {
73
+ if (isWidgetSelected(editor.editing.view.document.selection)) {
74
+ evt.stop();
75
+ }
76
+ }, { priority: 'high' });
77
+ }
78
+ this._balloon = this.editor.plugins.get('ContextualBalloon');
79
+ this.on('change:isEnabled', () => {
80
+ this._updateToolbarsVisibility();
81
+ });
82
+ this.listenTo(editor.ui, 'update', () => {
83
+ this._updateToolbarsVisibility();
84
+ });
85
+ // UI#update is not fired after focus is back in editor, we need to check if balloon panel should be visible.
86
+ this.listenTo(editor.ui.focusTracker, 'change:isFocused', () => {
87
+ this._updateToolbarsVisibility();
88
+ }, { priority: 'low' });
89
+ }
90
+ destroy() {
91
+ super.destroy();
92
+ for (const toolbarConfig of this._toolbarDefinitions.values()) {
93
+ toolbarConfig.view.destroy();
94
+ }
95
+ }
96
+ /**
97
+ * Registers toolbar in the WidgetToolbarRepository. It renders it in the `ContextualBalloon` based on the value of the invoked
98
+ * `getRelatedElement` function. Toolbar items are gathered from `items` array.
99
+ * The balloon's CSS class is by default `ck-toolbar-container` and may be override with the `balloonClassName` option.
100
+ *
101
+ * Note: This method should be called in the {@link module:core/plugin~PluginInterface#afterInit `Plugin#afterInit()`}
102
+ * callback (or later) to make sure that the given toolbar items were already registered by other plugins.
103
+ *
104
+ * @param toolbarId An id for the toolbar. Used to
105
+ * @param options.ariaLabel Label used by assistive technologies to describe this toolbar element.
106
+ * @param options.items Array of toolbar items.
107
+ * @param options.getRelatedElement Callback which returns an element the toolbar should be attached to.
108
+ * @param options.balloonClassName CSS class for the widget balloon.
109
+ */
110
+ register(toolbarId, { ariaLabel, items, getRelatedElement, balloonClassName = 'ck-toolbar-container' }) {
111
+ // Trying to register a toolbar without any item.
112
+ if (!items.length) {
113
+ /**
114
+ * When {@link module:widget/widgettoolbarrepository~WidgetToolbarRepository#register registering} a new widget toolbar, you
115
+ * need to provide a non-empty array with the items that will be inserted into the toolbar.
116
+ *
117
+ * If you see this error when integrating the editor, you likely forgot to configure one of the widget toolbars.
118
+ *
119
+ * See for instance:
120
+ *
121
+ * * {@link module:table/tableconfig~TableConfig#contentToolbar `config.table.contentToolbar`}
122
+ * * {@link module:image/imageconfig~ImageConfig#toolbar `config.image.toolbar`}
123
+ *
124
+ * @error widget-toolbar-no-items
125
+ * @param toolbarId The id of the toolbar that has not been configured correctly.
126
+ */
127
+ logWarning('widget-toolbar-no-items', { toolbarId });
128
+ return;
129
+ }
130
+ const editor = this.editor;
131
+ const t = editor.t;
132
+ const toolbarView = new ToolbarView(editor.locale);
133
+ toolbarView.ariaLabel = ariaLabel || t('Widget toolbar');
134
+ if (this._toolbarDefinitions.has(toolbarId)) {
135
+ /**
136
+ * Toolbar with the given id was already added.
137
+ *
138
+ * @error widget-toolbar-duplicated
139
+ * @param toolbarId Toolbar id.
140
+ */
141
+ throw new CKEditorError('widget-toolbar-duplicated', this, { toolbarId });
142
+ }
143
+ const toolbarDefinition = {
144
+ view: toolbarView,
145
+ getRelatedElement,
146
+ balloonClassName,
147
+ itemsConfig: items,
148
+ initialized: false
149
+ };
150
+ // Register the toolbar so it becomes available for Alt+F10 and Esc navigation.
151
+ editor.ui.addToolbar(toolbarView, {
152
+ isContextual: true,
153
+ beforeFocus: () => {
154
+ const relatedElement = getRelatedElement(editor.editing.view.document.selection);
155
+ if (relatedElement) {
156
+ this._showToolbar(toolbarDefinition, relatedElement);
157
+ }
158
+ },
159
+ afterBlur: () => {
160
+ this._hideToolbar(toolbarDefinition);
161
+ }
162
+ });
163
+ this._toolbarDefinitions.set(toolbarId, toolbarDefinition);
164
+ }
165
+ /**
166
+ * Iterates over stored toolbars and makes them visible or hidden.
167
+ */
168
+ _updateToolbarsVisibility() {
169
+ let maxRelatedElementDepth = 0;
170
+ let deepestRelatedElement = null;
171
+ let deepestToolbarDefinition = null;
172
+ for (const definition of this._toolbarDefinitions.values()) {
173
+ const relatedElement = definition.getRelatedElement(this.editor.editing.view.document.selection);
174
+ if (!this.isEnabled || !relatedElement) {
175
+ if (this._isToolbarInBalloon(definition)) {
176
+ this._hideToolbar(definition);
177
+ }
178
+ }
179
+ else if (!this.editor.ui.focusTracker.isFocused) {
180
+ if (this._isToolbarVisible(definition)) {
181
+ this._hideToolbar(definition);
182
+ }
183
+ }
184
+ else {
185
+ const relatedElementDepth = relatedElement.getAncestors().length;
186
+ // Many toolbars can express willingness to be displayed but they do not know about
187
+ // each other. Figure out which toolbar is deepest in the view tree to decide which
188
+ // should be displayed. For instance, if a selected image is inside a table cell, display
189
+ // the ImageToolbar rather than the TableToolbar (#60).
190
+ if (relatedElementDepth > maxRelatedElementDepth) {
191
+ maxRelatedElementDepth = relatedElementDepth;
192
+ deepestRelatedElement = relatedElement;
193
+ deepestToolbarDefinition = definition;
194
+ }
195
+ }
196
+ }
197
+ if (deepestToolbarDefinition) {
198
+ this._showToolbar(deepestToolbarDefinition, deepestRelatedElement);
199
+ }
200
+ }
201
+ /**
202
+ * Hides the given toolbar.
203
+ */
204
+ _hideToolbar(toolbarDefinition) {
205
+ this._balloon.remove(toolbarDefinition.view);
206
+ this.stopListening(this._balloon, 'change:visibleView');
207
+ }
208
+ /**
209
+ * Shows up the toolbar if the toolbar is not visible.
210
+ * Otherwise, repositions the toolbar's balloon when toolbar's view is the most top view in balloon stack.
211
+ *
212
+ * It might happen here that the toolbar's view is under another view. Then do nothing as the other toolbar view
213
+ * should be still visible after the {@link module:ui/editorui/editorui~EditorUI#event:update}.
214
+ */
215
+ _showToolbar(toolbarDefinition, relatedElement) {
216
+ if (this._isToolbarVisible(toolbarDefinition)) {
217
+ repositionContextualBalloon(this.editor, relatedElement);
218
+ }
219
+ else if (!this._isToolbarInBalloon(toolbarDefinition)) {
220
+ if (!toolbarDefinition.initialized) {
221
+ toolbarDefinition.initialized = true;
222
+ toolbarDefinition.view.fillFromConfig(toolbarDefinition.itemsConfig, this.editor.ui.componentFactory);
223
+ }
224
+ this._balloon.add({
225
+ view: toolbarDefinition.view,
226
+ position: getBalloonPositionData(this.editor, relatedElement),
227
+ balloonClassName: toolbarDefinition.balloonClassName
228
+ });
229
+ // Update toolbar position each time stack with toolbar view is switched to visible.
230
+ // This is in a case target element has changed when toolbar was in invisible stack
231
+ // e.g. target image was wrapped by a block quote.
232
+ // See https://github.com/ckeditor/ckeditor5-widget/issues/92.
233
+ this.listenTo(this._balloon, 'change:visibleView', () => {
234
+ for (const definition of this._toolbarDefinitions.values()) {
235
+ if (this._isToolbarVisible(definition)) {
236
+ const relatedElement = definition.getRelatedElement(this.editor.editing.view.document.selection);
237
+ repositionContextualBalloon(this.editor, relatedElement);
238
+ }
239
+ }
240
+ });
241
+ }
242
+ }
243
+ _isToolbarVisible(toolbar) {
244
+ return this._balloon.visibleView === toolbar.view;
245
+ }
246
+ _isToolbarInBalloon(toolbar) {
247
+ return this._balloon.hasView(toolbar.view);
248
+ }
249
+ }
250
+ function repositionContextualBalloon(editor, relatedElement) {
251
+ const balloon = editor.plugins.get('ContextualBalloon');
252
+ const position = getBalloonPositionData(editor, relatedElement);
253
+ balloon.updatePosition(position);
254
+ }
255
+ function getBalloonPositionData(editor, relatedElement) {
256
+ const editingView = editor.editing.view;
257
+ const defaultPositions = BalloonPanelView.defaultPositions;
258
+ return {
259
+ target: editingView.domConverter.mapViewToDom(relatedElement),
260
+ positions: [
261
+ defaultPositions.northArrowSouth,
262
+ defaultPositions.northArrowSouthWest,
263
+ defaultPositions.northArrowSouthEast,
264
+ defaultPositions.southArrowNorth,
265
+ defaultPositions.southArrowNorthWest,
266
+ defaultPositions.southArrowNorthEast,
267
+ defaultPositions.viewportStickyNorth
268
+ ]
269
+ };
270
+ }
271
+ function isWidgetSelected(selection) {
272
+ const viewElement = selection.getSelectedElement();
273
+ return !!(viewElement && isWidget(viewElement));
274
+ }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ import type { DocumentSelection, DomConverter, Element, Schema, Selection, ViewElement } from '@ckeditor/ckeditor5-engine';
6
+ /**
7
+ * The name of the type around model selection attribute responsible for
8
+ * displaying a fake caret next to a selected widget.
9
+ */
10
+ export declare const TYPE_AROUND_SELECTION_ATTRIBUTE = "widget-type-around";
11
+ /**
12
+ * Checks if an element is a widget that qualifies to get the widget type around UI.
13
+ */
14
+ export declare function isTypeAroundWidget(viewElement: ViewElement | undefined, modelElement: Element, schema: Schema): boolean;
15
+ /**
16
+ * For the passed HTML element, this helper finds the closest widget type around button ancestor.
17
+ */
18
+ export declare function getClosestTypeAroundDomButton(domElement: HTMLElement): HTMLElement | null;
19
+ /**
20
+ * For the passed widget type around button element, this helper determines at which position
21
+ * the paragraph would be inserted into the content if, for instance, the button was
22
+ * clicked by the user.
23
+ *
24
+ * @returns The position of the button.
25
+ */
26
+ export declare function getTypeAroundButtonPosition(domElement: HTMLElement): 'before' | 'after';
27
+ /**
28
+ * For the passed HTML element, this helper returns the closest view widget ancestor.
29
+ */
30
+ export declare function getClosestWidgetViewElement(domElement: HTMLElement, domConverter: DomConverter): ViewElement;
31
+ /**
32
+ * For the passed selection instance, it returns the position of the fake caret displayed next to a widget.
33
+ *
34
+ * **Note**: If the fake caret is not currently displayed, `null` is returned.
35
+ *
36
+ * @returns The position of the fake caret or `null` when none is present.
37
+ */
38
+ export declare function getTypeAroundFakeCaretPosition(selection: Selection | DocumentSelection): 'before' | 'after' | null;
@@ -0,0 +1,52 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module widget/widgettypearound/utils
7
+ */
8
+ import { isWidget } from '../utils.js';
9
+ /**
10
+ * The name of the type around model selection attribute responsible for
11
+ * displaying a fake caret next to a selected widget.
12
+ */
13
+ export const TYPE_AROUND_SELECTION_ATTRIBUTE = 'widget-type-around';
14
+ /**
15
+ * Checks if an element is a widget that qualifies to get the widget type around UI.
16
+ */
17
+ export function isTypeAroundWidget(viewElement, modelElement, schema) {
18
+ return !!viewElement && isWidget(viewElement) && !schema.isInline(modelElement);
19
+ }
20
+ /**
21
+ * For the passed HTML element, this helper finds the closest widget type around button ancestor.
22
+ */
23
+ export function getClosestTypeAroundDomButton(domElement) {
24
+ return domElement.closest('.ck-widget__type-around__button');
25
+ }
26
+ /**
27
+ * For the passed widget type around button element, this helper determines at which position
28
+ * the paragraph would be inserted into the content if, for instance, the button was
29
+ * clicked by the user.
30
+ *
31
+ * @returns The position of the button.
32
+ */
33
+ export function getTypeAroundButtonPosition(domElement) {
34
+ return domElement.classList.contains('ck-widget__type-around__button_before') ? 'before' : 'after';
35
+ }
36
+ /**
37
+ * For the passed HTML element, this helper returns the closest view widget ancestor.
38
+ */
39
+ export function getClosestWidgetViewElement(domElement, domConverter) {
40
+ const widgetDomElement = domElement.closest('.ck-widget');
41
+ return domConverter.mapDomToView(widgetDomElement);
42
+ }
43
+ /**
44
+ * For the passed selection instance, it returns the position of the fake caret displayed next to a widget.
45
+ *
46
+ * **Note**: If the fake caret is not currently displayed, `null` is returned.
47
+ *
48
+ * @returns The position of the fake caret or `null` when none is present.
49
+ */
50
+ export function getTypeAroundFakeCaretPosition(selection) {
51
+ return selection.getAttribute(TYPE_AROUND_SELECTION_ATTRIBUTE);
52
+ }
@@ -0,0 +1,233 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module widget/widgettypearound/widgettypearound
7
+ */
8
+ import { Plugin } from '@ckeditor/ckeditor5-core';
9
+ import { Enter } from '@ckeditor/ckeditor5-enter';
10
+ import { Delete } from '@ckeditor/ckeditor5-typing';
11
+ import '../../theme/widgettypearound.css';
12
+ /**
13
+ * A plugin that allows users to type around widgets where normally it is impossible to place the caret due
14
+ * to limitations of web browsers. These "tight spots" occur, for instance, before (or after) a widget being
15
+ * the first (or last) child of its parent or between two block widgets.
16
+ *
17
+ * This plugin extends the {@link module:widget/widget~Widget `Widget`} plugin and injects the user interface
18
+ * with two buttons into each widget instance in the editor. Each of the buttons can be clicked by the
19
+ * user if the widget is next to the "tight spot". Once clicked, a paragraph is created with the selection anchored
20
+ * in it so that users can type (or insert content, paste, etc.) straight away.
21
+ */
22
+ export default class WidgetTypeAround extends Plugin {
23
+ /**
24
+ * A reference to the model widget element that has the fake caret active
25
+ * on either side of it. It is later used to remove CSS classes associated with the fake caret
26
+ * when the widget no longer needs it.
27
+ */
28
+ private _currentFakeCaretModelElement;
29
+ /**
30
+ * @inheritDoc
31
+ */
32
+ static get pluginName(): "WidgetTypeAround";
33
+ /**
34
+ * @inheritDoc
35
+ */
36
+ static get isOfficialPlugin(): true;
37
+ /**
38
+ * @inheritDoc
39
+ */
40
+ static get requires(): readonly [typeof Enter, typeof Delete];
41
+ /**
42
+ * @inheritDoc
43
+ */
44
+ init(): void;
45
+ /**
46
+ * @inheritDoc
47
+ */
48
+ destroy(): void;
49
+ /**
50
+ * Inserts a new paragraph next to a widget element with the selection anchored in it.
51
+ *
52
+ * **Note**: This method is heavily user-oriented and will both focus the editing view and scroll
53
+ * the viewport to the selection in the inserted paragraph.
54
+ *
55
+ * @param widgetModelElement The model widget element next to which a paragraph is inserted.
56
+ * @param position The position where the paragraph is inserted. Either `'before'` or `'after'` the widget.
57
+ */
58
+ private _insertParagraph;
59
+ /**
60
+ * A wrapper for the {@link module:utils/emittermixin~Emitter#listenTo} method that executes the callbacks only
61
+ * when the plugin {@link #isEnabled is enabled}.
62
+ *
63
+ * @param emitter The object that fires the event.
64
+ * @param event The name of the event.
65
+ * @param callback The function to be called on event.
66
+ * @param options Additional options.
67
+ * @param options.priority The priority of this event callback. The higher the priority value the sooner
68
+ * the callback will be fired. Events having the same priority are called in the order they were added.
69
+ */
70
+ private _listenToIfEnabled;
71
+ /**
72
+ * Similar to {@link #_insertParagraph}, this method inserts a paragraph except that it
73
+ * does not expect a position. Instead, it performs the insertion next to a selected widget
74
+ * according to the `widget-type-around` model selection attribute value (fake caret position).
75
+ *
76
+ * Because this method requires the `widget-type-around` attribute to be set,
77
+ * the insertion can only happen when the widget's fake caret is active (e.g. activated
78
+ * using the keyboard).
79
+ *
80
+ * @returns Returns `true` when the paragraph was inserted (the attribute was present) and `false` otherwise.
81
+ */
82
+ private _insertParagraphAccordingToFakeCaretPosition;
83
+ /**
84
+ * Creates a listener in the editing conversion pipeline that injects the widget type around
85
+ * UI into every single widget instance created in the editor.
86
+ *
87
+ * The UI is delivered as a {@link module:engine/view/uielement~UIElement}
88
+ * wrapper which renders DOM buttons that users can use to insert paragraphs.
89
+ */
90
+ private _enableTypeAroundUIInjection;
91
+ /**
92
+ * Brings support for the fake caret that appears when either:
93
+ *
94
+ * * the selection moves to a widget from a position next to it using arrow keys,
95
+ * * the arrow key is pressed when the widget is already selected.
96
+ *
97
+ * The fake caret lets the user know that they can start typing or just press
98
+ * <kbd>Enter</kbd> to insert a paragraph at the position next to a widget as suggested by the fake caret.
99
+ *
100
+ * The fake caret disappears when the user changes the selection or the editor
101
+ * gets blurred.
102
+ *
103
+ * The whole idea is as follows:
104
+ *
105
+ * 1. A user does one of the 2 scenarios described at the beginning.
106
+ * 2. The "keydown" listener is executed and the decision is made whether to show or hide the fake caret.
107
+ * 3. If it should show up, the `widget-type-around` model selection attribute is set indicating
108
+ * on which side of the widget it should appear.
109
+ * 4. The selection dispatcher reacts to the selection attribute and sets CSS classes responsible for the
110
+ * fake caret on the view widget.
111
+ * 5. If the fake caret should disappear, the selection attribute is removed and the dispatcher
112
+ * does the CSS class clean-up in the view.
113
+ * 6. Additionally, `change:range` and `FocusTracker#isFocused` listeners also remove the selection
114
+ * attribute (the former also removes widget CSS classes).
115
+ */
116
+ private _enableTypeAroundFakeCaretActivationUsingKeyboardArrows;
117
+ /**
118
+ * A listener executed on each "keydown" in the view document, a part of
119
+ * {@link #_enableTypeAroundFakeCaretActivationUsingKeyboardArrows}.
120
+ *
121
+ * It decides whether the arrow keypress should activate the fake caret or not (also whether it should
122
+ * be deactivated).
123
+ *
124
+ * The fake caret activation is done by setting the `widget-type-around` model selection attribute
125
+ * in this listener, and stopping and preventing the event that would normally be handled by the widget
126
+ * plugin that is responsible for the regular keyboard navigation near/across all widgets (that
127
+ * includes inline widgets, which are ignored by the widget type around plugin).
128
+ */
129
+ private _handleArrowKeyPress;
130
+ /**
131
+ * Handles the keyboard navigation on "keydown" when a widget is currently selected and activates or deactivates
132
+ * the fake caret for that widget, depending on the current value of the `widget-type-around` model
133
+ * selection attribute and the direction of the pressed arrow key.
134
+ *
135
+ * @param isForward `true` when the pressed arrow key was responsible for the forward model selection movement
136
+ * as in {@link module:utils/keyboard~isForwardArrowKeyCode}.
137
+ * @returns Returns `true` when the keypress was handled and no other keydown listener of the editor should
138
+ * process the event any further. Returns `false` otherwise.
139
+ */
140
+ private _handleArrowKeyPressOnSelectedWidget;
141
+ /**
142
+ * Handles the keyboard navigation on "keydown" when **no** widget is selected but the selection is **directly** next
143
+ * to one and upon the fake caret should become active for this widget upon arrow keypress
144
+ * (AKA entering/selecting the widget).
145
+ *
146
+ * **Note**: This code mirrors the implementation from the widget plugin but also adds the selection attribute.
147
+ * Unfortunately, there is no safe way to let the widget plugin do the selection part first and then just set the
148
+ * selection attribute here in the widget type around plugin. This is why this code must duplicate some from the widget plugin.
149
+ *
150
+ * @param isForward `true` when the pressed arrow key was responsible for the forward model selection movement
151
+ * as in {@link module:utils/keyboard~isForwardArrowKeyCode}.
152
+ * @returns Returns `true` when the keypress was handled and no other keydown listener of the editor should
153
+ * process the event any further. Returns `false` otherwise.
154
+ */
155
+ private _handleArrowKeyPressWhenSelectionNextToAWidget;
156
+ /**
157
+ * Handles the keyboard navigation on "keydown" when a widget is currently selected (together with some other content)
158
+ * and the widget is the first or last element in the selection. It activates or deactivates the fake caret for that widget.
159
+ *
160
+ * @param isForward `true` when the pressed arrow key was responsible for the forward model selection movement
161
+ * as in {@link module:utils/keyboard~isForwardArrowKeyCode}.
162
+ * @returns Returns `true` when the keypress was handled and no other keydown listener of the editor should
163
+ * process the event any further. Returns `false` otherwise.
164
+ */
165
+ private _handleArrowKeyPressWhenNonCollapsedSelection;
166
+ /**
167
+ * Registers a `mousedown` listener for the view document which intercepts events
168
+ * coming from the widget type around UI, which happens when a user clicks one of the buttons
169
+ * that insert a paragraph next to a widget.
170
+ */
171
+ private _enableInsertingParagraphsOnButtonClick;
172
+ /**
173
+ * Creates the <kbd>Enter</kbd> key listener on the view document that allows the user to insert a paragraph
174
+ * near the widget when either:
175
+ *
176
+ * * The fake caret was first activated using the arrow keys,
177
+ * * The entire widget is selected in the model.
178
+ *
179
+ * In the first case, the new paragraph is inserted according to the `widget-type-around` selection
180
+ * attribute (see {@link #_handleArrowKeyPress}).
181
+ *
182
+ * In the second case, the new paragraph is inserted based on whether a soft (<kbd>Shift</kbd>+<kbd>Enter</kbd>) keystroke
183
+ * was pressed or not.
184
+ */
185
+ private _enableInsertingParagraphsOnEnterKeypress;
186
+ /**
187
+ * Similar to the {@link #_enableInsertingParagraphsOnEnterKeypress}, it allows the user
188
+ * to insert a paragraph next to a widget when the fake caret was activated using arrow
189
+ * keys but it responds to typing instead of <kbd>Enter</kbd>.
190
+ *
191
+ * Listener enabled by this method will insert a new paragraph according to the `widget-type-around`
192
+ * model selection attribute as the user simply starts typing, which creates the impression that the fake caret
193
+ * behaves like a real one rendered by the browser (AKA your text appears where the caret was).
194
+ *
195
+ * **Note**: At the moment this listener creates 2 undo steps: one for the `insertParagraph` command
196
+ * and another one for actual typing. It is not a disaster but this may need to be fixed
197
+ * sooner or later.
198
+ */
199
+ private _enableInsertingParagraphsOnTypingKeystroke;
200
+ /**
201
+ * It creates a "delete" event listener on the view document to handle cases when the <kbd>Delete</kbd> or <kbd>Backspace</kbd>
202
+ * is pressed and the fake caret is currently active.
203
+ *
204
+ * The fake caret should create an illusion of a real browser caret so that when it appears before or after
205
+ * a widget, pressing <kbd>Delete</kbd> or <kbd>Backspace</kbd> should remove a widget or delete the content
206
+ * before or after a widget (depending on the content surrounding the widget).
207
+ */
208
+ private _enableDeleteIntegration;
209
+ /**
210
+ * Attaches the {@link module:engine/model/model~Model#event:insertContent} event listener that, for instance, allows the user to paste
211
+ * content near a widget when the fake caret is first activated using the arrow keys.
212
+ *
213
+ * The content is inserted according to the `widget-type-around` selection attribute (see {@link #_handleArrowKeyPress}).
214
+ */
215
+ private _enableInsertContentIntegration;
216
+ /**
217
+ * Attaches the {@link module:engine/model/model~Model#event:insertObject} event listener that modifies the
218
+ * `options.findOptimalPosition`parameter to position of fake caret in relation to selected element
219
+ * to reflect user's intent of desired insertion position.
220
+ *
221
+ * The object is inserted according to the `widget-type-around` selection attribute (see {@link #_handleArrowKeyPress}).
222
+ */
223
+ private _enableInsertObjectIntegration;
224
+ /**
225
+ * Attaches the {@link module:engine/model/model~Model#event:deleteContent} event listener to block the event when the fake
226
+ * caret is active.
227
+ *
228
+ * This is required for cases that trigger {@link module:engine/model/model~Model#deleteContent `model.deleteContent()`}
229
+ * before calling {@link module:engine/model/model~Model#insertContent `model.insertContent()`} like, for instance,
230
+ * plain text pasting.
231
+ */
232
+ private _enableDeleteContentIntegration;
233
+ }