Skip to content

Commit f68e5f6

Browse files
authored
CSE Machine: Rewrite the Config enum to be a constant object (source-academy#2853)
* Rewrite Config enums to be a constant object instead, and remove all unneeded toString and Number calls * Fix some type errors and formatting
1 parent f233458 commit f68e5f6

16 files changed

+173
-177
lines changed
Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,64 @@
11
/** configs for dimensions */
2-
export enum Config {
3-
CanvasMinHeight = 800,
4-
CanvasMinWidth = 800,
5-
CanvasPaddingX = 30,
6-
CanvasPaddingY = 30,
2+
export const Config = Object.freeze({
3+
CanvasMinHeight: 800,
4+
CanvasMinWidth: 800,
5+
CanvasPaddingX: 30,
6+
CanvasPaddingY: 30,
77

8-
LevelPaddingX = 10,
9-
LevelPaddingY = 10,
8+
LevelPaddingX: 10,
9+
LevelPaddingY: 10,
1010

11-
FrameMinWidth = 100,
12-
FramePaddingX = 20,
13-
FramePaddingY = 30,
14-
FrameMarginX = 30,
15-
FrameMarginY = 10,
16-
FrameCornerRadius = 3,
11+
FrameMinWidth: 100,
12+
FramePaddingX: 20,
13+
FramePaddingY: 30,
14+
FrameMarginX: 30,
15+
FrameMarginY: 10,
16+
FrameCornerRadius: 3,
1717

18-
FnRadius = 15,
19-
FnInnerRadius = 3,
20-
FnTooltipOpacity = 0.3,
18+
FnRadius: 15,
19+
FnInnerRadius: 3,
20+
FnTooltipOpacity: 0.3,
2121

22-
DataMinWidth = 20,
23-
DataUnitWidth = 40,
24-
DataUnitHeight = 40,
25-
DataCornerRadius = 3,
26-
DataHitStrokeWidth = 5,
22+
DataMinWidth: 20,
23+
DataUnitWidth: 40,
24+
DataUnitHeight: 40,
25+
DataCornerRadius: 3,
26+
DataHitStrokeWidth: 5,
2727

28-
TextPaddingX = 10,
29-
TextPaddingY = 30,
30-
TextMargin = 5,
31-
TextMinWidth = 30,
32-
FontFamily = 'monospace, monospace',
33-
FontSize = 15,
34-
FontStyle = 'normal',
35-
FontVariant = 'normal',
28+
TextPaddingX: 10,
29+
TextPaddingY: 30,
30+
TextMargin: 5,
31+
TextMinWidth: 30,
32+
FontFamily: 'monospace, monospace',
33+
FontSize: 15,
34+
FontStyle: 'normal',
35+
FontVariant: 'normal',
3636

37-
HoveredColor = '#32CD32',
37+
HoveredColor: '#32CD32',
3838

39-
ArrowHeadSize = 10,
40-
ArrowStrokeWidth = 1,
41-
ArrowHitStrokeWidth = 5,
42-
ArrowHoveredStrokeWidth = 2,
43-
ArrowCornerRadius = 40,
39+
ArrowHeadSize: 10,
40+
ArrowStrokeWidth: 1,
41+
ArrowHitStrokeWidth: 5,
42+
ArrowHoveredStrokeWidth: 2,
43+
ArrowCornerRadius: 40,
4444

45-
MaxExportWidth = 20000,
46-
MaxExportHeight = 12000,
45+
MaxExportWidth: 20000,
46+
MaxExportHeight: 12000,
4747

48-
SA_WHITE = '#999999',
49-
SA_BLUE = '#2c3e50',
50-
PRINT_BACKGROUND = 'white',
51-
SA_CURRENT_ITEM = '#030fff',
48+
SA_WHITE: '#999999',
49+
SA_BLUE: '#2c3e50',
50+
PRINT_BACKGROUND: 'white',
51+
SA_CURRENT_ITEM: '#030fff',
5252

53-
ConstantColon = ':= ',
54-
VariableColon = ': ',
55-
Ellipsis = '…',
53+
ConstantColon: ':= ',
54+
VariableColon: ': ',
55+
Ellipsis: '…',
5656

57-
UnassignedData = '',
58-
GlobalFrameDefaultText = ':::pre-declared names::',
59-
GlobalEnvId = '-1'
60-
}
57+
UnassignedData: '',
58+
GlobalFrameDefaultText: ':::pre-declared names::',
59+
GlobalEnvId: '-1'
60+
});
6161

62-
export const ShapeDefaultProps = {
62+
export const ShapeDefaultProps = Object.freeze({
6363
preventDefault: false
64-
};
64+
});
Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
1-
export enum ControlStashConfig {
2-
ControlPosX = 20,
3-
ControlPosY = 5,
4-
StashPosX = 140,
5-
StashPosY = 5,
1+
export const ControlStashConfig = Object.freeze({
2+
ControlPosX: 20,
3+
ControlPosY: 5,
4+
StashPosX: 140,
5+
StashPosY: 5,
66

7-
ControlItemWidth = 120,
8-
ControlItemTextPadding = 10,
9-
ControlItemCornerRadius = 5,
10-
ControlMaxTextWidth = 95,
11-
ControlMaxTextHeight = 125,
12-
StashMaxTextWidth = ControlMaxTextWidth,
13-
StashItemHeight = 15,
14-
StashItemTextPadding = ControlItemTextPadding,
15-
StashMaxTextHeight = 30,
16-
StashItemCornerRadius = ControlItemCornerRadius,
7+
ControlItemWidth: 120,
8+
ControlItemTextPadding: 10,
9+
ControlMaxTextWidth: 95,
10+
ControlMaxTextHeight: 125,
11+
ControlItemCornerRadius: 5,
1712

18-
ShowMoreButtonWidth = 80,
19-
ShowMoreButtonHeight = 15,
20-
ShowMoreButtonX = 40,
21-
ShowMoreButtonY = 25,
13+
StashItemHeight: 15,
14+
StashItemTextPadding: 10,
15+
StashMaxTextWidth: 95,
16+
StashMaxTextHeight: 30,
17+
StashItemCornerRadius: 5,
2218

23-
TooltipOpacity = 0.7,
24-
TooltipMargin = 5,
25-
TooltipPadding = 5,
19+
ShowMoreButtonWidth: 80,
20+
ShowMoreButtonHeight: 15,
21+
ShowMoreButtonX: 40,
22+
ShowMoreButtonY: 25,
2623

27-
FontFamily = 'monospace, monospace',
28-
FontSize = 15,
29-
FontStyle = 'normal',
30-
FontVariant = 'normal',
31-
FontStyleHeader = 'bold',
32-
FontSizeHeader = 18,
24+
TooltipOpacity: 0.7,
25+
TooltipMargin: 5,
26+
TooltipPadding: 5,
3327

34-
SA_WHITE = '#999999',
35-
SA_BLUE = '#2c3e50',
36-
PRINT_BACKGROUND = 'white',
37-
STASH_DANGER_ITEM = '#ff0000'
38-
}
28+
FontFamily: 'monospace, monospace',
29+
FontSize: 15,
30+
FontStyle: 'normal',
31+
FontVariant: 'normal',
32+
FontStyleHeader: 'bold',
33+
FontSizeHeader: 18,
34+
35+
SA_WHITE: '#999999',
36+
SA_BLUE: '#2c3e50',
37+
PRINT_BACKGROUND: 'white',
38+
STASH_DANGER_ITEM: '#ff0000'
39+
});

src/features/cseMachine/CseMachineLayout.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,8 @@ export class Layout {
444444
height: Layout.height(),
445445
overflow: 'hidden',
446446
backgroundColor: CseMachine.getPrintableMode()
447-
? Config.PRINT_BACKGROUND.toString()
448-
: Config.SA_BLUE.toString()
447+
? Config.PRINT_BACKGROUND
448+
: Config.SA_BLUE
449449
}}
450450
>
451451
<Stage
@@ -463,11 +463,7 @@ export class Layout {
463463
y={0}
464464
width={Layout.width()}
465465
height={Layout.height()}
466-
fill={
467-
CseMachine.getPrintableMode()
468-
? Config.PRINT_BACKGROUND.toString()
469-
: Config.SA_BLUE.toString()
470-
}
466+
fill={CseMachine.getPrintableMode() ? Config.PRINT_BACKGROUND : Config.SA_BLUE}
471467
key={Layout.key++}
472468
listening={false}
473469
/>

src/features/cseMachine/CseMachineUtils.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export function getTextHeight(
200200
text: string,
201201
width: number,
202202
font: string = `${Config.FontStyle} ${Config.FontSize}px ${Config.FontFamily}`,
203-
fontSize: number = Number(Config.FontSize)
203+
fontSize: number = Config.FontSize
204204
): number {
205205
const canvas = document.createElement('canvas');
206206
const context = canvas.getContext('2d');
@@ -266,8 +266,8 @@ export function setHoveredStyle(target: Node | Group, hoveredAttrs: any = {}): v
266266
nodes.push(target);
267267
nodes.forEach(node => {
268268
node.setAttrs({
269-
stroke: node.attrs.stroke ? Config.HoveredColor.toString() : node.attrs.stroke,
270-
fill: node.attrs.fill ? Config.HoveredColor.toString() : node.attrs.fill,
269+
stroke: node.attrs.stroke ? Config.HoveredColor : node.attrs.stroke,
270+
fill: node.attrs.fill ? Config.HoveredColor : node.attrs.fill,
271271
...hoveredAttrs
272272
});
273273
});
@@ -283,13 +283,13 @@ export function setUnhoveredStyle(target: Node | Group, unhoveredAttrs: any = {}
283283
node.setAttrs({
284284
stroke: node.attrs.stroke
285285
? CseMachine.getPrintableMode()
286-
? Config.SA_BLUE.toString()
287-
: Config.SA_WHITE.toString()
286+
? Config.SA_BLUE
287+
: Config.SA_WHITE
288288
: node.attrs.stroke,
289289
fill: node.attrs.fill
290290
? CseMachine.getPrintableMode()
291-
? Config.SA_BLUE.toString()
292-
: Config.SA_WHITE.toString()
291+
? Config.SA_BLUE
292+
: Config.SA_WHITE
293293
: node.attrs.fill,
294294
...unhoveredAttrs
295295
});
@@ -702,17 +702,17 @@ export const isStashItemInDanger = (stashIndex: number): boolean => {
702702
};
703703

704704
export const defaultSAColor = () =>
705-
CseMachine.getPrintableMode() ? Config.SA_BLUE.toString() : Config.SA_WHITE.toString();
705+
CseMachine.getPrintableMode() ? Config.SA_BLUE : Config.SA_WHITE;
706706

707707
export const stackItemSAColor = (index: number) =>
708708
isStashItemInDanger(index)
709-
? ControlStashConfig.STASH_DANGER_ITEM.toString()
709+
? ControlStashConfig.STASH_DANGER_ITEM
710710
: CseMachine.getPrintableMode()
711-
? ControlStashConfig.SA_BLUE.toString()
712-
: ControlStashConfig.SA_WHITE.toString();
711+
? ControlStashConfig.SA_BLUE
712+
: ControlStashConfig.SA_WHITE;
713713
export const currentItemSAColor = (test: boolean) =>
714714
test
715-
? Config.SA_CURRENT_ITEM.toString()
715+
? Config.SA_CURRENT_ITEM
716716
: CseMachine.getPrintableMode()
717-
? ControlStashConfig.SA_BLUE.toString()
718-
: ControlStashConfig.SA_WHITE.toString();
717+
? ControlStashConfig.SA_BLUE
718+
: ControlStashConfig.SA_WHITE;

src/features/cseMachine/animationComponents/AnimationComponents.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,16 @@ export class AnimatedTextboxComponent extends Animatable {
183183
super();
184184
const rectProps = {
185185
stroke: currentItemSAColor(false),
186-
cornerRadius: Number(ControlStashConfig.ControlItemCornerRadius)
186+
cornerRadius: ControlStashConfig.ControlItemCornerRadius
187187
};
188188
const textProps = {
189189
text,
190-
fill: ControlStashConfig.SA_WHITE.toString(),
191-
padding: Number(ControlStashConfig.ControlItemTextPadding),
192-
fontFamily: ControlStashConfig.FontFamily.toString(),
193-
fontSize: Number(ControlStashConfig.FontSize),
194-
fontStyle: ControlStashConfig.FontStyle.toString(),
195-
fontVariant: ControlStashConfig.FontVariant.toString()
190+
fill: ControlStashConfig.SA_WHITE,
191+
padding: ControlStashConfig.ControlItemTextPadding,
192+
fontFamily: ControlStashConfig.FontFamily,
193+
fontSize: ControlStashConfig.FontSize,
194+
fontStyle: ControlStashConfig.FontStyle,
195+
fontVariant: ControlStashConfig.FontVariant
196196
};
197197
this.rectComponent = new AnimatedRectComponent(from, to, rectProps, animationConfig);
198198
this.textComponent = new AnimatedTextComponent(from, to, textProps, animationConfig);

src/features/cseMachine/components/ArrayNullUnit.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class ArrayNullUnit extends Visible {
3131
key={Layout.key++}
3232
points={[this.x(), this.y() + this.height(), this.x() + this.width(), this.y()]}
3333
stroke={defaultSAColor()}
34-
hitStrokeWidth={Number(Config.DataHitStrokeWidth)}
34+
hitStrokeWidth={Config.DataHitStrokeWidth}
3535
ref={this.ref}
3636
listening={false}
3737
/>

src/features/cseMachine/components/ArrayUnit.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,9 @@ export class ArrayUnit extends Visible {
6666
lowerRight: 0
6767
};
6868

69-
if (this.isFirstUnit)
70-
cornerRadius.upperLeft = cornerRadius.lowerLeft = Number(Config.DataCornerRadius);
69+
if (this.isFirstUnit) cornerRadius.upperLeft = cornerRadius.lowerLeft = Config.DataCornerRadius;
7170
if (this.isLastUnit)
72-
cornerRadius.upperRight = cornerRadius.lowerRight = Number(Config.DataCornerRadius);
71+
cornerRadius.upperRight = cornerRadius.lowerRight = Config.DataCornerRadius;
7372

7473
return (
7574
<React.Fragment key={Layout.key++}>
@@ -80,7 +79,7 @@ export class ArrayUnit extends Visible {
8079
width={this.width()}
8180
height={this.height()}
8281
stroke={defaultSAColor()}
83-
hitStrokeWidth={Number(Config.DataHitStrokeWidth)}
82+
hitStrokeWidth={Config.DataHitStrokeWidth}
8483
fillEnabled={false}
8584
onMouseEnter={this.onMouseEnter}
8685
onMouseLeave={this.onMouseLeave}

src/features/cseMachine/components/ControlItemComponent.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,16 @@ export class ControlItemComponent extends Visible implements IHoverable {
8585

8686
draw(): React.ReactNode {
8787
const textProps = {
88-
fill: ControlStashConfig.SA_WHITE.toString(),
89-
padding: Number(ControlStashConfig.ControlItemTextPadding),
90-
fontFamily: ControlStashConfig.FontFamily.toString(),
91-
fontSize: Number(ControlStashConfig.FontSize),
92-
fontStyle: ControlStashConfig.FontStyle.toString(),
93-
fontVariant: ControlStashConfig.FontVariant.toString()
88+
fill: ControlStashConfig.SA_WHITE,
89+
padding: ControlStashConfig.ControlItemTextPadding,
90+
fontFamily: ControlStashConfig.FontFamily,
91+
fontSize: ControlStashConfig.FontSize,
92+
fontStyle: ControlStashConfig.FontStyle,
93+
fontVariant: ControlStashConfig.FontVariant
9494
};
9595
const tagProps = {
9696
stroke: currentItemSAColor(this.topItem),
97-
cornerRadius: Number(ControlStashConfig.ControlItemCornerRadius)
97+
cornerRadius: ControlStashConfig.ControlItemCornerRadius
9898
};
9999
return (
100100
<React.Fragment key={Layout.key++}>
@@ -124,13 +124,13 @@ export class ControlItemComponent extends Visible implements IHoverable {
124124
{...ShapeDefaultProps}
125125
stroke="black"
126126
fill={'black'}
127-
opacity={Number(ControlStashConfig.TooltipOpacity)}
127+
opacity={ControlStashConfig.TooltipOpacity}
128128
/>
129129
<Text
130130
{...ShapeDefaultProps}
131131
{...textProps}
132132
text={this.tooltip}
133-
padding={Number(ControlStashConfig.TooltipPadding)}
133+
padding={ControlStashConfig.TooltipPadding}
134134
/>
135135
</Label>
136136
{this.arrow?.draw()}

0 commit comments

Comments
 (0)