imagestyleui.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  6. import ImageStyleEditing from '../../src/imagestyle/imagestyleediting';
  7. import ImageStyleUI from '../../src/imagestyle/imagestyleui';
  8. import ImageEditing from '../../src/image/imageediting';
  9. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  10. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  11. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  12. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  13. describe( 'ImageStyleUI', () => {
  14. let editor, editorElement;
  15. const styles = [
  16. { name: 'style 1', title: 'Style 1 title', icon: 'style1-icon', isDefault: true },
  17. { name: 'style 2', title: 'Style 2 title', icon: 'style2-icon', cssClass: 'style2-class' },
  18. { name: 'style 3', title: 'Style 3 title', icon: 'style3-icon', cssClass: 'style3-class' }
  19. ];
  20. beforeEach( () => {
  21. editorElement = global.document.createElement( 'div' );
  22. global.document.body.appendChild( editorElement );
  23. return ClassicTestEditor
  24. .create( editorElement, {
  25. plugins: [ ImageEditing, ImageStyleEditing, ImageStyleUI ],
  26. image: {
  27. styles
  28. }
  29. } )
  30. .then( newEditor => {
  31. editor = newEditor;
  32. } );
  33. } );
  34. afterEach( () => {
  35. editorElement.remove();
  36. return editor.destroy();
  37. } );
  38. it( 'should be named', () => {
  39. expect( ImageStyleUI.pluginName ).to.equal( 'ImageStyleUI' );
  40. } );
  41. it( 'should register buttons for each style', () => {
  42. const spy = sinon.spy( editor, 'execute' );
  43. const command = editor.commands.get( 'imageStyle' );
  44. for ( const style of styles ) {
  45. const buttonView = editor.ui.componentFactory.create( `imageStyle:${ style.name }` );
  46. expect( buttonView ).to.be.instanceOf( ButtonView );
  47. expect( buttonView.label ).to.equal( style.title );
  48. expect( buttonView.icon ).to.equal( style.icon );
  49. expect( buttonView.isToggleable ).to.be.true;
  50. command.isEnabled = true;
  51. expect( buttonView.isEnabled ).to.be.true;
  52. command.isEnabled = false;
  53. expect( buttonView.isEnabled ).to.be.false;
  54. buttonView.fire( 'execute' );
  55. sinon.assert.calledWithExactly( editor.execute, 'imageStyle', { value: style.name } );
  56. spy.resetHistory();
  57. }
  58. } );
  59. it( 'should translate buttons if taken from default styles', () => {
  60. const editorElement = global.document.createElement( 'div' );
  61. global.document.body.appendChild( editorElement );
  62. class TranslationMock extends Plugin {
  63. init() {
  64. sinon.stub( this.editor, 't' ).returns( 'Default title' );
  65. }
  66. }
  67. return ClassicTestEditor
  68. .create( editorElement, {
  69. plugins: [ TranslationMock, ImageEditing, ImageStyleEditing, ImageStyleUI ],
  70. image: {
  71. styles: [
  72. { name: 'style 1', title: 'Side image', icon: 'style1-icon', isDefault: true }
  73. ]
  74. }
  75. } )
  76. .then( newEditor => {
  77. const buttonView = newEditor.ui.componentFactory.create( 'imageStyle:style 1' );
  78. expect( buttonView.label ).to.equal( 'Default title' );
  79. editorElement.remove();
  80. return newEditor.destroy();
  81. } );
  82. } );
  83. it( 'should not add buttons to image toolbar if configuration is present', () => {
  84. const editorElement = global.document.createElement( 'div' );
  85. global.document.body.appendChild( editorElement );
  86. return ClassicTestEditor
  87. .create( editorElement, {
  88. plugins: [ ImageEditing, ImageStyleEditing, ImageStyleUI ],
  89. image: {
  90. styles,
  91. toolbar: [ 'foo', 'bar' ]
  92. }
  93. } )
  94. .then( newEditor => {
  95. expect( newEditor.config.get( 'image.toolbar' ) ).to.deep.equal( [ 'foo', 'bar' ] );
  96. editorElement.remove();
  97. return newEditor.destroy();
  98. } );
  99. } );
  100. describe( 'localizedDefaultStylesTitles()', () => {
  101. it( 'should return localized titles of default styles', () => {
  102. return VirtualTestEditor
  103. .create( {
  104. plugins: [ ImageStyleUI ]
  105. } )
  106. .then( newEditor => {
  107. const plugin = newEditor.plugins.get( ImageStyleUI );
  108. expect( plugin.localizedDefaultStylesTitles ).to.deep.equal( {
  109. 'Full size image': 'Full size image',
  110. 'Side image': 'Side image',
  111. 'Left aligned image': 'Left aligned image',
  112. 'Centered image': 'Centered image',
  113. 'Right aligned image': 'Right aligned image'
  114. } );
  115. return newEditor.destroy();
  116. } );
  117. } );
  118. } );
  119. } );