imagetoolbar.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals window, document, console:false */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classic';
  7. import ClickObserver from '@ckeditor/ckeditor5-engine/src/view/observer/clickobserver';
  8. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  9. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  12. import Image from '@ckeditor/ckeditor5-image/src/image';
  13. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  14. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  15. import Template from '../../../src/template';
  16. import ToolbarView from '../../../src/toolbar/toolbarview';
  17. import BalloonPanelView from '../../../src/panel/balloon/balloonpanelview';
  18. ClassicEditor.create( document.querySelector( '#editor' ), {
  19. plugins: [ Enter, Typing, Paragraph, Undo, Bold, Italic, Image ],
  20. toolbar: [ 'bold', 'italic', 'undo', 'redo' ]
  21. } )
  22. .then( editor => {
  23. createImageToolbar( editor );
  24. window.editor = editor;
  25. } )
  26. .catch( err => {
  27. console.error( err.stack );
  28. } );
  29. function createImageToolbar( editor ) {
  30. // Create a plain toolbar instance.
  31. const toolbar = new ToolbarView();
  32. // Create a BalloonPanelView instance.
  33. const panel = new BalloonPanelView( editor.locale );
  34. Template.extend( panel.template, {
  35. attributes: {
  36. class: [
  37. 'ck-toolbar-container',
  38. ]
  39. }
  40. } );
  41. // Putting the toolbar inside of the balloon panel.
  42. panel.content.add( toolbar );
  43. editor.ui.view.body.add( panel ).then( () => {
  44. const editingView = editor.editing.view;
  45. // Fill the toolbar with some buttons. Simply copy default editor toolbar.
  46. toolbar.fillFromConfig( editor.config.get( 'toolbar' ), editor.ui.componentFactory );
  47. // Let the focusTracker know about new focusable UI element.
  48. editor.ui.focusTracker.add( panel.element );
  49. // Hide the panel when editor loses focus but no the other way around.
  50. panel.listenTo( editor.ui.focusTracker, 'change:isFocused', ( evt, name, is, was ) => {
  51. if ( was && !is ) {
  52. panel.hide();
  53. }
  54. } );
  55. editingView.addObserver( ClickObserver );
  56. // Check if the toolbar should be displayed each time the user clicked in editable.
  57. editor.listenTo( editingView, 'click', () => {
  58. if ( editingView.selection.isFake ) {
  59. attachToolbar();
  60. // TODO: These 2 need interval–based event debouncing for performance
  61. // reasons. I guess even lodash offers such a helper.
  62. editor.ui.view.listenTo( window, 'scroll', attachToolbar );
  63. editor.ui.view.listenTo( window, 'resize', attachToolbar );
  64. } else {
  65. panel.hide();
  66. editor.ui.view.stopListening( window, 'scroll', attachToolbar );
  67. editor.ui.view.stopListening( window, 'resize', attachToolbar );
  68. }
  69. } );
  70. function attachToolbar() {
  71. const defaultPositions = BalloonPanelView.defaultPositions;
  72. panel.attachTo( {
  73. target: editingView.domConverter.viewRangeToDom( editingView.selection.getFirstRange() ),
  74. positions: [ defaultPositions.northArrowSouth, defaultPositions.southArrowNorth ]
  75. } );
  76. }
  77. } );
  78. }