tableselection.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. /**
  6. * @module table/tableselection
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import TableWalker from './tablewalker';
  10. import TableUtils from './tableutils';
  11. import { setupTableSelectionHighlighting } from './tableselection/converters';
  12. import MouseSelectionHandler from './tableselection/mouseselectionhandler';
  13. import { findAncestor } from './commands/utils';
  14. import cropTable from './tableselection/croptable';
  15. import '../theme/tableselection.css';
  16. /**
  17. * The table selection plugin.
  18. *
  19. * It introduces the ability to select table cells. The table selection is described by two nodes: start and end.
  20. * Both are the oposite corners of an rectangle that spans over them.
  21. *
  22. * Consider a table:
  23. *
  24. * 0 1 2 3
  25. * +---+---+---+---+
  26. * 0 | a | b | c | d |
  27. * +-------+ +---+
  28. * 1 | e | f | | g |
  29. * +---+---+---+---+
  30. * 2 | h | i | j |
  31. * +---+---+---+---+
  32. *
  33. * Setting the table selection start in table cell "b" and the end in table cell "g" will select table cells: "b", "c", "d", "f", and "g".
  34. * The cells that span over multiple rows or columns can extend over the selection rectangle. For instance, setting a selection from
  35. * the table cell "a" to the table cell "i" will create a selection in which the table cell "i" will be (partially) outside
  36. * the rectangle of selected cells: "a", "b", "e", "f", "h", and "i".
  37. *
  38. * @extends module:core/plugin~Plugin
  39. */
  40. export default class TableSelection extends Plugin {
  41. /**
  42. * @inheritDoc
  43. */
  44. static get pluginName() {
  45. return 'TableSelection';
  46. }
  47. /**
  48. * @inheritDoc
  49. */
  50. static get requires() {
  51. return [ TableUtils ];
  52. }
  53. /**
  54. * @inheritDoc
  55. */
  56. constructor( editor ) {
  57. super( editor );
  58. /**
  59. * A mouse selection handler.
  60. *
  61. * @private
  62. * @readonly
  63. * @member {module:table/tableselection/mouseselectionhandler~MouseSelectionHandler}
  64. */
  65. this._mouseHandler = new MouseSelectionHandler( this, this.editor.editing );
  66. /**
  67. * A reference to the table utilities used across the class.
  68. *
  69. * @private
  70. * @readonly
  71. * @member {module:table/tableutils~TableUtils} #_tableUtils
  72. */
  73. }
  74. /**
  75. * A flag indicating that there are selected table cells and the selection includes more than one table cell.
  76. *
  77. * @type {Boolean}
  78. */
  79. get hasMultiCellSelection() {
  80. return !!this._startElement && !!this._endElement && this._startElement !== this._endElement;
  81. }
  82. /**
  83. * @inheritDoc
  84. */
  85. init() {
  86. const editor = this.editor;
  87. const selection = editor.model.document.selection;
  88. this._tableUtils = editor.plugins.get( 'TableUtils' );
  89. setupTableSelectionHighlighting( editor, this );
  90. selection.on( 'change:range', () => this._clearSelectionOnExternalChange( selection ) );
  91. }
  92. /**
  93. * @inheritDoc
  94. */
  95. destroy() {
  96. super.destroy();
  97. this._mouseHandler.stopListening();
  98. }
  99. /**
  100. * Marks the table cell as a start of a table selection.
  101. *
  102. * editor.plugins.get( 'TableSelection' ).startSelectingFrom( tableCell );
  103. *
  104. * This method will clear the previous selection. The model selection will not be updated until
  105. * the {@link #setSelectingTo} method is used.
  106. *
  107. * @param {module:engine/model/element~Element} tableCell
  108. */
  109. startSelectingFrom( tableCell ) {
  110. this.clearSelection();
  111. this._startElement = tableCell;
  112. this._endElement = tableCell;
  113. }
  114. /**
  115. * Updates the current table selection end element. Table selection is defined by a start and an end element.
  116. * This method updates the end element. Must be preceded by {@link #startSelectingFrom}.
  117. *
  118. * editor.plugins.get( 'TableSelection' ).startSelectingFrom( startTableCell );
  119. *
  120. * editor.plugins.get( 'TableSelection' ).setSelectingTo( endTableCell );
  121. *
  122. * This method will update model selection if start and end cells are different and belongs to the same table.
  123. *
  124. * @param {module:engine/model/element~Element} tableCell
  125. */
  126. setSelectingTo( tableCell ) {
  127. if ( !this._startElement ) {
  128. this._startElement = tableCell;
  129. }
  130. const table = this._startElement.parent.parent;
  131. // Do not add tableCell to selection if it is from other table or is already set as end element.
  132. if ( table !== tableCell.parent.parent || this._endElement === tableCell ) {
  133. return;
  134. }
  135. this._endElement = tableCell;
  136. this._updateModelSelection();
  137. }
  138. /**
  139. * Stops the selection process (but do not clear the current selection).
  140. * The selection process is finished but the selection in the model remains.
  141. *
  142. * editor.plugins.get( 'TableSelection' ).startSelectingFrom( startTableCell );
  143. * editor.plugins.get( 'TableSelection' ).setSelectingTo( endTableCell );
  144. * editor.plugins.get( 'TableSelection' ).stopSelection();
  145. *
  146. * To clear the selection use {@link #clearSelection}.
  147. *
  148. * @param {module:engine/model/element~Element} [tableCell]
  149. */
  150. stopSelection( tableCell ) {
  151. if ( tableCell && tableCell.parent.parent === this._startElement.parent.parent ) {
  152. this._endElement = tableCell;
  153. }
  154. this._updateModelSelection();
  155. }
  156. /**
  157. * Stops the current selection process and clears the table selection in the model.
  158. *
  159. * editor.plugins.get( 'TableSelection' ).startSelectingFrom( startTableCell );
  160. * editor.plugins.get( 'TableSelection' ).setSelectingTo( endTableCell );
  161. * editor.plugins.get( 'TableSelection' ).stopSelection();
  162. *
  163. * editor.plugins.get( 'TableSelection' ).clearSelection();
  164. */
  165. clearSelection() {
  166. this._startElement = undefined;
  167. this._endElement = undefined;
  168. }
  169. /**
  170. * Returns an iterator for selected table cells.
  171. *
  172. * tableSelection.startSelectingFrom( startTableCell );
  173. * tableSelection.stopSelection( endTableCell );
  174. *
  175. * const selectedTableCells = Array.from( tableSelection.getSelectedTableCells() );
  176. * // The above array will represent a rectangular table selection.
  177. *
  178. * @returns {Iterable.<module:engine/model/element~Element>}
  179. */
  180. * getSelectedTableCells() {
  181. if ( !this.hasMultiCellSelection ) {
  182. return;
  183. }
  184. const startLocation = this._tableUtils.getCellLocation( this._startElement );
  185. const endLocation = this._tableUtils.getCellLocation( this._endElement );
  186. const startRow = Math.min( startLocation.row, endLocation.row );
  187. const endRow = Math.max( startLocation.row, endLocation.row );
  188. const startColumn = Math.min( startLocation.column, endLocation.column );
  189. const endColumn = Math.max( startLocation.column, endLocation.column );
  190. for ( const cellInfo of new TableWalker( findAncestor( 'table', this._startElement ), { startRow, endRow } ) ) {
  191. if ( cellInfo.column >= startColumn && cellInfo.column <= endColumn ) {
  192. yield cellInfo.cell;
  193. }
  194. }
  195. }
  196. /**
  197. * Returns selected table fragment as a document fragment.
  198. *
  199. * @returns {module:engine/model/documentfragment~DocumentFragment|undefined}
  200. */
  201. getSelectionAsFragment() {
  202. if ( !this.hasMultiCellSelection ) {
  203. return;
  204. }
  205. return this.editor.model.change( writer => {
  206. const documentFragment = writer.createDocumentFragment();
  207. const table = cropTable( this.getSelectedTableCells(), this._tableUtils, writer );
  208. writer.insert( table, documentFragment, 0 );
  209. return documentFragment;
  210. } );
  211. }
  212. /**
  213. * Synchronizes the model selection with currently selected table cells.
  214. *
  215. * @private
  216. */
  217. _updateModelSelection() {
  218. if ( !this.hasMultiCellSelection ) {
  219. return;
  220. }
  221. const editor = this.editor;
  222. const model = editor.model;
  223. const modelRanges = [];
  224. for ( const tableCell of this.getSelectedTableCells() ) {
  225. modelRanges.push( model.createRangeOn( tableCell ) );
  226. }
  227. // Update model's selection
  228. model.change( writer => {
  229. writer.setSelection( modelRanges );
  230. } );
  231. }
  232. /**
  233. * Checks if the selection has changed via an external change and if it is required to clear the internal state of the plugin.
  234. *
  235. * @param {module:engine/model/documentselection~DocumentSelection} selection
  236. * @private
  237. */
  238. _clearSelectionOnExternalChange( selection ) {
  239. if ( selection.rangeCount <= 1 && this.hasMultiCellSelection ) {
  240. this.clearSelection();
  241. }
  242. }
  243. }