@@ -5,113 +5,129 @@ import type { TBrickType, TColor, TExtent } from '../@types/brick';
55
66let idCounter = 0 ;
77function generateUUID ( prefix : string ) : string {
8- return `${ prefix } _${ ++ idCounter } ` ;
8+ return `${ prefix } _${ ++ idCounter } ` ;
99}
1010
1111// Default colors
1212const defaultColors = {
13- simple : {
14- colorBg : '#bbdefb' as TColor ,
15- colorFg : '#222' as TColor ,
16- strokeColor : '#1976d2' as TColor ,
17- } ,
18- expression : {
19- colorBg : '#b2fab4' as TColor ,
20- colorFg : '#222' as TColor ,
21- strokeColor : '#2e7d32' as TColor ,
22- } ,
23- compound : {
24- colorBg : '#b9f6ca' as TColor ,
25- colorFg : '#222' as TColor ,
26- strokeColor : '#43a047' as TColor ,
27- } ,
13+ simple : {
14+ colorBg : '#bbdefb' as TColor ,
15+ colorFg : '#222' as TColor ,
16+ strokeColor : '#1976d2' as TColor ,
17+ } ,
18+ expression : {
19+ colorBg : '#b2fab4' as TColor ,
20+ colorFg : '#222' as TColor ,
21+ strokeColor : '#2e7d32' as TColor ,
22+ } ,
23+ compound : {
24+ colorBg : '#b9f6ca' as TColor ,
25+ colorFg : '#222' as TColor ,
26+ strokeColor : '#43a047' as TColor ,
27+ } ,
2828} ;
2929
3030const defaultLabelType = 'text' as const ;
3131const defaultScale = 1 ;
3232const defaultBBoxArgs : TExtent [ ] = [ { w : 40 , h : 20 } ] ;
3333
34- export function createSimpleBrick ( overrides : Partial < ConstructorParameters < typeof SimpleBrick > [ 0 ] > = { } ) {
35- const idx = idCounter + 1 ;
36- // By default, SimpleBrick has two argument slots (for arguments/inputs)
37- return new SimpleBrick ( {
38- uuid : generateUUID ( 'simple' ) ,
39- name : overrides . name ?? `Simple${ idx } ` ,
40- label : overrides . label ?? `Simple${ idx } ` ,
41- labelType : overrides . labelType ?? defaultLabelType ,
42- colorBg : overrides . colorBg ?? defaultColors . simple . colorBg ,
43- colorFg : overrides . colorFg ?? defaultColors . simple . colorFg ,
44- strokeColor : overrides . strokeColor ?? defaultColors . simple . strokeColor ,
45- shadow : overrides . shadow ?? false ,
46- scale : overrides . scale ?? defaultScale ,
47- bboxArgs : overrides . bboxArgs ?? [ { w : 40 , h : 20 } , { w : 40 , h : 20 } ] ,
48- topNotch : overrides . topNotch ?? true ,
49- bottomNotch : overrides . bottomNotch ?? true ,
50- tooltip : overrides . tooltip ,
51- ...overrides ,
52- } ) ;
34+ export function createSimpleBrick (
35+ overrides : Partial < ConstructorParameters < typeof SimpleBrick > [ 0 ] > = { } ,
36+ ) {
37+ const idx = idCounter + 1 ;
38+ // By default, SimpleBrick has two argument slots (for arguments/inputs)
39+ return new SimpleBrick ( {
40+ uuid : generateUUID ( 'simple' ) ,
41+ name : overrides . name ?? `Simple${ idx } ` ,
42+ label : overrides . label ?? `Simple${ idx } ` ,
43+ labelType : overrides . labelType ?? defaultLabelType ,
44+ colorBg : overrides . colorBg ?? defaultColors . simple . colorBg ,
45+ colorFg : overrides . colorFg ?? defaultColors . simple . colorFg ,
46+ strokeColor : overrides . strokeColor ?? defaultColors . simple . strokeColor ,
47+ shadow : overrides . shadow ?? false ,
48+ scale : overrides . scale ?? defaultScale ,
49+ bboxArgs : overrides . bboxArgs ?? [
50+ { w : 40 , h : 20 } ,
51+ { w : 40 , h : 20 } ,
52+ ] ,
53+ topNotch : overrides . topNotch ?? true ,
54+ bottomNotch : overrides . bottomNotch ?? true ,
55+ tooltip : overrides . tooltip ,
56+ ...overrides ,
57+ } ) ;
5358}
5459
5560// ExpressionBrick is used as an argument value, not as an argument-receiving brick
56- export function createExpressionBrick ( overrides : Partial < ConstructorParameters < typeof ExpressionBrick > [ 0 ] > = { } ) {
57- const idx = idCounter + 1 ;
58- return new ExpressionBrick ( {
59- uuid : generateUUID ( 'expr' ) ,
60- name : overrides . name ?? `Expr${ idx } ` ,
61- label : overrides . label ?? `Expr${ idx } ` ,
62- labelType : overrides . labelType ?? defaultLabelType ,
63- colorBg : overrides . colorBg ?? defaultColors . expression . colorBg ,
64- colorFg : overrides . colorFg ?? defaultColors . expression . colorFg ,
65- strokeColor : overrides . strokeColor ?? defaultColors . expression . strokeColor ,
66- shadow : overrides . shadow ?? false ,
67- scale : overrides . scale ?? defaultScale ,
68- bboxArgs : overrides . bboxArgs ?? [ { w : 40 , h : 20 } ] ,
69- value : overrides . value ,
70- isValueSelectOpen : overrides . isValueSelectOpen ?? false ,
71- tooltip : overrides . tooltip ,
72- ...overrides ,
73- } ) ;
61+ export function createExpressionBrick (
62+ overrides : Partial < ConstructorParameters < typeof ExpressionBrick > [ 0 ] > = { } ,
63+ ) {
64+ const idx = idCounter + 1 ;
65+ return new ExpressionBrick ( {
66+ uuid : generateUUID ( 'expr' ) ,
67+ name : overrides . name ?? `Expr${ idx } ` ,
68+ label : overrides . label ?? `Expr${ idx } ` ,
69+ labelType : overrides . labelType ?? defaultLabelType ,
70+ colorBg : overrides . colorBg ?? defaultColors . expression . colorBg ,
71+ colorFg : overrides . colorFg ?? defaultColors . expression . colorFg ,
72+ strokeColor : overrides . strokeColor ?? defaultColors . expression . strokeColor ,
73+ shadow : overrides . shadow ?? false ,
74+ scale : overrides . scale ?? defaultScale ,
75+ bboxArgs : overrides . bboxArgs ?? [ { w : 40 , h : 20 } ] ,
76+ value : overrides . value ,
77+ isValueSelectOpen : overrides . isValueSelectOpen ?? false ,
78+ tooltip : overrides . tooltip ,
79+ ...overrides ,
80+ } ) ;
7481}
7582
76- export function createCompoundBrick ( overrides : Partial < ConstructorParameters < typeof CompoundBrick > [ 0 ] > = { } ) {
77- const idx = idCounter + 1 ;
78- return new CompoundBrick ( {
79- uuid : generateUUID ( 'compound' ) ,
80- name : overrides . name ?? `Compound${ idx } ` ,
81- label : overrides . label ?? `Compound${ idx } ` ,
82- labelType : overrides . labelType ?? defaultLabelType ,
83- colorBg : overrides . colorBg ?? defaultColors . compound . colorBg ,
84- colorFg : overrides . colorFg ?? defaultColors . compound . colorFg ,
85- strokeColor : overrides . strokeColor ?? defaultColors . compound . strokeColor ,
86- shadow : overrides . shadow ?? false ,
87- scale : overrides . scale ?? defaultScale ,
88- bboxArgs : overrides . bboxArgs ?? defaultBBoxArgs ,
89- bboxNest : overrides . bboxNest ?? [ ] ,
90- topNotch : overrides . topNotch ?? true ,
91- bottomNotch : overrides . bottomNotch ?? true ,
92- isFolded : overrides . isFolded ?? false ,
93- tooltip : overrides . tooltip ,
94- ...overrides ,
95- } ) ;
83+ export function createCompoundBrick (
84+ overrides : Partial < ConstructorParameters < typeof CompoundBrick > [ 0 ] > = { } ,
85+ ) {
86+ const idx = idCounter + 1 ;
87+ return new CompoundBrick ( {
88+ uuid : generateUUID ( 'compound' ) ,
89+ name : overrides . name ?? `Compound${ idx } ` ,
90+ label : overrides . label ?? `Compound${ idx } ` ,
91+ labelType : overrides . labelType ?? defaultLabelType ,
92+ colorBg : overrides . colorBg ?? defaultColors . compound . colorBg ,
93+ colorFg : overrides . colorFg ?? defaultColors . compound . colorFg ,
94+ strokeColor : overrides . strokeColor ?? defaultColors . compound . strokeColor ,
95+ shadow : overrides . shadow ?? false ,
96+ scale : overrides . scale ?? defaultScale ,
97+ bboxArgs : overrides . bboxArgs ?? defaultBBoxArgs ,
98+ bboxNest : overrides . bboxNest ?? [ ] ,
99+ topNotch : overrides . topNotch ?? true ,
100+ bottomNotch : overrides . bottomNotch ?? true ,
101+ isFolded : overrides . isFolded ?? false ,
102+ tooltip : overrides . tooltip ,
103+ ...overrides ,
104+ } ) ;
96105}
97106
98107export function resetFactoryCounter ( ) {
99- idCounter = 0 ;
108+ idCounter = 0 ;
100109}
101110
102111export function getFactoryCounter ( ) {
103- return idCounter ;
112+ return idCounter ;
104113}
105114
106- export function createBrick ( type : TBrickType , overrides : any = { } ) {
107- switch ( type ) {
108- case 'Simple' :
109- return createSimpleBrick ( overrides ) ;
110- case 'Expression' :
111- return createExpressionBrick ( overrides ) ;
112- case 'Compound' :
113- return createCompoundBrick ( overrides ) ;
114- default :
115- throw new Error ( `Unsupported brick type: ${ type } ` ) ;
116- }
115+ export function createBrick (
116+ type : TBrickType ,
117+ overrides : Partial <
118+ | ConstructorParameters < typeof SimpleBrick > [ 0 ]
119+ | ConstructorParameters < typeof ExpressionBrick > [ 0 ]
120+ | ConstructorParameters < typeof CompoundBrick > [ 0 ]
121+ > = { } ,
122+ ) {
123+ switch ( type ) {
124+ case 'Simple' :
125+ return createSimpleBrick ( overrides ) ;
126+ case 'Expression' :
127+ return createExpressionBrick ( overrides ) ;
128+ case 'Compound' :
129+ return createCompoundBrick ( overrides ) ;
130+ default :
131+ throw new Error ( `Unsupported brick type: ${ type } ` ) ;
132+ }
117133}
0 commit comments