tablecellpropertiesui.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. it( 'should destroy the #view', () => {
  84. const spy = sinon.spy( tableCellPropertiesView, 'destroy' );
  85. tableCellPropertiesUI.destroy();
  86. sinon.assert.calledOnce( spy );
  87. } );
  88. } );
  89. describe( 'Properties #view', () => {
  90. beforeEach( () => {
  91. editor.model.change( writer => {
  92. writer.setSelection( editor.model.document.getRoot().getChild( 0 ).getChild( 0 ).getChild( 0 ), 0 );
  93. } );
  94. } );
  95. it( 'should hide on #submit', () => {
  96. tableCellPropertiesButton.fire( 'execute' );
  97. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  98. tableCellPropertiesView.fire( 'submit' );
  99. expect( contextualBalloon.visibleView ).to.be.null;
  100. } );
  101. it( 'should undo the entire batch of changes on #cancel', () => {
  102. // Show the view. New batch will be created.
  103. tableCellPropertiesButton.fire( 'execute' );
  104. // Do the changes like a user.
  105. tableCellPropertiesView.borderStyle = 'dotted';
  106. tableCellPropertiesView.backgroundColor = 'red';
  107. expect( getModelData( editor.model ) ).to.equal(
  108. '<table>' +
  109. '<tableRow>' +
  110. '<tableCell backgroundColor="red" borderStyle="dotted">' +
  111. '<paragraph>[]foo</paragraph>' +
  112. '</tableCell>' +
  113. '</tableRow>' +
  114. '</table>' +
  115. '<paragraph>bar</paragraph>'
  116. );
  117. tableCellPropertiesView.fire( 'cancel' );
  118. expect( getModelData( editor.model ) ).to.equal(
  119. '<table>' +
  120. '<tableRow>' +
  121. '<tableCell>' +
  122. '<paragraph>[]foo</paragraph>' +
  123. '</tableCell>' +
  124. '</tableRow>' +
  125. '</table>' +
  126. '<paragraph>bar</paragraph>'
  127. );
  128. } );
  129. it( 'should hide on #cancel', () => {
  130. tableCellPropertiesButton.fire( 'execute' );
  131. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  132. tableCellPropertiesView.fire( 'cancel' );
  133. expect( contextualBalloon.visibleView ).to.be.null;
  134. } );
  135. it( 'should hide on the Esc key press', () => {
  136. const keyEvtData = {
  137. keyCode: keyCodes.esc,
  138. preventDefault: sinon.spy(),
  139. stopPropagation: sinon.spy()
  140. };
  141. tableCellPropertiesButton.fire( 'execute' );
  142. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  143. tableCellPropertiesView.keystrokes.press( keyEvtData );
  144. expect( contextualBalloon.visibleView ).to.be.null;
  145. } );
  146. it( 'should hide if the table cell is no longer selected on EditorUI#update', () => {
  147. tableCellPropertiesButton.fire( 'execute' );
  148. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  149. editor.model.change( writer => {
  150. // Set selection in the paragraph.
  151. writer.setSelection( editor.model.document.getRoot().getChild( 1 ), 0 );
  152. } );
  153. expect( contextualBalloon.visibleView ).to.be.null;
  154. } );
  155. it( 'should reposition if table cell is still selected on on EditorUI#update', () => {
  156. tableCellPropertiesButton.fire( 'execute' );
  157. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  158. editor.model.change( writer => {
  159. writer.insertText( 'qux', editor.model.document.selection.getFirstPosition() );
  160. } );
  161. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  162. } );
  163. it( 'should hide if clicked outside the balloon', () => {
  164. tableCellPropertiesButton.fire( 'execute' );
  165. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  166. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  167. expect( contextualBalloon.visibleView ).to.be.null;
  168. } );
  169. describe( 'property changes', () => {
  170. it( 'should affect the editor state', () => {
  171. const spy = testUtils.sinon.stub( editor, 'execute' );
  172. tableCellPropertiesUI._undoStepBatch = 'foo';
  173. tableCellPropertiesView.borderStyle = 'dotted';
  174. sinon.assert.calledOnce( spy );
  175. sinon.assert.calledWithExactly( spy, 'tableCellBorderStyle', { value: 'dotted', batch: 'foo' } );
  176. } );
  177. it( 'should not affect the editor state if internal property has changed', () => {
  178. const spy = testUtils.sinon.stub( editor, 'execute' );
  179. tableCellPropertiesView.set( 'internalProp', 'foo' );
  180. tableCellPropertiesUI._undoStepBatch = 'foo';
  181. tableCellPropertiesView.internalProp = 'bar';
  182. sinon.assert.notCalled( spy );
  183. } );
  184. } );
  185. } );
  186. describe( 'Showing the #view', () => {
  187. beforeEach( () => {
  188. editor.model.change( writer => {
  189. writer.setSelection( editor.model.document.getRoot().getChild( 0 ).getChild( 0 ).getChild( 0 ), 0 );
  190. } );
  191. } );
  192. it( 'should create a new undoable batch for further #view cancel', () => {
  193. tableCellPropertiesButton.fire( 'execute' );
  194. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  195. const firstBatch = tableCellPropertiesUI._undoStepBatch;
  196. expect( firstBatch ).to.be.instanceOf( Batch );
  197. tableCellPropertiesView.fire( 'submit' );
  198. expect( contextualBalloon.visibleView ).to.be.null;
  199. tableCellPropertiesButton.fire( 'execute' );
  200. const secondBatch = tableCellPropertiesUI._undoStepBatch;
  201. expect( secondBatch ).to.be.instanceOf( Batch );
  202. expect( firstBatch ).to.not.equal( secondBatch );
  203. } );
  204. describe( 'initial data', () => {
  205. it( 'should be set from the command values', () => {
  206. editor.commands.get( 'tableCellBorderStyle' ).value = 'a';
  207. editor.commands.get( 'tableCellBorderColor' ).value = 'b';
  208. editor.commands.get( 'tableCellBorderWidth' ).value = 'c';
  209. editor.commands.get( 'tableCellPadding' ).value = 'd';
  210. editor.commands.get( 'tableCellBackgroundColor' ).value = 'e';
  211. editor.commands.get( 'tableCellHorizontalAlignment' ).value = 'f';
  212. editor.commands.get( 'tableCellVerticalAlignment' ).value = 'g';
  213. tableCellPropertiesButton.fire( 'execute' );
  214. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  215. expect( tableCellPropertiesView ).to.include( {
  216. borderStyle: 'a',
  217. borderColor: 'b',
  218. borderWidth: 'c',
  219. padding: 'd',
  220. backgroundColor: 'e',
  221. horizontalAlignment: 'f',
  222. verticalAlignment: 'g'
  223. } );
  224. } );
  225. it( 'should use default values when command has no value', () => {
  226. editor.commands.get( 'tableCellBorderStyle' ).value = null;
  227. editor.commands.get( 'tableCellBorderColor' ).value = null;
  228. editor.commands.get( 'tableCellBorderWidth' ).value = null;
  229. editor.commands.get( 'tableCellPadding' ).value = null;
  230. editor.commands.get( 'tableCellBackgroundColor' ).value = null;
  231. editor.commands.get( 'tableCellHorizontalAlignment' ).value = null;
  232. editor.commands.get( 'tableCellVerticalAlignment' ).value = null;
  233. tableCellPropertiesButton.fire( 'execute' );
  234. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  235. expect( tableCellPropertiesView ).to.include( {
  236. borderStyle: 'none',
  237. borderColor: '',
  238. borderWidth: '',
  239. padding: '',
  240. backgroundColor: '',
  241. horizontalAlignment: 'left',
  242. verticalAlignment: 'middle'
  243. } );
  244. } );
  245. } );
  246. it( 'should focus the form view', () => {
  247. const spy = testUtils.sinon.spy( tableCellPropertiesView, 'focus' );
  248. tableCellPropertiesButton.fire( 'execute' );
  249. sinon.assert.calledOnce( spy );
  250. } );
  251. } );
  252. describe( 'Hiding the #view', () => {
  253. beforeEach( () => {
  254. editor.model.change( writer => {
  255. writer.setSelection( editor.model.document.getRoot().getChild( 0 ).getChild( 0 ).getChild( 0 ), 0 );
  256. } );
  257. } );
  258. it( 'should stop listening to EditorUI#update', () => {
  259. const spy = testUtils.sinon.spy( tableCellPropertiesUI, 'stopListening' );
  260. tableCellPropertiesButton.fire( 'execute' );
  261. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  262. tableCellPropertiesView.fire( 'submit' );
  263. expect( contextualBalloon.visibleView ).to.be.null;
  264. sinon.assert.calledOnce( spy );
  265. sinon.assert.calledWithExactly( spy, editor.ui, 'update' );
  266. } );
  267. it( 'should focus the editing view so the focus is not lost', () => {
  268. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  269. tableCellPropertiesButton.fire( 'execute' );
  270. expect( contextualBalloon.visibleView ).to.equal( tableCellPropertiesView );
  271. tableCellPropertiesView.fire( 'submit' );
  272. sinon.assert.calledOnce( spy );
  273. } );
  274. } );
  275. } );
  276. } );