From 650d827fc0ac7980774ce08e567e7ea0492f17bc Mon Sep 17 00:00:00 2001 From: Duc Trung LE Date: Tue, 17 Jan 2023 11:46:23 +0100 Subject: [PATCH] Add schema for `options` keyword of Jcad file --- src/mainview.tsx | 3 +-- src/model.ts | 27 ++++++++++++++++----------- src/schema/jcad.json | 17 +++++++++++++++-- src/types.ts | 15 ++++++++++----- 4 files changed, 42 insertions(+), 20 deletions(-) diff --git a/src/mainview.tsx b/src/mainview.tsx index 7796b542..d562e7f7 100644 --- a/src/mainview.tsx +++ b/src/mainview.tsx @@ -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'; @@ -795,7 +794,7 @@ export class MainView extends React.Component { 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) { diff --git a/src/model.ts b/src/model.ts index 87454a17..fa9264cb 100644 --- a/src/model.ts +++ b/src/model.ts @@ -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, @@ -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); } diff --git a/src/schema/jcad.json b/src/schema/jcad.json index 06610750..e4059a35 100644 --- a/src/schema/jcad.json +++ b/src/schema/jcad.json @@ -8,12 +8,14 @@ "$ref": "#/definitions/jcadModel" }, "options": { - "type": "object" + "$ref": "#/definitions/jcadOptions" }, "metadata": { "type": "object", "patternProperties": { - "^.*$": { "type": "string" } + "^.*$": { + "type": "string" + } }, "additionalProperties": false } @@ -71,6 +73,17 @@ "items": { "$ref": "#/definitions/jcadObject" } + }, + "jcadOptions": { + "title": "IJCadOptions", + "type": "object", + "default": {}, + "additionalProperties": false, + "properties": { + "guidata": { + "type": "object" + } + } } } } diff --git a/src/types.ts b/src/types.ts index d603e887..add48eaf 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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 { [key: string]: T; @@ -149,9 +154,9 @@ export interface IJupyterCadDoc extends YDocument { addObjects(value: Array): 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;