12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061 |
- // Copyright (c) Jupyter Development Team.
- // Distributed under the terms of the Modified BSD License.
- import {
- Contents, ContentsManager, Kernel
- } from '@jupyterlab/services';
- import {
- IIterator, each, empty, map
- } from 'phosphor/lib/algorithm/iteration';
- import {
- find, findIndex, indexOf
- } from 'phosphor/lib/algorithm/searching';
- import {
- Vector
- } from 'phosphor/lib/collections/vector';
- import {
- IDisposable, DisposableDelegate
- } from 'phosphor/lib/core/disposable';
- import {
- ISignal, clearSignalData, defineSignal
- } from 'phosphor/lib/core/signaling';
- import {
- Token
- } from 'phosphor/lib/core/token';
- import {
- Widget
- } from 'phosphor/lib/ui/widget';
- import {
- CodeEditor
- } from '../codeeditor';
- import {
- IChangedArgs as IChangedArgsGeneric
- } from '../common/interfaces';
- /* tslint:disable */
- /**
- * The document registry token.
- */
- export
- const IDocumentRegistry = new Token<IDocumentRegistry>('jupyter.services.document-registry');
- /* tslint:enable */
- /**
- * The interface for a document registry.
- */
- export
- interface IDocumentRegistry extends DocumentRegistry {}
- /**
- * The document registry.
- */
- export
- class DocumentRegistry implements IDisposable {
- /**
- * A signal emitted when the registry has changed.
- */
- readonly changed: ISignal<this, DocumentRegistry.IChangedArgs>;
- /**
- * Get whether the document registry has been disposed.
- */
- get isDisposed(): boolean {
- return this._widgetFactories === null;
- }
- /**
- * Dispose of the resources held by the document registery.
- */
- dispose(): void {
- if (this._widgetFactories === null) {
- return;
- }
- let widgetFactories = this._widgetFactories;
- let modelFactories = this._modelFactories;
- let extenders = this._extenders;
- this._widgetFactories = null;
- this._modelFactories = null;
- this._extenders = null;
- for (let modelName in modelFactories) {
- modelFactories[modelName].dispose();
- }
- for (let widgetName in widgetFactories) {
- widgetFactories[widgetName].dispose();
- }
- for (let widgetName in extenders) {
- extenders[widgetName].clear();
- }
- this._fileTypes.clear();
- this._creators.clear();
- clearSignalData(this);
- }
- /**
- * Add a widget factory to the registry.
- *
- * @param factory - The factory instance to register.
- *
- * @returns A disposable which will unregister the factory.
- *
- * #### Notes
- * If a factory with the given `'displayName'` is already registered,
- * a warning will be logged, and this will be a no-op.
- * If `'*'` is given as a default extension, the factory will be registered
- * as the global default.
- * If an extension or global default is already registered, this factory
- * will override the existing default.
- */
- addWidgetFactory(factory: DocumentRegistry.WidgetFactory): IDisposable {
- let name = factory.name.toLowerCase();
- if (this._widgetFactories[name]) {
- console.warn(`Duplicate registered factory ${name}`);
- return new DisposableDelegate(null);
- }
- this._widgetFactories[name] = factory;
- for (let ext of factory.defaultFor) {
- if (factory.fileExtensions.indexOf(ext) === -1) {
- continue;
- }
- if (ext === '*') {
- this._defaultWidgetFactory = name;
- } else {
- this._defaultWidgetFactories[ext] = name;
- }
- }
- // For convenience, store a mapping of ext -> name
- for (let ext of factory.fileExtensions) {
- if (!this._widgetFactoryExtensions[ext]) {
- this._widgetFactoryExtensions[ext] = new Vector<string>();
- }
- this._widgetFactoryExtensions[ext].pushBack(name);
- }
- this.changed.emit({
- type: 'widgetFactory',
- name,
- change: 'added'
- });
- return new DisposableDelegate(() => {
- delete this._widgetFactories[name];
- if (this._defaultWidgetFactory === name) {
- this._defaultWidgetFactory = '';
- }
- for (let ext of Object.keys(this._defaultWidgetFactories)) {
- if (this._defaultWidgetFactories[ext] === name) {
- delete this._defaultWidgetFactories[ext];
- }
- }
- for (let ext of Object.keys(this._widgetFactoryExtensions)) {
- this._widgetFactoryExtensions[ext].remove(name);
- if (this._widgetFactoryExtensions[ext].length === 0) {
- delete this._widgetFactoryExtensions[ext];
- }
- }
- this.changed.emit({
- type: 'widgetFactory',
- name,
- change: 'removed'
- });
- });
- }
- /**
- * Add a model factory to the registry.
- *
- * @param factory - The factory instance.
- *
- * @returns A disposable which will unregister the factory.
- *
- * #### Notes
- * If a factory with the given `name` is already registered, or
- * the given factory is already registered, a warning will be logged
- * and this will be a no-op.
- */
- addModelFactory(factory: DocumentRegistry.ModelFactory): IDisposable {
- let name = factory.name.toLowerCase();
- if (this._modelFactories[name]) {
- console.warn(`Duplicate registered factory ${name}`);
- return new DisposableDelegate(null);
- }
- this._modelFactories[name] = factory;
- this.changed.emit({
- type: 'modelFactory',
- name,
- change: 'added'
- });
- return new DisposableDelegate(() => {
- delete this._modelFactories[name];
- this.changed.emit({
- type: 'modelFactory',
- name,
- change: 'removed'
- });
- });
- }
- /**
- * Add a widget extension to the registry.
- *
- * @param widgetName - The name of the widget factory.
- *
- * @param extension - A widget extension.
- *
- * @returns A disposable which will unregister the extension.
- *
- * #### Notes
- * If the extension is already registered for the given
- * widget name, a warning will be logged and this will be a no-op.
- */
- addWidgetExtension(widgetName: string, extension: DocumentRegistry.WidgetExtension): IDisposable {
- widgetName = widgetName.toLowerCase();
- if (!(widgetName in this._extenders)) {
- this._extenders[widgetName] = new Vector<DocumentRegistry.WidgetExtension>();
- }
- let extenders = this._extenders[widgetName];
- let index = indexOf(extenders, extension);
- if (index !== -1) {
- console.warn(`Duplicate registered extension for ${widgetName}`);
- return new DisposableDelegate(null);
- }
- this._extenders[widgetName].pushBack(extension);
- this.changed.emit({
- type: 'widgetExtension',
- name: null,
- change: 'added'
- });
- return new DisposableDelegate(() => {
- this._extenders[widgetName].remove(extension);
- this.changed.emit({
- type: 'widgetExtension',
- name: null,
- change: 'removed'
- });
- });
- }
- /**
- * Add a file type to the document registry.
- *
- * @params fileType - The file type object to register.
- *
- * @returns A disposable which will unregister the command.
- *
- * #### Notes
- * These are used to populate the "Create New" dialog.
- */
- addFileType(fileType: DocumentRegistry.IFileType): IDisposable {
- this._fileTypes.pushBack(fileType);
- this.changed.emit({
- type: 'fileType',
- name: fileType.name,
- change: 'added'
- });
- return new DisposableDelegate(() => {
- this._fileTypes.remove(fileType);
- this.changed.emit({
- type: 'fileType',
- name: fileType.name,
- change: 'removed'
- });
- });
- }
- /**
- * Add a creator to the registry.
- *
- * @params creator - The file creator object to register.
- *
- * @returns A disposable which will unregister the creator.
- */
- addCreator(creator: DocumentRegistry.IFileCreator): IDisposable {
- let index = findIndex(this._creators, (value) => {
- return value.name.localeCompare(creator.name) > 0;
- });
- if (index !== -1) {
- this._creators.insert(index, creator);
- } else {
- this._creators.pushBack(creator);
- }
- this.changed.emit({
- type: 'fileCreator',
- name: creator.name,
- change: 'added'
- });
- return new DisposableDelegate(() => {
- this._creators.remove(creator);
- this.changed.emit({
- type: 'fileCreator',
- name: creator.name,
- change: 'removed'
- });
- });
- }
- /**
- * Get a list of the preferred widget factories.
- *
- * @param ext - An optional file extension to filter the results.
- *
- * @returns A new array of widget factories.
- *
- * #### Notes
- * Only the widget factories whose associated model factory have
- * been registered will be returned.
- * The first item is considered the default. The returned iterator
- * has widget factories in the following order:
- * - extension-specific default factory
- * - global default factory
- * - all other extension-specific factories
- * - all other global factories
- */
- preferredWidgetFactories(ext: string = '*'): DocumentRegistry.WidgetFactory[] {
- let factories = new Set<string>();
- ext = Private.normalizeExtension(ext);
- let last = '.' + ext.split('.').pop();
- // Start with the extension-specific default factory.
- if (ext.length > 1) {
- if (ext in this._defaultWidgetFactories) {
- factories.add(this._defaultWidgetFactories[ext]);
- }
- }
- // Handle multi-part extension default factories.
- if (last !== ext) {
- if (last in this._defaultWidgetFactories) {
- factories.add(this._defaultWidgetFactories[last]);
- }
- }
- // Add the global default factory.
- if (this._defaultWidgetFactory) {
- factories.add(this._defaultWidgetFactory);
- }
- // Add the extension-specific factories in registration order.
- if (ext.length > 1) {
- if (ext in this._widgetFactoryExtensions) {
- each(this._widgetFactoryExtensions[ext], n => {
- factories.add(n);
- });
- }
- }
- // Handle multi-part extension-specific factories.
- if (last !== ext) {
- if (last in this._widgetFactoryExtensions) {
- each(this._widgetFactoryExtensions[last], n => {
- factories.add(n);
- });
- }
- }
- // Add the rest of the global factories, in registration order.
- if ('*' in this._widgetFactoryExtensions) {
- each(this._widgetFactoryExtensions['*'], n => {
- factories.add(n);
- });
- }
- // Construct the return list, checking to make sure the corresponding
- // model factories are registered.
- let factoryList: DocumentRegistry.WidgetFactory[] = [];
- factories.forEach(name => {
- if (this._widgetFactories[name].modelName in this._modelFactories) {
- factoryList.push(this._widgetFactories[name]);
- }
- });
- return factoryList;
- }
- /**
- * Get the default widget factory for an extension.
- *
- * @param ext - An optional file extension to filter the results.
- *
- * @returns The default widget factory for an extension.
- *
- * #### Notes
- * This is equivalent to the first value in [[preferredWidgetFactories]].
- */
- defaultWidgetFactory(ext: string = '*'): DocumentRegistry.WidgetFactory {
- return this.preferredWidgetFactories(ext)[0];
- }
- /**
- * Create an iterator over the widget factories that have been registered.
- *
- * @returns A new iterator of widget factories.
- */
- widgetFactories(): IIterator<DocumentRegistry.WidgetFactory> {
- return map(Object.keys(this._widgetFactories), name => {
- return this._widgetFactories[name];
- });
- }
- /**
- * Create an iterator over the model factories that have been registered.
- *
- * @returns A new iterator of model factories.
- */
- modelFactories(): IIterator<DocumentRegistry.ModelFactory> {
- return map(Object.keys(this._modelFactories), name => {
- return this._modelFactories[name];
- });
- }
- /**
- * Create an iterator over the registered extensions for a given widget.
- *
- * @param widgetName - The name of the widget factory.
- *
- * @returns A new iterator over the widget extensions.
- */
- widgetExtensions(widgetName: string): IIterator<DocumentRegistry.WidgetExtension> {
- widgetName = widgetName.toLowerCase();
- if (!(widgetName in this._extenders)) {
- return empty<DocumentRegistry.WidgetExtension>();
- }
- return this._extenders[widgetName].iter();
- }
- /**
- * Create an iterator over the file types that have been registered.
- *
- * @returns A new iterator of file types.
- */
- fileTypes(): IIterator<DocumentRegistry.IFileType> {
- return this._fileTypes.iter();
- }
- /**
- * Create an iterator over the file creators that have been registered.
- *
- * @returns A new iterator of file creatores.
- */
- creators(): IIterator<DocumentRegistry.IFileCreator> {
- return this._creators.iter();
- }
- /**
- * Get a widget factory by name.
- *
- * @param widgetName - The name of the widget factory.
- *
- * @returns A widget factory instance.
- */
- getWidgetFactory(widgetName: string): DocumentRegistry.WidgetFactory {
- return this._widgetFactories[widgetName.toLowerCase()];
- }
- /**
- * Get a model factory by name.
- *
- * @param name - The name of the model factory.
- *
- * @returns A model factory instance.
- */
- getModelFactory(name: string): DocumentRegistry.ModelFactory {
- return this._modelFactories[name.toLowerCase()];
- }
- /**
- * Get a file type by name.
- */
- getFileType(name: string): DocumentRegistry.IFileType {
- name = name.toLowerCase();
- return find(this._fileTypes, fileType => {
- return fileType.name.toLowerCase() === name;
- });
- }
- /**
- * Get a creator by name.
- */
- getCreator(name: string): DocumentRegistry.IFileCreator {
- name = name.toLowerCase();
- return find(this._creators, creator => {
- return creator.name.toLowerCase() === name;
- });
- }
- /**
- * Get a kernel preference.
- *
- * @param ext - The file extension.
- *
- * @param widgetName - The name of the widget factory.
- *
- * @returns A kernel preference.
- */
- getKernelPreference(ext: string, widgetName: string): DocumentRegistry.IKernelPreference {
- ext = Private.normalizeExtension(ext);
- widgetName = widgetName.toLowerCase();
- let widgetFactory = this._widgetFactories[widgetName];
- if (!widgetFactory) {
- return void 0;
- }
- let modelFactory = this.getModelFactory(widgetFactory.modelName);
- if (!modelFactory) {
- return void 0;
- }
- let language = modelFactory.preferredLanguage(ext);
- return {
- language,
- preferKernel: widgetFactory.preferKernel,
- canStartKernel: widgetFactory.canStartKernel
- };
- }
- private _modelFactories: { [key: string]: DocumentRegistry.ModelFactory } = Object.create(null);
- private _widgetFactories: { [key: string]: DocumentRegistry.WidgetFactory } = Object.create(null);
- private _defaultWidgetFactory = '';
- private _defaultWidgetFactories: { [key: string]: string } = Object.create(null);
- private _widgetFactoryExtensions: {[key: string]: Vector<string> } = Object.create(null);
- private _fileTypes = new Vector<DocumentRegistry.IFileType>();
- private _creators = new Vector<DocumentRegistry.IFileCreator>();
- private _extenders: { [key: string] : Vector<DocumentRegistry.WidgetExtension> } = Object.create(null);
- }
- /**
- * The namespace for the `DocumentRegistry` class statics.
- */
- export
- namespace DocumentRegistry {
- /**
- * The interface for a document model.
- */
- export
- interface IModel extends IDisposable {
- /**
- * A signal emitted when the document content changes.
- */
- contentChanged: ISignal<this, void>;
- /**
- * A signal emitted when the model state changes.
- */
- stateChanged: ISignal<this, IChangedArgsGeneric<any>>;
- /**
- * The dirty state of the model.
- *
- * #### Notes
- * This should be cleared when the document is loaded from
- * or saved to disk.
- */
- dirty: boolean;
- /**
- * The read-only state of the model.
- */
- readOnly: boolean;
- /**
- * The default kernel name of the document.
- */
- readonly defaultKernelName: string;
- /**
- * The default kernel language of the document.
- */
- readonly defaultKernelLanguage: string;
- /**
- * Serialize the model to a string.
- */
- toString(): string;
- /**
- * Deserialize the model from a string.
- *
- * #### Notes
- * Should emit a [contentChanged] signal.
- */
- fromString(value: string): void;
- /**
- * Serialize the model to JSON.
- */
- toJSON(): any;
- /**
- * Deserialize the model from JSON.
- *
- * #### Notes
- * Should emit a [contentChanged] signal.
- */
- fromJSON(value: any): void;
- }
- /**
- * The interface for a document model that represents code.
- */
- export
- interface ICodeModel extends IModel, CodeEditor.IModel { }
- /**
- * The document context object.
- */
- export
- interface IContext<T extends IModel> extends IDisposable {
- /**
- * A signal emitted when the kernel changes.
- */
- kernelChanged: ISignal<this, Kernel.IKernel>;
- /**
- * A signal emitted when the path changes.
- */
- pathChanged: ISignal<this, string>;
- /**
- * A signal emitted when the contentsModel changes.
- */
- fileChanged: ISignal<this, Contents.IModel>;
- /**
- * A signal emitted when the context is disposed.
- */
- disposed: ISignal<this, void>;
- /**
- * Get the model associated with the document.
- */
- readonly model: T;
- /**
- * The current kernel associated with the document.
- */
- readonly kernel: Kernel.IKernel;
- /**
- * The current path associated with the document.
- */
- readonly path: string;
- /**
- * The current contents model associated with the document
- *
- * #### Notes
- * The model will have an empty `contents` field.
- * It will be `null` until the context is ready.
- */
- readonly contentsModel: Contents.IModel;
- /**
- * Whether the context is ready.
- */
- readonly isReady: boolean;
- /**
- * A promise that is fulfilled when the context is ready.
- */
- readonly ready: Promise<void>;
- /**
- * Start the default kernel for the context.
- *
- * @returns A promise that resolves with the new kernel.
- */
- startDefaultKernel(): Promise<Kernel.IKernel>;
- /**
- * Change the current kernel associated with the document.
- *
- * #### Notes
- * If no options are given, the session is shut down.
- */
- changeKernel(options?: Kernel.IModel): Promise<Kernel.IKernel>;
- /**
- * Save the document contents to disk.
- */
- save(): Promise<void>;
- /**
- * Save the document to a different path chosen by the user.
- */
- saveAs(): Promise<void>;
- /**
- * Revert the document contents to disk contents.
- */
- revert(): Promise<void>;
- /**
- * Create a checkpoint for the file.
- *
- * @returns A promise which resolves with the new checkpoint model when the
- * checkpoint is created.
- */
- createCheckpoint(): Promise<Contents.ICheckpointModel>;
- /**
- * Delete a checkpoint for the file.
- *
- * @param checkpointID - The id of the checkpoint to delete.
- *
- * @returns A promise which resolves when the checkpoint is deleted.
- */
- deleteCheckpoint(checkpointID: string): Promise<void>;
- /**
- * Restore the file to a known checkpoint state.
- *
- * @param checkpointID - The optional id of the checkpoint to restore,
- * defaults to the most recent checkpoint.
- *
- * @returns A promise which resolves when the checkpoint is restored.
- */
- restoreCheckpoint(checkpointID?: string): Promise<void>;
- /**
- * List available checkpoints for the file.
- *
- * @returns A promise which resolves with a list of checkpoint models for
- * the file.
- */
- listCheckpoints(): Promise<Contents.ICheckpointModel[]>;
- /**
- * Resolve a url to a correct server path.
- *
- * @param url - The source url.
- *
- * @param local - Whether to to return the local path versus
- * a download path.
- *
- * @returns a promise which resolves with the resolved url.
- */
- resolveUrl(url: string, local: boolean): Promise<string>;
- /**
- * Add a sibling widget to the document manager.
- *
- * @param widget - The widget to add to the document manager.
- *
- * @returns A disposable used to remove the sibling if desired.
- *
- * #### Notes
- * It is assumed that the widget has the same model and context
- * as the original widget.
- */
- addSibling(widget: Widget): IDisposable;
- }
- /**
- * A type alias for a context.
- */
- export
- type Context = IContext<IModel>;
- /**
- * A type alias for a code context.
- */
- export
- type CodeContext = IContext<ICodeModel>;
- /**
- * The options used to initialize a widget factory.
- */
- export
- interface IWidgetFactoryOptions {
- /**
- * The file extensions the widget can view.
- *
- * #### Notes
- * Use "*" to denote all files. Specific file extensions must be preceded
- * with '.', like '.png', '.txt', etc. They may themselves contain a
- * period (e.g. .table.json).
- */
- readonly fileExtensions: string[];
- /**
- * The name of the widget to display in dialogs.
- */
- readonly name: string;
- /**
- * The file extensions for which the factory should be the default.
- *
- * #### Notes
- * Use "*" to denote all files. Specific file extensions must be preceded
- * with '.', like '.png', '.txt', etc. Entries in this attribute must also
- * be included in the fileExtensions attribute.
- * The default is an empty array.
- *
- * **See also:** [[fileExtensions]].
- */
- readonly defaultFor?: string[];
- /**
- * The registered name of the model type used to create the widgets.
- */
- readonly modelName?: string;
- /**
- * Whether the widgets prefer having a kernel started.
- */
- readonly preferKernel?: boolean;
- /**
- * Whether the widgets can start a kernel when opened.
- */
- readonly canStartKernel?: boolean;
- }
- /**
- * The interface for a widget factory.
- */
- export
- interface IWidgetFactory<T extends Widget, U extends IModel> extends IDisposable, IWidgetFactoryOptions {
- /**
- * A signal emitted when a widget is created.
- */
- widgetCreated: ISignal<IWidgetFactory<T, U>, T>;
- /**
- * Create a new widget given a context.
- *
- * #### Notes
- * It should emit the [widgetCreated] signal with the new widget.
- */
- createNew(context: IContext<U>): T;
- }
- /**
- * A type alias for a standard widget factory.
- */
- export
- type WidgetFactory = IWidgetFactory<Widget, IModel>;
- /**
- * An interface for a widget extension.
- */
- export
- interface IWidgetExtension<T extends Widget, U extends IModel> {
- /**
- * Create a new extension for a given widget.
- */
- createNew(widget: T, context: IContext<U>): IDisposable;
- }
- /**
- * A type alias for a standard widget extension.
- */
- export
- type WidgetExtension = IWidgetExtension<Widget, IModel>;
- /**
- * The interface for a model factory.
- */
- export
- interface IModelFactory<T extends IModel> extends IDisposable {
- /**
- * The name of the model.
- */
- readonly name: string;
- /**
- * The content type of the file (defaults to `"file"`).
- */
- readonly contentType: Contents.ContentType;
- /**
- * The format of the file (defaults to `"text"`).
- */
- readonly fileFormat: Contents.FileFormat;
- /**
- * Create a new model for a given path.
- *
- * @param languagePreference - An optional kernel language preference.
- *
- * @returns A new document model.
- */
- createNew(languagePreference?: string): T;
- /**
- * Get the preferred kernel language given an extension.
- */
- preferredLanguage(ext: string): string;
- }
- /**
- * A type alias for a standard model factory.
- */
- export
- type ModelFactory = IModelFactory<IModel>;
- /**
- * A type alias for a code model factory.
- */
- export
- type CodeModelFactory = IModelFactory<ICodeModel>;
- /**
- * A kernel preference for a given file path and widget.
- */
- export
- interface IKernelPreference {
- /**
- * The preferred kernel language.
- */
- readonly language: string;
- /**
- * Whether to prefer having a kernel started when opening.
- */
- readonly preferKernel: boolean;
- /**
- * Whether a kernel when can be started when opening.
- */
- readonly canStartKernel: boolean;
- }
- /**
- * An interface for a file type.
- */
- export
- interface IFileType {
- /**
- * The name of the file type.
- */
- readonly name: string;
- /**
- * The extension of the file type (e.g. `".txt"`). Can be a compound
- * extension (e.g. `".table.json:`).
- */
- readonly extension: string;
- /**
- * The optional mimetype of the file type.
- */
- readonly mimetype?: string;
- /**
- * The optional icon class to use for the file type.
- */
- readonly icon?: string;
- /**
- * The content type of the new file (defaults to `"file"`).
- */
- readonly contentType?: Contents.ContentType;
- /**
- * The format of the new file (default to `"text"`).
- */
- readonly fileFormat?: Contents.FileFormat;
- }
- /**
- * An interface for a "Create New" item.
- */
- export
- interface IFileCreator {
- /**
- * The name of the file creator.
- */
- readonly name: string;
- /**
- * The filetype name associated with the creator.
- */
- readonly fileType: string;
- /**
- * The optional widget name.
- */
- readonly widgetName?: string;
- /**
- * The optional kernel name.
- */
- readonly kernelName?: string;
- }
- /**
- * An arguments object for the `changed` signal.
- */
- export
- interface IChangedArgs {
- /**
- * The type of the changed item.
- */
- readonly type: 'widgetFactory' | 'modelFactory' | 'widgetExtension' | 'fileCreator' | 'fileType';
- /**
- * The name of the item.
- */
- readonly name: string;
- /**
- * Whether the item was added or removed.
- */
- readonly change: 'added' | 'removed';
- }
- /**
- * Get the extension name of a path.
- *
- * @param file - string.
- *
- * #### Notes
- * Dotted filenames (e.g. `".table.json"` are allowed.
- */
- export
- function extname(path: string): string {
- let parts = ContentsManager.basename(path).split('.');
- parts.shift();
- return '.' + parts.join('.');
- }
- }
- // Define the signals for the `DocumentRegistry` class.
- defineSignal(DocumentRegistry.prototype, 'changed');
- /**
- * A private namespace for DocumentRegistry data.
- */
- namespace Private {
- /**
- * Normalize a file extension to be of the type `'.foo'`.
- *
- * Adds a leading dot if not present and converts to lower case.
- */
- export
- function normalizeExtension(extension: string): string {
- if (extension === '*') {
- return extension;
- }
- if (extension === '.*') {
- return '*';
- }
- if (extension.indexOf('.') !== 0) {
- extension = `.${extension}`;
- }
- return extension.toLowerCase();
- }
- }
|