imageballoon.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Image from '../../../src/image';
  6. import ImageBalloon from '../../../src/image/ui/imageballoon';
  7. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  8. import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/panel/balloon/balloonpanelview';
  9. import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextualballoon';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  12. import View from '@ckeditor/ckeditor5-ui/src/view';
  13. import Template from '@ckeditor/ckeditor5-ui/src/template';
  14. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  15. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  16. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  17. testUtils.createSinonSandbox();
  18. describe( 'ImageBalloon', () => {
  19. let editor, doc, editingView, plugin, view, editorElement;
  20. beforeEach( () => {
  21. editorElement = global.document.createElement( 'div' );
  22. global.document.body.appendChild( editorElement );
  23. return ClassicEditor.create( editorElement, {
  24. plugins: [ Image, ImageBalloon, Paragraph ],
  25. } )
  26. .then( newEditor => {
  27. editor = newEditor;
  28. doc = editor.document;
  29. editingView = editor.editing.view;
  30. plugin = editor.plugins.get( ImageBalloon );
  31. view = new View();
  32. view.template = new Template( { tag: 'div' } );
  33. view.init();
  34. } );
  35. } );
  36. afterEach( () => {
  37. editorElement.remove();
  38. return editor.destroy();
  39. } );
  40. it( 'has proper plugin name', () => {
  41. expect( ImageBalloon.pluginName ).to.equal( 'ImageBalloon' );
  42. } );
  43. describe( 'add()', () => {
  44. it( 'uses default position configuration if not specified', () => {
  45. const spy = testUtils.sinon.stub( ContextualBalloon.prototype, 'add' );
  46. const positionData = {};
  47. setData( doc, '[<image src=""></image>]' );
  48. plugin.add( positionData );
  49. sinon.assert.calledWithExactly( spy, {
  50. position: {
  51. target: editingView.domConverter.viewToDom( editingView.selection.getSelectedElement() ),
  52. positions: [
  53. BalloonPanelView.defaultPositions.northArrowSouth,
  54. BalloonPanelView.defaultPositions.southArrowNorth
  55. ]
  56. }
  57. } );
  58. expect( positionData.position ).to.be.undefined;
  59. } );
  60. it( 'uses specified position configuration', () => {
  61. const spy = testUtils.sinon.stub( ContextualBalloon.prototype, 'add' );
  62. const positionData = {
  63. position: {}
  64. };
  65. setData( doc, '[<image src=""></image>]' );
  66. plugin.add( positionData );
  67. sinon.assert.calledWithExactly( spy, {
  68. position: {}
  69. } );
  70. } );
  71. } );
  72. describe( 'editing view #render event handling', () => {
  73. let updatePositionSpy;
  74. beforeEach( () => {
  75. updatePositionSpy = testUtils.sinon.stub( ContextualBalloon.prototype, 'updatePosition', () => {} );
  76. } );
  77. it( 'does not engage if there is no #visibleView', () => {
  78. setData( doc, '[<image src=""></image>]' );
  79. editingView.fire( 'render' );
  80. sinon.assert.notCalled( updatePositionSpy );
  81. } );
  82. it( 'does not engage if there is no image selected', () => {
  83. setData( doc, '<paragraph>foo</paragraph>[<image src=""></image>]' );
  84. plugin.add( { view } );
  85. doc.enqueueChanges( () => {
  86. // Select the [<paragraph></paragraph>]
  87. doc.selection.setRanges( [
  88. Range.createIn( doc.getRoot().getChild( 0 ) )
  89. ] );
  90. } );
  91. editingView.fire( 'render' );
  92. sinon.assert.notCalled( updatePositionSpy );
  93. } );
  94. it( 'updates balloon position when the image is selected', () => {
  95. setData( doc, '[<image src=""></image>]' );
  96. plugin.add( { view } );
  97. editingView.fire( 'render' );
  98. sinon.assert.calledOnce( updatePositionSpy );
  99. } );
  100. } );
  101. describe( 'editor focus handling', () => {
  102. it( 'clears the balloon if the editor focus is lost', () => {
  103. const spy = testUtils.sinon.spy( plugin, 'clear' );
  104. editor.ui.focusTracker.isFocused = true;
  105. sinon.assert.notCalled( spy );
  106. editor.ui.focusTracker.isFocused = false;
  107. sinon.assert.calledOnce( spy );
  108. editor.ui.focusTracker.isFocused = true;
  109. sinon.assert.calledOnce( spy );
  110. } );
  111. } );
  112. } );