UNPKG

8.18 kBTypeScriptView Raw
1import { ISessionContext, SessionContext } from '@jupyterlab/apputils';
2import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
3import { Contents, ServiceManager } from '@jupyterlab/services';
4import { ITranslator } from '@jupyterlab/translation';
5import { IDisposable } from '@lumino/disposable';
6import { ISignal } from '@lumino/signaling';
7import { Widget } from '@lumino/widgets';
8import { DocumentRegistry } from './registry';
9/**
10 * An implementation of a document context.
11 *
12 * This class is typically instantiated by the document manager.
13 */
14export declare class Context<T extends DocumentRegistry.IModel = DocumentRegistry.IModel> implements DocumentRegistry.IContext<T> {
15 /**
16 * Construct a new document context.
17 */
18 constructor(options: Context.IOptions<T>);
19 /**
20 * A signal emitted when the path changes.
21 */
22 get pathChanged(): ISignal<this, string>;
23 /**
24 * A signal emitted when the model is saved or reverted.
25 */
26 get fileChanged(): ISignal<this, Omit<Contents.IModel, 'content'>>;
27 /**
28 * A signal emitted on the start and end of a saving operation.
29 */
30 get saveState(): ISignal<this, DocumentRegistry.SaveState>;
31 /**
32 * A signal emitted when the context is disposed.
33 */
34 get disposed(): ISignal<this, void>;
35 /**
36 * Configurable margin used to detect document modification conflicts, in milliseconds
37 */
38 get lastModifiedCheckMargin(): number;
39 set lastModifiedCheckMargin(value: number);
40 /**
41 * Get the model associated with the document.
42 */
43 get model(): T;
44 /**
45 * The client session object associated with the context.
46 */
47 readonly sessionContext: SessionContext;
48 /**
49 * The current path associated with the document.
50 */
51 get path(): string;
52 /**
53 * The current local path associated with the document.
54 * If the document is in the default notebook file browser,
55 * this is the same as the path.
56 */
57 get localPath(): string;
58 /**
59 * The document metadata, stored as a services contents model.
60 *
61 * #### Notes
62 * The contents model will be `null` until the context is populated.
63 * It will not have a `content` field.
64 */
65 get contentsModel(): Omit<Contents.IModel, 'content'> | null;
66 /**
67 * Get the model factory name.
68 *
69 * #### Notes
70 * This is not part of the `IContext` API.
71 */
72 get factoryName(): string;
73 /**
74 * Test whether the context is disposed.
75 */
76 get isDisposed(): boolean;
77 /**
78 * Dispose of the resources held by the context.
79 */
80 dispose(): void;
81 /**
82 * Whether the context is ready.
83 */
84 get isReady(): boolean;
85 /**
86 * A promise that is fulfilled when the context is ready.
87 */
88 get ready(): Promise<void>;
89 /**
90 * Whether the document can be saved via the Contents API.
91 */
92 protected get canSave(): boolean;
93 /**
94 * The url resolver for the context.
95 */
96 readonly urlResolver: IRenderMime.IResolver;
97 /**
98 * Initialize the context.
99 *
100 * @param isNew - Whether it is a new file.
101 *
102 * @returns a promise that resolves upon initialization.
103 */
104 initialize(isNew: boolean): Promise<void>;
105 /**
106 * Rename the document.
107 *
108 * @param newName - the new name for the document.
109 */
110 rename(newName: string): Promise<void>;
111 /**
112 * Save the document contents to disk.
113 */
114 save(): Promise<void>;
115 /**
116 * Save the document to a different path chosen by the user.
117 *
118 * It will be rejected if the user abort providing a new path.
119 */
120 saveAs(): Promise<void>;
121 /**
122 * Download a file.
123 *
124 * @returns A promise which resolves when the file has begun
125 * downloading.
126 */
127 download(): Promise<void>;
128 /**
129 * Revert the document contents to disk contents.
130 */
131 revert(): Promise<void>;
132 /**
133 * Create a checkpoint for the file.
134 */
135 createCheckpoint(): Promise<Contents.ICheckpointModel>;
136 /**
137 * Delete a checkpoint for the file.
138 */
139 deleteCheckpoint(checkpointId: string): Promise<void>;
140 /**
141 * Restore the file to a known checkpoint state.
142 */
143 restoreCheckpoint(checkpointId?: string): Promise<void>;
144 /**
145 * List available checkpoints for a file.
146 */
147 listCheckpoints(): Promise<Contents.ICheckpointModel[]>;
148 /**
149 * Add a sibling widget to the document manager.
150 *
151 * @param widget - The widget to add to the document manager.
152 *
153 * @param options - The desired options for adding the sibling.
154 *
155 * @returns A disposable used to remove the sibling if desired.
156 *
157 * #### Notes
158 * It is assumed that the widget has the same model and context
159 * as the original widget.
160 */
161 addSibling(widget: Widget, options?: DocumentRegistry.IOpenOptions): IDisposable;
162 /**
163 * Handle a change on the contents manager.
164 */
165 private _onFileChanged;
166 /**
167 * Handle a change to a session property.
168 */
169 private _onSessionChanged;
170 /**
171 * Update our contents model, without the content.
172 */
173 private _updateContentsModel;
174 private _updatePath;
175 /**
176 * Handle an initial population.
177 */
178 private _populate;
179 /**
180 * Rename the document.
181 *
182 * @param newName - the new name for the document.
183 */
184 private _rename;
185 /**
186 * Save the document contents to disk.
187 */
188 private _save;
189 /**
190 * Revert the document contents to disk contents.
191 *
192 * @param initializeModel - call the model's initialization function after
193 * deserializing the content.
194 */
195 private _revert;
196 /**
197 * Save a file, dealing with conflicts.
198 */
199 private _maybeSave;
200 /**
201 * Handle a save/load error with a dialog.
202 */
203 private _handleError;
204 /**
205 * Add a checkpoint the file is writable.
206 */
207 private _maybeCheckpoint;
208 /**
209 * Handle a time conflict.
210 */
211 private _raiseConflict;
212 /**
213 * Handle a time conflict.
214 */
215 private _maybeOverWrite;
216 /**
217 * Finish a saveAs operation given a new path.
218 */
219 private _finishSaveAs;
220 private _createSaveOptions;
221 protected translator: ITranslator;
222 private _isReady;
223 private _isDisposed;
224 private _isPopulated;
225 private _trans;
226 private _manager;
227 private _opener;
228 private _model;
229 private _path;
230 private _lineEnding;
231 private _factory;
232 private _contentsModel;
233 private _readyPromise;
234 private _populatedPromise;
235 private _pathChanged;
236 private _fileChanged;
237 private _saveState;
238 private _disposed;
239 private _dialogs;
240 private _lastModifiedCheckMargin;
241 private _conflictModalIsOpen;
242}
243/**
244 * A namespace for `Context` statics.
245 */
246export declare namespace Context {
247 /**
248 * The options used to initialize a context.
249 */
250 interface IOptions<T extends DocumentRegistry.IModel> {
251 /**
252 * A service manager instance.
253 */
254 manager: ServiceManager.IManager;
255 /**
256 * The model factory used to create the model.
257 */
258 factory: DocumentRegistry.IModelFactory<T>;
259 /**
260 * The initial path of the file.
261 */
262 path: string;
263 /**
264 * The kernel preference associated with the context.
265 */
266 kernelPreference?: ISessionContext.IKernelPreference;
267 /**
268 * An optional callback for opening sibling widgets.
269 */
270 opener?: (widget: Widget) => void;
271 /**
272 * A function to call when the kernel is busy.
273 */
274 setBusy?: () => IDisposable;
275 /**
276 * The dialogs used for the session context.
277 */
278 sessionDialogs?: ISessionContext.IDialogs;
279 /**
280 * The application language translator.
281 */
282 translator?: ITranslator;
283 /**
284 * Max acceptable difference, in milliseconds, between last modified timestamps on disk and client
285 */
286 lastModifiedCheckMargin?: number;
287 }
288}