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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from ckeditor/t/ckeditor5-image/241
Feature: Introduced image widget resizer. See ckeditor/ckeditor5-image#241.
- Loading branch information
Showing
5 changed files
with
983 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
*/ |
Oops, something went wrong.