UNPKG

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