tablecellpropertiesui.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. /* globals document, Event */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  8. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  9. import { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  11. import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
  12. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  13. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  14. import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextualballoon';
  15. import Table from '../../src/table';
  16. import TableCellPropertiesEditing from '../../src/tablecellproperties/tablecellpropertiesediting';
  17. import TableCellPropertiesUI from '../../src/tablecellproperties/tablecellpropertiesui';
  18. import TableCellPropertiesUIView from '../../src/tablecellproperties/ui/tablecellpropertiesview';
  19. describe( 'table cell properties', () => {
  20. describe( 'TableCellPropertiesUI', () => {
  21. let editor, editorElement, contextualBalloon,
  22. tableCellPropertiesUI, tableCellPropertiesView, tableCellPropertiesButton;
  23. testUtils.createSinonSandbox();
  24. beforeEach( () => {
  25. editorElement = document.createElement( 'div' );
  26. document.body.appendChild( editorElement );
  27. return ClassicTestEditor
  28. .create( editorElement, {
  29. plugins: [ Table, TableCellPropertiesEditing, TableCellPropertiesUI, Paragraph, Undo ],
  30. initialData: '<table><tr><td>foo</td></tr></table><p>bar</p>'
  31. } )
  32. .then( newEditor => {
  33. editor = newEditor;
  34. tableCellPropertiesUI = editor.plugins.get( TableCellPropertiesUI );
  35. tableCellPropertiesButton = editor.ui.componentFactory.create( 'tableCellProperties' );
  36. contextualBalloon = editor.plugins.get( ContextualBalloon );
  37. tableCellPropertiesView = tableCellPropertiesUI.view;
  38. // There is no point to execute BalloonPanelView attachTo and pin methods so lets override it.
  39. testUtils.sinon.stub( contextualBalloon.view, 'attachTo' ).returns( {} );
  40. testUtils.sinon.stub( contextualBalloon.view, 'pin' ).returns( {} );
  41. } );
  42. } );
  43. afterEach( () => {
  44. editorElement.remove();
  45. return editor.destroy();
  46. } );
  47. it( 'should be named', () => {
  48. expect( TableCellPropertiesUI.pluginName ).to.equal( 'TableCellPropertiesUI' );
  49. } );
  50. it( 'should load ContextualBalloon', () => {
  51. expect( editor.plugins.get( ContextualBalloon ) ).to.be.instanceOf( ContextualBalloon );
  52. } );
  53. describe( 'init()', () => {
  54. it( 'should set a batch', () => {
  55. expect( tableCellPropertiesUI._undoStepBatch ).to.be.null;
  56. } );
  57. describe( '#view', () => {
  58. it( 'should be created', () => {
  59. expect( tableCellPropertiesUI.view ).to.be.instanceOf( TableCellPropertiesUIView );
  60. } );
  61. it( 'should be rendered', () => {
  62. expect( tableCellPropertiesUI.view.isRendered ).to.be.true;
  63. } );
  64. } );
  65. describe( 'toolbar button', () => {
  66. it( 'should be registered', () => {
  67. expect( tableCellPropertiesButton ).to.be.instanceOf( ButtonView );
  68. } );
  69. it( 'should have a label', () => {
  70. expect( tableCellPropertiesButton.label ).to.equal( 'Cell properties' );
  71. } );
  72. it( 'should have a tooltip', () => {
  73. expect( tableCellPropertiesButton.tooltip ).to.be.true;
  74. } );
  75. it( 'should call #_showView upon #execute', () => {
  76. const spy = testUtils.sinon.stub( tableCellPropertiesUI, '_showView' ).returns( {} );
  77. tableCellPropertiesButton.fire( 'execute' );
  78. sinon.assert.calledOnce( spy );
  79. } );
  80. } );
  81. } );
  82. describe( 'destroy()', () => {
  83. } );
  84. describe( 'Properties #view', () => {
  85. beforeEach( () => {
  86. editor.model.change( writer => {
  87. writer.setSelection( editor.model.document.getRoot().getChild( 0 ).getChild( 0 ).getChild( 0 ), 0 );
  88. } );
  89. } );
  90. it( 'should hide on #submit', () => {
  91. tableCellPropertiesButton.fire( 'execute' );
  92. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  93. tableCellPropertiesView.fire( 'submit' );
  94. expect( contextualBalloon.visibleView ).to.be.null;
  95. } );
  96. it( 'should undo the entire batch of changes on #cancel', () => {
  97. // Show the view. New batch will be created.
  98. tableCellPropertiesButton.fire( 'execute' );
  99. // Do the changes like a user.
  100. tableCellPropertiesView.borderStyle = 'dotted';
  101. tableCellPropertiesView.backgroundColor = 'red';
  102. expect( getModelData( editor.model ) ).to.equal(
  103. '<table>' +
  104. '<tableRow>' +
  105. '<tableCell backgroundColor="red" borderStyle="dotted">' +
  106. '<paragraph>[]foo</paragraph>' +
  107. '</tableCell>' +
  108. '</tableRow>' +
  109. '</table>' +
  110. '<paragraph>bar</paragraph>'
  111. );
  112. tableCellPropertiesView.fire( 'cancel' );
  113. expect( getModelData( editor.model ) ).to.equal(
  114. '<table>' +
  115. '<tableRow>' +
  116. '<tableCell>' +
  117. '<paragraph>[]foo</paragraph>' +
  118. '</tableCell>' +
  119. '</tableRow>' +
  120. '</table>' +
  121. '<paragraph>bar</paragraph>'
  122. );
  123. } );
  124. it( 'should hide on #cancel', () => {
  125. tableCellPropertiesButton.fire( 'execute' );
  126. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  127. tableCellPropertiesView.fire( 'cancel' );
  128. expect( contextualBalloon.visibleView ).to.be.null;
  129. } );
  130. it( 'should hide on the Esc key press', () => {
  131. const keyEvtData = {
  132. keyCode: keyCodes.esc,
  133. preventDefault: sinon.spy(),
  134. stopPropagation: sinon.spy()
  135. };
  136. tableCellPropertiesButton.fire( 'execute' );
  137. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  138. tableCellPropertiesView.keystrokes.press( keyEvtData );
  139. expect( contextualBalloon.visibleView ).to.be.null;
  140. } );
  141. it( 'should hide if the table cell is no longer selected on EditorUI#update', () => {
  142. tableCellPropertiesButton.fire( 'execute' );
  143. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  144. editor.model.change( writer => {
  145. // Set selection in the paragraph.
  146. writer.setSelection( editor.model.document.getRoot().getChild( 1 ), 0 );
  147. } );
  148. expect( contextualBalloon.visibleView ).to.be.null;
  149. } );
  150. it( 'should reposition if table cell is still selected on on EditorUI#update', () => {
  151. tableCellPropertiesButton.fire( 'execute' );
  152. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  153. editor.model.change( writer => {
  154. writer.insertText( 'qux', editor.model.document.selection.getFirstPosition() );
  155. } );
  156. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  157. } );
  158. it( 'should hide if clicked outside the balloon', () => {
  159. tableCellPropertiesButton.fire( 'execute' );
  160. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  161. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  162. expect( contextualBalloon.visibleView ).to.be.null;
  163. } );
  164. describe( 'property changes', () => {
  165. it( 'should affect the editor state', () => {
  166. const spy = testUtils.sinon.stub( editor, 'execute' );
  167. tableCellPropertiesUI._undoStepBatch = 'foo';
  168. tableCellPropertiesView.borderStyle = 'dotted';
  169. sinon.assert.calledOnce( spy );
  170. sinon.assert.calledWithExactly( spy, 'tableCellBorderStyle', { value: 'dotted', batch: 'foo' } );
  171. } );
  172. it( 'should not affect the editor state if internal property has changed', () => {
  173. const spy = testUtils.sinon.stub( editor, 'execute' );
  174. tableCellPropertiesView.set( 'internalProp', 'foo' );
  175. tableCellPropertiesUI._undoStepBatch = 'foo';
  176. tableCellPropertiesView.internalProp = 'bar';
  177. sinon.assert.notCalled( spy );
  178. } );
  179. } );
  180. } );
  181. describe( 'Showing the #view', () => {
  182. beforeEach( () => {
  183. editor.model.change( writer => {
  184. writer.setSelection( editor.model.document.getRoot().getChild( 0 ).getChild( 0 ).getChild( 0 ), 0 );
  185. } );
  186. } );
  187. it( 'should create a new undoable batch for further #view cancel', () => {
  188. tableCellPropertiesButton.fire( 'execute' );
  189. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  190. const firstBatch = tableCellPropertiesUI._undoStepBatch;
  191. expect( firstBatch ).to.be.instanceOf( Batch );
  192. tableCellPropertiesView.fire( 'submit' );
  193. expect( contextualBalloon.visibleView ).to.be.null;
  194. tableCellPropertiesButton.fire( 'execute' );
  195. const secondBatch = tableCellPropertiesUI._undoStepBatch;
  196. expect( secondBatch ).to.be.instanceOf( Batch );
  197. expect( firstBatch ).to.not.equal( secondBatch );
  198. } );
  199. describe( 'initial data', () => {
  200. it( 'should be set from the command values', () => {
  201. editor.commands.get( 'tableCellBorderStyle' ).value = 'a';
  202. editor.commands.get( 'tableCellBorderColor' ).value = 'b';
  203. editor.commands.get( 'tableCellBorderWidth' ).value = 'c';
  204. editor.commands.get( 'tableCellPadding' ).value = 'd';
  205. editor.commands.get( 'tableCellBackgroundColor' ).value = 'e';
  206. editor.commands.get( 'tableCellHorizontalAlignment' ).value = 'f';
  207. editor.commands.get( 'tableCellVerticalAlignment' ).value = 'g';
  208. tableCellPropertiesButton.fire( 'execute' );
  209. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  210. expect( tableCellPropertiesView ).to.include( {
  211. borderStyle: 'a',
  212. borderColor: 'b',
  213. borderWidth: 'c',
  214. padding: 'd',
  215. backgroundColor: 'e',
  216. horizontalAlignment: 'f',
  217. verticalAlignment: 'g'
  218. } );
  219. } );
  220. it( 'should use default values when command has no value', () => {
  221. editor.commands.get( 'tableCellBorderStyle' ).value = null;
  222. editor.commands.get( 'tableCellBorderColor' ).value = null;
  223. editor.commands.get( 'tableCellBorderWidth' ).value = null;
  224. editor.commands.get( 'tableCellPadding' ).value = null;
  225. editor.commands.get( 'tableCellBackgroundColor' ).value = null;
  226. editor.commands.get( 'tableCellHorizontalAlignment' ).value = null;
  227. editor.commands.get( 'tableCellVerticalAlignment' ).value = null;
  228. tableCellPropertiesButton.fire( 'execute' );
  229. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  230. expect( tableCellPropertiesView ).to.include( {
  231. borderStyle: 'none',
  232. borderColor: '',
  233. borderWidth: '',
  234. padding: '',
  235. backgroundColor: '',
  236. horizontalAlignment: 'left',
  237. verticalAlignment: 'middle'
  238. } );
  239. } );
  240. } );
  241. it( 'should focus the form view', () => {
  242. const spy = testUtils.sinon.spy( tableCellPropertiesView, 'focus' );
  243. tableCellPropertiesButton.fire( 'execute' );
  244. sinon.assert.calledOnce( spy );
  245. } );
  246. } );
  247. describe( 'Hiding the #view', () => {
  248. beforeEach( () => {
  249. editor.model.change( writer => {
  250. writer.setSelection( editor.model.document.getRoot().getChild( 0 ).getChild( 0 ).getChild( 0 ), 0 );
  251. } );
  252. } );
  253. it( 'should stop listening to EditorUI#update', () => {
  254. const spy = testUtils.sinon.spy( tableCellPropertiesUI, 'stopListening' );
  255. tableCellPropertiesButton.fire( 'execute' );
  256. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  257. tableCellPropertiesView.fire( 'submit' );
  258. expect( contextualBalloon.visibleView ).to.be.null;
  259. sinon.assert.calledOnce( spy );
  260. sinon.assert.calledWithExactly( spy, editor.ui, 'update' );
  261. } );
  262. it( 'should focus the editing view so the focus is not lost', () => {
  263. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  264. tableCellPropertiesButton.fire( 'execute' );
  265. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  266. tableCellPropertiesView.fire( 'submit' );
  267. sinon.assert.calledOnce( spy );
  268. } );
  269. } );
  270. } );
  271. } );