tableselection.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import TableEditing from '../src/tableediting';
  9. import TableSelection from '../src/tableselection';
  10. import { assertSelectedCells, modelTable, viewTable } from './_utils/utils';
  11. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  12. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  13. import DocumentFragment from '@ckeditor/ckeditor5-engine/src/model/documentfragment';
  14. describe( 'table selection', () => {
  15. let editor, model, tableSelection, modelRoot;
  16. beforeEach( async () => {
  17. editor = await VirtualTestEditor.create( {
  18. plugins: [ TableEditing, TableSelection, Paragraph ]
  19. } );
  20. model = editor.model;
  21. modelRoot = model.document.getRoot();
  22. tableSelection = editor.plugins.get( TableSelection );
  23. setModelData( model, modelTable( [
  24. [ '11[]', '12', '13' ],
  25. [ '21', '22', '23' ],
  26. [ '31', '32', '33' ]
  27. ] ) );
  28. } );
  29. afterEach( async () => {
  30. await editor.destroy();
  31. } );
  32. describe( 'TableSelection', () => {
  33. describe( 'startSelectingFrom()', () => {
  34. it( 'should not change model selection', () => {
  35. const spy = sinon.spy();
  36. model.document.selection.on( 'change', spy );
  37. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  38. sinon.assert.notCalled( spy );
  39. } );
  40. } );
  41. describe( 'setSelectingTo()', () => {
  42. it( 'should set model selection on passed cell if startSelectingFrom() was not used', () => {
  43. const spy = sinon.spy();
  44. model.document.selection.on( 'change', spy );
  45. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  46. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  47. sinon.assert.calledOnce( spy );
  48. assertSelectedCells( model, [
  49. [ 1, 1, 0 ],
  50. [ 0, 0, 0 ],
  51. [ 0, 0, 0 ]
  52. ] );
  53. } );
  54. it( 'should change model selection if valid selection will be set', () => {
  55. const spy = sinon.spy();
  56. model.document.selection.on( 'change', spy );
  57. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  58. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  59. sinon.assert.calledOnce( spy );
  60. } );
  61. it( 'should not change model selection if passed table cell is from other table then start cell', () => {
  62. setModelData( model,
  63. modelTable( [
  64. [ '11[]', '12', '13' ],
  65. [ '21', '22', '23' ],
  66. [ '31', '32', '33' ]
  67. ] ) +
  68. modelTable( [
  69. [ 'a', 'b' ],
  70. [ 'c', 'd' ]
  71. ] )
  72. );
  73. const spy = sinon.spy();
  74. model.document.selection.on( 'change', spy );
  75. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  76. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 1, 0, 1 ] ) );
  77. sinon.assert.notCalled( spy );
  78. } );
  79. it( 'should select two table cells', () => {
  80. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  81. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  82. assertSelectedCells( model, [
  83. [ 1, 1, 0 ],
  84. [ 0, 0, 0 ],
  85. [ 0, 0, 0 ]
  86. ] );
  87. } );
  88. it( 'should select four table cells for diagonal selection', () => {
  89. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  90. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  91. assertSelectedCells( model, [
  92. [ 1, 1, 0 ],
  93. [ 1, 1, 0 ],
  94. [ 0, 0, 0 ]
  95. ] );
  96. } );
  97. it( 'should select row table cells', () => {
  98. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  99. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 2 ] ) );
  100. assertSelectedCells( model, [
  101. [ 1, 1, 1 ],
  102. [ 0, 0, 0 ],
  103. [ 0, 0, 0 ]
  104. ] );
  105. } );
  106. it( 'should select column table cells', () => {
  107. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  108. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 1 ] ) );
  109. assertSelectedCells( model, [
  110. [ 0, 1, 0 ],
  111. [ 0, 1, 0 ],
  112. [ 0, 1, 0 ]
  113. ] );
  114. } );
  115. it( 'should create proper selection on consecutive changes', () => {
  116. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  117. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 1 ] ) );
  118. assertSelectedCells( model, [
  119. [ 0, 0, 0 ],
  120. [ 0, 1, 0 ],
  121. [ 0, 1, 0 ]
  122. ] );
  123. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  124. assertSelectedCells( model, [
  125. [ 0, 1, 0 ],
  126. [ 0, 1, 0 ],
  127. [ 0, 0, 0 ]
  128. ] );
  129. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 2 ] ) );
  130. assertSelectedCells( model, [
  131. [ 0, 0, 0 ],
  132. [ 0, 1, 1 ],
  133. [ 0, 1, 1 ]
  134. ] );
  135. } );
  136. } );
  137. describe( 'stopSelection()', () => {
  138. it( 'should not clear currently selected cells if not cell was passed', () => {
  139. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  140. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  141. tableSelection.stopSelection();
  142. assertSelectedCells( model, [
  143. [ 1, 1, 0 ],
  144. [ 0, 0, 0 ],
  145. [ 0, 0, 0 ]
  146. ] );
  147. } );
  148. it( 'should change model selection if cell was passed', () => {
  149. const spy = sinon.spy();
  150. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  151. model.document.selection.on( 'change', spy );
  152. tableSelection.stopSelection( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  153. sinon.assert.calledOnce( spy );
  154. } );
  155. it( 'should extend selection to passed table cell', () => {
  156. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  157. tableSelection.stopSelection( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  158. assertSelectedCells( model, [
  159. [ 1, 1, 0 ],
  160. [ 0, 0, 0 ],
  161. [ 0, 0, 0 ]
  162. ] );
  163. } );
  164. } );
  165. describe( 'clearSelection()', () => {
  166. it( 'should not change model selection', () => {
  167. const spy = sinon.spy();
  168. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  169. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  170. model.document.selection.on( 'change', spy );
  171. tableSelection.clearSelection();
  172. sinon.assert.notCalled( spy );
  173. } );
  174. it( 'should not reset model selections', () => {
  175. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  176. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  177. tableSelection.clearSelection();
  178. assertSelectedCells( model, [
  179. [ 1, 1, 0 ],
  180. [ 0, 0, 0 ],
  181. [ 0, 0, 0 ]
  182. ] );
  183. } );
  184. } );
  185. describe( '* getSelectedTableCells()', () => {
  186. it( 'should return nothing if selection is not started', () => {
  187. expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [] );
  188. } );
  189. it( 'should return two table cells', () => {
  190. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  191. const lastCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  192. tableSelection.startSelectingFrom( firstCell );
  193. tableSelection.setSelectingTo( lastCell );
  194. expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [
  195. firstCell, lastCell
  196. ] );
  197. } );
  198. it( 'should return four table cells for diagonal selection', () => {
  199. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  200. const lastCell = modelRoot.getNodeByPath( [ 0, 1, 1 ] );
  201. tableSelection.startSelectingFrom( firstCell );
  202. tableSelection.setSelectingTo( lastCell );
  203. expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [
  204. firstCell, modelRoot.getNodeByPath( [ 0, 0, 1 ] ), modelRoot.getNodeByPath( [ 0, 1, 0 ] ), lastCell
  205. ] );
  206. } );
  207. it( 'should return row table cells', () => {
  208. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 0 ] );
  209. const lastCell = modelRoot.getNodeByPath( [ 0, 0, 2 ] );
  210. tableSelection.startSelectingFrom( firstCell );
  211. tableSelection.setSelectingTo( lastCell );
  212. expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [
  213. firstCell, modelRoot.getNodeByPath( [ 0, 0, 1 ] ), lastCell
  214. ] );
  215. } );
  216. it( 'should return column table cells', () => {
  217. const firstCell = modelRoot.getNodeByPath( [ 0, 0, 1 ] );
  218. const lastCell = modelRoot.getNodeByPath( [ 0, 2, 1 ] );
  219. tableSelection.startSelectingFrom( firstCell );
  220. tableSelection.setSelectingTo( lastCell );
  221. expect( Array.from( tableSelection.getSelectedTableCells() ) ).to.deep.equal( [
  222. firstCell, modelRoot.getNodeByPath( [ 0, 1, 1 ] ), lastCell
  223. ] );
  224. } );
  225. } );
  226. describe( 'getSelectionAsFragment()', () => {
  227. it( 'should return undefined if no table cells are selected', () => {
  228. expect( tableSelection.getSelectionAsFragment() ).to.be.undefined;
  229. } );
  230. it( 'should return document fragment for selected table cells', () => {
  231. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  232. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  233. expect( tableSelection.getSelectionAsFragment() ).to.be.instanceOf( DocumentFragment );
  234. } );
  235. } );
  236. describe( 'behavior', () => {
  237. it( 'should clear selection on external changes', () => {
  238. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  239. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  240. editor.model.change( writer => {
  241. writer.setSelection( modelRoot.getNodeByPath( [ 0, 0, 0, 0 ] ), 0 );
  242. } );
  243. assertSelectedCells( model, [
  244. [ 0, 0, 0 ],
  245. [ 0, 0, 0 ],
  246. [ 0, 0, 0 ]
  247. ] );
  248. expect( editor.editing.view.document.selection.isFake ).to.be.false;
  249. assertEqualMarkup( getViewData( editor.editing.view ), viewTable( [
  250. [ '{}11', '12', '13' ],
  251. [ '21', '22', '23' ],
  252. [ '31', '32', '33' ]
  253. ], { asWidget: true } ) );
  254. } );
  255. } );
  256. } );
  257. } );