weakinsertdelta.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import Delta from './delta.js';
  7. import { register } from '../batch-base.js';
  8. import InsertOperation from '../operation/insertoperation.js';
  9. import NodeList from '../nodelist.js';
  10. /**
  11. * To provide specific OT behavior and better collisions solving, the {@link treeModel.Batch#insert} method
  12. * uses the `WeakInsertDelta` class which inherits from the `Delta` class and may overwrite some methods.
  13. *
  14. * @class treeModel.delta.WeakInsertDelta
  15. */
  16. export default class WeakInsertDelta extends Delta {}
  17. /**
  18. * Inserts a node or nodes at the given position. {@link treeModel.Batch#weakInsert} is commonly used for actions
  19. * like typing or plain-text paste (without formatting). There are two differences between
  20. * {@link treeModel.Batch#insert} and {@link treeModel.Batch#weakInsert}:
  21. * * When using `weakInsert`, inserted nodes will have same attributes as the current attributes of
  22. * {@link treeModel.Document#selection document selection}.
  23. * * The above has to be reflected during {@link treeModel.operation.transform operational transformation}. Normal
  24. * behavior is that inserting inside range changed by {@link treeModel.operation.AttributeOperation} splits the operation
  25. * into two operations, which "omit" the inserted nodes. The correct behavior for `WeakInsertDelta` is that
  26. * {@link treeModel.operation.AttributeOperation} does not "break" and also applies attributes for inserted nodes.
  27. *
  28. * @chainable
  29. * @memberOf treeModel.Batch
  30. * @method weakInsert
  31. * @param {treeModel.Position} position Position of insertion.
  32. * @param {treeModel.NodeSet} nodes The list of nodes to be inserted.
  33. */
  34. register( 'weakInsert', function( position, nodes ) {
  35. const delta = new WeakInsertDelta();
  36. nodes = new NodeList( nodes );
  37. for ( let node of nodes._nodes ) {
  38. node._attrs = new Map( this.doc.selection.getAttributes() );
  39. }
  40. const operation = new InsertOperation( position, nodes, this.doc.version );
  41. this.doc.applyOperation( operation );
  42. delta.addOperation( operation );
  43. this.addDelta( delta );
  44. return this;
  45. } );