/
jsxcad.js
73 lines (58 loc) · 1.85 KB
/
jsxcad.js
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
import debugUtil from "debug";
import CSG from "@jscad/csg/api";
const { color } = CSG.color;
const { cube, sphere, cylinder } = CSG.primitives3d;
const { square, circle } = CSG.primitives2d;
const { linear_extrude } = CSG.extrusions;
const { union, difference, intersection } = CSG.booleanOps;
const { translate } = CSG.transformations;
const debug = debugUtil("jsxcad");
export function Union({ props, csgObject }) {
return union(csgObject);
}
export function Difference({ props, csgObject }) {
return difference(csgObject);
}
export function Intersection({ props, csgObject }) {
return intersection(csgObject);
}
export function Color({ props, csgObject }) {
return color(props.rgb, csgObject);
}
export function Cylinder({ props, csgObject }) {
return cylinder(props);
}
export function Sphere({ props, csgObject }) {
return sphere(props);
}
export function Cube({ props, csgObject }) {
return cube(props);
}
function JSXCAD() {
function render(element) {
const { operatorFn, props } = element;
debug("render", operatorFn, props);
const childElements = props.children || [];
const renderedChildren = childElements.map(childElement => {
return render(childElement);
});
const { children: _, ...operatorProps } = props;
return operatorFn({ csgObject: renderedChildren, props: operatorProps });
}
function createElement(operatorFn, config, ...args) {
const props = Object.assign({}, config);
const hasChildren = args.length > 0;
const rawChildren = hasChildren ? [].concat(...args) : [];
props.children = rawChildren.filter(c => c != null && c !== false);
return { operatorFn, props };
}
function Fragment({ props, csgObject }) {
return { main: params => csgObject, getParameterDefinitions: () => [] };
}
return {
render,
createElement,
Fragment
};
}
export default JSXCAD();