8
0

editableelement.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * @license Copyright (c) 2003-2015, 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.js';
  9. import mix from '../../utils/mix.js';
  10. import ObservableMixin from '../../utils/observablemixin.js';
  11. /**
  12. * Editable element which can be a {@link module:engine/view/rooteditableelement~RootEditableElement root}
  13. * or nested editable area in the editor.
  14. *
  15. * @extends module:engine/view/containerelement~ContainerElement
  16. * @mixes module:utils/observablemixin~ObservaleMixin
  17. */
  18. export default class EditableElement extends ContainerElement {
  19. /**
  20. * Creates an editable element.
  21. */
  22. constructor( document, name, attrs, children ) {
  23. super( name, attrs, children );
  24. /**
  25. * {@link module:engine/view/document~Document} that is an owner of this root.
  26. *
  27. * @private
  28. * @member {module:engine/view/document~Document} module:engine/view/rooteditableelement~RootEditableElement#_document
  29. */
  30. this._document = document;
  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} is changed and after each
  42. * {@link module:engine/view/document~Document#render render} method call.
  43. *
  44. * @readonly
  45. * @observable
  46. * @member {Boolean} module:engine/view/editableelement~EditableElement#isFocused
  47. */
  48. this.bind( 'isFocused' ).to(
  49. document,
  50. 'isFocused',
  51. ( isFocused ) => isFocused && document.selection.editableElement == this
  52. );
  53. // Update focus state after each rendering. Selection might be moved to different editable before rendering,
  54. // but this does not mean that editable has focus - it will be placed there after rendering.
  55. this.listenTo( document, 'render', () => {
  56. this.isFocused = document.isFocused && document.selection.editableElement == this;
  57. }, { priority: 'low' } );
  58. }
  59. /**
  60. * {@link module:engine/view/document~Document View document} reference that owns this editable element.
  61. *
  62. * @type {module:engine/view/document~Document|null}
  63. */
  64. get document() {
  65. return this._document;
  66. }
  67. }
  68. mix( EditableElement, ObservableMixin );