decouplededitorui.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module editor-decoupled/decouplededitorui
  7. */
  8. import EditorUI from '@ckeditor/ckeditor5-core/src/editor/editorui';
  9. import enableToolbarKeyboardFocus from '@ckeditor/ckeditor5-ui/src/toolbar/enabletoolbarkeyboardfocus';
  10. import normalizeToolbarConfig from '@ckeditor/ckeditor5-ui/src/toolbar/normalizetoolbarconfig';
  11. import { enablePlaceholder } from '@ckeditor/ckeditor5-engine/src/view/placeholder';
  12. /**
  13. * The decoupled editor UI class.
  14. *
  15. * @extends module:core/editor/editorui~EditorUI
  16. */
  17. export default class DecoupledEditorUI extends EditorUI {
  18. /**
  19. * Creates an instance of the decoupled editor UI class.
  20. *
  21. * @param {module:core/editor/editor~Editor} editor The editor instance.
  22. * @param {module:ui/editorui/editoruiview~EditorUIView} view The view of the UI.
  23. */
  24. constructor( editor, view ) {
  25. super( editor );
  26. /**
  27. * The main (top–most) view of the editor UI.
  28. *
  29. * @readonly
  30. * @member {module:ui/editorui/editoruiview~EditorUIView} #view
  31. */
  32. this.view = view;
  33. /**
  34. * A normalized `config.toolbar` object.
  35. *
  36. * @type {Object}
  37. * @private
  38. */
  39. this._toolbarConfig = normalizeToolbarConfig( editor.config.get( 'toolbar' ) );
  40. }
  41. /**
  42. * Initializes the UI.
  43. */
  44. init() {
  45. const editor = this.editor;
  46. const view = this.view;
  47. const editingView = editor.editing.view;
  48. const editable = view.editable;
  49. const editingRoot = editingView.document.getRoot();
  50. // The editable UI and editing root should share the same name. Then name is used
  51. // to recognize the particular editable, for instance in ARIA attributes.
  52. view.editable.name = editingRoot.rootName;
  53. view.render();
  54. // The editable UI element in DOM is available for sure only after the editor UI view has been rendered.
  55. // But it can be available earlier if a DOM element has been passed to DecoupledEditor.create().
  56. const editableElement = editable.element;
  57. // Register the editable UI view in the editor. A single editor instance can aggregate multiple
  58. // editable areas (roots) but the decoupled editor has only one.
  59. this.setEditableElement( editable.name, editableElement );
  60. // Let the global focus tracker know that the editable UI element is focusable and
  61. // belongs to the editor. From now on, the focus tracker will sustain the editor focus
  62. // as long as the editable is focused (e.g. the user is typing).
  63. this.focusTracker.add( editableElement );
  64. // Let the editable UI element respond to the changes in the global editor focus
  65. // tracker. It has been added to the same tracker a few lines above but, in reality, there are
  66. // many focusable areas in the editor, like balloons, toolbars or dropdowns and as long
  67. // as they have focus, the editable should act like it is focused too (although technically
  68. // it isn't), e.g. by setting the proper CSS class, visually announcing focus to the user.
  69. // Doing otherwise will result in editable focus styles disappearing, once e.g. the
  70. // toolbar gets focused.
  71. view.editable.bind( 'isFocused' ).to( this.focusTracker );
  72. // Bind the editable UI element to the editing view, making it an end– and entry–point
  73. // of the editor's engine. This is where the engine meets the UI.
  74. editingView.attachDomRoot( editableElement );
  75. this._initPlaceholder();
  76. this._initToolbar();
  77. this.fire( 'ready' );
  78. }
  79. /**
  80. * @inheritDoc
  81. */
  82. destroy() {
  83. const view = this.view;
  84. const editingView = this.editor.editing.view;
  85. editingView.detachDomRoot( view.editable.name );
  86. view.destroy();
  87. super.destroy();
  88. }
  89. /**
  90. * Initializes the inline editor toolbar and its panel.
  91. *
  92. * @private
  93. */
  94. _initToolbar() {
  95. const editor = this.editor;
  96. const view = this.view;
  97. const toolbar = view.toolbar;
  98. toolbar.fillFromConfig( this._toolbarConfig.items, this.componentFactory );
  99. enableToolbarKeyboardFocus( {
  100. origin: editor.editing.view,
  101. originFocusTracker: this.focusTracker,
  102. originKeystrokeHandler: editor.keystrokes,
  103. toolbar
  104. } );
  105. }
  106. /**
  107. * Enable the placeholder text on the editing root, if any was configured.
  108. *
  109. * @private
  110. */
  111. _initPlaceholder() {
  112. const editor = this.editor;
  113. const editingView = editor.editing.view;
  114. const editingRoot = editingView.document.getRoot();
  115. const sourceElement = editor.sourceElement;
  116. const placeholderText = editor.config.get( 'placeholder' ) ||
  117. sourceElement && sourceElement.tagName.toLowerCase() === 'textarea' && sourceElement.getAttribute( 'placeholder' );
  118. if ( placeholderText ) {
  119. enablePlaceholder( {
  120. view: editingView,
  121. element: editingRoot,
  122. text: placeholderText,
  123. isDirectHost: false
  124. } );
  125. }
  126. }
  127. }