imagestyleui.js 4.0 KB

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