ballooneditor.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals console:false, document, window */
  6. import BalloonEditor from '../../src/ballooneditor';
  7. import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  8. import { createObserver } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  9. window.editors = {};
  10. window.editables = [];
  11. window._observers = [];
  12. function initEditors() {
  13. init( '#editor-1' );
  14. init( '#editor-2' );
  15. function init( selector ) {
  16. BalloonEditor
  17. .create( document.querySelector( selector ), {
  18. plugins: [ ArticlePluginSet ],
  19. toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote' ],
  20. image: {
  21. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  22. }
  23. } )
  24. .then( editor => {
  25. console.log( `${ selector } has been initialized`, editor );
  26. console.log( 'It has been added to global `editors` and `editables`.' );
  27. window.editors[ selector ] = editor;
  28. window.editables.push( editor.editing.view.document.getRoot() );
  29. const observer = createObserver();
  30. observer.observe(
  31. `${ selector }.ui.focusTracker`,
  32. editor.ui.focusTracker,
  33. [ 'isFocused' ]
  34. );
  35. window._observers.push( observer );
  36. } )
  37. .catch( err => {
  38. console.error( err.stack );
  39. } );
  40. }
  41. }
  42. function destroyEditors() {
  43. for ( const selector in window.editors ) {
  44. window.editors[ selector ].destroy().then( () => {
  45. console.log( `${ selector } was destroyed.` );
  46. } );
  47. }
  48. for ( const observer of window._observers ) {
  49. observer.stopListening();
  50. }
  51. window.editors = {};
  52. window.editables.length = window._observers.length = 0;
  53. }
  54. document.getElementById( 'initEditors' ).addEventListener( 'click', initEditors );
  55. document.getElementById( 'destroyEditors' ).addEventListener( 'click', destroyEditors );