imagestyleui.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. 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;
  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. const 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. return editor.destroy();
  36. } );
  37. it( 'should be named', () => {
  38. expect( ImageStyleUI.pluginName ).to.equal( 'ImageStyleUI' );
  39. } );
  40. it( 'should register buttons for each style', () => {
  41. const spy = sinon.spy( editor, 'execute' );
  42. const command = editor.commands.get( 'imageStyle' );
  43. for ( const style of styles ) {
  44. const buttonView = editor.ui.componentFactory.create( `imageStyle:${ style.name }` );
  45. expect( buttonView ).to.be.instanceOf( ButtonView );
  46. expect( buttonView.label ).to.equal( style.title );
  47. expect( buttonView.icon ).to.equal( style.icon );
  48. command.isEnabled = true;
  49. expect( buttonView.isEnabled ).to.be.true;
  50. command.isEnabled = false;
  51. expect( buttonView.isEnabled ).to.be.false;
  52. buttonView.fire( 'execute' );
  53. sinon.assert.calledWithExactly( editor.execute, 'imageStyle', { value: style.name } );
  54. spy.resetHistory();
  55. }
  56. } );
  57. it( 'should translate buttons if taken from default styles', () => {
  58. const editorElement = global.document.createElement( 'div' );
  59. global.document.body.appendChild( editorElement );
  60. class TranslationMock extends Plugin {
  61. init() {
  62. sinon.stub( this.editor, 't' ).returns( 'Default title' );
  63. }
  64. }
  65. return ClassicTestEditor
  66. .create( editorElement, {
  67. plugins: [ TranslationMock, ImageEditing, ImageStyleEditing, ImageStyleUI ],
  68. image: {
  69. styles: [
  70. { name: 'style 1', title: 'Side image', icon: 'style1-icon', isDefault: true }
  71. ]
  72. }
  73. } )
  74. .then( newEditor => {
  75. editor = newEditor;
  76. const buttonView = editor.ui.componentFactory.create( 'imageStyle:style 1' );
  77. expect( buttonView.label ).to.equal( 'Default title' );
  78. } );
  79. } );
  80. it( 'should not add buttons to image toolbar if configuration is present', () => {
  81. const editorElement = global.document.createElement( 'div' );
  82. global.document.body.appendChild( editorElement );
  83. return ClassicTestEditor
  84. .create( editorElement, {
  85. plugins: [ ImageEditing, ImageStyleEditing, ImageStyleUI ],
  86. image: {
  87. styles,
  88. toolbar: [ 'foo', 'bar' ]
  89. }
  90. } )
  91. .then( newEditor => {
  92. expect( newEditor.config.get( 'image.toolbar' ) ).to.deep.equal( [ 'foo', 'bar' ] );
  93. newEditor.destroy();
  94. } );
  95. } );
  96. describe( 'localizedDefaultStylesTitles()', () => {
  97. it( 'should return localized titles of default styles', () => {
  98. return VirtualTestEditor
  99. .create( {
  100. plugins: [ ImageStyleUI ]
  101. } )
  102. .then( newEditor => {
  103. editor = newEditor;
  104. const plugin = editor.plugins.get( ImageStyleUI );
  105. expect( plugin.localizedDefaultStylesTitles ).to.deep.equal( {
  106. 'Full size image': 'Full size image',
  107. 'Side image': 'Side image',
  108. 'Left aligned image': 'Left aligned image',
  109. 'Centered image': 'Centered image',
  110. 'Right aligned image': 'Right aligned image'
  111. } );
  112. } );
  113. } );
  114. } );
  115. } );