inlineeditorui.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module editor-inline/inlineeditorui
  7. */
  8. import ComponentFactory from '@ckeditor/ckeditor5-ui/src/componentfactory';
  9. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  10. import enableToolbarKeyboardFocus from '@ckeditor/ckeditor5-ui/src/toolbar/enabletoolbarkeyboardfocus';
  11. /**
  12. * The inline editor UI class.
  13. *
  14. * @implements module:core/editor/editorui~EditorUI
  15. */
  16. export default class InlineEditorUI {
  17. /**
  18. * Creates an instance of the editor UI class.
  19. *
  20. * @param {module:core/editor/editor~Editor} editor The editor instance.
  21. * @param {module:ui/editorui/editoruiview~EditorUIView} view View of the ui.
  22. */
  23. constructor( editor, view ) {
  24. /**
  25. * @inheritDoc
  26. */
  27. this.editor = editor;
  28. /**
  29. * @inheritDoc
  30. */
  31. this.view = view;
  32. /**
  33. * @inheritDoc
  34. */
  35. this.componentFactory = new ComponentFactory( editor );
  36. /**
  37. * @inheritDoc
  38. */
  39. this.focusTracker = new FocusTracker();
  40. // Set–up the view#panel.
  41. view.panel.bind( 'isActive' ).to( this.focusTracker, 'isFocused' );
  42. view.panel.targetElement = view.editableElement;
  43. // Setup the editable.
  44. const editingRoot = editor.editing.createRoot( view.editableElement );
  45. view.editable.bind( 'isReadOnly' ).to( editingRoot );
  46. // Bind to focusTracker instead of editor.editing.view because otherwise
  47. // focused editable styles disappear when view#toolbar is focused.
  48. view.editable.bind( 'isFocused' ).to( this.focusTracker );
  49. view.editable.name = editingRoot.rootName;
  50. this.focusTracker.add( view.editableElement );
  51. }
  52. /**
  53. * Initializes the UI.
  54. *
  55. * @returns {Promise} A Promise resolved when the initialization process is finished.
  56. */
  57. init() {
  58. const editor = this.editor;
  59. return this.view.init()
  60. .then( () => {
  61. return this.view.toolbar.fillFromConfig( editor.config.get( 'toolbar' ), this.componentFactory );
  62. } )
  63. .then( () => {
  64. enableToolbarKeyboardFocus( {
  65. origin: editor.editing.view,
  66. originFocusTracker: this.focusTracker,
  67. originKeystrokeHandler: editor.keystrokes,
  68. toolbar: this.view.toolbar
  69. } );
  70. } );
  71. }
  72. /**
  73. * Destroys the UI.
  74. *
  75. * @returns {Promise} A Promise resolved when the destruction process is finished.
  76. */
  77. destroy() {
  78. return this.view.destroy();
  79. }
  80. }