editableelement.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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. * @extends module:engine/view/containerelement~ContainerElement
  20. * @mixes module:utils/observablemixin~ObservableMixin
  21. */
  22. export default class EditableElement extends ContainerElement {
  23. /**
  24. * Creates an editable element.
  25. *
  26. * @see module:engine/view/downcastwriter~DowncastWriter#createEditableElement
  27. * @protected
  28. */
  29. constructor( name, attrs, children ) {
  30. super( name, attrs, children );
  31. /**
  32. * Whether the editable is in read-write or read-only mode.
  33. *
  34. * @observable
  35. * @member {Boolean} module:engine/view/editableelement~EditableElement#isReadOnly
  36. */
  37. this.set( 'isReadOnly', false );
  38. /**
  39. * Whether the editable is focused.
  40. *
  41. * This property updates when {@link module:engine/view/document~Document#isFocused document.isFocused} or view
  42. * selection is changed.
  43. *
  44. * @readonly
  45. * @observable
  46. * @member {Boolean} module:engine/view/editableelement~EditableElement#isFocused
  47. */
  48. this.set( 'isFocused', false );
  49. /**
  50. * The {@link module:engine/view/document~Document} which is an owner of this root.
  51. * Can only by set once.
  52. * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `view-editableelement-document-already-set`
  53. * when document is already set.
  54. *
  55. * @member {module:engine/view/document~Document} #document
  56. */
  57. }
  58. /**
  59. * Returns document associated with the editable.
  60. *
  61. * @readonly
  62. * @returns {module:engine/view/document~Document}
  63. */
  64. get document() {
  65. return this.getCustomProperty( documentSymbol );
  66. }
  67. /**
  68. * Sets document of this editable element.
  69. *
  70. * @protected
  71. * @param {module:engine/view/document~Document} document
  72. */
  73. set _document( document ) {
  74. if ( this.getCustomProperty( documentSymbol ) ) {
  75. /**
  76. * View document is already set. It can only be set once.
  77. *
  78. * @error view-editableelement-document-already-set
  79. */
  80. throw new CKEditorError( 'view-editableelement-document-already-set: View document is already set.' );
  81. }
  82. this._setCustomProperty( documentSymbol, document );
  83. this.bind( 'isReadOnly' ).to( document );
  84. this.bind( 'isFocused' ).to(
  85. document,
  86. 'isFocused',
  87. isFocused => isFocused && document.selection.editableElement == this
  88. );
  89. // Update focus state based on selection changes.
  90. this.listenTo( document.selection, 'change', () => {
  91. this.isFocused = document.isFocused && document.selection.editableElement == this;
  92. } );
  93. }
  94. }
  95. mix( EditableElement, ObservableMixin );