@swnat/ckeditor-custom-build
Version:
The development environment of CKEditor 5 – the best browser-based rich text editor.
87 lines (72 loc) • 2.24 kB
JavaScript
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module image/imageresize/imageresizehandles
*/
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import WidgetResize from '@ckeditor/ckeditor5-widget/src/widgetresize';
/**
* The image resize by handles feature.
*
* It adds the ability to resize each image using handles or manually by
* {@link module:image/imageresize/imageresizebuttons~ImageResizeButtons} buttons.
*
* @extends module:core/plugin~Plugin
*/
export default class ImageResizeHandles extends Plugin {
/**
* @inheritDoc
*/
static get requires() {
return [ WidgetResize ];
}
/**
* @inheritDoc
*/
static get pluginName() {
return 'ImageResizeHandles';
}
/**
* @inheritDoc
*/
init() {
const editor = this.editor;
const command = editor.commands.get( 'imageResize' );
this.bind( 'isEnabled' ).to( command );
editor.editing.downcastDispatcher.on( 'insert:image', ( evt, data, conversionApi ) => {
const widget = conversionApi.mapper.toViewElement( data.item );
const resizer = editor.plugins
.get( WidgetResize )
.attachTo( {
unit: editor.config.get( 'image.resizeUnit' ),
modelElement: data.item,
viewElement: widget,
editor,
getHandleHost( domWidgetElement ) {
return domWidgetElement.querySelector( 'img' );
},
getResizeHost( domWidgetElement ) {
return domWidgetElement;
},
// TODO consider other positions.
isCentered() {
const imageStyle = data.item.getAttribute( 'imageStyle' );
return !imageStyle || imageStyle == 'full' || imageStyle == 'alignCenter';
},
onCommit( newValue ) {
editor.execute( 'imageResize', { width: newValue } );
}
} );
resizer.on( 'updateSize', () => {
if ( !widget.hasClass( 'image_resized' ) ) {
editor.editing.view.change( writer => {
writer.addClass( 'image_resized', widget );
} );
}
} );
resizer.bind( 'isEnabled' ).to( this );
}, { priority: 'low' } );
}
}