weakinsertdelta.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 InsertDelta from './insertdelta.js';
  7. import { register } from '../batch.js';
  8. import DeltaFactory from './deltafactory.js';
  9. import InsertOperation from '../operation/insertoperation.js';
  10. import NodeList from '../nodelist.js';
  11. /**
  12. * @classdesc
  13. * To provide specific OT behavior and better collisions solving, the {@link engine.model.Batch#insert} method
  14. * uses the `WeakInsertDelta` class which inherits from the `Delta` class and may overwrite some methods.
  15. *
  16. * @memberOf engine.model.delta
  17. */
  18. export default class WeakInsertDelta extends InsertDelta {
  19. /**
  20. * @inheritDoc
  21. */
  22. static get className() {
  23. return 'engine.model.delta.WeakInsertDelta';
  24. }
  25. }
  26. /**
  27. * Inserts a node or nodes at the given position. {@link engine.model.Batch#weakInsert weakInsert} is commonly used for actions
  28. * like typing or plain-text paste (without formatting). There are two differences between
  29. * {@link engine.model.Batch#insert insert} and {@link engine.model.Batch#weakInsert weakInsert}:
  30. *
  31. * * When using `weakInsert`, inserted nodes will have same attributes as the current attributes of
  32. * {@link engine.model.Document#selection document selection}.
  33. * * Normal behavior is that inserting inside range changed by
  34. * {@link engine.model.operation.AttributeOperation AttributeOperation} splits
  35. * the operation into two operations, which "omit" the inserted nodes. The correct behavior for `WeakInsertDelta` is that
  36. * {@link engine.model.operation.AttributeOperation AttributeOperation} does not "break" and also
  37. * applies attributes for inserted nodes.
  38. * The above has to be reflected during {@link engine.model.operation.transform operational transformation}.
  39. *
  40. * @chainable
  41. * @method engine.model.Batch#weakInsert
  42. * @param {engine.model.Position} position Position of insertion.
  43. * @param {engine.model.NodeSet} nodes The list of nodes to be inserted.
  44. */
  45. register( 'weakInsert', function( position, nodes ) {
  46. const delta = new WeakInsertDelta();
  47. this.addDelta( delta );
  48. nodes = new NodeList( nodes );
  49. for ( let node of nodes._nodes ) {
  50. node._attrs = new Map( this.doc.selection.getAttributes() );
  51. }
  52. const operation = new InsertOperation( position, nodes, this.doc.version );
  53. delta.addOperation( operation );
  54. this.doc.applyOperation( operation );
  55. return this;
  56. } );
  57. DeltaFactory.register( WeakInsertDelta );