@jupyter/docprovider 3.0.1 → 3.1.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,41 @@
1
+ import { ICollaborativeDrive } from '@jupyter/collaborative-drive';
2
+ import { Event } from '@jupyterlab/services';
3
+ import { ISignal } from '@lumino/signaling';
4
+ import { IAllForksResponse, IForkChangedEvent, IForkCreationResponse, IForkManager } from './tokens';
5
+ import { IForkProvider } from './ydrive';
6
+ export declare const JUPYTER_COLLABORATION_FORK_EVENTS_URI = "https://schema.jupyter.org/jupyter_collaboration/fork/v1";
7
+ export declare class ForkManager implements IForkManager {
8
+ constructor(options: ForkManager.IOptions);
9
+ get isDisposed(): boolean;
10
+ get forkAdded(): ISignal<ForkManager, IForkChangedEvent>;
11
+ get forkDeleted(): ISignal<ForkManager, IForkChangedEvent>;
12
+ dispose(): void;
13
+ createFork(options: {
14
+ rootId: string;
15
+ synchronize: boolean;
16
+ title?: string;
17
+ description?: string;
18
+ }): Promise<IForkCreationResponse | undefined>;
19
+ getAllForks(rootId: string): Promise<IAllForksResponse>;
20
+ deleteFork(options: {
21
+ forkId: string;
22
+ merge: boolean;
23
+ }): Promise<void>;
24
+ getProvider(options: {
25
+ documentPath: string;
26
+ format: string;
27
+ type: string;
28
+ }): IForkProvider | undefined;
29
+ private _handleEvent;
30
+ private _disposed;
31
+ private _drive;
32
+ private _eventManager;
33
+ private _forkAddedSignal;
34
+ private _forkDeletedSignal;
35
+ }
36
+ export declare namespace ForkManager {
37
+ interface IOptions {
38
+ drive: ICollaborativeDrive;
39
+ eventManager: Event.IManager;
40
+ }
41
+ }
@@ -0,0 +1,89 @@
1
+ /*
2
+ * Copyright (c) Jupyter Development Team.
3
+ * Distributed under the terms of the Modified BSD License.
4
+ */
5
+ import { URLExt } from '@jupyterlab/coreutils';
6
+ import { Signal } from '@lumino/signaling';
7
+ import { requestAPI, ROOM_FORK_URL } from './requests';
8
+ export const JUPYTER_COLLABORATION_FORK_EVENTS_URI = 'https://schema.jupyter.org/jupyter_collaboration/fork/v1';
9
+ export class ForkManager {
10
+ constructor(options) {
11
+ this._disposed = false;
12
+ this._forkAddedSignal = new Signal(this);
13
+ this._forkDeletedSignal = new Signal(this);
14
+ const { drive, eventManager } = options;
15
+ this._drive = drive;
16
+ this._eventManager = eventManager;
17
+ this._eventManager.stream.connect(this._handleEvent, this);
18
+ }
19
+ get isDisposed() {
20
+ return this._disposed;
21
+ }
22
+ get forkAdded() {
23
+ return this._forkAddedSignal;
24
+ }
25
+ get forkDeleted() {
26
+ return this._forkDeletedSignal;
27
+ }
28
+ dispose() {
29
+ var _a;
30
+ if (this._disposed) {
31
+ return;
32
+ }
33
+ (_a = this._eventManager) === null || _a === void 0 ? void 0 : _a.stream.disconnect(this._handleEvent);
34
+ this._disposed = true;
35
+ }
36
+ async createFork(options) {
37
+ const { rootId, title, description, synchronize } = options;
38
+ const init = {
39
+ method: 'PUT',
40
+ body: JSON.stringify({ title, description, synchronize })
41
+ };
42
+ const url = URLExt.join(ROOM_FORK_URL, rootId);
43
+ const response = await requestAPI(url, init);
44
+ return response;
45
+ }
46
+ async getAllForks(rootId) {
47
+ const url = URLExt.join(ROOM_FORK_URL, rootId);
48
+ const init = { method: 'GET' };
49
+ const response = await requestAPI(url, init);
50
+ return response;
51
+ }
52
+ async deleteFork(options) {
53
+ const { forkId, merge } = options;
54
+ const url = URLExt.join(ROOM_FORK_URL, forkId);
55
+ const query = URLExt.objectToQueryString({ merge });
56
+ const init = { method: 'DELETE' };
57
+ await requestAPI(`${url}${query}`, init);
58
+ }
59
+ getProvider(options) {
60
+ const { documentPath, format, type } = options;
61
+ const drive = this._drive;
62
+ if (drive) {
63
+ const driveName = drive.name;
64
+ let docPath = documentPath;
65
+ if (documentPath.startsWith(driveName)) {
66
+ docPath = documentPath.slice(driveName.length + 1);
67
+ }
68
+ const provider = drive.providers.get(`${format}:${type}:${docPath}`);
69
+ return provider;
70
+ }
71
+ return;
72
+ }
73
+ _handleEvent(_, emission) {
74
+ if (emission.schema_id === JUPYTER_COLLABORATION_FORK_EVENTS_URI) {
75
+ switch (emission.action) {
76
+ case 'create': {
77
+ this._forkAddedSignal.emit(emission);
78
+ break;
79
+ }
80
+ case 'delete': {
81
+ this._forkDeletedSignal.emit(emission);
82
+ break;
83
+ }
84
+ default:
85
+ break;
86
+ }
87
+ }
88
+ }
89
+ }
package/lib/index.d.ts CHANGED
@@ -8,3 +8,5 @@ export * from './requests';
8
8
  export * from './ydrive';
