8
0

imagestyleui.js 4.1 KB

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