imagetoolbar.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classic';
  6. import ImageToolbar from '../src/imagetoolbar';
  7. import Image from '../src/image';
  8. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  9. import ImageBalloonPanel from '../src/ui/imageballoonpanel';
  10. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  11. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  12. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  13. describe( 'ImageToolbar', () => {
  14. let editor, button, editingView, doc, panel, plugin;
  15. beforeEach( () => {
  16. const editorElement = global.document.createElement( 'div' );
  17. global.document.body.appendChild( editorElement );
  18. return ClassicEditor.create( editorElement, {
  19. plugins: [ Image, ImageToolbar, FakeButton ],
  20. image: {
  21. toolbar: [ 'fake_button' ]
  22. }
  23. } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. editingView = editor.editing.view;
  27. doc = editor.document;
  28. plugin = editor.plugins.get( ImageToolbar );
  29. panel = plugin._panel;
  30. } );
  31. } );
  32. afterEach( () => {
  33. return editor.destroy();
  34. } );
  35. it( 'should be loaded', () => {
  36. expect( editor.plugins.get( ImageToolbar ) ).to.be.instanceOf( ImageToolbar );
  37. } );
  38. it( 'should initialize image.defaultToolbar to an empty array', () => {
  39. expect( editor.config.get( 'image.defaultToolbar' ) ).to.eql( [] );
  40. } );
  41. it( 'should not initialize if there is no configuration', () => {
  42. const editorElement = global.document.createElement( 'div' );
  43. global.document.body.appendChild( editorElement );
  44. return ClassicEditor.create( editorElement, {
  45. plugins: [ ImageToolbar ],
  46. } )
  47. .then( newEditor => {
  48. expect( newEditor.plugins.get( ImageToolbar )._panel ).to.be.undefined;
  49. newEditor.destroy();
  50. } );
  51. } );
  52. it( 'should allow other plugins to alter default config', () => {
  53. const editorElement = global.document.createElement( 'div' );
  54. global.document.body.appendChild( editorElement );
  55. return ClassicEditor.create( editorElement, {
  56. plugins: [ ImageToolbar, FakeButton, AlterDefaultConfig ]
  57. } )
  58. .then( newEditor => {
  59. const panel = newEditor.plugins.get( ImageToolbar )._panel;
  60. const toolbar = panel.content.get( 0 );
  61. const button = toolbar.items.get( 0 );
  62. expect( newEditor.config.get( 'image.defaultToolbar' ) ).to.eql( [ 'fake_button' ] );
  63. expect( button.label ).to.equal( 'fake button' );
  64. newEditor.destroy();
  65. } );
  66. } );
  67. it( 'should add ImageBalloonPanel to view body', () => {
  68. expect( panel ).to.be.instanceOf( ImageBalloonPanel );
  69. } );
  70. it( 'should show the panel when editor gains focus and image is selected', () => {
  71. setData( doc, '[<image src=""></image>]' );
  72. editor.ui.focusTracker.isFocused = false;
  73. const spy = sinon.spy( plugin, 'show' );
  74. editor.ui.focusTracker.isFocused = true;
  75. sinon.assert.calledOnce( spy );
  76. } );
  77. it( 'should not show the panel when editor looses focus', () => {
  78. editor.ui.focusTracker.isFocused = true;
  79. const spy = sinon.spy( plugin, 'show' );
  80. editor.ui.focusTracker.isFocused = false;
  81. sinon.assert.notCalled( spy );
  82. } );
  83. it( 'should detach panel with hide() method', () => {
  84. const spy = sinon.spy( panel, 'detach' );
  85. plugin.hide();
  86. sinon.assert.calledOnce( spy );
  87. } );
  88. // Plugin that adds fake_button to editor's component factory.
  89. class FakeButton extends Plugin {
  90. init() {
  91. this.editor.ui.componentFactory.add( 'fake_button', ( locale ) => {
  92. const view = new ButtonView( locale );
  93. view.set( {
  94. label: 'fake button'
  95. } );
  96. button = view;
  97. return view;
  98. } );
  99. }
  100. }
  101. class AlterDefaultConfig extends Plugin {
  102. init() {
  103. const defaultImageToolbarConfig = this.editor.config.get( 'image.defaultToolbar' );
  104. if ( defaultImageToolbarConfig ) {
  105. defaultImageToolbarConfig.push( 'fake_button' );
  106. }
  107. }
  108. }
  109. } );