Skip to content

Commit 1474ba8

Browse files
committed
Bump yjs-widgets and make changes wrt
1 parent c52664f commit 1474ba8

File tree

6 files changed

+78
-46
lines changed

6 files changed

+78
-46
lines changed

python/jupytercad_lab/jupytercad_lab/notebook/cad_document.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
ShapeMetadata,
2929
IAny,
3030
)
31-
from .utils import normalize_path
3231

3332
logger = logging.getLogger(__file__)
3433

@@ -72,7 +71,7 @@ def _path_to_comm(cls, filePath: Optional[str]) -> Dict:
7271
contentType = None
7372

7473
if filePath is not None:
75-
path = normalize_path(filePath)
74+
path = filePath
7675
file_name = Path(path).name
7776
try:
7877
ext = file_name.split(".")[1].lower()

python/jupytercad_lab/jupytercad_lab/notebook/utils.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
from enum import Enum
32
from urllib.parse import urljoin
43

@@ -15,10 +14,3 @@ def multi_urljoin(*parts) -> str:
1514
parts[0],
1615
"/".join(part for part in parts[1:]),
1716
)
18-
19-
20-
def normalize_path(path: str) -> str:
21-
if os.path.isabs(path):
22-
return path
23-
else:
24-
return os.path.abspath(os.path.join(os.getcwd(), path))