9
9
  export * from './yprovider';
10
10
  export * from './TimelineSlider';
11
+ export * from './tokens';
12
+ export * from './forkManager';
package/lib/index.js CHANGED
@@ -12,3 +12,5 @@ export * from './requests';
12
12
  export * from './ydrive';
13
13
  export * from './yprovider';
14
14
  export * from './TimelineSlider';
15
+ export * from './tokens';
16
+ export * from './forkManager';
package/lib/requests.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Contents } from '@jupyterlab/services';
2
+ export declare const ROOM_FORK_URL = "api/collaboration/fork";
2
3
  /**
3
4
  * Document session model
4
5
  */
@@ -20,6 +21,14 @@ export interface ISessionModel {
20
21
  */
21
22
  sessionId: string;
22
23
  }
24
+ /**
25
+ * Call the API extension
26
+ *
27
+ * @param endPoint API REST end point for the extension
28
+ * @param init Initial values for the request
29
+ * @returns The response body interpreted as JSON
30
+ */
31
+ export declare function requestAPI<T = any>(endPoint?: string, init?: RequestInit): Promise<T>;
23
32
  export declare function requestDocSession(format: string, type: string, path: string): Promise<ISessionModel>;
24
33
  export declare function requestDocumentTimeline(format: string, type: string, path: string): Promise<any>;
25
34
  export declare function requestUndoRedo(roomid: string, action: 'undo' | 'redo' | 'restore', steps: number, forkRoom: string): Promise<any>;
package/lib/requests.js CHANGED
@@ -11,6 +11,39 @@ import { ServerConnection } from '@jupyterlab/services';
11
11
  const DOC_SESSION_URL = 'api/collaboration/session';
12
12
  const DOC_FORK_URL = 'api/collaboration/undo_redo';
13
13
  const TIMELINE_URL = 'api/collaboration/timeline';
