8
0

watchdog.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document, console */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  8. import Watchdog from '@ckeditor/ckeditor5-watchdog/src/watchdog';
  9. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  10. const firstEditorElement = document.getElementById( 'editor-1' );
  11. const secondEditorElement = document.getElementById( 'editor-2' );
  12. const restartButton = document.getElementById( 'restart' );
  13. const firstEditorErrorButton = document.getElementById( 'error-1' );
  14. const secondEditorErrorButton = document.getElementById( 'error-2' );
  15. const randomErrorButton = document.getElementById( 'random-error' );
  16. const editorConfig = {
  17. plugins: [
  18. ArticlePluginSet,
  19. ],
  20. toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote',
  21. 'insertTable', 'mediaEmbed', 'undo', 'redo' ],
  22. table: {
  23. contentToolbar: [
  24. 'tableColumn',
  25. 'tableRow',
  26. 'mergeTableCells'
  27. ]
  28. }
  29. };
  30. // Watchdog 1
  31. const watchdog1 = Watchdog.for( ClassicEditor );
  32. watchdog1.create( firstEditorElement, editorConfig ).then( () => {
  33. console.log( 'First editor created.' );
  34. firstEditorErrorButton.addEventListener( 'click', () => {
  35. throw new CKEditorError( 'Crash on the first editor model document', watchdog1.editor.model.document );
  36. } );
  37. } );
  38. watchdog1.on( 'crash', () => {
  39. console.log( 'First editor crashed!' );
  40. } );
  41. watchdog1.on( 'restart', () => {
  42. console.log( 'First editor restarted.' );
  43. } );
  44. // Watchdog 2
  45. const watchdog2 = Watchdog.for( ClassicEditor );
  46. watchdog2.create( secondEditorElement, editorConfig ).then( () => {
  47. console.log( 'Second editor created.' );
  48. secondEditorErrorButton.addEventListener( 'click', () => {
  49. throw new CKEditorError( 'Crash on the second editor model document', watchdog2.editor.model.document );
  50. } );
  51. } );
  52. watchdog2.on( 'crash', () => {
  53. console.log( 'Second editor crashed!' );
  54. } );
  55. watchdog2.on( 'restart', () => {
  56. console.log( 'Second editor restarted.' );
  57. } );
  58. restartButton.addEventListener( 'click', () => {
  59. watchdog1.restart();
  60. watchdog2.restart();
  61. } );
  62. randomErrorButton.addEventListener( 'click', () => {
  63. throw new Error( 'foo' );
  64. } );