Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a plugin to be able to swap the doc provider #10256

Merged
merged 17 commits into from May 20, 2021
1 change: 1 addition & 0 deletions buildutils/src/ensure-repo.ts
Expand Up @@ -158,6 +158,7 @@ const SKIP_CSS: Dict<string[]> = {
'@jupyterlab/debugger',
'@jupyterlab/debugger-extension',
'@jupyterlab/docmanager-extension',
'@jupyterlab/docprovider-extension',
'@jupyterlab/documentsearch-extension',
'@jupyterlab/extensionmanager',
'@jupyterlab/extensionmanager-extension',
Expand Down
4 changes: 4 additions & 0 deletions dev_mode/package.json
Expand Up @@ -38,6 +38,7 @@
"@jupyterlab/docmanager": "~3.1.0-alpha.8",
"@jupyterlab/docmanager-extension": "~3.1.0-alpha.8",
"@jupyterlab/docprovider": "~3.1.0-alpha.8",
"@jupyterlab/docprovider-extension": "~3.1.0-alpha.8",
"@jupyterlab/docregistry": "~3.1.0-alpha.8",
"@jupyterlab/documentsearch": "~3.1.0-alpha.8",
"@jupyterlab/documentsearch-extension": "~3.1.0-alpha.8",
Expand Down Expand Up @@ -132,6 +133,7 @@
"@jupyterlab/csvviewer-extension": "~3.1.0-alpha.8",
"@jupyterlab/debugger-extension": "~3.1.0-alpha.8",
"@jupyterlab/docmanager-extension": "~3.1.0-alpha.8",
"@jupyterlab/docprovider-extension": "~3.1.0-alpha.8",
"@jupyterlab/documentsearch-extension": "~3.1.0-alpha.8",
"@jupyterlab/extensionmanager-extension": "~3.1.0-alpha.8",
"@jupyterlab/filebrowser-extension": "~3.1.0-alpha.8",
Expand Down Expand Up @@ -211,6 +213,7 @@
"@jupyterlab/csvviewer-extension": "",
"@jupyterlab/debugger-extension": "",
"@jupyterlab/docmanager-extension": "",
"@jupyterlab/docprovider-extension": "",
"@jupyterlab/documentsearch-extension": "",
"@jupyterlab/extensionmanager-extension": "",
"@jupyterlab/filebrowser-extension": "",
Expand Down Expand Up @@ -322,6 +325,7 @@
"@jupyterlab/docmanager": "../packages/docmanager",
"@jupyterlab/docmanager-extension": "../packages/docmanager-extension",
"@jupyterlab/docprovider": "../packages/docprovider",
"@jupyterlab/docprovider-extension": "../packages/docprovider-extension",
"@jupyterlab/docregistry": "../packages/docregistry",
"@jupyterlab/documentsearch": "../packages/documentsearch",
"@jupyterlab/documentsearch-extension": "../packages/documentsearch-extension",
Expand Down
1 change: 1 addition & 0 deletions dev_mode/style.js
Expand Up @@ -10,6 +10,7 @@ import '@jupyterlab/console-extension/style/index.js';
import '@jupyterlab/csvviewer-extension/style/index.js';
import '@jupyterlab/debugger-extension/style/index.js';
import '@jupyterlab/docmanager-extension/style/index.js';
import '@jupyterlab/docprovider-extension/style/index.js';
import '@jupyterlab/documentsearch-extension/style/index.js';
import '@jupyterlab/extensionmanager-extension/style/index.js';
import '@jupyterlab/filebrowser-extension/style/index.js';
Expand Down
1 change: 1 addition & 0 deletions packages/docmanager-extension/package.json
Expand Up @@ -42,6 +42,7 @@
"@jupyterlab/apputils": "^3.1.0-alpha.8",
"@jupyterlab/coreutils": "^5.1.0-alpha.8",
"@jupyterlab/docmanager": "^3.1.0-alpha.8",
"@jupyterlab/docprovider": "^3.1.0-alpha.8",
"@jupyterlab/docregistry": "^3.1.0-alpha.8",
"@jupyterlab/mainmenu": "^3.1.0-alpha.8",
"@jupyterlab/services": "^6.1.0-alpha.8",
Expand Down
30 changes: 20 additions & 10 deletions packages/docmanager-extension/src/index.ts
Expand Up @@ -30,6 +30,8 @@ import {
SavingStatus
} from '@jupyterlab/docmanager';

import { IDocumentProviderFactory } from '@jupyterlab/docprovider';

import { DocumentRegistry } from '@jupyterlab/docregistry';

import { IMainMenu } from '@jupyterlab/mainmenu';
Expand Down Expand Up @@ -85,21 +87,25 @@ namespace CommandIDs {
export const showInFileBrowser = 'docmanager:show-in-file-browser';
}

const pluginId = '@jupyterlab/docmanager-extension:plugin';
/**
* The id of the document manager plugin.
*/
const docManagerPluginId = '@jupyterlab/docmanager-extension:plugin';

/**
* The default document manager provider.
*/
const docManagerPlugin: JupyterFrontEndPlugin<IDocumentManager> = {
id: pluginId,
id: docManagerPluginId,
provides: IDocumentManager,
requires: [ISettingRegistry, ITranslator],
optional: [
ILabStatus,
ICommandPalette,
ILabShell,
IMainMenu,
ISessionContextDialogs
ISessionContextDialogs,
IDocumentProviderFactory
],
activate: (
app: JupyterFrontEnd,
Expand All @@ -109,7 +115,8 @@ const docManagerPlugin: JupyterFrontEndPlugin<IDocumentManager> = {
palette: ICommandPalette | null,
labShell: ILabShell | null,
mainMenu: IMainMenu | null,
sessionDialogs: ISessionContextDialogs | null
sessionDialogs: ISessionContextDialogs | null,
docProviderFactory: IDocumentProviderFactory | null
): IDocumentManager => {
const trans = translator.load('jupyterlab');
const manager = app.serviceManager;
Expand Down Expand Up @@ -148,7 +155,8 @@ const docManagerPlugin: JupyterFrontEndPlugin<IDocumentManager> = {
setBusy: (status && (() => status.setBusy())) ?? undefined,
sessionDialogs: sessionDialogs || undefined,
translator,
collaborative: true
collaborative: true,
docProviderFactory: docProviderFactory ?? undefined
});

// Register the file operations commands.
Expand Down Expand Up @@ -209,7 +217,7 @@ const docManagerPlugin: JupyterFrontEndPlugin<IDocumentManager> = {
};

// Fetch the initial state of the settings.
Promise.all([settingRegistry.load(pluginId), app.restored])
Promise.all([settingRegistry.load(docManagerPluginId), app.restored])
.then(([settings]) => {
settings.changed.connect(onSettingsUpdated);
onSettingsUpdated(settings);
Expand All @@ -222,7 +230,7 @@ const docManagerPlugin: JupyterFrontEndPlugin<IDocumentManager> = {
// allowing us to dynamically populate a help string with the
// available document viewers and file types for the default
// viewer overrides.
settingRegistry.transform(pluginId, {
settingRegistry.transform(docManagerPluginId, {
fetch: plugin => {
// Get the available file types.
const fileTypes = toArray(registry.fileTypes())
Expand Down Expand Up @@ -260,7 +268,7 @@ Available file types:

// If the document registry gains or loses a factory or file type,
// regenerate the settings description with the available options.
registry.changed.connect(() => settingRegistry.reload(pluginId));
registry.changed.connect(() => settingRegistry.reload(docManagerPluginId));

return docManager;
}
Expand Down Expand Up @@ -729,9 +737,11 @@ function addCommands(
const value = !docManager.autosave;
const key = 'autosave';
return settingRegistry
.set(pluginId, key, value)
.set(docManagerPluginId, key, value)
.catch((reason: Error) => {
console.error(`Failed to set ${pluginId}:${key} - ${reason.message}`);
console.error(
`Failed to set ${docManagerPluginId}:${key} - ${reason.message}`
);
});
}
});
Expand Down
3 changes: 3 additions & 0 deletions packages/docmanager-extension/tsconfig.json
Expand Up @@ -18,6 +18,9 @@
{
"path": "../docmanager"
},
{
"path": "../docprovider"
},
{
"path": "../docregistry"
},
Expand Down
1 change: 1 addition & 0 deletions packages/docmanager/package.json
Expand Up @@ -44,6 +44,7 @@
"dependencies": {
"@jupyterlab/apputils": "^3.1.0-alpha.8",
"@jupyterlab/coreutils": "^5.1.0-alpha.8",
"@jupyterlab/docprovider": "^3.1.0-alpha.8",
"@jupyterlab/docregistry": "^3.1.0-alpha.8",
"@jupyterlab/services": "^6.1.0-alpha.8",
"@jupyterlab/statusbar": "^3.1.0-alpha.8",
Expand Down
17 changes: 14 additions & 3 deletions packages/docmanager/src/manager.ts
Expand Up @@ -5,20 +5,22 @@ import { ISessionContext, sessionContextDialogs } from '@jupyterlab/apputils';

import { PathExt } from '@jupyterlab/coreutils';

import { UUID } from '@lumino/coreutils';

import {
DocumentRegistry,
Context,
IDocumentWidget
} from '@jupyterlab/docregistry';

import { IDocumentProviderFactory } from '@jupyterlab/docprovider';

import { Contents, Kernel, ServiceManager } from '@jupyterlab/services';

import { nullTranslator, ITranslator } from '@jupyterlab/translation';

import { ArrayExt, find } from '@lumino/algorithm';

import { UUID } from '@lumino/coreutils';

import { IDisposable } from '@lumino/disposable';

import { AttachedProperty } from '@lumino/properties';
Expand Down Expand Up @@ -53,6 +55,7 @@ export class DocumentManager implements IDocumentManager {
this.services = options.manager;
this._collaborative = !!options.collaborative;
this._dialogs = options.sessionDialogs || sessionContextDialogs;
this._docProviderFactory = options.docProviderFactory;

this._opener = options.opener;
this._when = options.when || options.manager.ready;
Expand Down Expand Up @@ -483,7 +486,8 @@ export class DocumentManager implements IDocumentManager {
modelDBFactory,
setBusy: this._setBusy,
sessionDialogs: this._dialogs,
collaborative: this._collaborative
collaborative: this._collaborative,
docProviderFactory: this._docProviderFactory
});
const handler = new SaveHandler({
context,
Expand Down Expand Up @@ -610,6 +614,7 @@ export class DocumentManager implements IDocumentManager {
private _when: Promise<void>;
private _setBusy: (() => IDisposable) | undefined;
private _dialogs: ISessionContext.IDialogs;
private _docProviderFactory: IDocumentProviderFactory | undefined;
private _collaborative: boolean;
}

Expand Down Expand Up @@ -655,6 +660,12 @@ export namespace DocumentManager {
* The applicaton language translator.
*/
translator?: ITranslator;

/**
* A factory method for the document provider.
*/
docProviderFactory?: IDocumentProviderFactory;

/**
* Whether the context should be collaborative.
* If true, the context will connect through yjs_ws_server to share information if possible.
Expand Down
3 changes: 3 additions & 0 deletions packages/docmanager/tsconfig.json
Expand Up @@ -12,6 +12,9 @@
{
"path": "../coreutils"
},
{
"path": "../docprovider"
},
{
"path": "../docregistry"
},
Expand Down
6 changes: 6 additions & 0 deletions packages/docmanager/tsconfig.test.json
Expand Up @@ -8,6 +8,9 @@
{
"path": "../coreutils"
},
{
"path": "../docprovider"
},
{
"path": "../docregistry"
},
Expand All @@ -32,6 +35,9 @@
{
"path": "../coreutils"
},
{
"path": "../docprovider"
},
{
"path": "../docregistry"
},
Expand Down
54 changes: 54 additions & 0 deletions packages/docprovider-extension/package.json
@@ -0,0 +1,54 @@
{
"name": "@jupyterlab/docprovider-extension",
"version": "3.1.0-alpha.8",
"description": "JupyterLab - Document Provider Extension",
"homepage": "https://github.com/jupyterlab/jupyterlab",
"bugs": {
"url": "https://github.com/jupyterlab/jupyterlab/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/jupyterlab/jupyterlab.git"
},
"license": "BSD-3-Clause",
"author": "Project Jupyter",
"sideEffects": [
"style/*.css",
"style/index.js"
],
"main": "lib/index.js",
"types": "lib/index.d.ts",
"style": "style/index.css",
"directories": {
"lib": "lib/"
},
"files": [
"lib/**/*.{d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,woff2,ttf}",
"schema/*.json",
"style/**/*.{css,eot,gif,html,jpg,json,png,svg,woff2,ttf}",
"style/index.js"
],
"scripts": {
"build": "tsc -b",
"clean": "rimraf lib && rimraf tsconfig.tsbuildinfo",
"prepublishOnly": "npm run build",
"watch": "tsc -w --listEmittedFiles"
},
"dependencies": {
"@jupyterlab/application": "^3.1.0-alpha.8",
"@jupyterlab/coreutils": "^5.1.0-alpha.8",
"@jupyterlab/docprovider": "^3.1.0-alpha.8",
"@jupyterlab/services": "^6.1.0-alpha.8"
},
"devDependencies": {
"rimraf": "~3.0.0",
"typescript": "~4.1.3"
},
"publishConfig": {
"access": "public"
},
"jupyterlab": {
"extension": true
},
"styleModule": "style/index.js"
}
53 changes: 53 additions & 0 deletions packages/docprovider-extension/src/index.ts
@@ -0,0 +1,53 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
/**
* @packageDocumentation
* @module docprovider-extension
*/

import {
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';

import { PageConfig, URLExt } from '@jupyterlab/coreutils';

import {
IDocumentProvider,
IDocumentProviderFactory,
ProviderMock,
WebSocketProviderWithLocks
} from '@jupyterlab/docprovider';

import { ServerConnection } from '@jupyterlab/services';

/**
* The default document provider plugin
*/
const docProviderPlugin: JupyterFrontEndPlugin<IDocumentProviderFactory> = {
id: '@jupyterlab/docprovider-extension:plugin',
provides: IDocumentProviderFactory,
activate: (app: JupyterFrontEnd): IDocumentProviderFactory => {
const server = ServerConnection.makeSettings();
const url = URLExt.join(server.wsUrl, 'api/yjs');
const collaborative =
PageConfig.getOption('collaborative') == 'true' ? true : false;
const factory = (
options: IDocumentProviderFactory.IOptions
): IDocumentProvider => {
return collaborative
? new WebSocketProviderWithLocks({
...options,
url
})
: new ProviderMock();
};
return factory;
}
};

/**
* Export the plugins as default.
*/
const plugins: JupyterFrontEndPlugin<any>[] = [docProviderPlugin];
export default plugins;
7 changes: 7 additions & 0 deletions packages/docprovider-extension/style/index.css
@@ -0,0 +1,7 @@
/*-----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/

/* This file was auto-generated by ensurePackage() in @jupyterlab/buildutils */
@import url('~@jupyterlab/application/style/index.css');
7 changes: 7 additions & 0 deletions packages/docprovider-extension/style/index.js
@@ -0,0 +1,7 @@
/*-----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/

/* This file was auto-generated by ensurePackage() in @jupyterlab/buildutils */
import '@jupyterlab/application/style/index.js';