14
+ export const ROOM_FORK_URL = 'api/collaboration/fork';
15
+ /**
16
+ * Call the API extension
17
+ *
18
+ * @param endPoint API REST end point for the extension
19
+ * @param init Initial values for the request
20
+ * @returns The response body interpreted as JSON
21
+ */
22
+ export async function requestAPI(endPoint = '', init = {}) {
23
+ // Make request to Jupyter API
24
+ const settings = ServerConnection.makeSettings();
25
+ const requestUrl = URLExt.join(settings.baseUrl, endPoint);
26
+ let response;
27
+ try {
28
+ response = await ServerConnection.makeRequest(requestUrl, init, settings);
29
+ }
30
+ catch (error) {
31
+ throw new ServerConnection.NetworkError(error);
32
+ }
33
+ let data = await response.text();
34
+ if (data.length > 0) {
35
+ try {
36
+ data = JSON.parse(data);
37
+ }
38
+ catch (error) {
39
+ console.error('Not a JSON response body.', response);
40
+ }
41
+ }
42
+ if (!response.ok) {
43
+ throw new ServerConnection.ResponseError(response, data.message || data);
44
+ }
45
+ return data;
46
+ }
14
47
  export async function requestDocSession(format, type, path) {
15
48
  const settings = ServerConnection.makeSettings();
16
49
  const url = URLExt.join(settings.baseUrl, DOC_SESSION_URL, encodeURIComponent(path));
@@ -0,0 +1,103 @@
1
+ import { Token } from '@lumino/coreutils';
2
+ import { IDisposable } from '@lumino/disposable';
3
+ import { ISignal } from '@lumino/signaling';
4
+ import { IForkProvider } from './ydrive';
5
+ export interface IForkInfo {
6
+ description?: string;
7
+ root_roomid: string;
8
+ synchronize: boolean;
9
+ title?: string;
10
+ }
11
+ export interface IForkCreationResponse {
12
+ fork_info: IForkInfo;
13
+ fork_roomid: string;
14
+ sessionId: string;
15
+ }
16
+ export interface IAllForksResponse {
17
+ [forkId: string]: IForkInfo;
18
+ }
19
+ export interface IForkChangedEvent {
20
+ fork_info: IForkInfo;
21
+ fork_roomid: string;
22
+ username?: string;
23
+ }
24
+ /**
25
+ * Interface representing a Fork Manager that manages forked documents and
26
+ * provides signals for fork-related events.
27
+ *
28
+ * @interface IForkManager
29
+ * @extends IDisposable
30
+ */
31
+ export interface IForkManager extends IDisposable {
32
+ /**
33
+ * Get the fork provider of a given document.
34
+ *
35
+ * @param options.documentPath - The document path including the
36
+ * drive prefix.
37
+ * @param options.format - Format of the document.
38
+ * @param options.type - Content type of the document.
39
+ * @returns The fork provider of the document.
40
+ */
41
+ getProvider(options: {
42
+ documentPath: string;
43
+ format: string;
44
+ type: string;
45
+ }): IForkProvider | undefined;
46
+ /**
47
+ * Creates a new fork for a given document.
48
+ *
49
+ * @param options.rootId - The ID of the root document to fork.
50
+ * @param options.synchronize - A flag indicating whether the fork should be kept
51
+ * synchronized with the root document.
52
+ * @param options.title - An optional label for the fork.
53
+ * @param options.description - An optional description for the fork.
54
+ *
55
+ * @returns A promise that resolves to an `IForkCreationResponse` if the fork
56
+ * is created successfully, or `undefined` if the creation fails.
57
+ */
58
+ createFork(options: {
59
+ rootId: string;
60
+ synchronize: boolean;
61
+ title?: string;
62
+ description?: string;
63
+ }): Promise<IForkCreationResponse | undefined>;
64
+ /**
65
+ * Retrieves all forks associated with a specific document.
66
+ *
67
+ * @param documentId - The ID of the document for which forks are to be retrieved.
68
+ *
69
+ * @returns A promise that resolves to an `IAllForksResponse` containing information about all forks.
70
+ */
71
+ getAllForks(documentId: string): Promise<IAllForksResponse>;
72
+ /**
73
+ * Deletes a specified fork and optionally merges its changes.
74
+ *
75
+ * @param options - Options for deleting the fork.
76
+ * @param options.forkId - The ID of the fork to be deleted.
77
+ * @param options.merge - A flag indicating whether changes from the fork should be merged back into the root document.
78
+ *
79
+ * @returns A promise that resolves when the fork is successfully deleted.
80
+ */
81
+ deleteFork(options: {
82
+ forkId: string;
83
+ merge: boolean;
84
+ }): Promise<void>;
85
+ /**
86
+ * Signal emitted when a new fork is added.
87
+ *
88
+ * @event forkAdded
89
+ * @type ISignal<IForkManager, IForkChangedEvent>
90
+ */
91
+ forkAdded: ISignal<IForkManager, IForkChangedEvent>;
92
+ /**
93
+ * Signal emitted when a fork is deleted.
94
+ *
95
+ * @event forkDeleted
96
+ * @type ISignal<IForkManager, IForkChangedEvent>
97
+ */
98
+ forkDeleted: ISignal<IForkManager, IForkChangedEvent>;
99
+ }
100
+ /**
101
+ * Token providing a fork manager instance.
102
+ */
103
+ export declare const IForkManagerToken: Token<IForkManager>;
package/lib/tokens.js ADDED
@@ -0,0 +1,9 @@
1
+ /*
2
+ * Copyright (c) Jupyter Development Team.
3
+ * Distributed under the terms of the Modified BSD License.
4
+ */
5
+ import { Token } from '@lumino/coreutils';
6
+ /**
7
+ * Token providing a fork manager instance.
8
+ */
9
+ export const IForkManagerToken = new Token('@jupyter/docprovider:IForkManagerToken');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jupyter/docprovider",
3
- "version": "3.0.1",
3
+ "version": "3.1.0-alpha.0",
4
4
  "description": "JupyterLab - Document Provider",
5
5
  "homepage": "https://github.com/jupyterlab/jupyter-collaboration",
6
6
  "bugs": {
@@ -41,7 +41,7 @@
41
41
  "watch": "tsc -b --watch"
42
42
  },
43
43
  "dependencies": {
44
- "@jupyter/collaborative-drive": "^3.0.1",
44
+ "@jupyter/collaborative-drive": "^3.1.0-alpha.0",
45
45
  "@jupyter/ydoc": "^2.0.0 || ^3.0.0",
46
46
  "@jupyterlab/apputils": "^4.2.0",
47
47
  "@jupyterlab/cells": "^4.2.0",