editableelement.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. * @inheritDoc
  63. */
  64. is( type, name = null ) {
  65. if ( !name ) {
  66. return type == 'editableElement' || super.is( type );
  67. } else {
  68. return ( type == 'editableElement' && name == this.name ) || super.is( type, name );
  69. }
  70. }
  71. destroy() {
  72. this.stopListening();
  73. }
  74. /**
  75. * Returns document associated with the editable.
  76. *
  77. * @readonly
  78. * @returns {module:engine/view/document~Document}
  79. */
  80. get document() {
  81. return this.getCustomProperty( documentSymbol );
  82. }
  83. /**
  84. * Sets document of this editable element.
  85. *
  86. * @protected
  87. * @param {module:engine/view/document~Document} document
  88. */
  89. set _document( document ) {
  90. if ( this.getCustomProperty( documentSymbol ) ) {
  91. /**
  92. * View document is already set. It can only be set once.
  93. *
  94. * @error view-editableelement-document-already-set
  95. */
  96. throw new CKEditorError( 'view-editableelement-document-already-set: View document is already set.', this );
  97. }
  98. this._setCustomProperty( documentSymbol, document );
  99. this.bind( 'isReadOnly' ).to( document );
  100. this.bind( 'isFocused' ).to(
  101. document,
  102. 'isFocused',
  103. isFocused => isFocused && document.selection.editableElement == this
  104. );
  105. // Update focus state based on selection changes.
  106. this.listenTo( document.selection, 'change', () => {
  107. this.isFocused = document.isFocused && document.selection.editableElement == this;
  108. } );
  109. }
  110. }
  111. mix( EditableElement, ObservableMixin );