decouplededitor.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 DecoupledEditor from '../../src/decouplededitor';
  7. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  8. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  9. import Heading from '@ckeditor/ckeditor5-heading/src/heading';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  12. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  13. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  14. import { createObserver } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  15. const editorData = '<h2>Hello world</h2><p>This is the decoupled editor.</p>';
  16. let editor, editable, observer;
  17. function initEditor() {
  18. DecoupledEditor
  19. .create( editorData, {
  20. plugins: [ Enter, Typing, Paragraph, Undo, Heading, Bold, Italic ],
  21. toolbar: [ 'heading', '|', 'bold', 'italic', 'undo', 'redo' ]
  22. } )
  23. .then( newEditor => {
  24. console.log( 'Editor was initialized', newEditor );
  25. console.log( 'You can now play with it using global `editor` and `editable` variables.' );
  26. document.querySelector( '.toolbar-container' ).appendChild( newEditor.ui.view.toolbar.element );
  27. document.querySelector( '.editable-container' ).appendChild( newEditor.ui.view.editable.element );
  28. window.editor = editor = newEditor;
  29. window.editable = editable = editor.editing.view.document.getRoot();
  30. observer = createObserver();
  31. observer.observe( 'Editable', editable, [ 'isFocused' ] );
  32. } )
  33. .catch( err => {
  34. console.error( err.stack );
  35. } );
  36. }
  37. function destroyEditor() {
  38. editor.destroy()
  39. .then( () => {
  40. window.editor = editor = null;
  41. window.editable = editable = null;
  42. observer.stopListening();
  43. observer = null;
  44. console.log( 'Editor was destroyed' );
  45. } );
  46. }
  47. document.getElementById( 'initEditor' ).addEventListener( 'click', initEditor );
  48. document.getElementById( 'destroyEditor' ).addEventListener( 'click', destroyEditor );