ballooneditorui.js 2.2 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-balloon/ballooneditorui
  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 balloon editor UI class.
  13. *
  14. * @implements module:core/editor/editorui~EditorUI
  15. */
  16. export default class BalloonEditorUI {
  17. /**
  18. * Creates an instance of the balloon editor UI class.
  19. *
  20. * @param {module:core/editor/editor~Editor} editor The editor instance.
  21. * @param {module:ui/editorui/editoruiview~EditorUIView} view The 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. // RootEditableElement should be created together with the editor structure.
  41. // See https://github.com/ckeditor/ckeditor5/issues/647.
  42. editor.editing.createRoot( view.editableElement );
  43. }
  44. /**
  45. * Initializes the UI.
  46. */
  47. init() {
  48. const editor = this.editor;
  49. const view = this.view;
  50. const contextualToolbar = editor.plugins.get( 'ContextualToolbar' );
  51. view.render();
  52. // Setup the editable.
  53. const editingRoot = editor.editing.view.getRoot();
  54. view.editable.bind( 'isReadOnly' ).to( editingRoot );
  55. // Bind to focusTracker instead of editor.editing.view because otherwise
  56. // focused editable styles disappear when view#toolbar is focused.
  57. view.editable.bind( 'isFocused' ).to( this.focusTracker );
  58. view.editable.name = editingRoot.rootName;
  59. this.focusTracker.add( view.editableElement );
  60. enableToolbarKeyboardFocus( {
  61. origin: editor.editing.view,
  62. originFocusTracker: this.focusTracker,
  63. originKeystrokeHandler: editor.keystrokes,
  64. toolbar: contextualToolbar.toolbarView,
  65. beforeFocus() {
  66. contextualToolbar.show();
  67. },
  68. afterBlur() {
  69. contextualToolbar.hide();
  70. }
  71. } );
  72. }
  73. /**
  74. * Destroys the UI.
  75. */
  76. destroy() {
  77. this.view.destroy();
  78. }
  79. }