inlineeditorui.js 5.7 KB

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