renameoperation.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/model/operation/renameoperation
  7. */
  8. import Operation from './operation';
  9. import Element from '../element';
  10. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  11. import Position from '../position';
  12. /**
  13. * Operation to change element's name.
  14. *
  15. * Using this class you can change element's name.
  16. *
  17. * @extends module:engine/model/operation/operation~Operation
  18. */
  19. export default class RenameOperation extends Operation {
  20. /**
  21. * Creates an operation that changes element's name.
  22. *
  23. * @param {module:engine/model/position~Position} position Position before an element to change.
  24. * @param {String} oldName Current name of the element.
  25. * @param {String} newName New name for the element.
  26. * @param {Number|null} baseVersion Document {@link module:engine/model/document~Document#version} on which operation
  27. * can be applied or `null` if the operation operates on detached (non-document) tree.
  28. */
  29. constructor( position, oldName, newName, baseVersion ) {
  30. super( baseVersion );
  31. /**
  32. * Position before an element to change.
  33. *
  34. * @member {module:engine/model/position~Position} module:engine/model/operation/renameoperation~RenameOperation#position
  35. */
  36. this.position = position;
  37. /**
  38. * Current name of the element.
  39. *
  40. * @member {String} module:engine/model/operation/renameoperation~RenameOperation#oldName
  41. */
  42. this.oldName = oldName;
  43. /**
  44. * New name for the element.
  45. *
  46. * @member {String} module:engine/model/operation/renameoperation~RenameOperation#newName
  47. */
  48. this.newName = newName;
  49. }
  50. /**
  51. * @inheritDoc
  52. */
  53. get type() {
  54. return 'rename';
  55. }
  56. /**
  57. * Creates and returns an operation that has the same parameters as this operation.
  58. *
  59. * @returns {module:engine/model/operation/renameoperation~RenameOperation} Clone of this operation.
  60. */
  61. clone() {
  62. return new RenameOperation( Position.createFromPosition( this.position ), this.oldName, this.newName, this.baseVersion );
  63. }
  64. /**
  65. * See {@link module:engine/model/operation/operation~Operation#getReversed `Operation#getReversed()`}.
  66. *
  67. * @returns {module:engine/model/operation/renameoperation~RenameOperation}
  68. */
  69. getReversed() {
  70. return new RenameOperation( Position.createFromPosition( this.position ), this.newName, this.oldName, this.baseVersion + 1 );
  71. }
  72. /**
  73. * @inheritDoc
  74. */
  75. _validate() {
  76. const element = this.position.nodeAfter;
  77. if ( !( element instanceof Element ) ) {
  78. /**
  79. * Given position is invalid or node after it is not instance of Element.
  80. *
  81. * @error rename-operation-wrong-position
  82. */
  83. throw new CKEditorError(
  84. 'rename-operation-wrong-position: Given position is invalid or node after it is not an instance of Element.'
  85. );
  86. } else if ( element.name !== this.oldName ) {
  87. /**
  88. * Element to change has different name than operation's old name.
  89. *
  90. * @error rename-operation-wrong-name
  91. */
  92. throw new CKEditorError(
  93. 'rename-operation-wrong-name: Element to change has different name than operation\'s old name.'
  94. );
  95. }
  96. }
  97. /**
  98. * @inheritDoc
  99. */
  100. _execute() {
  101. const element = this.position.nodeAfter;
  102. element.name = this.newName;
  103. }
  104. /**
  105. * @inheritDoc
  106. */
  107. static get className() {
  108. return 'engine.model.operation.RenameOperation';
  109. }
  110. /**
  111. * Creates `RenameOperation` object from deserialized object, i.e. from parsed JSON string.
  112. *
  113. * @param {Object} json Deserialized JSON object.
  114. * @param {module:engine/model/document~Document} document Document on which this operation will be applied.
  115. * @returns {module:engine/model/operation/attributeoperation~AttributeOperation}
  116. */
  117. static fromJSON( json, document ) {
  118. return new RenameOperation( Position.fromJSON( json.position, document ), json.oldName, json.newName, json.baseVersion );
  119. }
  120. }