watchdog.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 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 EditorWatchdog from '../../src/editorwatchdog';
  9. class TypingError {
  10. constructor( editor ) {
  11. this.editor = editor;
  12. }
  13. init() {
  14. const inputCommand = this.editor.commands.get( 'input' );
  15. inputCommand.on( 'execute', ( evt, data ) => {
  16. const commandArgs = data[ 0 ];
  17. if ( commandArgs.text === '1' ) {
  18. // Simulate error.
  19. this.editor.foo.bar = 'bom';
  20. }
  21. } );
  22. }
  23. }
  24. const editorConfig = {
  25. plugins: [
  26. ArticlePluginSet, TypingError
  27. ],
  28. toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote',
  29. 'insertTable', 'mediaEmbed', 'undo', 'redo' ],
  30. table: {
  31. contentToolbar: [
  32. 'tableColumn',
  33. 'tableRow',
  34. 'mergeTableCells'
  35. ]
  36. }
  37. };
  38. const watchdog1 = createWatchdog(
  39. document.getElementById( 'editor-1' ),
  40. document.getElementById( 'editor-1-state' ),
  41. 'First'
  42. );
  43. const watchdog2 = createWatchdog(
  44. document.getElementById( 'editor-2' ),
  45. document.getElementById( 'editor-2-state' ),
  46. 'Second'
  47. );
  48. Object.assign( window, { watchdog1, watchdog2 } );
  49. document.getElementById( 'random-error' ).addEventListener( 'click', () => {
  50. throw new Error( 'foo' );
  51. } );
  52. function createWatchdog( editorElement, stateElement, name ) {
  53. const watchdog = new EditorWatchdog( ClassicEditor );
  54. watchdog.create( editorElement, editorConfig );
  55. watchdog.on( 'error', () => {
  56. console.log( `${ name } editor crashed!` );
  57. } );
  58. watchdog.on( 'restart', () => {
  59. console.log( `${ name } editor restarted.` );
  60. } );
  61. watchdog.on( 'stateChange', () => {
  62. console.log( `${ name } watchdog state changed to '${ watchdog.state }'` );
  63. stateElement.innerText = watchdog.state;
  64. if ( watchdog.state === 'crashedPermanently' ) {
  65. watchdog.editor.isReadOnly = true;
  66. }
  67. } );
  68. stateElement.innerText = watchdog.state;
  69. return watchdog;
  70. }