Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions examples/Notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "050e5936-f9e1-44ff-8bc9-a8556d7643ee",
"metadata": {},
"outputs": [],
"source": [
"from jupytercad import CadDocument"
"from jupytercad_lab import CadDocument"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "9821d466-6ae7-418f-aea7-9d67153da7d6",
"metadata": {},
"outputs": [],
Expand All @@ -38,10 +38,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "e30499f0-6732-4611-a25e-46b27e606b08",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.ywidget-view+json": {
"model_id": "467aa8ee751f43ee8ee5e1b43254a69a",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"<jupytercad_lab.notebook.cad_document.CadDocument object at 0x110fc4380>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"doc.add_cone().add_sphere(radius=0.8).cut(color='#ff0000')"
]
Expand Down Expand Up @@ -254,7 +270,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
"version": "3.12.0"
}
},
"nbformat": 4,
Expand Down
40 changes: 17 additions & 23 deletions packages/base/src/annotation/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import {
IAnnotationModel,
IJupyterCadModel
} from '@jupytercad/schema';
import { DocumentRegistry } from '@jupyterlab/docregistry';
import { User } from '@jupyterlab/services';
import { ISignal, Signal } from '@lumino/signaling';

export class AnnotationModel implements IAnnotationModel {
constructor(options: AnnotationModel.IOptions) {
this.context = options.context;
this.model = options.model;
}

get updateSignal(): ISignal<this, null> {
Expand All @@ -21,39 +20,37 @@ export class AnnotationModel implements IAnnotationModel {
return this._user;
}

set context(
context: DocumentRegistry.IContext<IJupyterCadModel> | undefined
) {
this._context = context;
set model(model: IJupyterCadModel | undefined) {
this._model = model;

const state = this._context?.model.sharedModel.awareness.getLocalState();
const state = this._model?.sharedModel.awareness.getLocalState();
this._user = state?.user;

this._contextChanged.emit(void 0);
this._modelChanged.emit(void 0);
}

get context(): DocumentRegistry.IContext<IJupyterCadModel> | undefined {
return this._context;
get model(): IJupyterCadModel | undefined {
return this._model;
}

get contextChanged(): ISignal<this, void> {
return this._contextChanged;
get modelChanged(): ISignal<this, void> {
return this._modelChanged;
}

update(): void {
this._updateSignal.emit(null);
}

getAnnotation(id: string): IAnnotation | undefined {
const rawData = this._context?.model.sharedModel.getMetadata(id);
const rawData = this._model?.sharedModel.getMetadata(id);
if (rawData) {
return JSON.parse(rawData) as IAnnotation;
}
}

getAnnotationIds(): string[] {
const annotationIds: string[] = [];
for (const id in this._context?.model.sharedModel.metadata) {
for (const id in this._model?.sharedModel.metadata) {
if (id.startsWith('annotation')) {
annotationIds.push(id);
}
Expand All @@ -62,14 +59,14 @@ export class AnnotationModel implements IAnnotationModel {
}

addAnnotation(key: string, value: IAnnotation): void {
this._context?.model.sharedModel.setMetadata(
this._model?.sharedModel.setMetadata(
`annotation_${key}`,
JSON.stringify(value)
);
}

removeAnnotation(key: string): void {
this._context?.model.removeMetadata(key);
this._model?.removeMetadata(key);
}

addContent(id: string, value: string): void {
Expand All @@ -84,21 +81,18 @@ export class AnnotationModel implements IAnnotationModel {
contents: [...currentAnnotation.contents, newContent]
};

this._context?.model.sharedModel.setMetadata(
id,
JSON.stringify(newAnnotation)
);
this._model?.sharedModel.setMetadata(id, JSON.stringify(newAnnotation));
}
}

private _context: DocumentRegistry.IContext<IJupyterCadModel> | undefined;
private _contextChanged = new Signal<this, void>(this);
private _model: IJupyterCadModel | undefined;
private _modelChanged = new Signal<this, void>(this);
private _updateSignal = new Signal<this, null>(this);
private _user?: User.IIdentity;
}

namespace AnnotationModel {
export interface IOptions {
context: DocumentRegistry.IContext<IJupyterCadModel> | undefined;
model: IJupyterCadModel | undefined;
}
}
Loading
Loading