imagetoolbar.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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, 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.element.classList.contains( 'ck-editor-toolbar-container' ) ).to.be.true;
  52. expect( panel.content.get( 0 ).element.classList.contains( 'ck-editor-toolbar' ) ).to.be.true;
  53. } );
  54. it( 'should add ImageBalloonPanel to view body', () => {
  55. expect( panel ).to.be.instanceOf( ImageBalloonPanel );
  56. } );
  57. it( 'should show the panel when editor gains focus and image is selected', () => {
  58. setData( doc, '[<image src=""></image>]' );
  59. editor.ui.focusTracker.isFocused = false;
  60. const spy = sinon.spy( plugin, 'show' );
  61. editor.ui.focusTracker.isFocused = true;
  62. sinon.assert.calledOnce( spy );
  63. } );
  64. it( 'should not show the panel automatically when it is disabled', () => {
  65. plugin.isEnabled = false;
  66. setData( doc, '[<image src=""></image>]' );
  67. editor.ui.focusTracker.isFocused = true;
  68. const spy = sinon.spy( plugin, 'show' );
  69. editingView.render();
  70. sinon.assert.notCalled( spy );
  71. } );
  72. it( 'should not show the panel when editor looses focus', () => {
  73. editor.ui.focusTracker.isFocused = true;
  74. const spy = sinon.spy( plugin, 'show' );
  75. editor.ui.focusTracker.isFocused = false;
  76. sinon.assert.notCalled( spy );
  77. } );
  78. it( 'should detach panel with hide() method', () => {
  79. const spy = sinon.spy( panel, 'detach' );
  80. plugin.hide();
  81. sinon.assert.calledOnce( spy );
  82. } );
  83. // Plugin that adds fake_button to editor's component factory.
  84. class FakeButton extends Plugin {
  85. init() {
  86. this.editor.ui.componentFactory.add( 'fake_button', locale => {
  87. const view = new ButtonView( locale );
  88. view.set( {
  89. label: 'fake button'
  90. } );
  91. return view;
  92. } );
  93. }
  94. }
  95. } );