8
0

inlineeditorui.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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-inline/inlineeditorui
  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 inline editor UI class.
  14. *
  15. * @extends module:core/editor/editorui~EditorUI
  16. */
  17. export default class InlineEditorUI extends EditorUI {
  18. /**
  19. * Creates an instance of the inline 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. * @inheritDoc
  43. */
  44. get element() {
  45. return this.view.editable.element;
  46. }
  47. /**
  48. * Initializes the UI.
  49. */
  50. init() {
  51. const editor = this.editor;
  52. const view = this.view;
  53. const editingView = editor.editing.view;
  54. const editable = view.editable;
  55. const editingRoot = editingView.document.getRoot();
  56. // The editable UI and editing root should share the same name. Then name is used
  57. // to recognize the particular editable, for instance in ARIA attributes.
  58. editable.name = editingRoot.rootName;
  59. view.render();
  60. // The editable UI element in DOM is available for sure only after the editor UI view has been rendered.
  61. // But it can be available earlier if a DOM element has been passed to InlineEditor.create().
  62. const editableElement = editable.element;
  63. // Register the editable UI view in the editor. A single editor instance can aggregate multiple
  64. // editable areas (roots) but the inline editor has only one.
  65. this.setEditableElement( editable.name, editableElement );
  66. // Let the global focus tracker know that the editable UI element is focusable and
  67. // belongs to the editor. From now on, the focus tracker will sustain the editor focus
  68. // as long as the editable is focused (e.g. the user is typing).
  69. this.focusTracker.add( editableElement );
  70. // Let the editable UI element respond to the changes in the global editor focus
  71. // tracker. It has been added to the same tracker a few lines above but, in reality, there are
  72. // many focusable areas in the editor, like balloons, toolbars or dropdowns and as long
  73. // as they have focus, the editable should act like it is focused too (although technically
  74. // it isn't), e.g. by setting the proper CSS class, visually announcing focus to the user.
  75. // Doing otherwise will result in editable focus styles disappearing, once e.g. the
  76. // toolbar gets focused.
  77. editable.bind( 'isFocused' ).to( this.focusTracker );
  78. // Bind the editable UI element to the editing view, making it an end– and entry–point
  79. // of the editor's engine. This is where the engine meets the UI.
  80. editingView.attachDomRoot( editableElement );
  81. this._initPlaceholder();
  82. this._initToolbar();
  83. this.fire( 'ready' );
  84. }
  85. /**
  86. * @inheritDoc
  87. */
  88. destroy() {
  89. const view = this.view;
  90. const editingView = this.editor.editing.view;
  91. editingView.detachDomRoot( view.editable.name );
  92. view.destroy();
  93. super.destroy();
  94. }
  95. /**
  96. * Initializes the inline editor toolbar and its panel.
  97. *
  98. * @private
  99. */
  100. _initToolbar() {
  101. const editor = this.editor;
  102. const view = this.view;
  103. const editableElement = view.editable.element;
  104. const editingView = editor.editing.view;
  105. const toolbar = view.toolbar;
  106. // Set–up the view#panel.
  107. view.panel.bind( 'isVisible' ).to( this.focusTracker, 'isFocused' );
  108. if ( this._toolbarConfig.viewportTopOffset ) {
  109. view.viewportTopOffset = this._toolbarConfig.viewportTopOffset;
  110. }
  111. // https://github.com/ckeditor/ckeditor5-editor-inline/issues/4
  112. view.listenTo( editor.ui, 'update', () => {
  113. // Don't pin if the panel is not already visible. It prevents the panel
  114. // showing up when there's no focus in the UI.
  115. if ( view.panel.isVisible ) {
  116. view.panel.pin( {
  117. target: editableElement,
  118. positions: view.panelPositions
  119. } );
  120. }
  121. } );
  122. toolbar.fillFromConfig( this._toolbarConfig.items, this.componentFactory );
  123. enableToolbarKeyboardFocus( {
  124. origin: editingView,
  125. originFocusTracker: this.focusTracker,
  126. originKeystrokeHandler: editor.keystrokes,
  127. toolbar
  128. } );
  129. }
  130. /**
  131. * Enable the placeholder text on the editing root, if any was configured.
  132. *
  133. * @private
  134. */
  135. _initPlaceholder() {
  136. const editor = this.editor;
  137. const editingView = editor.editing.view;
  138. const editingRoot = editingView.document.getRoot();
  139. const sourceElement = editor.sourceElement;
  140. const placeholderText = editor.config.get( 'placeholder' ) ||
  141. sourceElement && sourceElement.tagName.toLowerCase() === 'textarea' && sourceElement.getAttribute( 'placeholder' );
  142. if ( placeholderText ) {
  143. enablePlaceholder( {
  144. view: editingView,
  145. element: editingRoot,
  146. text: placeholderText,
  147. isDirectHost: false
  148. } );
  149. }
  150. }
  151. }