tablewidgettoolbar.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * Copyright (c) 2016 - 2017, CKSource - Frederico Knabben. All rights reserved.
  3. */
  4. /* global document */
  5. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  6. import Table from '@ckeditor/ckeditor5-table/src/table';
  7. import TableToolbar from '@ckeditor/ckeditor5-table/src/tabletoolbar';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import TableWidgetToolbar from '../src/tablewidgettoolbar';
  10. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. import View from '@ckeditor/ckeditor5-ui/src/view';
  12. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  13. describe( 'TableWidgetToolbar', () => {
  14. let editor, element, tableWidgetToolbar, balloon, toolbar, model;
  15. testUtils.createSinonSandbox();
  16. beforeEach( () => {
  17. element = document.createElement( 'div' );
  18. document.body.appendChild( element );
  19. return ClassicTestEditor.create( element, {
  20. plugins: [ Paragraph, Table, TableToolbar, TableWidgetToolbar ],
  21. table: {
  22. widgetToolbar: []
  23. }
  24. } ).then( _editor => {
  25. editor = _editor;
  26. tableWidgetToolbar = editor.plugins.get( TableWidgetToolbar );
  27. toolbar = tableWidgetToolbar._toolbar;
  28. balloon = editor.plugins.get( 'ContextualBalloon' );
  29. model = editor.model;
  30. } );
  31. } );
  32. afterEach( () => {
  33. return editor.destroy()
  34. .then( () => element.remove() );
  35. } );
  36. it( 'should be loaded', () => {
  37. expect( editor.plugins.get( TableWidgetToolbar ) ).to.be.instanceOf( TableWidgetToolbar );
  38. } );
  39. // it( 'should have only one item - comment', () => {
  40. // expect( toolbar.items ).to.have.length( 1 );
  41. // expect( toolbar.items.get( 0 ).label ).to.equal( 'Inline comment' );
  42. // } );
  43. it( 'should set proper CSS classes', () => {
  44. const spy = sinon.spy( balloon, 'add' );
  45. editor.ui.focusTracker.isFocused = true;
  46. setData( model, '[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  47. sinon.assert.calledWithMatch( spy, {
  48. view: toolbar,
  49. balloonClassName: 'ck-toolbar-container'
  50. } );
  51. } );
  52. describe( 'integration with the editor focus', () => {
  53. it( 'should show the toolbar when the editor gains focus and the table is selected', () => {
  54. editor.ui.focusTracker.isFocused = true;
  55. setData( model, '[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  56. editor.ui.focusTracker.isFocused = false;
  57. expect( balloon.visibleView ).to.be.null;
  58. editor.ui.focusTracker.isFocused = true;
  59. expect( balloon.visibleView ).to.equal( toolbar );
  60. } );
  61. it( 'should hide the toolbar when the editor loses focus and the table is selected', () => {
  62. editor.ui.focusTracker.isFocused = false;
  63. setData( model, '[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  64. editor.ui.focusTracker.isFocused = true;
  65. expect( balloon.visibleView ).to.equal( toolbar );
  66. editor.ui.focusTracker.isFocused = false;
  67. expect( balloon.visibleView ).to.be.null;
  68. } );
  69. } );
  70. describe( 'integration with the editor selection', () => {
  71. beforeEach( () => {
  72. editor.ui.focusTracker.isFocused = true;
  73. } );
  74. it( 'should show the toolbar on ui#update when the table widget is selected', () => {
  75. setData( editor.model, '<paragraph>[foo]</paragraph><table><tableRow><tableCell></tableCell></tableRow></table>' );
  76. expect( balloon.visibleView ).to.be.null;
  77. editor.ui.fire( 'update' );
  78. expect( balloon.visibleView ).to.be.null;
  79. editor.model.change( writer => {
  80. // Select the [<table></table>]
  81. writer.setSelection( editor.model.document.getRoot().getChild( 1 ), 'on' );
  82. } );
  83. expect( balloon.visibleView ).to.equal( toolbar );
  84. // Make sure successive change does not throw, e.g. attempting
  85. // to insert the toolbar twice.
  86. editor.ui.fire( 'update' );
  87. expect( balloon.visibleView ).to.equal( toolbar );
  88. } );
  89. it( 'should not show the toolbar on ui#update when the selection is inside a table cell', () => {
  90. setData( editor.model, '<table><tableRow><tableCell>[]</tableCell></tableRow></table>' );
  91. expect( balloon.visibleView ).to.be.null;
  92. editor.ui.fire( 'update' );
  93. expect( balloon.visibleView ).to.be.null;
  94. } );
  95. it( 'should not engage when the toolbar is in the balloon yet invisible', () => {
  96. setData( model, '<table><tableRow><tableCell></tableCell></tableRow></table>' );
  97. expect( balloon.visibleView ).to.equal( toolbar );
  98. const lastView = new View();
  99. lastView.element = document.createElement( 'div' );
  100. balloon.add( {
  101. view: lastView,
  102. position: {
  103. target: document.body
  104. }
  105. } );
  106. expect( balloon.visibleView ).to.equal( lastView );
  107. editor.ui.fire( 'update' );
  108. expect( balloon.visibleView ).to.equal( lastView );
  109. } );
  110. it( 'should hide the toolbar on ui#update if the table is de–selected', () => {
  111. setData( model, '<paragraph>foo</paragraph>[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  112. expect( balloon.visibleView ).to.equal( toolbar );
  113. model.change( writer => {
  114. // Select the <paragraph>[...]</paragraph>
  115. writer.setSelection( model.document.getRoot().getChild( 0 ), 'in' );
  116. } );
  117. expect( balloon.visibleView ).to.be.null;
  118. // Make sure successive change does not throw, e.g. attempting
  119. // to remove the toolbar twice.
  120. editor.ui.fire( 'update' );
  121. expect( balloon.visibleView ).to.be.null;
  122. } );
  123. } );
  124. } );