-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathyWidget.ts
63 lines (58 loc) · 1.64 KB
/
yWidget.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import {
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import {
JupyterYModel,
IJupyterYModel,
IJupyterYWidgetManager,
IJupyterYWidget
} from 'yjs-widgets';
import * as Y from 'yjs';
import { IGlueSessionTracker } from './token';
export interface ICommMetadata {
create_ydoc: boolean;
path: string;
ymodel_name: string;
}
class YGlueSessionWidget implements IJupyterYWidget {
constructor(yModel: IJupyterYModel, node: HTMLElement, ydocFactory: any) {
this.yModel = yModel;
this.node = node;
yModel.sharedModel.ydoc = ydocFactory(yModel.sharedModel.commMetadata.path);
}
yModel: IJupyterYModel;
node: HTMLElement;
}
export const yGlueSessionWidgetPlugin: JupyterFrontEndPlugin<void> = {
id: 'glue-jupyterlab:yjswidget-plugin',
autoStart: true,
requires: [IJupyterYWidgetManager, IGlueSessionTracker],
activate: (
app: JupyterFrontEnd,
yWidgetManager: IJupyterYWidgetManager,
tracker: IGlueSessionTracker
): void => {
class YGlueSessionModel extends JupyterYModel {
ydocFactory(commMetadata: ICommMetadata): Y.Doc {
const path = commMetadata.path;
const sessionWidget = tracker.find(obj => {
const filePath = obj.context.path.split(':')[1];
return filePath === path;
});
let requestedYDoc: Y.Doc;
if (sessionWidget) {
requestedYDoc = sessionWidget.context.model.sharedModel.ydoc;
} else {
requestedYDoc = new Y.Doc();
}
return requestedYDoc;
}
}
yWidgetManager.registerWidget(
'GlueSession',
YGlueSessionModel,
YGlueSessionWidget
);
}
};