tablepropertiesui.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 TablePropertiesEditing from '../../src/tableproperties/tablepropertiesediting';
  17. import TablePropertiesUI from '../../src/tableproperties/tablepropertiesui';
  18. import TablePropertiesUIView from '../../src/tableproperties/ui/tablepropertiesview';
  19. describe( 'table properties', () => {
  20. describe( 'TablePropertiesUI', () => {
  21. let editor, editorElement, contextualBalloon,
  22. tablePropertiesUI, tablePropertiesView, tablePropertiesButton,
  23. clock;
  24. testUtils.createSinonSandbox();
  25. beforeEach( () => {
  26. clock = sinon.useFakeTimers();
  27. editorElement = document.createElement( 'div' );
  28. document.body.appendChild( editorElement );
  29. return ClassicTestEditor
  30. .create( editorElement, {
  31. plugins: [ Table, TablePropertiesEditing, TablePropertiesUI, Paragraph, Undo ],
  32. initialData: '<table><tr><td>foo</td></tr></table><p>bar</p>'
  33. } )
  34. .then( newEditor => {
  35. editor = newEditor;
  36. tablePropertiesUI = editor.plugins.get( TablePropertiesUI );
  37. tablePropertiesButton = editor.ui.componentFactory.create( 'tableProperties' );
  38. contextualBalloon = editor.plugins.get( ContextualBalloon );
  39. tablePropertiesView = tablePropertiesUI.view;
  40. // There is no point to execute BalloonPanelView attachTo and pin methods so lets override it.
  41. testUtils.sinon.stub( contextualBalloon.view, 'attachTo' ).returns( {} );
  42. testUtils.sinon.stub( contextualBalloon.view, 'pin' ).returns( {} );
  43. } );
  44. } );
  45. afterEach( () => {
  46. clock.restore();
  47. editorElement.remove();
  48. return editor.destroy();
  49. } );
  50. it( 'should be named', () => {
  51. expect( TablePropertiesUI.pluginName ).to.equal( 'TablePropertiesUI' );
  52. } );
  53. it( 'should load ContextualBalloon', () => {
  54. expect( editor.plugins.get( ContextualBalloon ) ).to.be.instanceOf( ContextualBalloon );
  55. } );
  56. describe( 'init()', () => {
  57. it( 'should set a batch', () => {
  58. expect( tablePropertiesUI._undoStepBatch ).to.be.null;
  59. } );
  60. describe( '#view', () => {
  61. it( 'should be created', () => {
  62. expect( tablePropertiesUI.view ).to.be.instanceOf( TablePropertiesUIView );
  63. } );
  64. it( 'should be rendered', () => {
  65. expect( tablePropertiesUI.view.isRendered ).to.be.true;
  66. } );
  67. } );
  68. describe( 'toolbar button', () => {
  69. it( 'should be registered', () => {
  70. expect( tablePropertiesButton ).to.be.instanceOf( ButtonView );
  71. } );
  72. it( 'should have a label', () => {
  73. expect( tablePropertiesButton.label ).to.equal( 'Table properties' );
  74. } );
  75. it( 'should have a tooltip', () => {
  76. expect( tablePropertiesButton.tooltip ).to.be.true;
  77. } );
  78. it( 'should call #_showView upon #execute', () => {
  79. const spy = testUtils.sinon.stub( tablePropertiesUI, '_showView' ).returns( {} );
  80. tablePropertiesButton.fire( 'execute' );
  81. sinon.assert.calledOnce( spy );
  82. } );
  83. } );
  84. } );
  85. describe( 'destroy()', () => {
  86. it( 'should destroy the #view', () => {
  87. const spy = sinon.spy( tablePropertiesView, 'destroy' );
  88. tablePropertiesUI.destroy();
  89. sinon.assert.calledOnce( spy );
  90. } );
  91. } );
  92. describe( 'Properties #view', () => {
  93. beforeEach( () => {
  94. editor.model.change( writer => {
  95. writer.setSelection( editor.model.document.getRoot().getChild( 0 ).getChild( 0 ).getChild( 0 ), 0 );
  96. } );
  97. } );
  98. it( 'should hide on #submit', () => {
  99. tablePropertiesButton.fire( 'execute' );
  100. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  101. tablePropertiesView.fire( 'submit' );
  102. expect( contextualBalloon.visibleView ).to.be.null;
  103. } );
  104. it( 'should undo the entire batch of changes on #cancel', () => {
  105. // Show the view. New batch will be created.
  106. tablePropertiesButton.fire( 'execute' );
  107. // Do the changes like a user.
  108. tablePropertiesView.borderStyle = 'dotted';
  109. tablePropertiesView.backgroundColor = 'red';
  110. expect( getModelData( editor.model ) ).to.equal(
  111. '<table backgroundColor="red" borderStyle="dotted">' +
  112. '<tableRow>' +
  113. '<tableCell>' +
  114. '<paragraph>[]foo</paragraph>' +
  115. '</tableCell>' +
  116. '</tableRow>' +
  117. '</table>' +
  118. '<paragraph>bar</paragraph>'
  119. );
  120. tablePropertiesView.fire( 'cancel' );
  121. expect( getModelData( editor.model ) ).to.equal(
  122. '<table>' +
  123. '<tableRow>' +
  124. '<tableCell>' +
  125. '<paragraph>[]foo</paragraph>' +
  126. '</tableCell>' +
  127. '</tableRow>' +
  128. '</table>' +
  129. '<paragraph>bar</paragraph>'
  130. );
  131. } );
  132. it( 'should hide on #cancel', () => {
  133. tablePropertiesButton.fire( 'execute' );
  134. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  135. tablePropertiesView.fire( 'cancel' );
  136. expect( contextualBalloon.visibleView ).to.be.null;
  137. } );
  138. it( 'should hide on the Esc key press', () => {
  139. const keyEvtData = {
  140. keyCode: keyCodes.esc,
  141. preventDefault: sinon.spy(),
  142. stopPropagation: sinon.spy()
  143. };
  144. tablePropertiesButton.fire( 'execute' );
  145. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  146. tablePropertiesView.keystrokes.press( keyEvtData );
  147. expect( contextualBalloon.visibleView ).to.be.null;
  148. } );
  149. it( 'should hide if the table is no longer selected on EditorUI#update', () => {
  150. tablePropertiesButton.fire( 'execute' );
  151. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  152. editor.model.change( writer => {
  153. // Set selection in the paragraph.
  154. writer.setSelection( editor.model.document.getRoot().getChild( 1 ), 0 );
  155. } );
  156. expect( contextualBalloon.visibleView ).to.be.null;
  157. } );
  158. it( 'should reposition if table is still selected on on EditorUI#update', () => {
  159. tablePropertiesButton.fire( 'execute' );
  160. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  161. editor.model.change( writer => {
  162. writer.insertText( 'qux', editor.model.document.selection.getFirstPosition() );
  163. } );
  164. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  165. } );
  166. it( 'should hide if clicked outside the balloon', () => {
  167. tablePropertiesButton.fire( 'execute' );
  168. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  169. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  170. expect( contextualBalloon.visibleView ).to.be.null;
  171. } );
  172. describe( 'property changes', () => {
  173. beforeEach( () => {
  174. tablePropertiesUI._undoStepBatch = 'foo';
  175. } );
  176. describe( '#borderStyle', () => {
  177. it( 'should affect the editor state', () => {
  178. const spy = testUtils.sinon.stub( editor, 'execute' );
  179. tablePropertiesView.borderStyle = 'dotted';
  180. sinon.assert.calledOnce( spy );
  181. sinon.assert.calledWithExactly( spy, 'tableBorderStyle', { value: 'dotted', batch: 'foo' } );
  182. } );
  183. } );
  184. describe( '#borderColor', () => {
  185. it( 'should affect the editor state', () => {
  186. const spy = testUtils.sinon.stub( editor, 'execute' );
  187. tablePropertiesView.borderColor = '#FFAAFF';
  188. sinon.assert.calledOnce( spy );
  189. sinon.assert.calledWithExactly( spy, 'tableBorderColor', { value: '#FFAAFF', batch: 'foo' } );
  190. } );
  191. it( 'should display an error message if value is invalid', () => {
  192. const spy = testUtils.sinon.stub( editor, 'execute' );
  193. // First, let's pass an invalid value and check what happens.
  194. tablePropertiesView.borderColor = '42';
  195. clock.tick( 500 );
  196. expect( tablePropertiesView.borderColorInput.errorText ).to.match( /^The color is invalid/ );
  197. sinon.assert.notCalled( spy );
  198. // And now let's pass a valid value and check if the error text will be gone.
  199. tablePropertiesView.borderColor = '#AAA';
  200. clock.tick( 500 );
  201. expect( tablePropertiesView.borderColorInput.errorText ).to.be.null;
  202. sinon.assert.calledWithExactly( spy, 'tableBorderColor', { value: '#AAA', batch: 'foo' } );
  203. } );
  204. } );
  205. describe( '#borderWidth', () => {
  206. it( 'should affect the editor state', () => {
  207. const spy = testUtils.sinon.stub( editor, 'execute' );
  208. tablePropertiesView.borderWidth = '12px';
  209. sinon.assert.calledOnce( spy );
  210. sinon.assert.calledWithExactly( spy, 'tableBorderWidth', { value: '12px', batch: 'foo' } );
  211. } );
  212. it( 'should display an error message if value is invalid', () => {
  213. const spy = testUtils.sinon.stub( editor, 'execute' );
  214. // First, let's pass an invalid value and check what happens.
  215. tablePropertiesView.borderWidth = 'wrong';
  216. clock.tick( 500 );
  217. expect( tablePropertiesView.borderWidthInput.errorText ).to.match( /^The value is invalid/ );
  218. sinon.assert.notCalled( spy );
  219. // And now let's pass a valid value and check if the error text will be gone.
  220. tablePropertiesView.borderWidth = '3em';
  221. clock.tick( 500 );
  222. expect( tablePropertiesView.backgroundInput.errorText ).to.be.null;
  223. sinon.assert.calledWithExactly( spy, 'tableBorderWidth', { value: '3em', batch: 'foo' } );
  224. } );
  225. } );
  226. describe( '#backgroundColor', () => {
  227. it( 'should affect the editor state', () => {
  228. const spy = testUtils.sinon.stub( editor, 'execute' );
  229. tablePropertiesView.backgroundColor = '#FFAAFF';
  230. sinon.assert.calledOnce( spy );
  231. sinon.assert.calledWithExactly( spy, 'tableBackgroundColor', { value: '#FFAAFF', batch: 'foo' } );
  232. } );
  233. it( 'should display an error message if value is invalid', () => {
  234. const spy = testUtils.sinon.stub( editor, 'execute' );
  235. // First, let's pass an invalid value and check what happens.
  236. tablePropertiesView.backgroundColor = '42';
  237. clock.tick( 500 );
  238. expect( tablePropertiesView.backgroundInput.errorText ).to.match( /^The color is invalid/ );
  239. sinon.assert.notCalled( spy );
  240. // And now let's pass a valid value and check if the error text will be gone.
  241. tablePropertiesView.backgroundColor = '#AAA';
  242. clock.tick( 500 );
  243. expect( tablePropertiesView.backgroundInput.errorText ).to.be.null;
  244. sinon.assert.calledWithExactly( spy, 'tableBackgroundColor', { value: '#AAA', batch: 'foo' } );
  245. } );
  246. } );
  247. describe( '#width', () => {
  248. it( 'should affect the editor state', () => {
  249. const spy = testUtils.sinon.stub( editor, 'execute' );
  250. tablePropertiesView.width = '12px';
  251. sinon.assert.calledOnce( spy );
  252. sinon.assert.calledWithExactly( spy, 'tableWidth', { value: '12px', batch: 'foo' } );
  253. } );
  254. it( 'should display an error message if value is invalid', () => {
  255. const spy = testUtils.sinon.stub( editor, 'execute' );
  256. // First, let's pass an invalid value and check what happens.
  257. tablePropertiesView.width = 'wrong';
  258. clock.tick( 500 );
  259. expect( tablePropertiesView.widthInput.errorText ).to.match( /^The value is invalid/ );
  260. sinon.assert.notCalled( spy );
  261. // And now let's pass a valid value and check if the error text will be gone.
  262. tablePropertiesView.width = '3em';
  263. clock.tick( 500 );
  264. expect( tablePropertiesView.backgroundInput.errorText ).to.be.null;
  265. sinon.assert.calledWithExactly( spy, 'tableWidth', { value: '3em', batch: 'foo' } );
  266. } );
  267. } );
  268. describe( '#height', () => {
  269. it( 'should affect the editor state', () => {
  270. const spy = testUtils.sinon.stub( editor, 'execute' );
  271. tablePropertiesView.height = '12px';
  272. sinon.assert.calledOnce( spy );
  273. sinon.assert.calledWithExactly( spy, 'tableHeight', { value: '12px', batch: 'foo' } );
  274. } );
  275. it( 'should display an error message if value is invalid', () => {
  276. const spy = testUtils.sinon.stub( editor, 'execute' );
  277. // First, let's pass an invalid value and check what happens.
  278. tablePropertiesView.height = 'wrong';
  279. clock.tick( 500 );
  280. expect( tablePropertiesView.heightInput.errorText ).to.match( /^The value is invalid/ );
  281. sinon.assert.notCalled( spy );
  282. // And now let's pass a valid value and check if the error text will be gone.
  283. tablePropertiesView.height = '3em';
  284. clock.tick( 500 );
  285. expect( tablePropertiesView.backgroundInput.errorText ).to.be.null;
  286. sinon.assert.calledWithExactly( spy, 'tableHeight', { value: '3em', batch: 'foo' } );
  287. } );
  288. } );
  289. describe( '#alignment', () => {
  290. it( 'should affect the editor state', () => {
  291. const spy = testUtils.sinon.stub( editor, 'execute' );
  292. tablePropertiesView.alignment = 'right';
  293. sinon.assert.calledOnce( spy );
  294. sinon.assert.calledWithExactly( spy, 'tableAlignment', { value: 'right', batch: 'foo' } );
  295. } );
  296. } );
  297. it( 'should not display an error text if user managed to fix the value before the UI timeout', () => {
  298. // First, let's pass an invalid value.
  299. tablePropertiesView.borderColor = '#';
  300. clock.tick( 100 );
  301. // Then the user managed to quickly type the correct value.
  302. tablePropertiesView.borderColor = '#aaa';
  303. clock.tick( 400 );
  304. // Because they were quick, they should see no error
  305. expect( tablePropertiesView.borderColorInput.errorText ).to.be.null;
  306. } );
  307. it( 'should not affect the editor state if internal property has changed', () => {
  308. const spy = testUtils.sinon.stub( editor, 'execute' );
  309. tablePropertiesView.set( 'internalProp', 'foo' );
  310. tablePropertiesView.internalProp = 'bar';
  311. sinon.assert.notCalled( spy );
  312. } );
  313. } );
  314. } );
  315. describe( 'Showing the #view', () => {
  316. beforeEach( () => {
  317. editor.model.change( writer => {
  318. writer.setSelection( editor.model.document.getRoot().getChild( 0 ).getChild( 0 ).getChild( 0 ), 0 );
  319. } );
  320. } );
  321. it( 'should create a new undoable batch for further #view cancel', () => {
  322. tablePropertiesButton.fire( 'execute' );
  323. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  324. const firstBatch = tablePropertiesUI._undoStepBatch;
  325. expect( firstBatch ).to.be.instanceOf( Batch );
  326. tablePropertiesView.fire( 'submit' );
  327. expect( contextualBalloon.visibleView ).to.be.null;
  328. tablePropertiesButton.fire( 'execute' );
  329. const secondBatch = tablePropertiesUI._undoStepBatch;
  330. expect( secondBatch ).to.be.instanceOf( Batch );
  331. expect( firstBatch ).to.not.equal( secondBatch );
  332. } );
  333. describe( 'initial data', () => {
  334. it( 'should be set from the command values', () => {
  335. editor.commands.get( 'tableBorderStyle' ).value = 'a';
  336. editor.commands.get( 'tableBorderColor' ).value = 'b';
  337. editor.commands.get( 'tableBorderWidth' ).value = 'c';
  338. editor.commands.get( 'tableBackgroundColor' ).value = 'd';
  339. editor.commands.get( 'tableWidth' ).value = 'e';
  340. editor.commands.get( 'tableHeight' ).value = 'f';
  341. editor.commands.get( 'tableAlignment' ).value = 'g';
  342. tablePropertiesButton.fire( 'execute' );
  343. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  344. expect( tablePropertiesView ).to.include( {
  345. borderStyle: 'a',
  346. borderColor: 'b',
  347. borderWidth: 'c',
  348. backgroundColor: 'd',
  349. width: 'e',
  350. height: 'f',
  351. alignment: 'g'
  352. } );
  353. } );
  354. it( 'should use default values when command has no value', () => {
  355. editor.commands.get( 'tableBorderStyle' ).value = null;
  356. editor.commands.get( 'tableBorderColor' ).value = null;
  357. editor.commands.get( 'tableBorderWidth' ).value = null;
  358. editor.commands.get( 'tableBackgroundColor' ).value = null;
  359. editor.commands.get( 'tableWidth' ).value = null;
  360. editor.commands.get( 'tableHeight' ).value = null;
  361. editor.commands.get( 'tableAlignment' ).value = null;
  362. tablePropertiesButton.fire( 'execute' );
  363. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  364. expect( tablePropertiesView ).to.include( {
  365. borderStyle: 'none',
  366. borderColor: '',
  367. borderWidth: '',
  368. backgroundColor: '',
  369. width: '',
  370. height: '',
  371. alignment: 'center'
  372. } );
  373. } );
  374. } );
  375. it( 'should focus the form view', () => {
  376. const spy = testUtils.sinon.spy( tablePropertiesView, 'focus' );
  377. tablePropertiesButton.fire( 'execute' );
  378. sinon.assert.calledOnce( spy );
  379. } );
  380. } );
  381. describe( 'Hiding the #view', () => {
  382. beforeEach( () => {
  383. editor.model.change( writer => {
  384. writer.setSelection( editor.model.document.getRoot().getChild( 0 ).getChild( 0 ).getChild( 0 ), 0 );
  385. } );
  386. } );
  387. it( 'should stop listening to EditorUI#update', () => {
  388. const spy = testUtils.sinon.spy( tablePropertiesUI, 'stopListening' );
  389. tablePropertiesButton.fire( 'execute' );
  390. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  391. tablePropertiesView.fire( 'submit' );
  392. expect( contextualBalloon.visibleView ).to.be.null;
  393. sinon.assert.calledOnce( spy );
  394. sinon.assert.calledWithExactly( spy, editor.ui, 'update' );
  395. } );
  396. it( 'should focus the editing view so the focus is not lost', () => {
  397. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  398. tablePropertiesButton.fire( 'execute' );
  399. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  400. tablePropertiesView.fire( 'submit' );
  401. sinon.assert.calledOnce( spy );
  402. } );
  403. } );
  404. } );
  405. } );