This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwidgetresize.js
162 lines (131 loc) · 4.09 KB
/
widgetresize.js
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
/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module widget/widgetresize
*/
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import Resizer from './widgetresize/resizer';
import DomEmitterMixin from '@ckeditor/ckeditor5-utils/src/dom/emittermixin';
import global from '@ckeditor/ckeditor5-utils/src/dom/global';
import { throttle } from 'lodash-es';
/**
* Widget resize feature plugin.
*
* Use the {@link module:widget/widgetresize~WidgetResize#attachTo} method to create a resizer for the specified widget.
*
* @extends module:core/plugin~Plugin
*/
export default class WidgetResize extends Plugin {
/**
* @inheritDoc
*/
static get pluginName() {
return 'WidgetResize';
}
init() {
this.resizers = [];
this.activeResizer = null;
const domDocument = global.window.document;
const THROTTLE_THRESHOLD = 16; // 16ms = ~60fps
this.editor.model.schema.setAttributeProperties( 'width', {
isFormatting: true
} );
this._observer = Object.create( DomEmitterMixin );
this._observer.listenTo( domDocument, 'mousedown', ( event, domEventData ) => {
if ( !Resizer.isResizeHandle( domEventData.target ) ) {
return;
}
const resizeHandle = domEventData.target;
this.activeResizer = this._getResizerByHandle( resizeHandle );
if ( this.activeResizer ) {
this.activeResizer.begin( resizeHandle );
}
} );
this._observer.listenTo( domDocument, 'mousemove', throttle( ( event, domEventData ) => {
if ( this.activeResizer ) {
this.activeResizer.updateSize( domEventData );
}
}, THROTTLE_THRESHOLD ) );
this._observer.listenTo( domDocument, 'mouseup', () => {
if ( this.activeResizer ) {
this.activeResizer.commit();
this.activeResizer = null;
}
} );
const redrawResizers = throttle( () => {
for ( const context of this.resizers ) {
context.redraw();
}
}, THROTTLE_THRESHOLD );
// Redrawing on any change of the UI of the editor (including content changes).
this.editor.ui.on( 'update', redrawResizers );
// Resizers need to be redrawn upon window resize, because new window might shrink resize host.
this._observer.listenTo( global.window, 'resize', redrawResizers );
}
destroy() {
this._observer.stopListening();
}
/**
* @param {module:widget/widgetresize~ResizerOptions} [options] Resizer options.
* @returns {module:widget/widgetresize/resizer~Resizer}
*/
attachTo( options ) {
const resizer = new Resizer( options );
resizer.attach();
this.editor.editing.view.once( 'render', () => resizer.redraw() );
this.resizers.push( resizer );
return resizer;
}
_getResizerByHandle( domResizeHandle ) {
for ( const resizer of this.resizers ) {
if ( resizer.containsHandle( domResizeHandle ) ) {
return resizer;
}
}
}
}
/**
* Interface describing a resizer. It allows to specify resizing host, custom logic for calculating aspect ratio etc.
*
* @interface ResizerOptions
*/
/**
* @member {module:engine/model/element~Element} module:widget/widgetresize~ResizerOptions#modelElement
*/
/**
* @member {module:engine/view/containerelement~ContainerElement} module:widget/widgetresize~ResizerOptions#viewElement
*/
/**
* @member {module:engine/view/downcastwriter~DowncastWriter} module:widget/widgetresize~ResizerOptions#downcastWriter
*/
/**
* A callback to be executed once resizing process is done.
*
* It receives a `Number` (`newValue`) as a parameter.
*
* For example, {@link module:image/imageresize~ImageResize} uses it to execute image resize command,
* which puts new value into the model.
*
* ```js
* {
* modelElement: data.item,
* viewElement: widget,
* downcastWriter: conversionApi.writer,
*
* onCommit( newValue ) {
* editor.execute( 'imageResize', { width: newValue } );
* }
* };
* ```
*
*
* @member {Function} module:widget/widgetresize~ResizerOptions#onCommit
*/
/**
* @member {Function} module:widget/widgetresize~ResizerOptions#getResizeHost
*/
/**
* @member {Function} module:widget/widgetresize~ResizerOptions#isCentered
*/