8
0

tablecellpropertiesui.js 12 KB

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