Skip to content

Commit

Permalink
Add schema for options keyword of Jcad file
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc committed Jan 17, 2023
1 parent 956671c commit 650d827
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
3 changes: 1 addition & 2 deletions src/mainview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
} from './types';

import { ContextMenu } from '@lumino/widgets';
import { JSONObject } from '@lumino/coreutils';
import { CommandRegistry } from '@lumino/commands';
import { MapChange } from '@jupyter/ydoc';
import { FloatingAnnotation } from './annotation/view';
Expand Down Expand Up @@ -795,7 +794,7 @@ export class MainView extends React.Component<IProps, IStates> {
sender: IJupyterCadDoc,
change: MapChange
): void {
const guidata = sender.getOption('guidata') as JSONObject | undefined;
const guidata = sender.getOption('guidata');

if (guidata) {
for (const objName in guidata) {
Expand Down
27 changes: 16 additions & 11 deletions src/model.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { MapChange, YDocument } from '@jupyter/ydoc';
import { IChangedArgs } from '@jupyterlab/coreutils';
import {
JSONExt,
JSONObject,
JSONValue,
PartialJSONObject
} from '@lumino/coreutils';
import { JSONExt, JSONObject, PartialJSONObject } from '@lumino/coreutils';
import { ISignal, Signal } from '@lumino/signaling';
import Ajv from 'ajv';
import * as Y from 'yjs';

import { IJCadContent, IJCadModel, IJCadObject } from './_interface/jcad';
import {
IJCadContent,
IJCadModel,
IJCadObject,
IJCadOptions
} from './_interface/jcad';
import jcadSchema from './schema/jcad.json';
import {
Camera,
IAnnotationModel,
IDict,
IJcadObjectDocChange,
IJupyterCadClientState,
IJupyterCadDoc,
Expand Down Expand Up @@ -316,15 +317,19 @@ export class JupyterCadDoc
this.transact(() => obj.set(key, value));
}

getOption(key: string): JSONValue {
return JSONExt.deepCopy(this._options.get(key));
getOption(key: keyof IJCadOptions): IDict | undefined {
const content = this._options.get(key);
if (!content) {
return;
}
return JSONExt.deepCopy(content) as IDict;
}

setOption(key: string, value: JSONValue): void {
setOption(key: keyof IJCadOptions, value: IDict): void {
this._options.set(key, value);
}

setOptions(options: JSONObject): void {
setOptions(options: IJCadOptions): void {
for (const [key, value] of Object.entries(options)) {
this._options.set(key, value);
}
Expand Down
17 changes: 15 additions & 2 deletions src/schema/jcad.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
"$ref": "#/definitions/jcadModel"
},
"options": {
"type": "object"
"$ref": "#/definitions/jcadOptions"
},
"metadata": {
"type": "object",
"patternProperties": {
"^.*$": { "type": "string" }
"^.*$": {
"type": "string"
}
},
"additionalProperties": false
}
Expand Down Expand Up @@ -71,6 +73,17 @@
"items": {
"$ref": "#/definitions/jcadObject"
}
},
"jcadOptions": {
"title": "IJCadOptions",
"type": "object",
"default": {},
"additionalProperties": false,
"properties": {
"guidata": {
"type": "object"
}
}
}
}
}
15 changes: 10 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import { User } from '@jupyterlab/services';
import { MapChange, YDocument, StateChange } from '@jupyter/ydoc';

import { ISignal, Signal } from '@lumino/signaling';
import { JSONObject, JSONValue } from '@lumino/coreutils';
import { JSONObject } from '@lumino/coreutils';

import { IJupyterCadTracker } from './token';
import { IJCadContent, IJCadObject, IJCadModel } from './_interface/jcad';
import {
IJCadContent,
IJCadObject,
IJCadModel,
IJCadOptions
} from './_interface/jcad';

export interface IDict<T = any> {
[key: string]: T;
Expand Down Expand Up @@ -149,9 +154,9 @@ export interface IJupyterCadDoc extends YDocument<IJupyterCadDocChange> {
addObjects(value: Array<IJCadObject>): void;
updateObjectByName(name: string, key: string, value: any): void;

getOption(key: string): JSONValue;
setOption(key: string, value: JSONValue): void;
setOptions(options: JSONObject): void;
getOption(key: keyof IJCadOptions): IDict | undefined;
setOption(key: keyof IJCadOptions, value: IDict): void;
setOptions(options: IJCadOptions): void;

getMetadata(key: string): string | undefined;
setMetadata(key: string, value: string): void;
Expand Down

0 comments on commit 650d827

Please sign in to comment.