forked from jupytercad/JupyterCAD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.ts
82 lines (69 loc) · 1.78 KB
/
model.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import {
IJCadObject,
IJcadObjectDocChange,
IJupyterCadDoc,
JupyterCadDoc
} from '@jupytercad/schema';
import { JSONExt } from '@lumino/coreutils';
import { ISignal, Signal } from '@lumino/signaling';
import * as Y from 'yjs';
export class JupyterCadStepDoc extends JupyterCadDoc {
constructor() {
super();
this._source = this.ydoc.getText('source');
this._source.observeDeep(this._sourceObserver);
}
set source(value: string) {
this._source.insert(0, value);
}
get version(): string {
return '0.1.0';
}
get objectsChanged(): ISignal<IJupyterCadDoc, IJcadObjectDocChange> {
return this._objectChanged;
}
get objects(): Array<IJCadObject> {
const source = this._source.toJSON();
if (!source) {
return [];
}
return [
{
name: 'Step File', // TODO get file name?
visible: true,
shape: 'Part::Any',
parameters: {
Content: this._source.toJSON(),
Type: 'STEP'
}
}
];
}
static create(): JupyterCadStepDoc {
return new JupyterCadStepDoc();
}
editable = false;
toJcadEndpoint = 'jupytercad/export';
private _sourceObserver = (events: Y.YEvent<any>[]): void => {
const changes: Array<{
name: string;
key: keyof IJCadObject;
newValue: IJCadObject;
}> = [];
events.forEach(event => {
event.keys.forEach((change, key) => {
changes.push({
name: 'Step File',
key: key as any,
newValue: JSONExt.deepCopy(event.target.toJSON())
});
});
});
this._objectChanged.emit({ objectChange: changes });
this._changed.emit({ objectChange: changes });
};
private _source: Y.Text;
private _objectChanged = new Signal<IJupyterCadDoc, IJcadObjectDocChange>(
this
);
}