integration.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  6. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import BalloonToolbar from '@ckeditor/ckeditor5-ui/src/toolbar/balloon/balloontoolbar';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  10. import Image from '../src/image';
  11. import ImageToolbar from '../src/imagetoolbar';
  12. import View from '@ckeditor/ckeditor5-ui/src/view';
  13. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  14. import ImageTextAlternative from '../src/imagetextalternative';
  15. describe( 'ImageToolbar integration', () => {
  16. describe( 'with the BalloonToolbar', () => {
  17. let balloon, balloonToolbar, newEditor, editorElement;
  18. testUtils.createSinonSandbox();
  19. beforeEach( () => {
  20. editorElement = global.document.createElement( 'div' );
  21. global.document.body.appendChild( editorElement );
  22. return ClassicTestEditor
  23. .create( editorElement, {
  24. plugins: [ Image, ImageTextAlternative, ImageToolbar, BalloonToolbar, Paragraph ],
  25. image: {
  26. toolbar: [ 'imageTextAlternative' ]
  27. }
  28. } )
  29. .then( editor => {
  30. newEditor = editor;
  31. balloon = newEditor.plugins.get( 'ContextualBalloon' );
  32. balloonToolbar = newEditor.plugins.get( 'BalloonToolbar' );
  33. const button = new View();
  34. button.element = global.document.createElement( 'div' );
  35. // There must be at least one toolbar items which is not disabled to show it.
  36. // https://github.com/ckeditor/ckeditor5-ui/issues/269
  37. balloonToolbar.toolbarView.items.add( button );
  38. newEditor.editing.view.isFocused = true;
  39. newEditor.editing.view.getDomRoot().focus();
  40. } );
  41. } );
  42. afterEach( () => {
  43. editorElement.remove();
  44. return newEditor.destroy();
  45. } );
  46. it( 'should prevent the BalloonToolbar from being displayed when an image is selected', () => {
  47. // When image is selected along with text.
  48. setModelData( newEditor.model, '<paragraph>fo[o</paragraph><image alt="alt text" src="/assets/sample.png"></image>]' );
  49. balloonToolbar.show();
  50. // BalloonToolbar should be visible.
  51. expect( balloon.visibleView ).to.equal( balloonToolbar.toolbarView );
  52. // When only image is selected.
  53. setModelData( newEditor.model, '<paragraph>foo</paragraph>[<image alt="alt text" src="/assets/sample.png"></image>]' );
  54. balloonToolbar.show();
  55. // BalloonToolbar should not be visible.
  56. expect( balloon.visibleView ).to.be.null;
  57. } );
  58. it( 'should listen to BalloonToolbar#show event with the high priority', () => {
  59. const highestPrioritySpy = sinon.spy();
  60. const highPrioritySpy = sinon.spy();
  61. const normalPrioritySpy = sinon.spy();
  62. // Select an image
  63. setModelData( newEditor.model, '<paragraph>foo</paragraph>[<image alt="alt text" src="/assets/sample.png"></image>]' );
  64. newEditor.listenTo( balloonToolbar, 'show', highestPrioritySpy, { priority: 'highest' } );
  65. newEditor.listenTo( balloonToolbar, 'show', highPrioritySpy, { priority: 'high' } );
  66. newEditor.listenTo( balloonToolbar, 'show', normalPrioritySpy, { priority: 'normal' } );
  67. balloonToolbar.show();
  68. sinon.assert.calledOnce( highestPrioritySpy );
  69. sinon.assert.notCalled( highPrioritySpy );
  70. sinon.assert.notCalled( normalPrioritySpy );
  71. } );
  72. } );
  73. } );