tableselection.js 9.5 KB

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