/** * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ 'use strict'; import CKEditorError from '../../utils/ckeditorerror.js'; /** * The Batch class groups document changes (deltas). All deltas grouped in a single Batch can be * reverted together, so you can think about the Batch as a single undo step. If you want to extend one * undo step you can call another method on the same Batch object. If you want to create a separate undo step * you can create a new Batch. * * For example to create two separate undo steps you can call: * * doc.batch().insert( firstPosition, 'foo' ); * doc.batch().insert( secondPosition, 'bar' ); * * To create a single undo step: * * const batch = doc.batch(); * batch.insert( firstPosition, 'foo' ); * batch.insert( secondPosition, 'bar' ); * * Note that all document modification methods (insert, remove, split, etc.) are chainable so you can shorten code to: * * doc.batch().insert( firstPosition, 'foo' ).insert( secondPosition, 'bar' ); * * @memberOf engine.treeModel */ export default class Batch { /** * Creates Batch instance. Not recommended to use directly, use {@link engine.treeModel.Document#batch} instead. * * @param {engine.treeModel.Document} doc Document which this Batch changes. */ constructor( doc ) { /** * Document which this Batch changes. * * @member {engine.treeModel.Document} engine.treeModel.Batch#doc * @readonly */ this.doc = doc; /** * Array of deltas which compose Batch. * * @member {Array.} engine.treeModel.Batch#deltas * @readonly */ this.deltas = []; } /** * Adds delta to the Batch instance. All modification methods (insert, remove, split, etc.) use this method * to add created deltas. * * @param {engine.treeModel.delta.Delta} delta Delta to add. * @return {engine.treeModel.delta.Delta} Added delta. */ addDelta( delta ) { delta.batch = this; this.deltas.push( delta ); return delta; } /** * Gets an iterable collection of operations. * * @returns {Iterable.