tablepropertiesui.js 18 KB

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