imagetoolbar.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/image/ui/imageballoonpanelview';
  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 not initialize if there is no configuration', () => {
  39. const editorElement = global.document.createElement( 'div' );
  40. global.document.body.appendChild( editorElement );
  41. return ClassicEditor.create( editorElement, {
  42. plugins: [ ImageToolbar ],
  43. } )
  44. .then( newEditor => {
  45. expect( newEditor.plugins.get( ImageToolbar )._panel ).to.be.undefined;
  46. newEditor.destroy();
  47. } );
  48. } );
  49. it( 'should set css classes', () => {
  50. expect( panel.element.classList.contains( 'ck-toolbar-container' ) ).to.be.true;
  51. expect( panel.content.get( 0 ).element.classList.contains( 'ck-editor-toolbar' ) ).to.be.true;
  52. } );
  53. it( 'should add ImageBalloonPanel to view body', () => {
  54. expect( panel ).to.be.instanceOf( ImageBalloonPanel );
  55. } );
  56. it( 'should show the panel when editor gains focus and image is selected', () => {
  57. setData( doc, '[<image src=""></image>]' );
  58. editor.ui.focusTracker.isFocused = false;
  59. const spy = sinon.spy( plugin, 'show' );
  60. editor.ui.focusTracker.isFocused = true;
  61. sinon.assert.calledOnce( spy );
  62. } );
  63. it( 'should not show the panel automatically when it is disabled', () => {
  64. plugin.isEnabled = false;
  65. setData( doc, '[<image src=""></image>]' );
  66. editor.ui.focusTracker.isFocused = true;
  67. const spy = sinon.spy( plugin, 'show' );
  68. editingView.render();
  69. sinon.assert.notCalled( spy );
  70. } );
  71. it( 'should not show the panel when editor looses focus', () => {
  72. editor.ui.focusTracker.isFocused = true;
  73. const spy = sinon.spy( plugin, 'show' );
  74. editor.ui.focusTracker.isFocused = false;
  75. sinon.assert.notCalled( spy );
  76. } );
  77. it( 'should detach panel with hide() method', () => {
  78. const spy = sinon.spy( panel, 'detach' );
  79. plugin.hide();
  80. sinon.assert.calledOnce( spy );
  81. } );
  82. // Plugin that adds fake_button to editor's component factory.
  83. class FakeButton extends Plugin {
  84. init() {
  85. this.editor.ui.componentFactory.add( 'fake_button', ( locale ) => {
  86. const view = new ButtonView( locale );
  87. view.set( {
  88. label: 'fake button'
  89. } );
  90. button = view;
  91. return view;
  92. } );
  93. }
  94. }
  95. } );