decouplededitorui.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module editor-decoupled/decouplededitorui
  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. import normalizeToolbarConfig from '@ckeditor/ckeditor5-ui/src/toolbar/normalizetoolbarconfig';
  12. /**
  13. * The decoupled editor UI class.
  14. *
  15. * @implements module:core/editor/editorui~EditorUI
  16. */
  17. export default class DecoupledEditorUI {
  18. /**
  19. * Creates an instance of the 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. /**
  26. * @inheritDoc
  27. */
  28. this.editor = editor;
  29. /**
  30. * @inheritDoc
  31. */
  32. this.view = view;
  33. /**
  34. * @inheritDoc
  35. */
  36. this.componentFactory = new ComponentFactory( editor );
  37. /**
  38. * @inheritDoc
  39. */
  40. this.focusTracker = new FocusTracker();
  41. /**
  42. * A normalized `config.toolbar` object.
  43. *
  44. * @type {Object}
  45. * @private
  46. */
  47. this._toolbarConfig = normalizeToolbarConfig( editor.config.get( 'toolbar' ) );
  48. }
  49. /**
  50. * Initializes the UI.
  51. */
  52. init() {
  53. const editor = this.editor;
  54. const view = this.view;
  55. view.render();
  56. // Set up the editable.
  57. const editingRoot = editor.editing.view.document.getRoot();
  58. view.editable.bind( 'isReadOnly' ).to( editingRoot );
  59. view.editable.bind( 'isFocused' ).to( editor.editing.view.document );
  60. editor.editing.view.attachDomRoot( view.editableElement );
  61. view.editable.name = editingRoot.rootName;
  62. this.focusTracker.add( this.view.editableElement );
  63. this.view.toolbar.fillFromConfig( this._toolbarConfig.items, this.componentFactory );
  64. enableToolbarKeyboardFocus( {
  65. origin: editor.editing.view,
  66. originFocusTracker: this.focusTracker,
  67. originKeystrokeHandler: editor.keystrokes,
  68. toolbar: this.view.toolbar
  69. } );
  70. }
  71. /**
  72. * Destroys the UI.
  73. */
  74. destroy() {
  75. this.view.destroy();
  76. }
  77. }