8
0

imagetoolbar.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/classiceditor';
  6. import ImageToolbar from '../src/imagetoolbar';
  7. import Image from '../src/image';
  8. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  9. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  10. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  11. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  12. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  13. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. describe( 'ImageToolbar', () => {
  15. let editor, doc, editingView, plugin, toolbar, imageBalloon, editorElement;
  16. beforeEach( () => {
  17. editorElement = global.document.createElement( 'div' );
  18. global.document.body.appendChild( editorElement );
  19. return ClassicEditor.create( editorElement, {
  20. plugins: [ Paragraph, Image, ImageToolbar, FakeButton ],
  21. image: {
  22. toolbar: [ 'fake_button' ]
  23. }
  24. } )
  25. .then( newEditor => {
  26. editor = newEditor;
  27. doc = editor.document;
  28. plugin = editor.plugins.get( ImageToolbar );
  29. toolbar = plugin._toolbar;
  30. editingView = editor.editing.view;
  31. imageBalloon = editor.plugins.get( 'ImageBalloon' );
  32. } );
  33. } );
  34. afterEach( () => {
  35. editorElement.remove();
  36. return editor.destroy();
  37. } );
  38. it( 'should be loaded', () => {
  39. expect( editor.plugins.get( ImageToolbar ) ).to.be.instanceOf( ImageToolbar );
  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( editor => {
  48. expect( editor.plugins.get( ImageToolbar )._toolbar ).to.be.undefined;
  49. editorElement.remove();
  50. editor.destroy();
  51. } );
  52. } );
  53. describe( 'toolbar', () => {
  54. it( 'should use the config.image.toolbar to create items', () => {
  55. expect( toolbar.items ).to.have.length( 1 );
  56. expect( toolbar.items.get( 0 ).label ).to.equal( 'fake button' );
  57. } );
  58. it( 'should set proper CSS classes', () => {
  59. const spy = sinon.spy( imageBalloon, 'add' );
  60. setData( doc, '[<image src=""></image>]' );
  61. expect( toolbar.element.classList.contains( 'ck-editor-toolbar' ) ).to.be.true;
  62. sinon.assert.calledWithMatch( spy, {
  63. view: toolbar,
  64. balloonClassName: 'ck-toolbar-container ck-editor-toolbar-container'
  65. } );
  66. } );
  67. } );
  68. describe( 'integration with the editor focus', () => {
  69. it( 'should show the toolbar when the editor gains focus and the image is selected', () => {
  70. editor.ui.focusTracker.isFocused = true;
  71. setData( doc, '[<image src=""></image>]' );
  72. editor.ui.focusTracker.isFocused = false;
  73. expect( imageBalloon.visibleView ).to.be.null;
  74. editor.ui.focusTracker.isFocused = true;
  75. expect( imageBalloon.visibleView ).to.equal( toolbar );
  76. } );
  77. it( 'should hide the toolbar when the editor loses focus and the image is selected', () => {
  78. editor.ui.focusTracker.isFocused = false;
  79. setData( doc, '[<image src=""></image>]' );
  80. editor.ui.focusTracker.isFocused = true;
  81. expect( imageBalloon.visibleView ).to.equal( toolbar );
  82. editor.ui.focusTracker.isFocused = false;
  83. expect( imageBalloon.visibleView ).to.be.null;
  84. } );
  85. } );
  86. describe( 'integration with the editor selection (#render event)', () => {
  87. it( 'should show the toolbar on render when the image is selected', () => {
  88. setData( doc, '<paragraph>[foo]</paragraph><image src=""></image>' );
  89. expect( imageBalloon.visibleView ).to.be.null;
  90. editingView.fire( 'render' );
  91. expect( imageBalloon.visibleView ).to.be.null;
  92. doc.enqueueChanges( () => {
  93. // Select the [<image></image>]
  94. doc.selection.setRanges( [
  95. Range.createOn( doc.getRoot().getChild( 1 ) )
  96. ] );
  97. } );
  98. expect( imageBalloon.visibleView ).to.equal( toolbar );
  99. // Make sure successive render does not throw, e.g. attempting
  100. // to insert the toolbar twice.
  101. editingView.fire( 'render' );
  102. expect( imageBalloon.visibleView ).to.equal( toolbar );
  103. } );
  104. it( 'should hide the toolbar on render if the image is de–selected', () => {
  105. setData( doc, '<paragraph>foo</paragraph>[<image src=""></image>]' );
  106. expect( imageBalloon.visibleView ).to.equal( toolbar );
  107. doc.enqueueChanges( () => {
  108. // Select the <paragraph>[...]</paragraph>
  109. doc.selection.setRanges( [
  110. Range.createIn( doc.getRoot().getChild( 0 ) )
  111. ] );
  112. } );
  113. expect( imageBalloon.visibleView ).to.be.null;
  114. // Make sure successive render does not throw, e.g. attempting
  115. // to remove the toolbar twice.
  116. editingView.fire( 'render' );
  117. expect( imageBalloon.visibleView ).to.be.null;
  118. } );
  119. } );
  120. // Plugin that adds fake_button to editor's component factory.
  121. class FakeButton extends Plugin {
  122. init() {
  123. this.editor.ui.componentFactory.add( 'fake_button', locale => {
  124. const view = new ButtonView( locale );
  125. view.set( {
  126. label: 'fake button'
  127. } );
  128. return view;
  129. } );
  130. }
  131. }
  132. } );