|
16 | 16 |
|
17 | 17 | import { Widget } from '@phosphor/widgets'; |
18 | 18 | import { Message } from '@phosphor/messaging'; |
19 | | -import { Event } from '../common/event'; |
| 19 | +import { Emitter, Event } from '../common/event'; |
20 | 20 | import { MaybePromise } from '../common/types'; |
21 | 21 | import { Key } from './keyboard/keys'; |
22 | 22 | import { AbstractDialog } from './dialogs'; |
23 | 23 | import { waitForClosed } from './widgets'; |
24 | 24 | import { nls } from '../common/nls'; |
25 | | -import { isObject } from '../common'; |
| 25 | +import { Disposable, isObject } from '../common'; |
26 | 26 |
|
27 | 27 | export interface Saveable { |
28 | 28 | readonly dirty: boolean; |
@@ -50,6 +50,45 @@ export interface SaveableSource { |
50 | 50 | readonly saveable: Saveable; |
51 | 51 | } |
52 | 52 |
|
| 53 | +export class SaveableDelegate implements Saveable { |
| 54 | + dirty = false; |
| 55 | + protected readonly onDirtyChangedEmitter = new Emitter<void>(); |
| 56 | + |
| 57 | + get onDirtyChanged(): Event<void> { |
| 58 | + return this.onDirtyChangedEmitter.event; |
| 59 | + } |
| 60 | + autoSave: 'off' | 'afterDelay' | 'onFocusChange' | 'onWindowChange' = 'off'; |
| 61 | + |
| 62 | + async save(options?: SaveOptions): Promise<void> { |
| 63 | + await this.delegate?.save(options); |
| 64 | + } |
| 65 | + |
| 66 | + revert?(options?: Saveable.RevertOptions): Promise<void>; |
| 67 | + createSnapshot?(): Saveable.Snapshot; |
| 68 | + applySnapshot?(snapshot: object): void; |
| 69 | + |
| 70 | + protected delegate?: Saveable; |
| 71 | + protected toDispose?: Disposable; |
| 72 | + |
| 73 | + set(delegate: Saveable): void { |
| 74 | + this.toDispose?.dispose(); |
| 75 | + this.delegate = delegate; |
| 76 | + this.toDispose = this.delegate.onDirtyChanged(() => { |
| 77 | + this.dirty = delegate.dirty; |
| 78 | + this.onDirtyChangedEmitter.fire(); |
| 79 | + }); |
| 80 | + this.autoSave = delegate.autoSave; |
| 81 | + if (this.dirty !== delegate.dirty) { |
| 82 | + this.dirty = delegate.dirty; |
| 83 | + this.onDirtyChangedEmitter.fire(); |
| 84 | + } |
| 85 | + this.revert = delegate.revert?.bind(delegate); |
| 86 | + this.createSnapshot = delegate.createSnapshot?.bind(delegate); |
| 87 | + this.applySnapshot = delegate.applySnapshot?.bind(delegate); |
| 88 | + } |
| 89 | + |
| 90 | +} |
| 91 | + |
53 | 92 | export namespace Saveable { |
54 | 93 | export interface RevertOptions { |
55 | 94 | /** |
|
0 commit comments