editableelement.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module engine/view/editableelement
  7. */
  8. import ContainerElement from './containerelement';
  9. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  10. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  11. import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
  12. const documentSymbol = Symbol( 'document' );
  13. /**
  14. * Editable element which can be a {@link module:engine/view/rooteditableelement~RootEditableElement root}
  15. * or nested editable area in the editor.
  16. *
  17. * Editable is automatically read-only when its {@link module:engine/view/document~Document Document} is read-only.
  18. *
  19. * The constructor of this class shouldn't be used directly. To create new `EditableElement` use the
  20. * {@link module:engine/view/downcastwriter~DowncastWriter#createEditableElement `downcastWriter#createEditableElement()`} method.
  21. *
  22. * @extends module:engine/view/containerelement~ContainerElement
  23. * @mixes module:utils/observablemixin~ObservableMixin
  24. */
  25. export default class EditableElement extends ContainerElement {
  26. /**
  27. * Creates an editable element.
  28. *
  29. * @see module:engine/view/downcastwriter~DowncastWriter#createEditableElement
  30. * @protected
  31. */
  32. constructor( name, attrs, children ) {
  33. super( name, attrs, children );
  34. /**
  35. * Whether the editable is in read-write or read-only mode.
  36. *
  37. * @observable
  38. * @member {Boolean} module:engine/view/editableelement~EditableElement#isReadOnly
  39. */
  40. this.set( 'isReadOnly', false );
  41. /**
  42. * Whether the editable is focused.
  43. *
  44. * This property updates when {@link module:engine/view/document~Document#isFocused document.isFocused} or view
  45. * selection is changed.
  46. *
  47. * @readonly
  48. * @observable
  49. * @member {Boolean} module:engine/view/editableelement~EditableElement#isFocused
  50. */
  51. this.set( 'isFocused', false );
  52. /**
  53. * The {@link module:engine/view/document~Document} which is an owner of this root.
  54. * Can only by set once.
  55. * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `view-editableelement-document-already-set`
  56. * when document is already set.
  57. *
  58. * @member {module:engine/view/document~Document} #document
  59. */
  60. }
  61. /**
  62. * Checks whether this object is of the given.
  63. *
  64. * editableElement.is( 'editableElement' ); // -> true
  65. * editableElement.is( 'element' ); // -> true
  66. * editableElement.is( 'node' ); // -> true
  67. * editableElement.is( 'view:editableElement' ); // -> true
  68. * editableElement.is( 'view:element' ); // -> true
  69. * editableElement.is( 'view:node' ); // -> true
  70. *
  71. * editableElement.is( 'model:element' ); // -> false
  72. * editableElement.is( 'documentFragment' ); // -> false
  73. *
  74. * Assuming that the object being checked is an editbale element, you can also check its
  75. * {@link module:engine/view/editableelement~EditableElement#name name}:
  76. *
  77. * editableElement.is( 'div' ); // -> true if this is a div element
  78. * editableElement.is( 'editableElement', 'div' ); // -> same as above
  79. * text.is( 'div' ); -> false
  80. *
  81. * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
  82. *
  83. * @param {String} type Type to check when `name` parameter is present.
  84. * Otherwise, it acts like the `name` parameter.
  85. * @param {String} [name] Element name.
  86. * @returns {Boolean}
  87. */
  88. is( type, name = null ) {
  89. const cutType = type && type.replace( /^view:/, '' );
  90. if ( !name ) {
  91. return cutType == 'editableElement' || super.is( type );
  92. } else {
  93. return ( cutType == 'editableElement' && name == this.name ) || super.is( type, name );
  94. }
  95. }
  96. destroy() {
  97. this.stopListening();
  98. }
  99. /**
  100. * Returns document associated with the editable.
  101. *
  102. * @readonly
  103. * @returns {module:engine/view/document~Document}
  104. */
  105. get document() {
  106. return this.getCustomProperty( documentSymbol );
  107. }
  108. /**
  109. * Sets document of this editable element.
  110. *
  111. * @protected
  112. * @param {module:engine/view/document~Document} document
  113. */
  114. set _document( document ) {
  115. if ( this.getCustomProperty( documentSymbol ) ) {
  116. /**
  117. * View document is already set. It can only be set once.
  118. *
  119. * @error view-editableelement-document-already-set
  120. */
  121. throw new CKEditorError( 'view-editableelement-document-already-set: View document is already set.', this );
  122. }
  123. this._setCustomProperty( documentSymbol, document );
  124. this.bind( 'isReadOnly' ).to( document );
  125. this.bind( 'isFocused' ).to(
  126. document,
  127. 'isFocused',
  128. isFocused => isFocused && document.selection.editableElement == this
  129. );
  130. // Update focus state based on selection changes.
  131. this.listenTo( document.selection, 'change', () => {
  132. this.isFocused = document.isFocused && document.selection.editableElement == this;
  133. } );
  134. }
  135. }
  136. mix( EditableElement, ObservableMixin );