rooteditableelement.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/rooteditableelement
  7. */
  8. import EditableElement from './editableelement';
  9. const rootNameSymbol = Symbol( 'rootName' );
  10. /**
  11. * Class representing a single root in the data view. A root can be either {@link ~RootEditableElement#isReadOnly editable or read-only},
  12. * but in both cases it is called "an editable". Roots can contain other {@link module:engine/view/editableelement~EditableElement
  13. * editable elements} making them "nested editables".
  14. *
  15. * @extends module:engine/view/editableelement~EditableElement
  16. */
  17. export default class RootEditableElement extends EditableElement {
  18. /**
  19. * Creates root editable element.
  20. *
  21. * @param {String} name Node name.
  22. */
  23. constructor( name ) {
  24. super( name );
  25. /**
  26. * Name of this root inside {@link module:engine/view/document~Document} that is an owner of this root. If no
  27. * other name is set, `main` name is used.
  28. *
  29. * @readonly
  30. * @member {String}
  31. */
  32. this.rootName = 'main';
  33. }
  34. /**
  35. * Checks whether this object is of the given.
  36. *
  37. * rootEditableElement.is( 'rootEditableElement' ); // -> true
  38. * rootEditableElement.is( 'editableElement' ); // -> true
  39. * rootEditableElement.is( 'element' ); // -> true
  40. * rootEditableElement.is( 'node' ); // -> true
  41. * rootEditableElement.is( 'view:rootEditableElement' ); // -> true
  42. * rootEditableElement.is( 'view:editableElement' ); // -> true
  43. * rootEditableElement.is( 'view:element' ); // -> true
  44. * rootEditableElement.is( 'view:node' ); // -> true
  45. *
  46. * rootEditableElement.is( 'model:element' ); // -> false
  47. * rootEditableElement.is( 'documentFragment' ); // -> false
  48. *
  49. * Assuming that the object being checked is a root editbale element, you can also check its
  50. * {@link module:engine/view/rooteditableelement~RootEditableElement#name name}:
  51. *
  52. * rootEditableElement.is( 'div' ); // -> true if this is a div root editable element
  53. * rootEditableElement.is( 'rootEditableElement', 'div' ); // -> same as above
  54. * text.is( 'div' ); -> false
  55. *
  56. * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
  57. *
  58. * @param {String} type Type to check when `name` parameter is present.
  59. * Otherwise, it acts like the `name` parameter.
  60. * @param {String} [name] Element name.
  61. * @returns {Boolean}
  62. */
  63. is( type, name = null ) {
  64. const cutType = type.replace( /^view:/, '' );
  65. if ( !name ) {
  66. return cutType == 'rootElement' || super.is( type );
  67. } else {
  68. return ( cutType == 'rootElement' && name == this.name ) || super.is( type, name );
  69. }
  70. }
  71. get rootName() {
  72. return this.getCustomProperty( rootNameSymbol );
  73. }
  74. set rootName( rootName ) {
  75. this._setCustomProperty( rootNameSymbol, rootName );
  76. }
  77. /**
  78. * Overrides old element name and sets new one.
  79. * This is needed because view roots are created before they are attached to the DOM.
  80. * The name of the root element is temporary at this stage. It has to be changed when the
  81. * view root element is attached to the DOM element.
  82. *
  83. * @protected
  84. * @param {String} name The new name of element.
  85. */
  86. set _name( name ) {
  87. this.name = name;
  88. }
  89. }