UNPKG

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