document.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. 'document/rootelement',
  9. 'emittermixin',
  10. 'utils',
  11. 'ckeditorerror'
  12. ], function( Element, RootElement, EmitterMixin, utils, CKEditorError ) {
  13. var graveyardSymbol = Symbol( 'graveyard' );
  14. /**
  15. * Document model.
  16. *
  17. * @class document.Document
  18. */
  19. class Document {
  20. /**
  21. * Creates an empty document instance.
  22. *
  23. * @constructor
  24. */
  25. constructor() {
  26. /**
  27. * List of roots that are owned and managed by this document.
  28. *
  29. * @readonly
  30. * @property {Map} roots
  31. */
  32. this.roots = new Map();
  33. // Graveyard tree root. Document always have a graveyard root, which stores removed nodes.
  34. this.createRoot( graveyardSymbol );
  35. /**
  36. * Document version. It starts from `0` and every operation increases the version number. It is used to ensure that
  37. * operations are applied on the proper document version. If the {@link document.operation.Operation#baseVersion} will
  38. * not match document version the {@link document-applyOperation-wrong-version} error is thrown.
  39. *
  40. * @readonly
  41. * @property {Number} version
  42. */
  43. this.version = 0;
  44. }
  45. /**
  46. * This is the only entry point for all document changes.
  47. *
  48. * @param {document.operation.Operation} operation Operation to be applied.
  49. */
  50. applyOperation( operation ) {
  51. if ( operation.baseVersion !== this.version ) {
  52. /**
  53. * Only operations with matching versions can be applied.
  54. *
  55. * @error document-applyOperation-wrong-version
  56. * @param {document.operation.Operation} operation
  57. */
  58. throw new CKEditorError(
  59. 'document-applyOperation-wrong-version: Only operations with matching versions can be applied.',
  60. { operation: operation } );
  61. }
  62. operation._execute();
  63. this.version++;
  64. this.fire( 'operationApplied', operation );
  65. }
  66. /**
  67. * Creates a new top-level root.
  68. *
  69. * @param {String|Symbol} name Unique root name.
  70. * @returns {document.RootElement} Created root.
  71. */
  72. createRoot( name ) {
  73. if ( this.roots.has( name ) ) {
  74. /**
  75. * Root with specified name already exists.
  76. *
  77. * @error document-createRoot-name-exists
  78. * @param {document.Document} doc
  79. * @param {String} name
  80. */
  81. throw new CKEditorError(
  82. 'document-createRoot-name-exists: Root with specified name already exists.',
  83. { name: name }
  84. );
  85. }
  86. var root = new RootElement( this );
  87. this.roots.set( name, root );
  88. return root;
  89. }
  90. /**
  91. * Returns top-level root by it's name.
  92. *
  93. * @param {String|Symbol} name Name of the root to get.
  94. * @returns (document.RootElement} Root registered under given name.
  95. */
  96. getRoot( name ) {
  97. if ( !this.roots.has( name ) ) {
  98. /**
  99. * Root with specified name does not exist.
  100. *
  101. * @error document-createRoot-root-not-exist
  102. * @param {String} name
  103. */
  104. throw new CKEditorError(
  105. 'document-createRoot-root-not-exist: Root with specified name does not exist.',
  106. { name: name }
  107. );
  108. }
  109. return this.roots.get( name );
  110. }
  111. /**
  112. * Graveyard tree root. Document always have a graveyard root, which stores removed nodes.
  113. *
  114. * @protected
  115. * @readonly
  116. * @property {document.RootElement} _graveyard
  117. */
  118. get _graveyard() {
  119. return this.getRoot( graveyardSymbol );
  120. }
  121. }
  122. utils.extend( Document.prototype, EmitterMixin );
  123. return Document;
  124. } );