|
| 1 | +import { Component, Inject, NgZone } from '@angular/core'; |
| 2 | +import type { WatchdogConfig } from 'ckeditor5'; |
| 3 | +import { AngularEditor } from 'src/editor/editor'; |
| 4 | + |
| 5 | +@Component( { |
| 6 | + selector: 'watchdog-demo', |
| 7 | + templateUrl: './editor-watchdog-demo.html', |
| 8 | + styleUrls: [ './editor-watchdog-demo.css' ] |
| 9 | +} ) |
| 10 | +export class EditorWatchdogDemoComponent { |
| 11 | + public Editor = AngularEditor; |
| 12 | + |
| 13 | + public ready = false; |
| 14 | + public isDisabled = false; |
| 15 | + public isWatchdogDisabled = false; |
| 16 | + public errors: Array<{ timestamp: Date; message: string }> = []; |
| 17 | + public editorWatchdogConfig: WatchdogConfig = { |
| 18 | + crashNumberLimit: 500, |
| 19 | + minimumNonErrorTimePeriod: 1000, |
| 20 | + saveInterval: 1200 |
| 21 | + }; |
| 22 | + |
| 23 | + private editor?: AngularEditor; |
| 24 | + private ngZone: NgZone; |
| 25 | + |
| 26 | + constructor( @Inject( NgZone ) ngZone: NgZone ) { |
| 27 | + this.ngZone = ngZone; |
| 28 | + } |
| 29 | + |
| 30 | + public onReady( editor: AngularEditor ): void { |
| 31 | + console.log( 'Editor initialized', editor ); |
| 32 | + this.editor = editor; |
| 33 | + } |
| 34 | + |
| 35 | + public toggle(): void { |
| 36 | + this.isDisabled = !this.isDisabled; |
| 37 | + } |
| 38 | + |
| 39 | + public toggleWatchdog(): void { |
| 40 | + this.isWatchdogDisabled = !this.isWatchdogDisabled; |
| 41 | + } |
| 42 | + |
| 43 | + public simulateError(): void { |
| 44 | + this.ngZone.runOutsideAngular( () => { |
| 45 | + setTimeout( () => { |
| 46 | + const err: any = new Error( 'foo' ); |
| 47 | + |
| 48 | + err.context = this.editor; |
| 49 | + err.is = () => !!this.editor; |
| 50 | + |
| 51 | + throw err; |
| 52 | + } ); |
| 53 | + } ); |
| 54 | + } |
| 55 | + |
| 56 | + public onError( error: any ): void { |
| 57 | + // Watchdog should return undefined error when it restarts the editor. |
| 58 | + this.errors.unshift( { |
| 59 | + timestamp: new Date(), |
| 60 | + message: error?.toString() ?? 'Watchdog restarted editor.' |
| 61 | + } ); |
| 62 | + } |
| 63 | +} |
0 commit comments