tableselection.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. this.listenTo( editor.editing.view.document, 'delete', evt => {
  92. if ( this.hasMultiCellSelection ) {
  93. evt.stop();
  94. this.clearSelectedTableCells();
  95. }
  96. }, { priority: 'high' } );
  97. }
  98. /**
  99. * @inheritDoc
  100. */
  101. destroy() {
  102. super.destroy();
  103. this._mouseHandler.stopListening();
  104. }
  105. /**
  106. * Marks the table cell as a start of a table selection.
  107. *
  108. * editor.plugins.get( 'TableSelection' ).startSelectingFrom( tableCell );
  109. *
  110. * This method will clear the previous selection. The model selection will not be updated until
  111. * the {@link #setSelectingTo} method is used.
  112. *
  113. * @param {module:engine/model/element~Element} tableCell
  114. */
  115. startSelectingFrom( tableCell ) {
  116. this.clearSelection();
  117. this._startElement = tableCell;
  118. this._endElement = tableCell;
  119. }
  120. /**
  121. * Updates the current table selection end element. Table selection is defined by a start and an end element.
  122. * This method updates the end element. Must be preceded by {@link #startSelectingFrom}.
  123. *
  124. * editor.plugins.get( 'TableSelection' ).startSelectingFrom( startTableCell );
  125. *
  126. * editor.plugins.get( 'TableSelection' ).setSelectingTo( endTableCell );
  127. *
  128. * This method will update model selection if start and end cells are different and belongs to the same table.
  129. *
  130. * @param {module:engine/model/element~Element} tableCell
  131. */
  132. setSelectingTo( tableCell ) {
  133. if ( !this._startElement ) {
  134. this._startElement = tableCell;
  135. }
  136. const table = this._startElement.parent.parent;
  137. // Do not add tableCell to selection if it is from other table or is already set as end element.
  138. if ( table !== tableCell.parent.parent || this._endElement === tableCell ) {
  139. return;
  140. }
  141. this._endElement = tableCell;
  142. this._updateModelSelection();
  143. }
  144. /**
  145. * Stops the selection process (but do not clear the current selection).
  146. * The selection process is finished but the selection in the model remains.
  147. *
  148. * editor.plugins.get( 'TableSelection' ).startSelectingFrom( startTableCell );
  149. * editor.plugins.get( 'TableSelection' ).setSelectingTo( endTableCell );
  150. * editor.plugins.get( 'TableSelection' ).stopSelection();
  151. *
  152. * To clear the selection use {@link #clearSelection}.
  153. *
  154. * @param {module:engine/model/element~Element} [tableCell]
  155. */
  156. stopSelection( tableCell ) {
  157. if ( tableCell && tableCell.parent.parent === this._startElement.parent.parent ) {
  158. this._endElement = tableCell;
  159. }
  160. this._updateModelSelection();
  161. }
  162. /**
  163. * Stops the current selection process and clears the table selection in the model.
  164. *
  165. * editor.plugins.get( 'TableSelection' ).startSelectingFrom( startTableCell );
  166. * editor.plugins.get( 'TableSelection' ).setSelectingTo( endTableCell );
  167. * editor.plugins.get( 'TableSelection' ).stopSelection();
  168. *
  169. * editor.plugins.get( 'TableSelection' ).clearSelection();
  170. */
  171. clearSelection() {
  172. this._startElement = undefined;
  173. this._endElement = undefined;
  174. }
  175. /**
  176. * Returns an iterator for selected table cells.
  177. *
  178. * tableSelection.startSelectingFrom( startTableCell );
  179. * tableSelection.stopSelection( endTableCell );
  180. *
  181. * const selectedTableCells = Array.from( tableSelection.getSelectedTableCells() );
  182. * // The above array will represent a rectangular table selection.
  183. *
  184. * @returns {Iterable.<module:engine/model/element~Element>}
  185. */
  186. * getSelectedTableCells() {
  187. if ( !this.hasMultiCellSelection ) {
  188. return;
  189. }
  190. const startLocation = this._tableUtils.getCellLocation( this._startElement );
  191. const endLocation = this._tableUtils.getCellLocation( this._endElement );
  192. const startRow = Math.min( startLocation.row, endLocation.row );
  193. const endRow = Math.max( startLocation.row, endLocation.row );
  194. const startColumn = Math.min( startLocation.column, endLocation.column );
  195. const endColumn = Math.max( startLocation.column, endLocation.column );
  196. for ( const cellInfo of new TableWalker( findAncestor( 'table', this._startElement ), { startRow, endRow } ) ) {
  197. if ( cellInfo.column >= startColumn && cellInfo.column <= endColumn ) {
  198. yield cellInfo.cell;
  199. }
  200. }
  201. }
  202. /**
  203. * Returns selected table fragment as a document fragment.
  204. *
  205. * @returns {module:engine/model/documentfragment~DocumentFragment|undefined}
  206. */
  207. getSelectionAsFragment() {
  208. if ( !this.hasMultiCellSelection ) {
  209. return;
  210. }
  211. return this.editor.model.change( writer => {
  212. const documentFragment = writer.createDocumentFragment();
  213. const table = cropTable( this.getSelectedTableCells(), this._tableUtils, writer );
  214. writer.insert( table, documentFragment, 0 );
  215. return documentFragment;
  216. } );
  217. }
  218. // TODO: helper function?
  219. clearSelectedTableCells() {
  220. const model = this.editor.model;
  221. model.change( writer => {
  222. for ( const tableCell of this.getSelectedTableCells() ) {
  223. model.deleteContent( writer.createSelection( tableCell, 'in' ) );
  224. }
  225. } );
  226. }
  227. /**
  228. * Synchronizes the model selection with currently selected table cells.
  229. *
  230. * @private
  231. */
  232. _updateModelSelection() {
  233. if ( !this.hasMultiCellSelection ) {
  234. return;
  235. }
  236. const editor = this.editor;
  237. const model = editor.model;
  238. const modelRanges = [];
  239. for ( const tableCell of this.getSelectedTableCells() ) {
  240. modelRanges.push( model.createRangeOn( tableCell ) );
  241. }
  242. // Update model's selection
  243. model.change( writer => {
  244. writer.setSelection( modelRanges );
  245. } );
  246. }
  247. /**
  248. * Checks if the selection has changed via an external change and if it is required to clear the internal state of the plugin.
  249. *
  250. * @param {module:engine/model/documentselection~DocumentSelection} selection
  251. * @private
  252. */
  253. _clearSelectionOnExternalChange( selection ) {
  254. if ( selection.rangeCount <= 1 && this.hasMultiCellSelection ) {
  255. this.clearSelection();
  256. }
  257. }
  258. }