8
0

imagetoolbar.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/classiceditor';
  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
  19. .create( document.querySelector( '#editor' ), {
  20. plugins: [ Enter, Typing, Paragraph, Undo, Bold, Italic, Image ],
  21. toolbar: [ 'bold', 'italic', 'undo', 'redo' ]
  22. } )
  23. .then( editor => {
  24. createImageToolbar( editor );
  25. window.editor = editor;
  26. } )
  27. .catch( err => {
  28. console.error( err.stack );
  29. } );
  30. function createImageToolbar( editor ) {
  31. // Create a plain toolbar instance.
  32. const toolbar = new ToolbarView();
  33. // Create a BalloonPanelView instance.
  34. const panel = new BalloonPanelView( editor.locale );
  35. Template.extend( panel.template, {
  36. attributes: {
  37. class: [
  38. 'ck-toolbar-container',
  39. ]
  40. }
  41. } );
  42. // Putting the toolbar inside of the balloon panel.
  43. panel.content.add( toolbar );
  44. editor.ui.view.body.add( panel ).then( () => {
  45. const editingView = editor.editing.view;
  46. // Fill the toolbar with some buttons. Simply copy default editor toolbar.
  47. toolbar.fillFromConfig( editor.config.get( 'toolbar' ), editor.ui.componentFactory );
  48. // Let the focusTracker know about new focusable UI element.
  49. editor.ui.focusTracker.add( panel.element );
  50. // Hide the panel when editor loses focus but no the other way around.
  51. panel.listenTo( editor.ui.focusTracker, 'change:isFocused', ( evt, name, is, was ) => {
  52. if ( was && !is ) {
  53. panel.hide();
  54. }
  55. } );
  56. editingView.addObserver( ClickObserver );
  57. // Check if the toolbar should be displayed each time the user clicked in editable.
  58. editor.listenTo( editingView, 'click', () => {
  59. if ( editingView.selection.isFake ) {
  60. attachToolbar();
  61. // TODO: These 2 need interval–based event debouncing for performance
  62. // reasons. I guess even lodash offers such a helper.
  63. editor.ui.view.listenTo( window, 'scroll', attachToolbar );
  64. editor.ui.view.listenTo( window, 'resize', attachToolbar );
  65. } else {
  66. panel.hide();
  67. editor.ui.view.stopListening( window, 'scroll', attachToolbar );
  68. editor.ui.view.stopListening( window, 'resize', attachToolbar );
  69. }
  70. } );
  71. function attachToolbar() {
  72. const defaultPositions = BalloonPanelView.defaultPositions;
  73. panel.attachTo( {
  74. target: editingView.domConverter.viewRangeToDom( editingView.selection.getFirstRange() ),
  75. positions: [ defaultPositions.northArrowSouth, defaultPositions.southArrowNorth ]
  76. } );
  77. }
  78. } );
  79. }