tablewidgettoolbar.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import TableWidgetToolbar from '../src/tablewidgettoolbar';
  9. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  11. import View from '@ckeditor/ckeditor5-ui/src/view';
  12. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  13. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  14. describe( 'TableWidgetToolbar', () => {
  15. let editor, element, widgetToolbarRepository, balloon, toolbar, model;
  16. testUtils.createSinonSandbox();
  17. beforeEach( () => {
  18. element = document.createElement( 'div' );
  19. document.body.appendChild( element );
  20. return ClassicTestEditor.create( element, {
  21. plugins: [ Paragraph, Table, TableWidgetToolbar, FakeButton ],
  22. table: {
  23. widgetToolbar: [ 'fake_button' ]
  24. }
  25. } ).then( _editor => {
  26. editor = _editor;
  27. widgetToolbarRepository = editor.plugins.get( 'WidgetToolbarRepository' );
  28. toolbar = widgetToolbarRepository._toolbars.get( 'tableWidget' ).view;
  29. balloon = editor.plugins.get( 'ContextualBalloon' );
  30. model = editor.model;
  31. } );
  32. } );
  33. afterEach( () => {
  34. return editor.destroy()
  35. .then( () => element.remove() );
  36. } );
  37. it( 'should be loaded', () => {
  38. expect( editor.plugins.get( TableWidgetToolbar ) ).to.be.instanceOf( TableWidgetToolbar );
  39. } );
  40. describe( 'toolbar', () => {
  41. it( 'should use the config.table.tableWidget to create items', () => {
  42. expect( toolbar.items ).to.have.length( 1 );
  43. expect( toolbar.items.get( 0 ).label ).to.equal( 'fake button' );
  44. } );
  45. it( 'should set proper CSS classes', () => {
  46. const spy = sinon.spy( balloon, 'add' );
  47. editor.ui.focusTracker.isFocused = true;
  48. setData( model, '[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  49. sinon.assert.calledWithMatch( spy, {
  50. view: toolbar,
  51. balloonClassName: 'ck-toolbar-container'
  52. } );
  53. } );
  54. } );
  55. describe( 'integration with the editor focus', () => {
  56. it( 'should show the toolbar when the editor gains focus and the table is selected', () => {
  57. editor.ui.focusTracker.isFocused = true;
  58. setData( model, '[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  59. editor.ui.focusTracker.isFocused = false;
  60. expect( balloon.visibleView ).to.be.null;
  61. editor.ui.focusTracker.isFocused = true;
  62. expect( balloon.visibleView ).to.equal( toolbar );
  63. } );
  64. it( 'should hide the toolbar when the editor loses focus and the table is selected', () => {
  65. editor.ui.focusTracker.isFocused = false;
  66. setData( model, '[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  67. editor.ui.focusTracker.isFocused = true;
  68. expect( balloon.visibleView ).to.equal( toolbar );
  69. editor.ui.focusTracker.isFocused = false;
  70. expect( balloon.visibleView ).to.be.null;
  71. } );
  72. } );
  73. describe( 'integration with the editor selection', () => {
  74. beforeEach( () => {
  75. editor.ui.focusTracker.isFocused = true;
  76. } );
  77. it( 'should show the toolbar on ui#update when the table widget is selected', () => {
  78. setData( editor.model, '<paragraph>[foo]</paragraph><table><tableRow><tableCell></tableCell></tableRow></table>' );
  79. expect( balloon.visibleView ).to.be.null;
  80. editor.ui.fire( 'update' );
  81. expect( balloon.visibleView ).to.be.null;
  82. editor.model.change( writer => {
  83. // Select the [<table></table>]
  84. writer.setSelection( editor.model.document.getRoot().getChild( 1 ), 'on' );
  85. } );
  86. expect( balloon.visibleView ).to.equal( toolbar );
  87. // Make sure successive change does not throw, e.g. attempting
  88. // to insert the toolbar twice.
  89. editor.ui.fire( 'update' );
  90. expect( balloon.visibleView ).to.equal( toolbar );
  91. } );
  92. it( 'should not show the toolbar on ui#update when the selection is inside a table cell', () => {
  93. setData( editor.model, '<table><tableRow><tableCell>[]</tableCell></tableRow></table>' );
  94. expect( balloon.visibleView ).to.be.null;
  95. editor.ui.fire( 'update' );
  96. expect( balloon.visibleView ).to.be.null;
  97. } );
  98. it( 'should not engage when the toolbar is in the balloon yet invisible', () => {
  99. setData( model, '<table><tableRow><tableCell></tableCell></tableRow></table>' );
  100. expect( balloon.visibleView ).to.equal( toolbar );
  101. const lastView = new View();
  102. lastView.element = document.createElement( 'div' );
  103. balloon.add( {
  104. view: lastView,
  105. position: {
  106. target: document.body
  107. }
  108. } );
  109. expect( balloon.visibleView ).to.equal( lastView );
  110. editor.ui.fire( 'update' );
  111. expect( balloon.visibleView ).to.equal( lastView );
  112. } );
  113. it( 'should hide the toolbar on ui#update if the table is de–selected', () => {
  114. setData( model, '<paragraph>foo</paragraph>[<table><tableRow><tableCell></tableCell></tableRow></table>]' );
  115. expect( balloon.visibleView ).to.equal( toolbar );
  116. model.change( writer => {
  117. // Select the <paragraph>[...]</paragraph>
  118. writer.setSelection( model.document.getRoot().getChild( 0 ), 'in' );
  119. } );
  120. expect( balloon.visibleView ).to.be.null;
  121. // Make sure successive change does not throw, e.g. attempting
  122. // to remove the toolbar twice.
  123. editor.ui.fire( 'update' );
  124. expect( balloon.visibleView ).to.be.null;
  125. } );
  126. } );
  127. // Plugin that adds fake_button to editor's component factory.
  128. class FakeButton extends Plugin {
  129. init() {
  130. this.editor.ui.componentFactory.add( 'fake_button', locale => {
  131. const view = new ButtonView( locale );
  132. view.set( {
  133. label: 'fake button'
  134. } );
  135. return view;
  136. } );
  137. }
  138. }
  139. } );