tablepropertiesui.js 20 KB

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