8
0

deletecommand.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module typing/deletecommand
  7. */
  8. import Command from '../core/command/command.js';
  9. import Selection from '../engine/model/selection.js';
  10. import ChangeBuffer from './changebuffer.js';
  11. import count from '../utils/count.js';
  12. /**
  13. * The delete command. Used by the {@link module:typing/delete~Delete delete feature} to handle the <kbd>Delete</kbd> and
  14. * <kbd>Backspace</kbd> keys.
  15. *
  16. * @extends core.command.Command
  17. */
  18. export default class DeleteCommand extends Command {
  19. /**
  20. * Creates an instance of the command.
  21. *
  22. * @param {module:core/editor/editor~Editor} editor
  23. * @param {'forward'|'backward'} direction The directionality of the delete describing in what direction it
  24. * should consume the content when the selection is collapsed.
  25. */
  26. constructor( editor, direction ) {
  27. super( editor );
  28. /**
  29. * The directionality of the delete describing in what direction it should
  30. * consume the content when the selection is collapsed.
  31. *
  32. * @readonly
  33. * @member {'forward'|'backward'} #direction
  34. */
  35. this.direction = direction;
  36. /**
  37. * Delete's change buffer used to group subsequent changes into batches.
  38. *
  39. * @readonly
  40. * @private
  41. * @member {typing.ChangeBuffer} #buffer
  42. */
  43. this._buffer = new ChangeBuffer( editor.document, editor.config.get( 'undo.step' ) );
  44. }
  45. /**
  46. * Executes the delete command. Depending on whether the selection is collapsed or not, deletes its content
  47. * or a piece of content in the {@link #direction defined direction}.
  48. *
  49. * @param {Object} [options] The command options.
  50. * @param {'character'} [options.unit='character'] See {@link module:engine/controller/modifyselection~modifySelection}'s
  51. * options.
  52. */
  53. _doExecute( options = {} ) {
  54. const doc = this.editor.document;
  55. const dataController = this.editor.data;
  56. doc.enqueueChanges( () => {
  57. const selection = Selection.createFromSelection( doc.selection );
  58. // Try to extend the selection in the specified direction.
  59. if ( selection.isCollapsed ) {
  60. dataController.modifySelection( selection, { direction: this.direction, unit: options.unit } );
  61. }
  62. // If selection is still collapsed, then there's nothing to delete.
  63. if ( selection.isCollapsed ) {
  64. return;
  65. }
  66. let changeCount = 0;
  67. selection.getFirstRange().getMinimalFlatRanges().forEach( ( range ) => {
  68. changeCount += count(
  69. range.getWalker( { singleCharacters: true, ignoreElementEnd: true, shallow: true } )
  70. );
  71. } );
  72. dataController.deleteContent( selection, this._buffer.batch, { merge: true } );
  73. this._buffer.input( changeCount );
  74. doc.selection.setRanges( selection.getRanges(), selection.isBackward );
  75. } );
  76. }
  77. }