inputcommand.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/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~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.document, undoStepSize );
  33. }
  34. /**
  35. * @inheritDoc
  36. */
  37. destroy() {
  38. super.destroy();
  39. this._buffer.destroy();
  40. this._buffer = null;
  41. }
  42. /**
  43. * The current change buffer.
  44. *
  45. * @type {module:typing/changebuffer~ChangeBuffer}
  46. */
  47. get buffer() {
  48. return this._buffer;
  49. }
  50. /**
  51. * Executes the input command. It replaces the content within the given range with the given text.
  52. * Replacing is a two step process, first content within the range is removed and then new text is inserted
  53. * on the beginning of the range (which after removal is a collapsed range).
  54. *
  55. * @param {Object} [options] The command options.
  56. * @param {String} [options.text] Text to be inserted.
  57. * @param {module:engine/model/range~Range} [options.range] Range in which the text is inserted. Defaults
  58. * to the first range in the current selection.
  59. * @param {module:engine/model/position~Position} [options.resultPosition] Position at which 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. _doExecute( options = {} ) {
  64. const doc = this.editor.document;
  65. const text = options.text;
  66. const range = options.range || doc.selection.getFirstRange();
  67. const resultPosition = options.resultPosition;
  68. let textInsertions = 0;
  69. if ( range && text !== undefined ) {
  70. doc.enqueueChanges( () => {
  71. const isCollapsedRange = range.isCollapsed;
  72. if ( !isCollapsedRange ) {
  73. this._buffer.batch.remove( range );
  74. }
  75. if ( text && text.length ) {
  76. textInsertions = text.length;
  77. this._buffer.batch.weakInsert( range.start, text );
  78. }
  79. if ( resultPosition ) {
  80. this.editor.data.model.selection.collapse( resultPosition );
  81. } else if ( isCollapsedRange ) {
  82. // If range was collapsed just shift the selection by the number of inserted characters.
  83. this.editor.data.model.selection.collapse( range.start.getShiftedBy( textInsertions ) );
  84. }
  85. this._buffer.input( textInsertions );
  86. } );
  87. }
  88. }
  89. }