Skip to content

Commit

Permalink
Create schema registry
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc committed Dec 15, 2023
1 parent e2ab3ab commit 722fde8
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 119 deletions.
80 changes: 48 additions & 32 deletions examples/test.jcad
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,69 @@
"objects": [
{
"name": "box",
"visible": true,
"shape": "Part::Box",
"shapeMetadata": {
"mass": 1995,
"centerOfMass": [
3.5,
7.5,
9.499999999999998
],
"matrixOfInertia": [
[
0,
-2.9103830456733704e-11,
45552.5
],
[
97422.50000000006,
0,
0
],
[
0,
68162.50000000006,
-2.9103830456733704e-11
]
]
},
"parameters": {
"Height": 19.0,
"Placement": {
"Axis": [
1.0,
0.0,
0.0
1,
0,
0
],
"Position": [
0.0,
0.0,
0.0
0,
0,
0
],
"Angle": 0.0
"Angle": 0
},
"Width": 15.0,
"Length": 7.0
},
"visible": true
"Width": 15,
"Length": 7,
"Height": 19
}
},
{
"visible": true,
"name": "Mesh 1",
"shape": "Post::Operator",
"parameters": {
"Length": 10.0,
"Placement": {
"Position": [
0.0,
0.0,
0.0
],
"Axis": [
1.0,
0.0,
0.0
],
"Angle": 2.0
},
"Height": 9.0,
"Width": 25.0
"NumberOfSegment": 5,
"Object": "box"
},
"shape": "Part::Box",
"name": "box2"
"visible": true
}
],
"options": {
"foo": 1.0
"foo": 1,
"guidata": {
"box": {
"visibility": false
}
}
},
"metadata": {}
}
13 changes: 10 additions & 3 deletions packages/base/src/panelview/objectproperties.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
IDict,
IJCadFormSchemaRegistry,
IJCadModel,
IJcadObjectDocChange,
IJupyterCadClientState,
IJupyterCadDoc,
IJupyterCadModel
} from '@jupytercad/schema';
import formSchema from '@jupytercad/schema/lib/_interface/forms.json';
import { ReactWidget } from '@jupyterlab/apputils';
import { PanelWithToolbar } from '@jupyterlab/ui-components';
import { Panel } from '@lumino/widgets';
Expand All @@ -26,7 +26,10 @@ export class ObjectProperties extends PanelWithToolbar {
super(params);
this.title.label = 'Objects Properties';
const body = ReactWidget.create(
<ObjectPropertiesReact cpModel={params.controlPanelModel} />
<ObjectPropertiesReact
cpModel={params.controlPanelModel}
formSchemaRegistry={params.formSchemaRegistry}
/>
);
this.addWidget(body);
this.addClass('jpcad-sidebar-propertiespanel');
Expand All @@ -47,6 +50,7 @@ interface IStates {

interface IProps {
cpModel: IControlPanelModel;
formSchemaRegistry: IJCadFormSchemaRegistry;
}

class ObjectPropertiesReact extends React.Component<IProps, IStates> {
Expand All @@ -58,6 +62,7 @@ class ObjectPropertiesReact extends React.Component<IProps, IStates> {
clientId: null,
id: uuid()
};
this._formSchema = props.formSchemaRegistry.getSchemas();
this.props.cpModel.jcadModel?.sharedObjectsChanged.connect(
this._sharedJcadModelChanged
);
Expand Down Expand Up @@ -219,7 +224,7 @@ class ObjectPropertiesReact extends React.Component<IProps, IStates> {
}

if (selectedObj.shape) {
schema = formSchema[selectedObj.shape];
schema = this._formSchema.get(selectedObj.shape);
}
const selectedObjectData = selectedObj['parameters'];
this.setState(old => ({
Expand Down Expand Up @@ -251,6 +256,7 @@ class ObjectPropertiesReact extends React.Component<IProps, IStates> {
}

private _lastSelectedPropFieldId?: string;
private _formSchema: Map<string, IDict>;
}

export namespace ObjectProperties {
Expand All @@ -259,5 +265,6 @@ export namespace ObjectProperties {
*/
export interface IOptions extends Panel.IOptions {
controlPanelModel: IControlPanelModel;
formSchemaRegistry: IJCadFormSchemaRegistry;
}
}
6 changes: 4 additions & 2 deletions packages/base/src/panelview/rightpanel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JupyterCadDoc } from '@jupytercad/schema';
import { IJCadFormSchemaRegistry, JupyterCadDoc } from '@jupytercad/schema';
import { SidePanel } from '@jupyterlab/ui-components';

import { IControlPanelModel } from '../types';
Expand All @@ -13,7 +13,8 @@ export class RightPanelWidget extends SidePanel {
const header = new ControlPanelHeader();
this.header.addWidget(header);
const properties = new ObjectProperties({
controlPanelModel: this._model
controlPanelModel: this._model,
formSchemaRegistry: options.formSchemaRegistry
});

this.addWidget(properties);
Expand All @@ -35,6 +36,7 @@ export class RightPanelWidget extends SidePanel {
export namespace RightPanelWidget {
export interface IOptions {
model: IControlPanelModel;
formSchemaRegistry: IJCadFormSchemaRegistry;
}
export interface IProps {
filePath?: string;
Expand Down
28 changes: 28 additions & 0 deletions packages/schema/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,31 @@ export interface IJCadWorkerRegistry {
export type IJupyterCadWidget = IDocumentWidget<ReactWidget, IJupyterCadModel>;

export type IJupyterCadTracker = IWidgetTracker<IJupyterCadWidget>;

export interface IJCadFormSchemaRegistry {
/**
*
*
* @return {*} {IDict}
* @memberof IJCadFormSchemaRegistry
*/
getSchemas(): Map<string, IDict>;

/**
*
*
* @param {string} name
* @param {IDict} schema
* @memberof IJCadFormSchemaRegistry
*/
registerSchema(name: string, schema: IDict): void;

/**
*
*
* @param {string} name
* @return {*} {boolean}
* @memberof IJCadFormSchemaRegistry
*/
has(name: string): boolean;
}
7 changes: 6 additions & 1 deletion packages/schema/src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { Token } from '@lumino/coreutils';
import {
IJCadWorkerRegistry,
IJupyterCadTracker,
IAnnotationModel
IAnnotationModel,
IJCadFormSchemaRegistry
} from './interfaces';

export const IJupyterCadDocTracker = new Token<IJupyterCadTracker>(
Expand All @@ -17,3 +18,7 @@ export const IAnnotationToken = new Token<IAnnotationModel>(
export const IJCadWorkerRegistryToken = new Token<IJCadWorkerRegistry>(
'jupytercadWorkerRegistry'
);

export const IJCadFormSchemaRegistryToken = new Token<IJCadFormSchemaRegistry>(
'jupytercadFormSchemaRegistry'
);
2 changes: 2 additions & 0 deletions python/jupytercad_core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import jcadPlugin from './jcadplugin/plugins';
import {
annotationPlugin,
formSchemaRegistryPlugin,
trackerPlugin,
workerRegistryPlugin
} from './plugin';
Expand All @@ -11,5 +12,6 @@ export default [
trackerPlugin,
annotationPlugin,
workerRegistryPlugin,
formSchemaRegistryPlugin,
jcadPlugin
];
15 changes: 15 additions & 0 deletions python/jupytercad_core/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { AnnotationModel, JupyterCadWidget } from '@jupytercad/base';
import {
IAnnotationModel,
IAnnotationToken,
IJCadFormSchemaRegistry,
IJCadFormSchemaRegistryToken,
IJCadWorkerRegistry,
IJCadWorkerRegistryToken,
IJupyterCadDocTracker,
Expand All @@ -16,6 +18,7 @@ import { IMainMenu } from '@jupyterlab/mainmenu';
import { ITranslator } from '@jupyterlab/translation';

import { JupyterCadWorkerRegistry } from './workerregistry';
import { JupyterCadFormSchemaRegistry } from './schemaregistry';

const NAME_SPACE = 'jupytercad';

Expand Down Expand Up @@ -66,3 +69,15 @@ export const workerRegistryPlugin: JupyterFrontEndPlugin<IJCadWorkerRegistry> =
return workerRegistry;
}
};

export const formSchemaRegistryPlugin: JupyterFrontEndPlugin<IJCadFormSchemaRegistry> =
{
id: 'jupytercad:core:form-schema-registry',
autoStart: true,
requires: [],
provides: IJCadFormSchemaRegistryToken,
activate: (app: JupyterFrontEnd): IJCadFormSchemaRegistry => {
const registry = new JupyterCadFormSchemaRegistry();
return registry;
}
};
27 changes: 27 additions & 0 deletions python/jupytercad_core/src/schemaregistry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { IDict, IJCadFormSchemaRegistry } from '@jupytercad/schema';
import formSchema from '@jupytercad/schema/lib/_interface/forms.json';

export class JupyterCadFormSchemaRegistry implements IJCadFormSchemaRegistry {
constructor() {
this._registry = new Map<string, IDict>(Object.entries(formSchema));
console.log('_registry', this._registry);
}

registerSchema(name: string, schema: IDict): void {
if (!this._registry.has(name)) {
this._registry.set(name, schema);
} else {
console.error('Worker is already registered!');
}
}

has(name: string): boolean {
return this._registry.has(name);
}

getSchemas(): Map<string, IDict> {
return this._registry;
}

private _registry: Map<string, IDict>;
}
17 changes: 14 additions & 3 deletions python/jupytercad_lab/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
import {
IAnnotationModel,
IAnnotationToken,
IJCadFormSchemaRegistry,
IJCadFormSchemaRegistryToken,
IJupyterCadDocTracker,
IJupyterCadTracker
} from '@jupytercad/schema';
Expand Down Expand Up @@ -61,12 +63,18 @@ const plugin: JupyterFrontEndPlugin<void> = {
const controlPanel: JupyterFrontEndPlugin<void> = {
id: 'jupytercad:lab:controlpanel',
autoStart: true,
requires: [ILayoutRestorer, IJupyterCadDocTracker, IAnnotationToken],
requires: [
ILayoutRestorer,
IJupyterCadDocTracker,
IAnnotationToken,
IJCadFormSchemaRegistryToken
],
activate: (
app: JupyterFrontEnd,
restorer: ILayoutRestorer,
tracker: IJupyterCadTracker,
annotationModel: IAnnotationModel
annotationModel: IAnnotationModel,
formSchemaRegistry: IJCadFormSchemaRegistry
) => {
const controlModel = new ControlPanelModel({ tracker });

Expand All @@ -79,7 +87,10 @@ const controlPanel: JupyterFrontEndPlugin<void> = {
leftControlPanel.title.caption = 'JupyterCad Control Panel';
leftControlPanel.title.icon = jcLightIcon;

const rightControlPanel = new RightPanelWidget({ model: controlModel });
const rightControlPanel = new RightPanelWidget({
model: controlModel,
formSchemaRegistry
});
rightControlPanel.id = 'jupytercad::rightControlPanel';
rightControlPanel.title.caption = 'JupyterCad Control Panel';
rightControlPanel.title.icon = jcLightIcon;
Expand Down
Loading

0 comments on commit 722fde8

Please sign in to comment.