-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathwidgetresize.ts
331 lines (267 loc) · 8.48 KB
/
widgetresize.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module widget/widgetresize
*/
import Resizer, {
type ResizerBeginEvent,
type ResizerCancelEvent,
type ResizerCommitEvent
} from './widgetresize/resizer.js';
import type WidgetToolbarRepository from './widgettoolbarrepository.js';
import {
Plugin,
type Editor
} from '@ckeditor/ckeditor5-core';
import {
MouseObserver,
type DocumentChangeEvent,
type DomEventData,
type Element,
type ViewContainerElement,
type ViewDocumentMouseDownEvent,
type ViewSelectionChangeEvent
} from '@ckeditor/ckeditor5-engine';
import type { EditorUIUpdateEvent } from '@ckeditor/ckeditor5-ui';
import {
DomEmitterMixin,
global,
type DomEmitter,
type EventInfo
} from '@ckeditor/ckeditor5-utils';
import { throttle, type DebouncedFunc } from 'lodash-es';
import '../theme/widgetresize.css';
/**
* The widget resize feature plugin.
*
* Use the {@link module:widget/widgetresize~WidgetResize#attachTo} method to create a resizer for the specified widget.
*/
export default class WidgetResize extends Plugin {
/**
* The currently selected resizer.
*
* @observable
*/
declare public selectedResizer: Resizer | null;
/**
* References an active resizer.
*
* Active resizer means a resizer which handle is actively used by the end user.
*
* @internal
* @observable
*/
declare public _activeResizer: Resizer | null;
/**
* A map of resizers created using this plugin instance.
*/
private _resizers = new Map<ViewContainerElement, Resizer>();
private _observer!: DomEmitter;
private _redrawSelectedResizerThrottled!: DebouncedFunc<() => void>;
/**
* @inheritDoc
*/
public static get pluginName() {
return 'WidgetResize' as const;
}
/**
* @inheritDoc
*/
public init(): void {
const editing = this.editor.editing;
const domDocument = global.window.document;
this.set( 'selectedResizer', null );
this.set( '_activeResizer', null );
editing.view.addObserver( MouseObserver );
this._observer = new ( DomEmitterMixin() )();
this.listenTo<ViewDocumentMouseDownEvent>(
editing.view.document,
'mousedown',
this._mouseDownListener.bind( this ),
{ priority: 'high' }
);
this._observer.listenTo( domDocument, 'mousemove', this._mouseMoveListener.bind( this ) );
this._observer.listenTo( domDocument, 'mouseup', this._mouseUpListener.bind( this ) );
this._redrawSelectedResizerThrottled = throttle( () => this.redrawSelectedResizer(), 200 );
// Redrawing on any change of the UI of the editor (including content changes).
this.editor.ui.on<EditorUIUpdateEvent>( 'update', this._redrawSelectedResizerThrottled );
// Remove view widget-resizer mappings for widgets that have been removed from the document.
// https://github.com/ckeditor/ckeditor5/issues/10156
// https://github.com/ckeditor/ckeditor5/issues/10266
this.editor.model.document.on<DocumentChangeEvent>( 'change', () => {
for ( const [ viewElement, resizer ] of this._resizers ) {
if ( !viewElement.isAttached() ) {
this._resizers.delete( viewElement );
resizer.destroy();
}
}
}, { priority: 'lowest' } );
// Resizers need to be redrawn upon window resize, because new window might shrink resize host.
this._observer.listenTo( global.window, 'resize', this._redrawSelectedResizerThrottled );
const viewSelection = this.editor.editing.view.document.selection;
viewSelection.on<ViewSelectionChangeEvent>( 'change', () => {
const selectedElement = viewSelection.getSelectedElement() as ViewContainerElement;
const resizer = this.getResizerByViewElement( selectedElement ) || null;
if ( resizer ) {
this.select( resizer );
} else {
this.deselect();
}
} );
}
/**
* Redraws the selected resizer if there is any selected resizer and if it is visible.
*/
public redrawSelectedResizer(): void {
if ( this.selectedResizer && this.selectedResizer.isVisible ) {
this.selectedResizer.redraw();
}
}
/**
* @inheritDoc
*/
public override destroy(): void {
super.destroy();
this._observer.stopListening();
for ( const resizer of this._resizers.values() ) {
resizer.destroy();
}
this._redrawSelectedResizerThrottled.cancel();
}
/**
* Marks resizer as selected.
*/
public select( resizer: Resizer ): void {
this.deselect();
this.selectedResizer = resizer;
this.selectedResizer.isSelected = true;
}
/**
* Deselects currently set resizer.
*/
public deselect(): void {
if ( this.selectedResizer ) {
this.selectedResizer.isSelected = false;
}
this.selectedResizer = null;
}
/**
* @param options Resizer options.
*/
public attachTo( options: ResizerOptions ): Resizer {
const resizer = new Resizer( options );
const plugins = this.editor.plugins;
resizer.attach();
if ( plugins.has( 'WidgetToolbarRepository' ) ) {
// Hiding widget toolbar to improve the performance
// (https://github.com/ckeditor/ckeditor5-widget/pull/112#issuecomment-564528765).
const widgetToolbarRepository: WidgetToolbarRepository = plugins.get( 'WidgetToolbarRepository' );
resizer.on<ResizerBeginEvent>( 'begin', () => {
widgetToolbarRepository.forceDisabled( 'resize' );
}, { priority: 'lowest' } );
resizer.on<ResizerCancelEvent>( 'cancel', () => {
widgetToolbarRepository.clearForceDisabled( 'resize' );
}, { priority: 'highest' } );
resizer.on<ResizerCommitEvent>( 'commit', () => {
widgetToolbarRepository.clearForceDisabled( 'resize' );
}, { priority: 'highest' } );
}
this._resizers.set( options.viewElement, resizer );
const viewSelection = this.editor.editing.view.document.selection;
const selectedElement = viewSelection.getSelectedElement() as ViewContainerElement;
// If the element the resizer is created for is currently focused, it should become visible.
if ( this.getResizerByViewElement( selectedElement ) == resizer ) {
this.select( resizer );
}
return resizer;
}
/**
* Returns a resizer created for a given view element (widget element).
*
* @param viewElement View element associated with the resizer.
*/
public getResizerByViewElement( viewElement: ViewContainerElement ): Resizer | undefined {
return this._resizers.get( viewElement );
}
/**
* Returns a resizer that contains a given resize handle.
*/
private _getResizerByHandle( domResizeHandle: HTMLElement ): Resizer | undefined {
for ( const resizer of this._resizers.values() ) {
if ( resizer.containsHandle( domResizeHandle ) ) {
return resizer;
}
}
}
/**
* @param domEventData Native DOM event.
*/
private _mouseDownListener( event: EventInfo, domEventData: DomEventData ) {
const resizeHandle = domEventData.domTarget;
if ( !Resizer.isResizeHandle( resizeHandle ) ) {
return;
}
this._activeResizer = this._getResizerByHandle( resizeHandle ) || null;
if ( this._activeResizer ) {
this._activeResizer.begin( resizeHandle );
// Do not call other events when resizing. See: #6755.
event.stop();
domEventData.preventDefault();
}
}
/**
* @param domEventData Native DOM event.
*/
private _mouseMoveListener( event: unknown, domEventData: MouseEvent ) {
if ( this._activeResizer ) {
this._activeResizer.updateSize( domEventData );
}
}
private _mouseUpListener(): void {
if ( this._activeResizer ) {
this._activeResizer.commit();
this._activeResizer = null;
}
}
}
/**
* Interface describing a resizer. It allows to specify the resizing host, custom logic for calculating aspect ratio, etc.
*/
export interface ResizerOptions {
/**
* Editor instance associated with the resizer.
*/
editor: Editor;
modelElement: Element;
/**
* A view of an element to be resized. Typically it's the main widget's view instance.
*/
viewElement: ViewContainerElement;
unit?: 'px' | '%';
/**
* A callback to be executed once the resizing process is done.
*
* It receives a `Number` (`newValue`) as a parameter.
*
* For example, {@link module:image/imageresize~ImageResize} uses it to execute the resize image command
* which puts the new value into the model.
*
* ```ts
* {
* editor,
* modelElement: data.item,
* viewElement: widget,
*
* onCommit( newValue ) {
* editor.execute( 'resizeImage', { width: newValue } );
* }
* };
* ```
*/
onCommit: ( newValue: string ) => void;
getResizeHost: ( widgetWrapper: HTMLElement ) => HTMLElement;
getHandleHost: ( widgetWrapper: HTMLElement ) => HTMLElement;
isCentered?: ( resizer: Resizer ) => boolean;
}