8
0

inputcommand.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module typing/inputcommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import ChangeBuffer from './changebuffer';
  10. /**
  11. * The input command. Used by the {@link module:typing/input~Input input feature} to handle typing.
  12. *
  13. * @extends module:core/command~Command
  14. */
  15. export default class InputCommand extends Command {
  16. /**
  17. * Creates an instance of the command.
  18. *
  19. * @param {module:core/editor/editor~Editor} editor
  20. * @param {Number} undoStepSize The maximum number of atomic changes
  21. * which can be contained in one batch in the command buffer.
  22. */
  23. constructor( editor, undoStepSize ) {
  24. super( editor );
  25. /**
  26. * Typing's change buffer used to group subsequent changes into batches.
  27. *
  28. * @readonly
  29. * @private
  30. * @member {module:typing/changebuffer~ChangeBuffer} #_buffer
  31. */
  32. this._buffer = new ChangeBuffer( editor.model, undoStepSize );
  33. }
  34. /**
  35. * The current change buffer.
  36. *
  37. * @type {module:typing/changebuffer~ChangeBuffer}
  38. */
  39. get buffer() {
  40. return this._buffer;
  41. }
  42. /**
  43. * @inheritDoc
  44. */
  45. destroy() {
  46. super.destroy();
  47. this._buffer.destroy();
  48. }
  49. /**
  50. * Executes the input command. It replaces the content within the given range with the given text.
  51. * Replacing is a two step process, first the content within the range is removed and then the new text is inserted
  52. * at the beginning of the range (which after the removal is a collapsed range).
  53. *
  54. * @fires execute
  55. * @param {Object} [options] The command options.
  56. * @param {String} [options.text=''] The text to be inserted.
  57. * @param {module:engine/model/range~Range} [options.range] The range in which the text is inserted. Defaults
  58. * to the first range in the current selection.
  59. * @param {module:engine/model/range~Range} [options.resultRange] The range where the selection
  60. * should be placed after the insertion. If not specified, the selection will be placed right after
  61. * the inserted text.
  62. */
  63. execute( options = {} ) {
  64. const model = this.editor.model;
  65. const doc = model.document;
  66. const text = options.text || '';
  67. const textInsertions = text.length;
  68. const range = options.range || doc.selection.getFirstRange();
  69. const resultRange = options.resultRange;
  70. model.enqueueChange( this._buffer.batch, writer => {
  71. const isCollapsedRange = range.isCollapsed;
  72. this._buffer.lock();
  73. if ( !isCollapsedRange ) {
  74. writer.remove( range );
  75. }
  76. if ( text ) {
  77. writer.insertText( text, doc.selection.getAttributes(), range.start );
  78. }
  79. if ( resultRange ) {
  80. doc.selection.setRanges( [ resultRange ] );
  81. } else if ( isCollapsedRange ) {
  82. // If range was collapsed just shift the selection by the number of inserted characters.
  83. doc.selection.setCollapsedAt( range.start.getShiftedBy( textInsertions ) );
  84. }
  85. this._buffer.unlock();
  86. this._buffer.input( textInsertions );
  87. } );
  88. }
  89. }