8
0

inlineeditor.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 InlineEditor from '../../src/inlineeditor';
  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. InlineEditor
  17. .create( document.querySelector( selector ), {
  18. plugins: [ ArticlePluginSet ],
  19. toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote', 'undo', 'redo' ]
  20. } )
  21. .then( editor => {
  22. console.log( `${ selector } has been initialized`, editor );
  23. console.log( 'It has been added to global `editors` and `editables`.' );
  24. window.editors[ selector ] = editor;
  25. window.editables.push( editor.editing.view.document.getRoot() );
  26. const observer = createObserver();
  27. observer.observe(
  28. `${ selector }.ui.focusTracker`,
  29. editor.ui.focusTracker,
  30. [ 'isFocused' ]
  31. );
  32. window._observers.push( observer );
  33. } )
  34. .catch( err => {
  35. console.error( err.stack );
  36. } );
  37. }
  38. }
  39. function destroyEditors() {
  40. for ( const selector in window.editors ) {
  41. window.editors[ selector ].destroy().then( () => {
  42. console.log( `${ selector } was destroyed.` );
  43. } );
  44. }
  45. for ( const observer of window._observers ) {
  46. observer.stopListening();
  47. }
  48. window.editors = {};
  49. window.editables.length = window._observers.length = 0;
  50. }
  51. document.getElementById( 'initEditors' ).addEventListener( 'click', initEditors );
  52. document.getElementById( 'destroyEditors' ).addEventListener( 'click', destroyEditors );