UNPKG

4.63 kBTypeScriptView Raw
1import { Widget } from '@phosphor/widgets';
2import { Message } from '@phosphor/messaging';
3import { Emitter, Event } from '../common/event';
4import { MaybePromise } from '../common/types';
5import { AbstractDialog } from './dialogs';
6import { Disposable } from '../common';
7export interface Saveable {
8 readonly dirty: boolean;
9 readonly onDirtyChanged: Event<void>;
10 readonly autoSave: 'off' | 'afterDelay' | 'onFocusChange' | 'onWindowChange';
11 /**
12 * Saves dirty changes.
13 */
14 save(options?: SaveOptions): MaybePromise<void>;
15 /**
16 * Reverts dirty changes.
17 */
18 revert?(options?: Saveable.RevertOptions): Promise<void>;
19 /**
20 * Creates a snapshot of the dirty state.
21 */
22 createSnapshot?(): Saveable.Snapshot;
23 /**
24 * Applies the given snapshot to the dirty state.
25 */
26 applySnapshot?(snapshot: object): void;
27}
28export interface SaveableSource {
29 readonly saveable: Saveable;
30}
31export declare class DelegatingSaveable implements Saveable {
32 dirty: boolean;
33 protected readonly onDirtyChangedEmitter: Emitter<void>;
34 get onDirtyChanged(): Event<void>;
35 autoSave: 'off' | 'afterDelay' | 'onFocusChange' | 'onWindowChange';
36 save(options?: SaveOptions): Promise<void>;
37 revert?(options?: Saveable.RevertOptions): Promise<void>;
38 createSnapshot?(): Saveable.Snapshot;
39 applySnapshot?(snapshot: object): void;
40 protected _delegate?: Saveable;
41 protected toDispose?: Disposable;
42 set delegate(delegate: Saveable);
43}
44export declare namespace Saveable {
45 interface RevertOptions {
46 /**
47 * If soft then only dirty flag should be updated, otherwise
48 * the underlying data should be reverted as well.
49 */
50 soft?: boolean;
51 }
52 type Snapshot = {
53 value: string;
54 } | {
55 read(): string | null;
56 };
57 function isSource(arg: unknown): arg is SaveableSource;
58 function is(arg: unknown): arg is Saveable;
59 function get(arg: unknown): Saveable | undefined;
60 function getDirty(arg: unknown): Saveable | undefined;
61 function isDirty(arg: unknown): boolean;
62 function save(arg: unknown, options?: SaveOptions): Promise<void>;
63 function confirmSaveBeforeClose(toClose: Iterable<Widget>, others: Widget[]): Promise<boolean | undefined>;
64 function apply(widget: Widget, getOtherSaveables?: () => Array<Widget | SaveableWidget>, doSave?: (widget: Widget, options?: SaveOptions) => Promise<void>): SaveableWidget | undefined;
65 function shouldSave(saveable: Saveable, cb: () => MaybePromise<boolean | undefined>): Promise<boolean | undefined>;
66}
67export interface SaveableWidget extends Widget {
68 /**
69 * @param doRevert whether the saveable should be reverted before being saved. Defaults to `true`.
70 */
71 closeWithoutSaving(doRevert?: boolean): Promise<void>;
72 closeWithSaving(options?: SaveableWidget.CloseOptions): Promise<void>;
73}
74export declare const close: unique symbol;
75/**
76 * An interface describing saveable widgets that are created by the `Saveable.apply` function.
77 * The original `close` function is reassigned to a locally-defined `Symbol`
78 */
79export interface PostCreationSaveableWidget extends SaveableWidget {
80 /**
81 * The original `close` function of the widget
82 */
83 [close](): void;
84}
85export declare namespace SaveableWidget {
86 function is(widget: Widget | undefined): widget is SaveableWidget;
87 function getDirty<T extends Widget>(widgets: Iterable<T>): IterableIterator<SaveableWidget & T>;
88 function get<T extends Widget>(widgets: Iterable<T>, filter?: (widget: T) => boolean): IterableIterator<SaveableWidget & T>;
89 interface CloseOptions {
90 shouldSave?(): MaybePromise<boolean | undefined>;
91 }
92}
93/**
94 * Possible formatting types when saving.
95 */
96export declare const enum FormatType {
97 /**
98 * Formatting should occur (default).
99 */
100 ON = 1,
101 /**
102 * Formatting should not occur.
103 */
104 OFF = 2,
105 /**
106 * Formatting should only occur if the resource is dirty.
107 */
108 DIRTY = 3
109}
110export interface SaveOptions {
111 /**
112 * Formatting type to apply when saving.
113 */
114 readonly formatType?: FormatType;
115}
116export declare function setDirty(widget: Widget, dirty: boolean): void;
117export declare class ShouldSaveDialog extends AbstractDialog<boolean> {
118 protected shouldSave: boolean;
119 protected readonly dontSaveButton: HTMLButtonElement;
120 constructor(widget: Widget);
121 protected appendDontSaveButton(): HTMLButtonElement;
122 protected onAfterAttach(msg: Message): void;
123 get value(): boolean;
124}
125//# sourceMappingURL=saveable.d.ts.map
\No newline at end of file