imagetoolbar.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/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 initialize image.defaultToolbar to an empty array', () => {
  39. const editorElement = global.document.createElement( 'div' );
  40. global.document.body.appendChild( editorElement );
  41. return ClassicEditor.create( editorElement, {
  42. plugins: [ ImageToolbar ],
  43. } )
  44. .then( editor => {
  45. expect( editor.config.get( 'image.defaultToolbar' ) ).to.eql( [] );
  46. return editor.destroy();
  47. } );
  48. } );
  49. it( 'should not initialize if there is no configuration', () => {
  50. const editorElement = global.document.createElement( 'div' );
  51. global.document.body.appendChild( editorElement );
  52. return ClassicEditor.create( editorElement, {
  53. plugins: [ ImageToolbar ],
  54. } )
  55. .then( newEditor => {
  56. expect( newEditor.plugins.get( ImageToolbar )._panel ).to.be.undefined;
  57. newEditor.destroy();
  58. } );
  59. } );
  60. it( 'should allow other plugins to alter default config', () => {
  61. const editorElement = global.document.createElement( 'div' );
  62. global.document.body.appendChild( editorElement );
  63. return ClassicEditor.create( editorElement, {
  64. plugins: [ ImageToolbar, FakeButton, AlterDefaultConfig ]
  65. } )
  66. .then( newEditor => {
  67. const panel = newEditor.plugins.get( ImageToolbar )._panel;
  68. const toolbar = panel.content.get( 0 );
  69. const button = toolbar.items.get( 0 );
  70. expect( newEditor.config.get( 'image.defaultToolbar' ) ).to.eql( [ 'fake_button' ] );
  71. expect( button.label ).to.equal( 'fake button' );
  72. newEditor.destroy();
  73. } );
  74. } );
  75. it( 'should add ImageBalloonPanel to view body', () => {
  76. expect( panel ).to.be.instanceOf( ImageBalloonPanel );
  77. } );
  78. it( 'should show the panel when editor gains focus and image is selected', () => {
  79. setData( doc, '[<image src=""></image>]' );
  80. editor.ui.focusTracker.isFocused = false;
  81. const spy = sinon.spy( plugin, 'show' );
  82. editor.ui.focusTracker.isFocused = true;
  83. sinon.assert.calledOnce( spy );
  84. } );
  85. it( 'should not show the panel automatically when it is disabled', () => {
  86. plugin.isEnabled = false;
  87. setData( doc, '[<image src=""></image>]' );
  88. editor.ui.focusTracker.isFocused = true;
  89. const spy = sinon.spy( plugin, 'show' );
  90. editingView.render();
  91. sinon.assert.notCalled( spy );
  92. } );
  93. it( 'should not show the panel when editor looses focus', () => {
  94. editor.ui.focusTracker.isFocused = true;
  95. const spy = sinon.spy( plugin, 'show' );
  96. editor.ui.focusTracker.isFocused = false;
  97. sinon.assert.notCalled( spy );
  98. } );
  99. it( 'should detach panel with hide() method', () => {
  100. const spy = sinon.spy( panel, 'detach' );
  101. plugin.hide();
  102. sinon.assert.calledOnce( spy );
  103. } );
  104. // Plugin that adds fake_button to editor's component factory.
  105. class FakeButton extends Plugin {
  106. init() {
  107. this.editor.ui.componentFactory.add( 'fake_button', ( locale ) => {
  108. const view = new ButtonView( locale );
  109. view.set( {
  110. label: 'fake button'
  111. } );
  112. button = view;
  113. return view;
  114. } );
  115. }
  116. }
  117. class AlterDefaultConfig extends Plugin {
  118. init() {
  119. const defaultImageToolbarConfig = this.editor.config.get( 'image.defaultToolbar' );
  120. if ( defaultImageToolbarConfig ) {
  121. defaultImageToolbarConfig.push( 'fake_button' );
  122. }
  123. }
  124. }
  125. } );