UNPKG

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