tablepropertiesui.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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/ui/utils';
  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. } );
  99. } );
  100. describe( 'destroy()', () => {
  101. it( 'should destroy the #view', () => {
  102. const spy = sinon.spy( tablePropertiesView, 'destroy' );
  103. tablePropertiesUI.destroy();
  104. sinon.assert.calledOnce( spy );
  105. } );
  106. } );
  107. describe( 'Properties #view', () => {
  108. beforeEach( () => {
  109. editor.model.change( writer => {
  110. writer.setSelection( editor.model.document.getRoot().getChild( 0 ).getChild( 0 ).getChild( 0 ), 0 );
  111. } );
  112. } );
  113. it( 'should hide on #submit', () => {
  114. tablePropertiesButton.fire( 'execute' );
  115. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  116. tablePropertiesView.fire( 'submit' );
  117. expect( contextualBalloon.visibleView ).to.be.null;
  118. } );
  119. describe( '#cancel event', () => {
  120. // https://github.com/ckeditor/ckeditor5/issues/6180
  121. it( 'should not undo if it there were no changes made to the property fields', () => {
  122. const spy = sinon.spy( editor, 'execute' );
  123. // Show the view. New batch will be created.
  124. tablePropertiesButton.fire( 'execute' );
  125. // Cancel the view immediately.
  126. tablePropertiesView.fire( 'cancel' );
  127. sinon.assert.notCalled( spy );
  128. } );
  129. it( 'should undo the entire batch of changes if there were some', () => {
  130. const spy = sinon.spy( editor, 'execute' );
  131. // Show the view. New batch will be created.
  132. tablePropertiesButton.fire( 'execute' );
  133. // Do the changes like a user.
  134. tablePropertiesView.borderStyle = 'dotted';
  135. tablePropertiesView.backgroundColor = 'red';
  136. expect( getModelData( editor.model ) ).to.equal(
  137. '<table backgroundColor="red" borderStyle="dotted">' +
  138. '<tableRow>' +
  139. '<tableCell>' +
  140. '<paragraph>[]foo</paragraph>' +
  141. '</tableCell>' +
  142. '</tableRow>' +
  143. '</table>' +
  144. '<paragraph>bar</paragraph>'
  145. );
  146. tablePropertiesView.fire( 'cancel' );
  147. expect( getModelData( editor.model ) ).to.equal(
  148. '<table>' +
  149. '<tableRow>' +
  150. '<tableCell>' +
  151. '<paragraph>[]foo</paragraph>' +
  152. '</tableCell>' +
  153. '</tableRow>' +
  154. '</table>' +
  155. '<paragraph>bar</paragraph>'
  156. );
  157. sinon.assert.calledWith( spy, 'undo', tablePropertiesUI._undoStepBatch );
  158. } );
  159. it( 'should hide the view', () => {
  160. tablePropertiesButton.fire( 'execute' );
  161. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  162. tablePropertiesView.fire( 'cancel' );
  163. expect( contextualBalloon.visibleView ).to.be.null;
  164. } );
  165. } );
  166. it( 'should hide on the Esc key press', () => {
  167. const keyEvtData = {
  168. keyCode: keyCodes.esc,
  169. preventDefault: sinon.spy(),
  170. stopPropagation: sinon.spy()
  171. };
  172. tablePropertiesButton.fire( 'execute' );
  173. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  174. tablePropertiesView.keystrokes.press( keyEvtData );
  175. expect( contextualBalloon.visibleView ).to.be.null;
  176. } );
  177. it( 'should hide if the table is no longer selected on EditorUI#update', () => {
  178. tablePropertiesButton.fire( 'execute' );
  179. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  180. editor.model.change( writer => {
  181. // Set selection in the paragraph.
  182. writer.setSelection( editor.model.document.getRoot().getChild( 1 ), 0 );
  183. } );
  184. expect( contextualBalloon.visibleView ).to.be.null;
  185. } );
  186. it( 'should reposition if table is still selected on on EditorUI#update', () => {
  187. tablePropertiesButton.fire( 'execute' );
  188. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  189. editor.model.change( writer => {
  190. writer.insertText( 'qux', editor.model.document.selection.getFirstPosition() );
  191. } );
  192. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  193. } );
  194. it( 'should hide if clicked outside the balloon', () => {
  195. tablePropertiesButton.fire( 'execute' );
  196. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  197. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  198. expect( contextualBalloon.visibleView ).to.be.null;
  199. } );
  200. describe( 'property changes', () => {
  201. beforeEach( () => {
  202. tablePropertiesUI._undoStepBatch = 'foo';
  203. } );
  204. describe( '#borderStyle', () => {
  205. it( 'should affect the editor state', () => {
  206. const spy = testUtils.sinon.stub( editor, 'execute' );
  207. tablePropertiesView.borderStyle = 'dotted';
  208. sinon.assert.calledOnce( spy );
  209. sinon.assert.calledWithExactly( spy, 'tableBorderStyle', { value: 'dotted', batch: 'foo' } );
  210. } );
  211. } );
  212. describe( '#borderColor', () => {
  213. it( 'should affect the editor state', () => {
  214. const spy = testUtils.sinon.stub( editor, 'execute' );
  215. tablePropertiesView.borderColor = '#FFAAFF';
  216. sinon.assert.calledOnce( spy );
  217. sinon.assert.calledWithExactly( spy, 'tableBorderColor', { value: '#FFAAFF', batch: 'foo' } );
  218. } );
  219. it( 'should display an error message if value is invalid', () => {
  220. const spy = testUtils.sinon.stub( editor, 'execute' );
  221. // First, let's pass an invalid value and check what happens.
  222. tablePropertiesView.borderColor = '42';
  223. clock.tick( 500 );
  224. expect( tablePropertiesView.borderColorInput.errorText ).to.match( /^The color is invalid/ );
  225. sinon.assert.notCalled( spy );
  226. // And now let's pass a valid value and check if the error text will be gone.
  227. tablePropertiesView.borderColor = '#AAA';
  228. clock.tick( 500 );
  229. expect( tablePropertiesView.borderColorInput.errorText ).to.be.null;
  230. sinon.assert.calledWithExactly( spy, 'tableBorderColor', { value: '#AAA', batch: 'foo' } );
  231. } );
  232. } );
  233. describe( '#borderWidth', () => {
  234. it( 'should affect the editor state', () => {
  235. const spy = testUtils.sinon.stub( editor, 'execute' );
  236. tablePropertiesView.borderWidth = '12px';
  237. sinon.assert.calledOnce( spy );
  238. sinon.assert.calledWithExactly( spy, 'tableBorderWidth', { value: '12px', batch: 'foo' } );
  239. } );
  240. it( 'should display an error message if value is invalid', () => {
  241. const spy = testUtils.sinon.stub( editor, 'execute' );
  242. // First, let's pass an invalid value and check what happens.
  243. tablePropertiesView.borderWidth = 'wrong';
  244. clock.tick( 500 );
  245. expect( tablePropertiesView.borderWidthInput.errorText ).to.match( /^The value is invalid/ );
  246. sinon.assert.notCalled( spy );
  247. // And now let's pass a valid value and check if the error text will be gone.
  248. tablePropertiesView.borderWidth = '3em';
  249. clock.tick( 500 );
  250. expect( tablePropertiesView.backgroundInput.errorText ).to.be.null;
  251. sinon.assert.calledWithExactly( spy, 'tableBorderWidth', { value: '3em', batch: 'foo' } );
  252. } );
  253. } );
  254. describe( '#backgroundColor', () => {
  255. it( 'should affect the editor state', () => {
  256. const spy = testUtils.sinon.stub( editor, 'execute' );
  257. tablePropertiesView.backgroundColor = '#FFAAFF';
  258. sinon.assert.calledOnce( spy );
  259. sinon.assert.calledWithExactly( spy, 'tableBackgroundColor', { value: '#FFAAFF', batch: 'foo' } );
  260. } );
  261. it( 'should display an error message if value is invalid', () => {
  262. const spy = testUtils.sinon.stub( editor, 'execute' );
  263. // First, let's pass an invalid value and check what happens.
  264. tablePropertiesView.backgroundColor = '42';
  265. clock.tick( 500 );
  266. expect( tablePropertiesView.backgroundInput.errorText ).to.match( /^The color is invalid/ );
  267. sinon.assert.notCalled( spy );
  268. // And now let's pass a valid value and check if the error text will be gone.
  269. tablePropertiesView.backgroundColor = '#AAA';
  270. clock.tick( 500 );
  271. expect( tablePropertiesView.backgroundInput.errorText ).to.be.null;
  272. sinon.assert.calledWithExactly( spy, 'tableBackgroundColor', { value: '#AAA', batch: 'foo' } );
  273. } );
  274. } );
  275. describe( '#width', () => {
  276. it( 'should affect the editor state', () => {
  277. const spy = testUtils.sinon.stub( editor, 'execute' );
  278. tablePropertiesView.width = '12px';
  279. sinon.assert.calledOnce( spy );
  280. sinon.assert.calledWithExactly( spy, 'tableWidth', { value: '12px', batch: 'foo' } );
  281. } );
  282. it( 'should display an error message if value is invalid', () => {
  283. const spy = testUtils.sinon.stub( editor, 'execute' );
  284. // First, let's pass an invalid value and check what happens.
  285. tablePropertiesView.width = 'wrong';
  286. clock.tick( 500 );
  287. expect( tablePropertiesView.widthInput.errorText ).to.match( /^The value is invalid/ );
  288. sinon.assert.notCalled( spy );
  289. // And now let's pass a valid value and check if the error text will be gone.
  290. tablePropertiesView.width = '3em';
  291. clock.tick( 500 );
  292. expect( tablePropertiesView.backgroundInput.errorText ).to.be.null;
  293. sinon.assert.calledWithExactly( spy, 'tableWidth', { value: '3em', batch: 'foo' } );
  294. } );
  295. } );
  296. describe( '#height', () => {
  297. it( 'should affect the editor state', () => {
  298. const spy = testUtils.sinon.stub( editor, 'execute' );
  299. tablePropertiesView.height = '12px';
  300. sinon.assert.calledOnce( spy );
  301. sinon.assert.calledWithExactly( spy, 'tableHeight', { value: '12px', batch: 'foo' } );
  302. } );
  303. it( 'should display an error message if value is invalid', () => {
  304. const spy = testUtils.sinon.stub( editor, 'execute' );
  305. // First, let's pass an invalid value and check what happens.
  306. tablePropertiesView.height = 'wrong';
  307. clock.tick( 500 );
  308. expect( tablePropertiesView.heightInput.errorText ).to.match( /^The value is invalid/ );
  309. sinon.assert.notCalled( spy );
  310. // And now let's pass a valid value and check if the error text will be gone.
  311. tablePropertiesView.height = '3em';
  312. clock.tick( 500 );
  313. expect( tablePropertiesView.backgroundInput.errorText ).to.be.null;
  314. sinon.assert.calledWithExactly( spy, 'tableHeight', { value: '3em', batch: 'foo' } );
  315. } );
  316. } );
  317. describe( '#alignment', () => {
  318. it( 'should affect the editor state', () => {
  319. const spy = testUtils.sinon.stub( editor, 'execute' );
  320. tablePropertiesView.alignment = 'right';
  321. sinon.assert.calledOnce( spy );
  322. sinon.assert.calledWithExactly( spy, 'tableAlignment', { value: 'right', batch: 'foo' } );
  323. } );
  324. } );
  325. it( 'should not display an error text if user managed to fix the value before the UI timeout', () => {
  326. // First, let's pass an invalid value.
  327. tablePropertiesView.borderColor = '#';
  328. clock.tick( 100 );
  329. // Then the user managed to quickly type the correct value.
  330. tablePropertiesView.borderColor = '#aaa';
  331. clock.tick( 400 );
  332. // Because they were quick, they should see no error
  333. expect( tablePropertiesView.borderColorInput.errorText ).to.be.null;
  334. } );
  335. it( 'should not affect the editor state if internal property has changed', () => {
  336. const spy = testUtils.sinon.stub( editor, 'execute' );
  337. tablePropertiesView.set( 'internalProp', 'foo' );
  338. tablePropertiesView.internalProp = 'bar';
  339. sinon.assert.notCalled( spy );
  340. } );
  341. } );
  342. } );
  343. describe( 'Showing the #view', () => {
  344. beforeEach( () => {
  345. editor.model.change( writer => {
  346. writer.setSelection( editor.model.document.getRoot().getChild( 0 ).getChild( 0 ).getChild( 0 ), 0 );
  347. } );
  348. } );
  349. it( 'should create a new undoable batch for further #view cancel', () => {
  350. tablePropertiesButton.fire( 'execute' );
  351. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  352. const firstBatch = tablePropertiesUI._undoStepBatch;
  353. expect( firstBatch ).to.be.instanceOf( Batch );
  354. tablePropertiesView.fire( 'submit' );
  355. expect( contextualBalloon.visibleView ).to.be.null;
  356. tablePropertiesButton.fire( 'execute' );
  357. const secondBatch = tablePropertiesUI._undoStepBatch;
  358. expect( secondBatch ).to.be.instanceOf( Batch );
  359. expect( firstBatch ).to.not.equal( secondBatch );
  360. } );
  361. describe( 'initial data', () => {
  362. it( 'should be set from the command values', () => {
  363. editor.commands.get( 'tableBorderStyle' ).value = 'a';
  364. editor.commands.get( 'tableBorderColor' ).value = 'b';
  365. editor.commands.get( 'tableBorderWidth' ).value = 'c';
  366. editor.commands.get( 'tableBackgroundColor' ).value = 'd';
  367. editor.commands.get( 'tableWidth' ).value = 'e';
  368. editor.commands.get( 'tableHeight' ).value = 'f';
  369. editor.commands.get( 'tableAlignment' ).value = 'g';
  370. tablePropertiesButton.fire( 'execute' );
  371. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  372. expect( tablePropertiesView ).to.include( {
  373. borderStyle: 'a',
  374. borderColor: 'b',
  375. borderWidth: 'c',
  376. backgroundColor: 'd',
  377. width: 'e',
  378. height: 'f',
  379. alignment: 'g'
  380. } );
  381. } );
  382. it( 'should use default values when command has no value', () => {
  383. editor.commands.get( 'tableBorderStyle' ).value = null;
  384. editor.commands.get( 'tableBorderColor' ).value = null;
  385. editor.commands.get( 'tableBorderWidth' ).value = null;
  386. editor.commands.get( 'tableBackgroundColor' ).value = null;
  387. editor.commands.get( 'tableWidth' ).value = null;
  388. editor.commands.get( 'tableHeight' ).value = null;
  389. editor.commands.get( 'tableAlignment' ).value = null;
  390. tablePropertiesButton.fire( 'execute' );
  391. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  392. expect( tablePropertiesView ).to.include( {
  393. borderStyle: 'none',
  394. borderColor: '',
  395. borderWidth: '',
  396. backgroundColor: '',
  397. width: '',
  398. height: '',
  399. alignment: ''
  400. } );
  401. } );
  402. } );
  403. it( 'should focus the form view', () => {
  404. const spy = testUtils.sinon.spy( tablePropertiesView, 'focus' );
  405. tablePropertiesButton.fire( 'execute' );
  406. sinon.assert.calledOnce( spy );
  407. } );
  408. } );
  409. describe( 'Hiding the #view', () => {
  410. beforeEach( () => {
  411. editor.model.change( writer => {
  412. writer.setSelection( editor.model.document.getRoot().getChild( 0 ).getChild( 0 ).getChild( 0 ), 0 );
  413. } );
  414. } );
  415. it( 'should stop listening to EditorUI#update', () => {
  416. const spy = testUtils.sinon.spy( tablePropertiesUI, 'stopListening' );
  417. tablePropertiesButton.fire( 'execute' );
  418. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  419. tablePropertiesView.fire( 'submit' );
  420. expect( contextualBalloon.visibleView ).to.be.null;
  421. sinon.assert.calledOnce( spy );
  422. sinon.assert.calledWithExactly( spy, editor.ui, 'update' );
  423. } );
  424. it( 'should focus the editing view so the focus is not lost', () => {
  425. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  426. tablePropertiesButton.fire( 'execute' );
  427. expect( contextualBalloon.visibleView ).to.equal( tablePropertiesView );
  428. tablePropertiesView.fire( 'submit' );
  429. sinon.assert.calledOnce( spy );
  430. } );
  431. } );
  432. } );
  433. } );