8
0

weakinsertdelta.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/model/delta/weakinsertdelta
  7. */
  8. import InsertDelta from './insertdelta';
  9. import { register } from '../batch';
  10. import DeltaFactory from './deltafactory';
  11. import InsertOperation from '../operation/insertoperation';
  12. import { normalizeNodes } from './../writer';
  13. /**
  14. * @classdesc
  15. * To provide specific OT behavior and better collisions solving, the {@link module:engine/model/batch~Batch#insert} method
  16. * uses the `WeakInsertDelta` class which inherits from the `Delta` class and may overwrite some methods.
  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 given position. {@link module:engine/model/batch~Batch#weakInsert weakInsert} is commonly used for actions
  28. * like typing or plain-text paste (without formatting). There are two differences between
  29. * {@link module:engine/model/batch~Batch#insert insert} and {@link module:engine/model/batch~Batch#weakInsert weakInsert}:
  30. *
  31. * * When using `weakInsert`, inserted nodes will have same attributes as the current attributes of
  32. * {@link module:engine/model/document~Document#selection document selection}.
  33. * * If {@link module:engine/model/operation/insertoperation~InsertOperation insert operation} position is inside a range changed by
  34. * {@link module:engine/model/operation/attributeoperation~AttributeOperation attribute operation},
  35. * the attribute operation is split into two operations.
  36. * Thanks to this, attribute change "omits" the inserted nodes. The correct behavior for `WeakInsertDelta` is that
  37. * {@link module:engine/model/operation/attributeoperation~AttributeOperation AttributeOperation} does not "break" and also
  38. * applies attributes for inserted nodes. This behavior has to be reflected during
  39. * {@link module:engine/model/delta/transform~transform delta transformation}.
  40. *
  41. * @chainable
  42. * @method module:engine/model/batch~Batch#weakInsert
  43. * @param {module:engine/model/position~Position} position Position of insertion.
  44. * @param {module:engine/model/node~NodeSet} nodes The list of nodes to be inserted.
  45. */
  46. register( 'weakInsert', function( position, nodes ) {
  47. const delta = new WeakInsertDelta();
  48. this.addDelta( delta );
  49. nodes = normalizeNodes( nodes );
  50. for ( let node of nodes ) {
  51. node.setAttributesTo( this.document.selection.getAttributes() );
  52. }
  53. const operation = new InsertOperation( position, nodes, this.document.version );
  54. delta.addOperation( operation );
  55. this.document.applyOperation( operation );
  56. return this;
  57. } );
  58. DeltaFactory.register( WeakInsertDelta );