document.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. CKEDITOR.define( [
  7. 'document/element',
  8. 'emittermixin',
  9. 'utils',
  10. 'ckeditorerror'
  11. ], function( Element, EmitterMixin, utils, CKEditorError ) {
  12. /**
  13. * Document model.
  14. *
  15. * @class document.Document
  16. */
  17. class Document {
  18. /**
  19. * Create an empty document.
  20. *
  21. * @constructor
  22. */
  23. constructor() {
  24. /**
  25. * Document tree root. Document always have an root document.
  26. *
  27. * @readonly
  28. * @property {String} root
  29. */
  30. this.root = new Element( 'root' );
  31. /**
  32. * Document version. It starts from 0 and every operation increase the version. It is used to ensure that
  33. * operations is applied on the proper document version. If the {@link document.Operation#baseVersion} will
  34. * not match document version an {@link document-applyOperation-wrong-version} error is fired.
  35. *
  36. * @readonly
  37. * @property {Number} version
  38. */
  39. this.version = 0;
  40. }
  41. /**
  42. * This is the only entry point for all document changes.
  43. *
  44. * @param {document.Operation} operation Operation to be applied.
  45. */
  46. applyOperation( operation ) {
  47. if ( operation.baseVersion !== this.version ) {
  48. /**
  49. * Only operations with matching versions can be applied.
  50. *
  51. * @error document-applyOperation-wrong-version
  52. * @param {document.Document} doc
  53. * @param {document.Operation} operation
  54. * @param {Number} baseVersion
  55. * @param {Number} documentVersion
  56. */
  57. throw new CKEditorError(
  58. 'document-applyOperation-wrong-version: Only operations with matching versions can be applied.',
  59. { doc: this, operation: operation, baseVersion: operation.baseVersion, documentVersion: this.version } );
  60. }
  61. operation._execute();
  62. this.version++;
  63. this.fire( 'operationApplied', operation );
  64. }
  65. }
  66. utils.extend( Document.prototype, EmitterMixin );
  67. return Document;
  68. } );