utils.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  6. import Table from '../../src/table';
  7. import TableCellProperties from '../../src/tablecellproperties';
  8. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import View from '@ckeditor/ckeditor5-ui/src/view';
  11. import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/panel/balloon/balloonpanelview';
  12. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  13. import { repositionContextualBalloon, getBalloonCellPositionData } from '../../src/ui/utils';
  14. import { findAncestor } from '../../src/commands/utils';
  15. describe( 'Utils', () => {
  16. let editor, editingView, balloon, editorElement;
  17. beforeEach( () => {
  18. editorElement = global.document.createElement( 'div' );
  19. global.document.body.appendChild( editorElement );
  20. return ClassicEditor
  21. .create( editorElement, {
  22. plugins: [ Table, TableCellProperties, Paragraph ]
  23. } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. editingView = editor.editing.view;
  27. balloon = editor.plugins.get( 'ContextualBalloon' );
  28. } );
  29. } );
  30. afterEach( () => {
  31. editorElement.remove();
  32. return editor.destroy();
  33. } );
  34. describe( 'repositionContextualBalloon', () => {
  35. it( 'should re-position the ContextualBalloon when the table cell is selected', () => {
  36. const spy = sinon.spy( balloon, 'updatePosition' );
  37. const defaultPositions = BalloonPanelView.defaultPositions;
  38. const view = new View();
  39. view.element = global.document.createElement( 'div' );
  40. balloon.add( {
  41. view,
  42. position: {
  43. target: global.document.body
  44. }
  45. } );
  46. setData( editor.model,
  47. '<table><tableRow>' +
  48. '<tableCell><paragraph>foo</paragraph></tableCell>' +
  49. '<tableCell><paragraph>[bar]</paragraph></tableCell>' +
  50. '</tableRow></table>' );
  51. repositionContextualBalloon( editor );
  52. const modelCell = findAncestor( 'tableCell', editor.model.document.selection.getFirstPosition() );
  53. const viewCell = editor.editing.mapper.toViewElement( modelCell );
  54. sinon.assert.calledWithExactly( spy, {
  55. target: editingView.domConverter.viewToDom( viewCell ),
  56. positions: [
  57. defaultPositions.northArrowSouth,
  58. defaultPositions.northArrowSouthWest,
  59. defaultPositions.northArrowSouthEast,
  60. defaultPositions.southArrowNorth,
  61. defaultPositions.southArrowNorthWest,
  62. defaultPositions.southArrowNorthEast
  63. ]
  64. } );
  65. } );
  66. it( 'should not engage with no table is selected', () => {
  67. const spy = sinon.spy( balloon, 'updatePosition' );
  68. setData( editor.model, '<paragraph>foo</paragraph>' );
  69. repositionContextualBalloon( editor );
  70. sinon.assert.notCalled( spy );
  71. } );
  72. } );
  73. describe( 'getBalloonCellPositionData', () => {
  74. it( 'returns the position data', () => {
  75. const defaultPositions = BalloonPanelView.defaultPositions;
  76. setData( editor.model, '<table><tableRow>' +
  77. '<tableCell><paragraph>foo</paragraph></tableCell>' +
  78. '<tableCell><paragraph>[bar]</paragraph></tableCell>' +
  79. '</tableRow></table>' );
  80. const data = getBalloonCellPositionData( editor );
  81. const modelCell = findAncestor( 'tableCell', editor.model.document.selection.getFirstPosition() );
  82. const viewCell = editor.editing.mapper.toViewElement( modelCell );
  83. expect( data ).to.deep.equal( {
  84. target: editingView.domConverter.viewToDom( viewCell ),
  85. positions: [
  86. defaultPositions.northArrowSouth,
  87. defaultPositions.northArrowSouthWest,
  88. defaultPositions.northArrowSouthEast,
  89. defaultPositions.southArrowNorth,
  90. defaultPositions.southArrowNorthWest,
  91. defaultPositions.southArrowNorthEast
  92. ]
  93. } );
  94. } );
  95. } );
  96. } );