@ckeditor/ckeditor5-widget 39.0.2 → 40.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,188 +1,188 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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/widgetresize
7
- */
8
- import Resizer from './widgetresize/resizer';
9
- import { Plugin } from '@ckeditor/ckeditor5-core';
10
- import { MouseObserver } from '@ckeditor/ckeditor5-engine';
11
- import { DomEmitterMixin, global } from '@ckeditor/ckeditor5-utils';
12
- import { throttle } from 'lodash-es';
13
- import '../theme/widgetresize.css';
14
- /**
15
- * The widget resize feature plugin.
16
- *
17
- * Use the {@link module:widget/widgetresize~WidgetResize#attachTo} method to create a resizer for the specified widget.
18
- */
19
- export default class WidgetResize extends Plugin {
20
- constructor() {
21
- super(...arguments);
22
- /**
23
- * A map of resizers created using this plugin instance.
24
- */
25
- this._resizers = new Map();
26
- }
27
- /**
28
- * @inheritDoc
29
- */
30
- static get pluginName() {
31
- return 'WidgetResize';
32
- }
33
- /**
34
- * @inheritDoc
35
- */
36
- init() {
37
- const editing = this.editor.editing;
38
- const domDocument = global.window.document;
39
- this.set('selectedResizer', null);
40
- this.set('_activeResizer', null);
41
- editing.view.addObserver(MouseObserver);
42
- this._observer = new (DomEmitterMixin())();
43
- this.listenTo(editing.view.document, 'mousedown', this._mouseDownListener.bind(this), { priority: 'high' });
44
- this._observer.listenTo(domDocument, 'mousemove', this._mouseMoveListener.bind(this));
45
- this._observer.listenTo(domDocument, 'mouseup', this._mouseUpListener.bind(this));
46
- this._redrawSelectedResizerThrottled = throttle(() => this.redrawSelectedResizer(), 200);
47
- // Redrawing on any change of the UI of the editor (including content changes).
48
- this.editor.ui.on('update', this._redrawSelectedResizerThrottled);
49
- // Remove view widget-resizer mappings for widgets that have been removed from the document.
50
- // https://github.com/ckeditor/ckeditor5/issues/10156
51
- // https://github.com/ckeditor/ckeditor5/issues/10266
52
- this.editor.model.document.on('change', () => {
53
- for (const [viewElement, resizer] of this._resizers) {
54
- if (!viewElement.isAttached()) {
55
- this._resizers.delete(viewElement);
56
- resizer.destroy();
57
- }
58
- }
59
- }, { priority: 'lowest' });
60
- // Resizers need to be redrawn upon window resize, because new window might shrink resize host.
61
- this._observer.listenTo(global.window, 'resize', this._redrawSelectedResizerThrottled);
62
- const viewSelection = this.editor.editing.view.document.selection;
63
- viewSelection.on('change', () => {
64
- const selectedElement = viewSelection.getSelectedElement();
65
- const resizer = this.getResizerByViewElement(selectedElement) || null;
66
- if (resizer) {
67
- this.select(resizer);
68
- }
69
- else {
70
- this.deselect();
71
- }
72
- });
73
- }
74
- /**
75
- * Redraws the selected resizer if there is any selected resizer and if it is visible.
76
- */
77
- redrawSelectedResizer() {
78
- if (this.selectedResizer && this.selectedResizer.isVisible) {
79
- this.selectedResizer.redraw();
80
- }
81
- }
82
- /**
83
- * @inheritDoc
84
- */
85
- destroy() {
86
- super.destroy();
87
- this._observer.stopListening();
88
- for (const resizer of this._resizers.values()) {
89
- resizer.destroy();
90
- }
91
- this._redrawSelectedResizerThrottled.cancel();
92
- }
93
- /**
94
- * Marks resizer as selected.
95
- */
96
- select(resizer) {
97
- this.deselect();
98
- this.selectedResizer = resizer;
99
- this.selectedResizer.isSelected = true;
100
- }
101
- /**
102
- * Deselects currently set resizer.
103
- */
104
- deselect() {
105
- if (this.selectedResizer) {
106
- this.selectedResizer.isSelected = false;
107
- }
108
- this.selectedResizer = null;
109
- }
110
- /**
111
- * @param options Resizer options.
112
- */
113
- attachTo(options) {
114
- const resizer = new Resizer(options);
115
- const plugins = this.editor.plugins;
116
- resizer.attach();
117
- if (plugins.has('WidgetToolbarRepository')) {
118
- // Hiding widget toolbar to improve the performance
119
- // (https://github.com/ckeditor/ckeditor5-widget/pull/112#issuecomment-564528765).
120
- const widgetToolbarRepository = plugins.get('WidgetToolbarRepository');
121
- resizer.on('begin', () => {
122
- widgetToolbarRepository.forceDisabled('resize');
123
- }, { priority: 'lowest' });
124
- resizer.on('cancel', () => {
125
- widgetToolbarRepository.clearForceDisabled('resize');
126
- }, { priority: 'highest' });
127
- resizer.on('commit', () => {
128
- widgetToolbarRepository.clearForceDisabled('resize');
129
- }, { priority: 'highest' });
130
- }
131
- this._resizers.set(options.viewElement, resizer);
132
- const viewSelection = this.editor.editing.view.document.selection;
133
- const selectedElement = viewSelection.getSelectedElement();
134
- // If the element the resizer is created for is currently focused, it should become visible.
135
- if (this.getResizerByViewElement(selectedElement) == resizer) {
136
- this.select(resizer);
137
- }
138
- return resizer;
139
- }
140
- /**
141
- * Returns a resizer created for a given view element (widget element).
142
- *
143
- * @param viewElement View element associated with the resizer.
144
- */
145
- getResizerByViewElement(viewElement) {
146
- return this._resizers.get(viewElement);
147
- }
148
- /**
149
- * Returns a resizer that contains a given resize handle.
150
- */
151
- _getResizerByHandle(domResizeHandle) {
152
- for (const resizer of this._resizers.values()) {
153
- if (resizer.containsHandle(domResizeHandle)) {
154
- return resizer;
155
- }
156
- }
157
- }
158
- /**
159
- * @param domEventData Native DOM event.
160
- */
161
- _mouseDownListener(event, domEventData) {
162
- const resizeHandle = domEventData.domTarget;
163
- if (!Resizer.isResizeHandle(resizeHandle)) {
164
- return;
165
- }
166
- this._activeResizer = this._getResizerByHandle(resizeHandle) || null;
167
- if (this._activeResizer) {
168
- this._activeResizer.begin(resizeHandle);
169
- // Do not call other events when resizing. See: #6755.
170
- event.stop();
171
- domEventData.preventDefault();
172
- }
173
- }
174
- /**
175
- * @param domEventData Native DOM event.
176
- */
177
- _mouseMoveListener(event, domEventData) {
178
- if (this._activeResizer) {
179
- this._activeResizer.updateSize(domEventData);
180
- }
181
- }
182
- _mouseUpListener() {
183
- if (this._activeResizer) {
184
- this._activeResizer.commit();
185
- this._activeResizer = null;
186
- }
187
- }
188
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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/widgetresize
7
+ */
8
+ import Resizer from './widgetresize/resizer';
9
+ import { Plugin } from '@ckeditor/ckeditor5-core';
10
+ import { MouseObserver } from '@ckeditor/ckeditor5-engine';
11
+ import { DomEmitterMixin, global } from '@ckeditor/ckeditor5-utils';
12
+ import { throttle } from 'lodash-es';
13
+ import '../theme/widgetresize.css';
14
+ /**
15
+ * The widget resize feature plugin.
16
+ *
17
+ * Use the {@link module:widget/widgetresize~WidgetResize#attachTo} method to create a resizer for the specified widget.
18
+ */
19
+ export default class WidgetResize extends Plugin {
20
+ constructor() {
21
+ super(...arguments);
22
+ /**
23
+ * A map of resizers created using this plugin instance.
24
+ */
25
+ this._resizers = new Map();
26
+ }
27
+ /**
28
+ * @inheritDoc
29
+ */
30
+ static get pluginName() {
31
+ return 'WidgetResize';
32
+ }
33
+ /**
34
+ * @inheritDoc
35
+ */
36
+ init() {
37
+ const editing = this.editor.editing;
38
+ const domDocument = global.window.document;
39
+ this.set('selectedResizer', null);
40
+ this.set('_activeResizer', null);
41
+ editing.view.addObserver(MouseObserver);
42
+ this._observer = new (DomEmitterMixin())();
43
+ this.listenTo(editing.view.document, 'mousedown', this._mouseDownListener.bind(this), { priority: 'high' });
44
+ this._observer.listenTo(domDocument, 'mousemove', this._mouseMoveListener.bind(this));
45
+ this._observer.listenTo(domDocument, 'mouseup', this._mouseUpListener.bind(this));
46
+ this._redrawSelectedResizerThrottled = throttle(() => this.redrawSelectedResizer(), 200);
47
+ // Redrawing on any change of the UI of the editor (including content changes).
48
+ this.editor.ui.on('update', this._redrawSelectedResizerThrottled);
49
+ // Remove view widget-resizer mappings for widgets that have been removed from the document.
50
+ // https://github.com/ckeditor/ckeditor5/issues/10156
51
+ // https://github.com/ckeditor/ckeditor5/issues/10266
52
+ this.editor.model.document.on('change', () => {
53
+ for (const [viewElement, resizer] of this._resizers) {
54
+ if (!viewElement.isAttached()) {
55
+ this._resizers.delete(viewElement);
56
+ resizer.destroy();
57
+ }
58
+ }
59
+ }, { priority: 'lowest' });
60
+ // Resizers need to be redrawn upon window resize, because new window might shrink resize host.
61
+ this._observer.listenTo(global.window, 'resize', this._redrawSelectedResizerThrottled);
62
+ const viewSelection = this.editor.editing.view.document.selection;
63
+ viewSelection.on('change', () => {
64
+ const selectedElement = viewSelection.getSelectedElement();
65
+ const resizer = this.getResizerByViewElement(selectedElement) || null;
66
+ if (resizer) {
67
+ this.select(resizer);
68
+ }
69
+ else {
70
+ this.deselect();
71
+ }
72
+ });
73
+ }
74
+ /**
75
+ * Redraws the selected resizer if there is any selected resizer and if it is visible.
76
+ */
77
+ redrawSelectedResizer() {
78
+ if (this.selectedResizer && this.selectedResizer.isVisible) {
79
+ this.selectedResizer.redraw();
80
+ }
81
+ }
82
+ /**
83
+ * @inheritDoc
84
+ */
85
+ destroy() {
86
+ super.destroy();
87
+ this._observer.stopListening();
88
+ for (const resizer of this._resizers.values()) {
89
+ resizer.destroy();
90
+ }
91
+ this._redrawSelectedResizerThrottled.cancel();
92
+ }
93
+ /**
94
+ * Marks resizer as selected.
95
+ */
96
+ select(resizer) {
97
+ this.deselect();
98
+ this.selectedResizer = resizer;
99
+ this.selectedResizer.isSelected = true;
100
+ }
101
+ /**
102
+ * Deselects currently set resizer.
103
+ */
104
+ deselect() {
105
+ if (this.selectedResizer) {
106
+ this.selectedResizer.isSelected = false;
107
+ }
108
+ this.selectedResizer = null;
109
+ }
110
+ /**
111
+ * @param options Resizer options.
112
+ */
113
+ attachTo(options) {
114
+ const resizer = new Resizer(options);
115
+ const plugins = this.editor.plugins;
116
+ resizer.attach();
117
+ if (plugins.has('WidgetToolbarRepository')) {
118
+ // Hiding widget toolbar to improve the performance
119
+ // (https://github.com/ckeditor/ckeditor5-widget/pull/112#issuecomment-564528765).
120
+ const widgetToolbarRepository = plugins.get('WidgetToolbarRepository');
121
+ resizer.on('begin', () => {
122
+ widgetToolbarRepository.forceDisabled('resize');
123
+ }, { priority: 'lowest' });
124
+ resizer.on('cancel', () => {
125
+ widgetToolbarRepository.clearForceDisabled('resize');
126
+ }, { priority: 'highest' });
127
+ resizer.on('commit', () => {
128
+ widgetToolbarRepository.clearForceDisabled('resize');
129
+ }, { priority: 'highest' });
130
+ }
131
+ this._resizers.set(options.viewElement, resizer);
132
+ const viewSelection = this.editor.editing.view.document.selection;
133
+ const selectedElement = viewSelection.getSelectedElement();
134
+ // If the element the resizer is created for is currently focused, it should become visible.
135
+ if (this.getResizerByViewElement(selectedElement) == resizer) {
136
+ this.select(resizer);
137
+ }
138
+ return resizer;
139
+ }
140
+ /**
141
+ * Returns a resizer created for a given view element (widget element).
142
+ *
143
+ * @param viewElement View element associated with the resizer.
144
+ */
145
+ getResizerByViewElement(viewElement) {
146
+ return this._resizers.get(viewElement);
147
+ }
148
+ /**
149
+ * Returns a resizer that contains a given resize handle.
150
+ */
151
+ _getResizerByHandle(domResizeHandle) {
152
+ for (const resizer of this._resizers.values()) {
153
+ if (resizer.containsHandle(domResizeHandle)) {
154
+ return resizer;
155
+ }
156
+ }
157
+ }
158
+ /**
159
+ * @param domEventData Native DOM event.
160
+ */
161
+ _mouseDownListener(event, domEventData) {
162
+ const resizeHandle = domEventData.domTarget;
163
+ if (!Resizer.isResizeHandle(resizeHandle)) {
164
+ return;
165
+ }
166
+ this._activeResizer = this._getResizerByHandle(resizeHandle) || null;
167
+ if (this._activeResizer) {
168
+ this._activeResizer.begin(resizeHandle);
169
+ // Do not call other events when resizing. See: #6755.
170
+ event.stop();
171
+ domEventData.preventDefault();
172
+ }
173
+ }
174
+ /**
175
+ * @param domEventData Native DOM event.
176
+ */
177
+ _mouseMoveListener(event, domEventData) {
178
+ if (this._activeResizer) {
179
+ this._activeResizer.updateSize(domEventData);
180
+ }
181
+ }
182
+ _mouseUpListener() {
183
+ if (this._activeResizer) {
184
+ this._activeResizer.commit();
185
+ this._activeResizer = null;
186
+ }
187
+ }
188
+ }
@@ -1,94 +1,94 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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, type ToolbarConfigItem } from '@ckeditor/ckeditor5-core';
9
- import type { ViewDocumentSelection, ViewElement } from '@ckeditor/ckeditor5-engine';
10
- import { ContextualBalloon } from '@ckeditor/ckeditor5-ui';
11
- /**
12
- * Widget toolbar repository plugin. A central point for registering widget toolbars. This plugin handles the whole
13
- * toolbar rendering process and exposes a concise API.
14
- *
15
- * To add a toolbar for your widget use the {@link ~WidgetToolbarRepository#register `WidgetToolbarRepository#register()`} method.
16
- *
17
- * The following example comes from the {@link module:image/imagetoolbar~ImageToolbar} plugin:
18
- *
19
- * ```ts
20
- * class ImageToolbar extends Plugin {
21
- * static get requires() {
22
- * return [ WidgetToolbarRepository ];
23
- * }
24
- *
25
- * afterInit() {
26
- * const editor = this.editor;
27
- * const widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
28
- *
29
- * widgetToolbarRepository.register( 'image', {
30
- * items: editor.config.get( 'image.toolbar' ),
31
- * getRelatedElement: getClosestSelectedImageWidget
32
- * } );
33
- * }
34
- * }
35
- * ```
36
- */
37
- export default class WidgetToolbarRepository extends Plugin {
38
- /**
39
- * A map of toolbar definitions.
40
- */
41
- private _toolbarDefinitions;
42
- private _balloon;
43
- /**
44
- * @inheritDoc
45
- */
46
- static get requires(): readonly [typeof ContextualBalloon];
47
- /**
48
- * @inheritDoc
49
- */
50
- static get pluginName(): "WidgetToolbarRepository";
51
- /**
52
- * @inheritDoc
53
- */
54
- init(): void;
55
- destroy(): void;
56
- /**
57
- * Registers toolbar in the WidgetToolbarRepository. It renders it in the `ContextualBalloon` based on the value of the invoked
58
- * `getRelatedElement` function. Toolbar items are gathered from `items` array.
59
- * The balloon's CSS class is by default `ck-toolbar-container` and may be override with the `balloonClassName` option.
60
- *
61
- * Note: This method should be called in the {@link module:core/plugin~PluginInterface#afterInit `Plugin#afterInit()`}
62
- * callback (or later) to make sure that the given toolbar items were already registered by other plugins.
63
- *
64
- * @param toolbarId An id for the toolbar. Used to
65
- * @param options.ariaLabel Label used by assistive technologies to describe this toolbar element.
66
- * @param options.items Array of toolbar items.
67
- * @param options.getRelatedElement Callback which returns an element the toolbar should be attached to.
68
- * @param options.balloonClassName CSS class for the widget balloon.
69
- */
70
- register(toolbarId: string, { ariaLabel, items, getRelatedElement, balloonClassName }: {
71
- ariaLabel?: string;
72
- items: Array<ToolbarConfigItem>;
73
- getRelatedElement: (selection: ViewDocumentSelection) => (ViewElement | null);
74
- balloonClassName?: string;
75
- }): void;
76
- /**
77
- * Iterates over stored toolbars and makes them visible or hidden.
78
- */
79
- private _updateToolbarsVisibility;
80
- /**
81
- * Hides the given toolbar.
82
- */
83
- private _hideToolbar;
84
- /**
85
- * Shows up the toolbar if the toolbar is not visible.
86
- * Otherwise, repositions the toolbar's balloon when toolbar's view is the most top view in balloon stack.
87
- *
88
- * It might happen here that the toolbar's view is under another view. Then do nothing as the other toolbar view
89
- * should be still visible after the {@link module:ui/editorui/editorui~EditorUI#event:update}.
90
- */
91
- private _showToolbar;
92
- private _isToolbarVisible;
93
- private _isToolbarInBalloon;
94
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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, type ToolbarConfigItem } from '@ckeditor/ckeditor5-core';
9
+ import type { ViewDocumentSelection, ViewElement } from '@ckeditor/ckeditor5-engine';
10
+ import { ContextualBalloon } from '@ckeditor/ckeditor5-ui';
11
+ /**
12
+ * Widget toolbar repository plugin. A central point for registering widget toolbars. This plugin handles the whole
13
+ * toolbar rendering process and exposes a concise API.
14
+ *
15
+ * To add a toolbar for your widget use the {@link ~WidgetToolbarRepository#register `WidgetToolbarRepository#register()`} method.
16
+ *
17
+ * The following example comes from the {@link module:image/imagetoolbar~ImageToolbar} plugin:
18
+ *
19
+ * ```ts
20
+ * class ImageToolbar extends Plugin {
21
+ * static get requires() {
22
+ * return [ WidgetToolbarRepository ];
23
+ * }
24
+ *
25
+ * afterInit() {
26
+ * const editor = this.editor;
27
+ * const widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
28
+ *
29
+ * widgetToolbarRepository.register( 'image', {
30
+ * items: editor.config.get( 'image.toolbar' ),
31
+ * getRelatedElement: getClosestSelectedImageWidget
32
+ * } );
33
+ * }
34
+ * }
35
+ * ```
36
+ */
37
+ export default class WidgetToolbarRepository extends Plugin {
38
+ /**
39
+ * A map of toolbar definitions.
40
+ */
41
+ private _toolbarDefinitions;
42
+ private _balloon;
43
+ /**
44
+ * @inheritDoc
45
+ */
46
+ static get requires(): readonly [typeof ContextualBalloon];
47
+ /**
48
+ * @inheritDoc
49
+ */
50
+ static get pluginName(): "WidgetToolbarRepository";
51
+ /**
52
+ * @inheritDoc
53
+ */
54
+ init(): void;
55
+ destroy(): void;
56
+ /**
57
+ * Registers toolbar in the WidgetToolbarRepository. It renders it in the `ContextualBalloon` based on the value of the invoked
58
+ * `getRelatedElement` function. Toolbar items are gathered from `items` array.
59
+ * The balloon's CSS class is by default `ck-toolbar-container` and may be override with the `balloonClassName` option.
60
+ *
61
+ * Note: This method should be called in the {@link module:core/plugin~PluginInterface#afterInit `Plugin#afterInit()`}
62
+ * callback (or later) to make sure that the given toolbar items were already registered by other plugins.
63
+ *
64
+ * @param toolbarId An id for the toolbar. Used to
65
+ * @param options.ariaLabel Label used by assistive technologies to describe this toolbar element.
66
+ * @param options.items Array of toolbar items.
67
+ * @param options.getRelatedElement Callback which returns an element the toolbar should be attached to.
68
+ * @param options.balloonClassName CSS class for the widget balloon.
69
+ */
70
+ register(toolbarId: string, { ariaLabel, items, getRelatedElement, balloonClassName }: {
71
+ ariaLabel?: string;
72
+ items: Array<ToolbarConfigItem>;
73
+ getRelatedElement: (selection: ViewDocumentSelection) => (ViewElement | null);
74
+ balloonClassName?: string;
75
+ }): void;
76
+ /**
77
+ * Iterates over stored toolbars and makes them visible or hidden.
78
+ */
79
+ private _updateToolbarsVisibility;
80
+ /**
81
+ * Hides the given toolbar.
82
+ */
83
+ private _hideToolbar;
84
+ /**
85
+ * Shows up the toolbar if the toolbar is not visible.
86
+ * Otherwise, repositions the toolbar's balloon when toolbar's view is the most top view in balloon stack.
87
+ *
88
+ * It might happen here that the toolbar's view is under another view. Then do nothing as the other toolbar view
89
+ * should be still visible after the {@link module:ui/editorui/editorui~EditorUI#event:update}.
90
+ */
91
+ private _showToolbar;
92
+ private _isToolbarVisible;
93
+ private _isToolbarInBalloon;
94
+ }