watchdog.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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, window */
  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 randomErrorButton = document.getElementById( 'random-error' );
  14. class TypingError {
  15. constructor( editor ) {
  16. this.editor = editor;
  17. }
  18. init() {
  19. const inputCommand = this.editor.commands.get( 'input' );
  20. inputCommand.on( 'execute', ( evt, data ) => {
  21. const commandArgs = data[ 0 ];
  22. if ( commandArgs.text === '1' ) {
  23. throw new CKEditorError( 'Fake error - input command executed with value `1`', this );
  24. }
  25. } );
  26. }
  27. }
  28. const editorConfig = {
  29. plugins: [
  30. ArticlePluginSet, TypingError
  31. ],
  32. toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote',
  33. 'insertTable', 'mediaEmbed', 'undo', 'redo' ],
  34. table: {
  35. contentToolbar: [
  36. 'tableColumn',
  37. 'tableRow',
  38. 'mergeTableCells'
  39. ]
  40. }
  41. };
  42. // Watchdog 1
  43. const watchdog1 = Watchdog.for( ClassicEditor );
  44. window.watchdog1 = watchdog1;
  45. watchdog1.create( firstEditorElement, editorConfig ).then( () => {
  46. console.log( 'First editor created.' );
  47. } );
  48. watchdog1.on( 'error', () => {
  49. console.log( 'First editor crashed!' );
  50. } );
  51. watchdog1.on( 'restart', () => {
  52. console.log( 'First editor restarted.' );
  53. } );
  54. // Watchdog 2
  55. const watchdog2 = Watchdog.for( ClassicEditor );
  56. window.watchdog2 = watchdog2;
  57. watchdog2.create( secondEditorElement, editorConfig ).then( () => {
  58. console.log( 'Second editor created.' );
  59. } );
  60. watchdog2.on( 'error', () => {
  61. console.log( 'Second editor crashed!' );
  62. } );
  63. watchdog2.on( 'restart', () => {
  64. console.log( 'Second editor restarted.' );
  65. } );
  66. restartButton.addEventListener( 'click', () => {
  67. watchdog1.restart();
  68. watchdog2.restart();
  69. } );
  70. randomErrorButton.addEventListener( 'click', () => {
  71. throw new Error( 'foo' );
  72. } );