python/jupytercad_lab/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"@lumino/messaging": "^2.0.0",
6969
"@lumino/widgets": "^2.0.0",
7070
"react": "^18.0.1",
71-
"yjs-widgets": "^0.3.7"
71+
"yjs-widgets": "^0.4"
7272
},
7373
"devDependencies": {
7474
"@jupyterlab/builder": "^4.0.0",

python/jupytercad_lab/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ classifiers = [
2525
dependencies = [
2626
"pycrdt",
2727
"ypywidgets>=0.9.0,<0.10.0",
28-
"yjs-widgets>=0.3.7,<0.4",
28+
"yjs-widgets>=0.4,<0.5",
2929
"comm>=0.1.2,<0.2.0",
3030
"pydantic>=2,<3",
3131
"jupytercad_core>=3.0.0a0,<4",

python/jupytercad_lab/src/notebookrenderer.ts

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@ import {
1515
JupyterCadModel
1616
} from '@jupytercad/schema';
1717

18+
import { showErrorMessage } from '@jupyterlab/apputils';
1819
import {
1920
JupyterFrontEnd,
2021
JupyterFrontEndPlugin
2122
} from '@jupyterlab/application';
2223
import { Contents } from '@jupyterlab/services';
2324
import { MessageLoop } from '@lumino/messaging';
2425
import { Panel, Widget } from '@lumino/widgets';
25-
import * as Y from 'yjs';
2626
import {
2727
IJupyterYWidget,
2828
IJupyterYWidgetManager,
29-
JupyterYModel
29+
JupyterYModel,
30+
JupyterYDoc
3031
} from 'yjs-widgets';
3132
import { Toolbar } from '@jupyterlab/ui-components';
3233
import { ConsolePanel } from '@jupyterlab/console';
@@ -94,10 +95,6 @@ export class YJupyterCADLuminoWidget extends Panel {
9495
private _buildWidget = (options: IOptions) => {
9596
const { commands, workerRegistry, model, externalCommands, tracker } =
9697
options;
97-
// Ensure the model filePath is relevant with the shared model path.
98-
if (model.sharedModel.getState('path')) {
99-
model.filePath = model.sharedModel.getState('path') as string;
100-
}
10198
const content = new JupyterCadPanel({
10299
model: model,
103100
workerRegistry: workerRegistry as IJCadWorkerRegistry
@@ -153,17 +150,57 @@ export const notebookRenderePlugin: JupyterFrontEndPlugin<void> = {
153150
console.error('Missing IJupyterYWidgetManager token!');
154151
return;
155152
}
156-
if (!drive) {
157-
console.error('Missing ICollaborativeDrive token!');
158-
return;
159-
}
153+
160154
class YJupyterCADModelFactory extends YJupyterCADModel {
161-
ydocFactory(commMetadata: ICommMetadata): Y.Doc {
155+
protected async initialize(commMetadata: {
156+
[key: string]: any;
157+
}): Promise<void> {
162158
const { path, format, contentType } = commMetadata;
163159
const fileFormat = format as Contents.FileFormat;
164160

161+
if (!drive) {
162+
showErrorMessage(
163+
'Error using the JupyterCAD Python API',
164+
'You cannot use the JupyterCAD Python API without a collaborative drive. You need to install a package providing collaboration features (e.g. jupyter-collaboration).'
165+
);
166+
throw new Error(
167+
'Failed to create the YDoc without a collaborative drive'
168+
);
169+
}
170+
171+
// The path of the project is relative to the path of the notebook
172+
let currentWidgetPath = '';
173+
const currentWidget = app.shell.currentWidget;
174+
if (
175+
currentWidget instanceof NotebookPanel ||
176+
currentWidget instanceof ConsolePanel
177+
) {
178+
currentWidgetPath = currentWidget.sessionContext.path;
179+
}
180+
181+
let localPath = '';
182+
if (path) {
183+
localPath = PathExt.join(PathExt.dirname(currentWidgetPath), path);
184+
185+
// If the file does not exist yet, create it
186+
try {
187+
await app.serviceManager.contents.get(localPath);
188+
} catch (e) {
189+
await app.serviceManager.contents.save(localPath, {
190+
content: btoa('{}'),
191+
format: 'base64'
192+
});
193+
}
194+
} else {
195+
// If the user did not provide a path, do not create
196+
localPath = PathExt.join(
197+
PathExt.dirname(currentWidgetPath),
198+
'unsaved_project'
199+
);
200+
}
201+
165202
const sharedModel = drive!.sharedModelFactory.createNew({
166-
path,
203+
path: localPath,
167204
format: fileFormat,
168205
contentType,
169206
collaborative: true
@@ -174,28 +211,10 @@ export const notebookRenderePlugin: JupyterFrontEndPlugin<void> = {
174211
});
175212

176213
this.jupyterCADModel.contentsManager = app.serviceManager.contents;
214+
this.jupyterCADModel.filePath = localPath;
177215

178-
if (!sharedModel) {
179-
// The path of the project is set to the path of the notebook, to be able to
180-
// add local geoJSON/shape file in a "file-less" project.
181-
let currentWidgetPath: string | undefined = undefined;
182-
const currentWidget = app.shell.currentWidget;
183-
if (
184-
currentWidget instanceof NotebookPanel ||
185-
currentWidget instanceof ConsolePanel
186-
) {
187-
currentWidgetPath = currentWidget.sessionContext.path;
188-
}
189-
190-
if (currentWidgetPath) {
191-
this.jupyterCADModel.filePath = PathExt.join(
192-
PathExt.dirname(currentWidgetPath),
193-
'unsaved_project'
194-
);
195-
}
196-
}
197-
198-
return this.jupyterCADModel.sharedModel.ydoc;
216+
this.ydoc = this.jupyterCADModel.sharedModel.ydoc;
217+
this.sharedModel = new JupyterYDoc(commMetadata, this.ydoc);
199218
}
200219
}
201220

yarn.lock

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ __metadata:
937937
rimraf: ^3.0.2
938938
typescript: ^5
939939
yjs: ^13.5.0
940-
yjs-widgets: ^0.3.7
940+
yjs-widgets: ^0.4
941941
languageName: unknown
942942
linkType: soft
943943

@@ -13048,6 +13048,28 @@ __metadata:
1304813048
languageName: node
1304913049
linkType: hard
1305013050

13051+
"yjs-widgets@npm:^0.4":
13052+
version: 0.4.0
13053+
resolution: "yjs-widgets@npm:0.4.0"
13054+
dependencies:
13055+
"@jupyter/ydoc": ^2.0.0 || ^3.0.0-a3
13056+
"@jupyterlab/application": ^4.0.0
13057+
"@jupyterlab/apputils": ^4.0.0
13058+
"@jupyterlab/console": ^4.2.4
13059+
"@jupyterlab/coreutils": ^6.0.0
13060+
"@jupyterlab/notebook": ^4.0.0
13061+
"@jupyterlab/services": ^7.0.0
13062+
"@lumino/coreutils": ^2.1.1
13063+
"@lumino/signaling": ^2.1.1
13064+
"@lumino/widgets": ^2.1.1
13065+
lib0: ^0.2.76
13066+
uuid: ^9.0.0
13067+
webpack: ^5.77.0
13068+
webpack-cli: ^5.0.1
13069+
checksum: 147fc7ae1c3d13fd591d434af33d439bc20d23494ded601dd8da5986426c6c896d7ff2a6296e24bda8d5ee17921c5c390c3b095f14fd5195506c0f891747e2fd
13070+
languageName: node
13071+
linkType: hard
13072+
1305113073
"yjs@npm:^13.5.0, yjs@npm:^13.5.40":
1305213074
version: 13.6.21
1305313075
resolution: "yjs@npm:13.6.21"

0 commit comments

Comments
 (0)