imageresize.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module image/image/imageresize
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import WidgetResizer from '@ckeditor/ckeditor5-widget/src/widgetresizer';
  10. /**
  11. * Image resize plugin.
  12. *
  13. * @extends module:core/plugin~Plugin
  14. */
  15. export default class ImageResize extends Plugin {
  16. /**
  17. * @inheritDoc
  18. */
  19. static get requires() {
  20. return [ WidgetResizer ];
  21. }
  22. /**
  23. * @inheritDoc
  24. */
  25. static get pluginName() {
  26. return 'ImageResize';
  27. }
  28. /**
  29. * @inheritDoc
  30. */
  31. init() {
  32. const editor = this.editor;
  33. editor.editing.downcastDispatcher.on( 'insert:image', ( evt, data, conversionApi ) => {
  34. const widget = conversionApi.mapper.toViewElement( data.item );
  35. editor.plugins.get( 'WidgetResizer' ).apply( widget, conversionApi.writer, {
  36. getResizeHost( wrapper ) {
  37. return wrapper.querySelector( 'img' );
  38. },
  39. getAspectRatio( resizeHost ) {
  40. return resizeHost.naturalWidth / resizeHost.naturalHeight;
  41. },
  42. isCentered( context ) {
  43. const imageStyle = context._getModel( editor, context.widgetWrapperElement ).getAttribute( 'imageStyle' );
  44. return !imageStyle || imageStyle == 'full';
  45. }
  46. } );
  47. }, {
  48. priority: 'low'
  49. } );
  50. }
  51. }