imagestyle.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module image/imagestyle
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import ImageStyleEngine from './imagestyle/imagestyleengine';
  10. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  11. /**
  12. * The image style plugin.
  13. *
  14. * Uses {@link module:image/imagestyle/imagestyleengine~ImageStyleEngine}.
  15. *
  16. * @extends module:core/plugin~Plugin
  17. */
  18. export default class ImageStyle extends Plugin {
  19. /**
  20. * @inheritDoc
  21. */
  22. static get requires() {
  23. return [ ImageStyleEngine ];
  24. }
  25. /**
  26. * @inheritDoc
  27. */
  28. static get pluginName() {
  29. return 'image/imagestyle';
  30. }
  31. /**
  32. * @inheritDoc
  33. */
  34. init() {
  35. const styles = this.editor.config.get( 'image.styles' );
  36. for ( let style of styles ) {
  37. this._createButton( style );
  38. }
  39. }
  40. /**
  41. * Creates button for each style and stores it in editor's {@link module:ui/componentfactory~ComponentFactory ComponentFactory}.
  42. *
  43. * @private
  44. * @param {module:image/imagestyle/imagestyleengine~ImageStyleFormat} style
  45. */
  46. _createButton( style ) {
  47. const editor = this.editor;
  48. const command = editor.commands.get( style.name );
  49. editor.ui.componentFactory.add( style.name, ( locale ) => {
  50. const view = new ButtonView( locale );
  51. view.set( {
  52. label: style.title,
  53. icon: style.icon,
  54. tooltip: true
  55. } );
  56. view.bind( 'isEnabled' ).to( command, 'isEnabled' );
  57. view.bind( 'isOn' ).to( command, 'value' );
  58. this.listenTo( view, 'execute', () => editor.execute( style.name ) );
  59. return view;
  60. } );
  61. }
  62. }