UNPKG

9.27 kBPlain TextView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3
4import { IChangedArgs } from '@jupyterlab/coreutils';
5import { DocumentRegistry, IDocumentWidget } from '@jupyterlab/docregistry';
6import { Contents, Kernel, ServiceManager } from '@jupyterlab/services';
7import { Token } from '@lumino/coreutils';
8import { IDisposable } from '@lumino/disposable';
9import { ISignal } from '@lumino/signaling';
10import { Widget } from '@lumino/widgets';
11
12/**
13 * The document registry token.
14 */
15export const IDocumentManager = new Token<IDocumentManager>(
16 '@jupyterlab/docmanager:IDocumentManager',
17 `A service for the manager for all
18 documents used by the application. Use this if you want to open and close documents,
19 create and delete files, and otherwise interact with the file system.`
20);
21
22/**
23 * The document widget opener token.
24 */
25export const IDocumentWidgetOpener = new Token<IDocumentWidgetOpener>(
26 '@jupyterlab/docmanager:IDocumentWidgetOpener',
27 `A service to open a widget.`
28);
29
30/**
31 * The recent documents database token.
32 */
33export const IRecentsManager = new Token<IRecentsManager>(
34 '@jupyterlab/docmanager:IRecentsManager',
35 `A service providing information about recently opened and closed documents`
36);
37
38/**
39 * The interface for a document manager.
40 */
41export interface IDocumentManager extends IDisposable {
42 /**
43 * The registry used by the manager.
44 */
45 readonly registry: DocumentRegistry;
46
47 /**
48 * The service manager used by the manager.
49 */
50 readonly services: ServiceManager.IManager;
51
52 /**
53 * A signal emitted when one of the documents is activated.
54 */
55 readonly activateRequested: ISignal<this, string>;
56
57 /**
58 * Whether to autosave documents.
59 */
60 autosave: boolean;
61
62 /**
63 * Whether to ask confirmation to close a tab or not.
64 */
65 confirmClosingDocument: boolean;
66
67 /**
68 * Determines the time interval for autosave in seconds.
69 */
70 autosaveInterval: number;
71
72 /**
73 * Defines max acceptable difference, in milliseconds, between last modified timestamps on disk and client.
74 */
75 lastModifiedCheckMargin: number;
76
77 /**
78 * Whether to ask the user to rename untitled file on first manual save.
79 */
80 renameUntitledFileOnSave: boolean;
81
82 /**
83 * Signal triggered when an attribute changes.
84 */
85 readonly stateChanged: ISignal<IDocumentManager, IChangedArgs<any>>;
86
87 /**
88 * Clone a widget.
89 *
90 * @param widget - The source widget.
91 *
92 * @returns A new widget or `undefined`.
93 *
94 * #### Notes
95 * Uses the same widget factory and context as the source, or returns
96 * `undefined` if the source widget is not managed by this manager.
97 */
98 cloneWidget(widget: Widget): IDocumentWidget | undefined;
99
100 /**
101 * Close all of the open documents.
102 *
103 * @returns A promise resolving when the widgets are closed.
104 */
105 closeAll(): Promise<void>;
106
107 /**
108 * Close the widgets associated with a given path.
109 *
110 * @param path - The target path.
111 *
112 * @returns A promise resolving when the widgets are closed.
113 */
114 closeFile(path: string): Promise<void>;
115
116 /**
117 * Get the document context for a widget.
118 *
119 * @param widget - The widget of interest.
120 *
121 * @returns The context associated with the widget, or `undefined` if no such
122 * context exists.
123 */
124 contextForWidget(widget: Widget): DocumentRegistry.Context | undefined;
125
126 /**
127 * Copy a file.
128 *
129 * @param fromFile - The full path of the original file.
130 *
131 * @param toDir - The full path to the target directory.
132 *
133 * @returns A promise which resolves to the contents of the file.
134 */
135 copy(fromFile: string, toDir: string): Promise<Contents.IModel>;
136
137 /**
138 * Create a new file and return the widget used to view it.
139 *
140 * @param path - The file path to create.
141 *
142 * @param widgetName - The name of the widget factory to use. 'default' will use the default widget.
143 *
144 * @param kernel - An optional kernel name/id to override the default.
145 *
146 * @returns The created widget, or `undefined`.
147 *
148 * #### Notes
149 * This function will return `undefined` if a valid widget factory
150 * cannot be found.
151 */
152 createNew(
153 path: string,
154 widgetName?: string,
155 kernel?: Partial<Kernel.IModel>
156 ): Widget | undefined;
157
158 /**
159 * Delete a file.
160 *
161 * @param path - The full path to the file to be deleted.
162 *
163 * @returns A promise which resolves when the file is deleted.
164 *
165 * #### Notes
166 * If there is a running session associated with the file and no other
167 * sessions are using the kernel, the session will be shut down.
168 */
169 deleteFile(path: string): Promise<void>;
170
171 /**
172 * Duplicate a file.
173 *
174 * @param path - The full path to the file to be duplicated.
175 *
176 * @returns A promise which resolves when the file is duplicated.
177 *
178 */
179 duplicate(path: string): Promise<Contents.IModel>;
180
181 /**
182 * See if a widget already exists for the given path and widget name.
183 *
184 * @param path - The file path to use.
185 *
186 * @param widgetName - The name of the widget factory to use. 'default' will use the default widget.
187 *
188 * @returns The found widget, or `undefined`.
189 *
190 * #### Notes
191 * This can be used to find an existing widget instead of opening
192 * a new widget.
193 */
194 findWidget(
195 path: string,
196 widgetName?: string | null
197 ): IDocumentWidget | undefined;
198
199 /**
200 * Create a new untitled file.
201 *
202 * @param options - The file content creation options.
203 */
204 newUntitled(options: Contents.ICreateOptions): Promise<Contents.IModel>;
205
206 /**
207 * Open a file and return the widget used to view it.
208 *
209 * @param path - The file path to open.
210 *
211 * @param widgetName - The name of the widget factory to use. 'default' will use the default widget.
212 *
213 * @param kernel - An optional kernel name/id to override the default.
214 *
215 * @returns The created widget, or `undefined`.
216 *
217 * #### Notes
218 * This function will return `undefined` if a valid widget factory
219 * cannot be found.
220 */
221 open(
222 path: string,
223 widgetName?: string,
224 kernel?: Partial<Kernel.IModel>,
225 options?: DocumentRegistry.IOpenOptions
226 ): IDocumentWidget | undefined;
227
228 /**
229 * Open a file and return the widget used to view it.
230 * Reveals an already existing editor.
231 *
232 * @param path - The file path to open.
233 *
234 * @param widgetName - The name of the widget factory to use. 'default' will use the default widget.
235 *
236 * @param kernel - An optional kernel name/id to override the default.
237 *
238 * @returns The created widget, or `undefined`.
239 *
240 * #### Notes
241 * This function will return `undefined` if a valid widget factory
242 * cannot be found.
243 */
244 openOrReveal(
245 path: string,
246 widgetName?: string,
247 kernel?: Partial<Kernel.IModel>,
248 options?: DocumentRegistry.IOpenOptions
249 ): IDocumentWidget | undefined;
250
251 /**
252 * Overwrite a file.
253 *
254 * @param oldPath - The full path to the original file.
255 *
256 * @param newPath - The full path to the new file.
257 *
258 * @returns A promise containing the new file contents model.
259 */
260 overwrite(oldPath: string, newPath: string): Promise<Contents.IModel>;
261
262 /**
263 * Rename a file or directory.
264 *
265 * @param oldPath - The full path to the original file.
266 *
267 * @param newPath - The full path to the new file.
268 *
269 * @returns A promise containing the new file contents model. The promise
270 * will reject if the newPath already exists. Use [[overwrite]] to overwrite
271 * a file.
272 */
273 rename(oldPath: string, newPath: string): Promise<Contents.IModel>;
274}
275
276/**
277 * The interface for a widget opener.
278 */
279export interface IDocumentWidgetOpener {
280 /**
281 * Open the given widget.
282 */
283 open(widget: IDocumentWidget, options?: DocumentRegistry.IOpenOptions): void;
284
285 /**
286 * A signal emitted when a widget is opened
287 */
288 readonly opened: ISignal<IDocumentWidgetOpener, IDocumentWidget>;
289}
290
291/**
292 * Recent opened items manager.
293 */
294export interface IRecentsManager extends IDisposable {
295 /**
296 * Get the recently opened documents.
297 */
298 readonly recentlyOpened: RecentDocument[];
299
300 /**
301 * Get the recently closed items.
302 */
303 readonly recentlyClosed: RecentDocument[];
304
305 /**
306 * Signal emitted when either of the list changes.
307 */
308 readonly changed: ISignal<IRecentsManager, void>;
309
310 /**
311 * Check if the recent item is valid, remove if it from both lists if it is not.
312 */
313 validate(recent: RecentDocument): Promise<boolean>;
314
315 /**
316 * Add a new path to the recent list.
317 */
318 addRecent(
319 document: Omit<RecentDocument, 'root'>,
320 event: 'opened' | 'closed'
321 ): void;
322
323 /**
324 * Remove the document from recents list.
325 */
326 removeRecent(document: RecentDocument, event: 'opened' | 'closed'): void;
327}
328
329/**
330 * The interface for a recent document.
331 */
332export type RecentDocument = {
333 /**
334 * The server root path.
335 *
336 * Allows to select only the currently accessible documents.
337 */
338 root: string;
339 /**
340 * The path to the document.
341 */
342 path: string;
343 /**
344 * The document content type or `directory` literal for directories.
345 */
346 contentType: string;
347 /**
348 * The factory that was used when the document was most recently opened or closed.
349 */
350 factory?: string;
351};