Skip to content

Commit c506ca5

Browse files
committed
fix: resolved problems in the code review
1 parent c2f6e8c commit c506ca5

8 files changed

+124
-127
lines changed

src/components/svgs/nodes/chongqingrt-num-line-badge.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const ChongqingRTNumLineBadge = (props: NodeComponentProps<ChongqingRTNumLineBad
5656
};
5757

5858
/**
59-
* BerlinUBahnLineBadge specific props.
59+
* ChongqingRTNumLineBadge specific props.
6060
*/
6161
export interface ChongqingRTNumLineBadgeAttributes extends AttributesWithColor {
6262
num: number | string;

src/components/svgs/nodes/chongqingrt-text-line-badge.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const ChongqingRTTextLineBadge = (props: NodeComponentProps<ChongqingRTTextLineB
6969
};
7070

7171
/**
72-
* BerlinUBahnLineBadge specific props.
72+
* ChongqingRTTextLineBadge specific props.
7373
*/
7474
export interface ChongqingRTTextLineBadgeAttributes extends AttributesWithColor {
7575
names: [string, string];

src/components/svgs/nodes/chongqingrt2021-num-line-badge.tsx

+37-40
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { MonoColour } from '@railmapgen/rmg-palette-resources';
22
import React from 'react';
3-
import { CityCode } from '../../../constants/constants';
3+
import { useTranslation } from 'react-i18next';
4+
import { RmgFields, RmgFieldsField } from '@railmapgen/rmg-components';
5+
import { AttrsProps, CityCode } from '../../../constants/constants';
46
import { MiscNodeType, Node, NodeComponentProps } from '../../../constants/nodes';
57
import { AttributesWithColor, ColorField } from '../../panels/details/color-field';
6-
import { RmgFieldsFieldDetail, RmgFieldsFieldSpecificAttributes } from '../../panels/details/rmg-field-specific-attrs';
78

89
const ChongqingRT2021NumLineBadge = (props: NodeComponentProps<ChongqingRT2021NumLineBadgeAttributes>) => {
910
const { id, x, y, attrs, handlePointerDown, handlePointerMove, handlePointerUp } = props;
@@ -57,7 +58,7 @@ const ChongqingRT2021NumLineBadge = (props: NodeComponentProps<ChongqingRT2021Nu
5758
};
5859

5960
/**
60-
* BerlinUBahnLineBadge specific props.
61+
* ChongqingRT2021NumLineBadge specific props.
6162
*/
6263
export interface ChongqingRT2021NumLineBadgeAttributes extends AttributesWithColor {
6364
num: number | string;
@@ -68,43 +69,39 @@ const defaultChongqingRT2021NumLineBadgeAttributes: ChongqingRT2021NumLineBadgeA
6869
color: [CityCode.Chongqing, 'cq1', '#e4002b', MonoColour.white],
6970
};
7071

71-
const chongqingRT2021NumLineBadgeFields = [
72-
{
73-
type: 'input',
74-
label: 'panel.details.nodes.common.num',
75-
value: (attrs?: ChongqingRT2021NumLineBadgeAttributes) =>
76-
(attrs ?? defaultChongqingRT2021NumLineBadgeAttributes).num,
77-
validator: (val: string) => !Number.isNaN(val),
78-
onChange: (val: string | number, attrs_: ChongqingRT2021NumLineBadgeAttributes | undefined) => {
79-
// set default value if switched from another type
80-
const attrs = attrs_ ?? defaultChongqingRT2021NumLineBadgeAttributes;
81-
// set value
82-
if (Number.isNaN(Number(val))) {
83-
attrs.num = val;
84-
} else {
85-
attrs.num = Number(val);
86-
}
87-
// return modified attrs
88-
return attrs;
72+
const ChongqingRT2021NumLineBadgeAttrsComponent = (props: AttrsProps<ChongqingRT2021NumLineBadgeAttributes>) => {
73+
const { id, attrs, handleAttrsUpdate } = props;
74+
const { t } = useTranslation();
75+
const fields: RmgFieldsField[] = [
76+
{
77+
type: 'input',
78+
label: t('panel.details.nodes.common.num'),
79+
value: (attrs ?? defaultChongqingRT2021NumLineBadgeAttributes).num as string,
80+
validator: (val: string) => !Number.isNaN(val),
81+
onChange: (val: string | number) => {
82+
if (Number.isNaN(Number(val))) {
83+
attrs.num = val;
84+
} else {
85+
attrs.num = Number(val);
86+
}
87+
handleAttrsUpdate(id, attrs);
88+
},
89+
minW: 'full',
8990
},
90-
},
91-
{
92-
type: 'custom',
93-
label: 'color',
94-
component: (
95-
<ColorField
96-
type={MiscNodeType.ChongqingRT2021NumLineBadge}
97-
defaultTheme={defaultChongqingRT2021NumLineBadgeAttributes.color}
98-
/>
99-
),
100-
},
101-
];
102-
103-
const attrsComponent = () => (
104-
<RmgFieldsFieldSpecificAttributes
105-
fields={chongqingRT2021NumLineBadgeFields as RmgFieldsFieldDetail<ChongqingRT2021NumLineBadgeAttributes>}
106-
/>
107-
);
91+
{
92+
type: 'custom',
93+
label: t('color'),
94+
component: (
95+
<ColorField
96+
type={MiscNodeType.ChongqingRT2021NumLineBadge}
97+
defaultTheme={defaultChongqingRT2021NumLineBadgeAttributes.color}
98+
/>
99+
),
100+
minW: 'full',
101+
},
102+
];
103+
return <RmgFields fields={fields} />;
104+
};
108105

109106
const chongqingRT2021NumLineBadgeIcon = (
110107
<svg viewBox="0 0 21 21" height={40} width={40} focusable={false} style={{ padding: 3 }}>
@@ -120,7 +117,7 @@ const chongqingRT2021NumLineBadge: Node<ChongqingRT2021NumLineBadgeAttributes> =
120117
component: ChongqingRT2021NumLineBadge,
121118
icon: chongqingRT2021NumLineBadgeIcon,
122119
defaultAttrs: defaultChongqingRT2021NumLineBadgeAttributes,
123-
attrsComponent,
120+
attrsComponent: ChongqingRT2021NumLineBadgeAttrsComponent,
124121
metadata: {
125122
displayName: 'panel.details.nodes.chongqingRT2021NumLineBadge.displayName',
126123
tags: [],

src/components/svgs/nodes/chongqingrt2021-text-line-badge.tsx

+51-63
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { MonoColour } from '@railmapgen/rmg-palette-resources';
22
import React from 'react';
3-
import { CityCode } from '../../../constants/constants';
3+
import { useTranslation } from 'react-i18next';
4+
import { RmgFields, RmgFieldsField } from '@railmapgen/rmg-components';
5+
import { AttrsProps, CityCode } from '../../../constants/constants';
46
import { MiscNodeType, Node, NodeComponentProps } from '../../../constants/nodes';
57
import { AttributesWithColor, ColorField } from '../../panels/details/color-field';
6-
import { RmgFieldsFieldDetail, RmgFieldsFieldSpecificAttributes } from '../../panels/details/rmg-field-specific-attrs';
78
import { MultilineText } from '../common/multiline-text';
89

910
const ChongqingRT2021TextLineBadge = (props: NodeComponentProps<ChongqingRT2021TextLineBadgeAttributes>) => {
@@ -82,7 +83,7 @@ const ChongqingRT2021TextLineBadge = (props: NodeComponentProps<ChongqingRT2021T
8283
};
8384

8485
/**
85-
* BerlinUBahnLineBadge specific props.
86+
* ChongqingRT2021NumLineBadge specific props.
8687
*/
8788
export interface ChongqingRT2021TextLineBadgeAttributes extends AttributesWithColor {
8889
names: [string, string];
@@ -95,68 +96,55 @@ const defaultChongqingRT2021TextLineBadgeAttributes: ChongqingRT2021TextLineBadg
9596
isRapid: false,
9697
};
9798

98-
const chongqingRT2021TextLineBadgeFields = [
99-
{
100-
type: 'input',
101-
label: 'panel.details.nodes.common.nameZh',
102-
value: (attrs?: ChongqingRT2021TextLineBadgeAttributes) =>
103-
(attrs ?? defaultChongqingRT2021TextLineBadgeAttributes).names[0],
104-
onChange: (val: string | number, attrs_: ChongqingRT2021TextLineBadgeAttributes | undefined) => {
105-
// set default value if switched from another type
106-
const attrs = attrs_ ?? defaultChongqingRT2021TextLineBadgeAttributes;
107-
// set value
108-
attrs.names[0] = val.toString();
109-
// return modified attrs
110-
return attrs;
99+
const ChongqingRT2021NumLineBadgeAttrsComponent = (props: AttrsProps<ChongqingRT2021TextLineBadgeAttributes>) => {
100+
const { id, attrs, handleAttrsUpdate } = props;
101+
const { t } = useTranslation();
102+
const fields: RmgFieldsField[] = [
103+
{
104+
type: 'input',
105+
label: t('panel.details.nodes.common.nameZh'),
106+
value: (attrs ?? defaultChongqingRT2021TextLineBadgeAttributes).names[0],
107+
onChange: (val: string | number) => {
108+
attrs.names[0] = val.toString();
109+
handleAttrsUpdate(id, attrs);
110+
},
111+
minW: 'full',
111112
},
112-
},
113-
{
114-
type: 'textarea',
115-
label: 'panel.details.nodes.common.nameEn',
116-
value: (attrs?: ChongqingRT2021TextLineBadgeAttributes) =>
117-
(attrs ?? defaultChongqingRT2021TextLineBadgeAttributes).names[1],
118-
onChange: (val: string | number, attrs_: ChongqingRT2021TextLineBadgeAttributes | undefined) => {
119-
// set default value if switched from another type
120-
const attrs = attrs_ ?? defaultChongqingRT2021TextLineBadgeAttributes;
121-
// return if invalid
122-
// set value
123-
attrs.names[1] = val.toString();
124-
// return modified attrs
125-
return attrs;
113+
{
114+
type: 'textarea',
115+
label: t('panel.details.nodes.common.nameEn'),
116+
value: (attrs ?? defaultChongqingRT2021TextLineBadgeAttributes).names[1],
117+
onChange: (val: string | number) => {
118+
attrs.names[1] = val.toString();
119+
handleAttrsUpdate(id, attrs);
120+
},
121+
minW: 'full',
126122
},
127-
},
128-
{
129-
type: 'custom',
130-
label: 'color',
131-
component: (
132-
<ColorField
133-
type={MiscNodeType.ChongqingRT2021TextLineBadge}
134-
defaultTheme={defaultChongqingRT2021TextLineBadgeAttributes.color}
135-
/>
136-
),
137-
},
138-
{
139-
type: 'switch',
140-
label: 'panel.details.nodes.chongqingRT2021TextLineBadge.isRapid',
141-
value: (attrs?: ChongqingRT2021TextLineBadgeAttributes) =>
142-
(attrs ?? defaultChongqingRT2021TextLineBadgeAttributes).isRapid,
143-
onChange: (val: boolean, attrs_: ChongqingRT2021TextLineBadgeAttributes | undefined) => {
144-
// set default value if switched from another type
145-
const attrs = attrs_ ?? defaultChongqingRT2021TextLineBadgeAttributes;
146-
// set value
147-
attrs.isRapid = val;
148-
// return modified attrs
149-
return attrs;
123+
{
124+
type: 'custom',
125+
label: t('color'),
126+
component: (
127+
<ColorField
128+
type={MiscNodeType.ChongqingRT2021TextLineBadge}
129+
defaultTheme={defaultChongqingRT2021TextLineBadgeAttributes.color}
130+
/>
131+
),
132+
minW: 'full',
150133
},
151-
oneLine: true,
152-
},
153-
];
154-
155-
const attrsComponent = () => (
156-
<RmgFieldsFieldSpecificAttributes
157-
fields={chongqingRT2021TextLineBadgeFields as RmgFieldsFieldDetail<ChongqingRT2021TextLineBadgeAttributes>}
158-
/>
159-
);
134+
{
135+
type: 'switch',
136+
label: t('panel.details.nodes.chongqingRT2021TextLineBadge.isRapid'),
137+
isChecked: (attrs ?? defaultChongqingRT2021TextLineBadgeAttributes).isRapid,
138+
onChange: (val: boolean) => {
139+
attrs.isRapid = val;
140+
handleAttrsUpdate(id, attrs);
141+
},
142+
oneLine: true,
143+
minW: 'full',
144+
},
145+
];
146+
return <RmgFields fields={fields} />;
147+
};
160148

161149
const chongqingRT2021TextLineBadgeIcon = (
162150
<svg viewBox="0 0 21 21" height={40} width={40} focusable={false} style={{ padding: 3 }}>
@@ -183,7 +171,7 @@ const chongqingRT2021TextLineBadge: Node<ChongqingRT2021TextLineBadgeAttributes>
183171
component: ChongqingRT2021TextLineBadge,
184172
icon: chongqingRT2021TextLineBadgeIcon,
185173
defaultAttrs: defaultChongqingRT2021TextLineBadgeAttributes,
186-
attrsComponent,
174+
attrsComponent: ChongqingRT2021NumLineBadgeAttrsComponent,
187175
metadata: {
188176
displayName: 'panel.details.nodes.chongqingRT2021TextLineBadge.displayName',
189177
tags: [],

src/components/svgs/stations/chongqingrt2021-basic.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const ChongqingRT2021BasicStation = (props: StationComponentProps) => {
7070

7171
const [textX, textY] = getTextOffset(nameOffsetX, nameOffsetY);
7272
const textAnchor = nameOffsetX === 'left' ? 'end' : nameOffsetX === 'right' ? 'start' : 'middle';
73+
const isTextLine = new RegExp('[\\u4E00-\\u9FFF]+', 'g').test(lineCode);
7374

7475
return (
7576
<g id={id} transform={`translate(${x}, ${y})`}>
@@ -90,16 +91,16 @@ const ChongqingRT2021BasicStation = (props: StationComponentProps) => {
9091
style={{ cursor: 'move' }}
9192
/>
9293
<text
93-
fontSize={new RegExp('[\\u4E00-\\u9FFF]+', 'g').test(lineCode) ? 5 : 7}
94+
fontSize={isTextLine ? 5 : 7}
9495
textAnchor="middle"
9596
x={0}
96-
y={new RegExp('[\\u4E00-\\u9FFF]+', 'g').test(lineCode) ? -1.5 : -1}
97+
y={isTextLine ? -1.5 : -1}
9798
style={{ pointerEvents: 'none' }}
9899
>
99100
{lineCode}
100101
</text>
101102
<text fontSize={7} textAnchor="middle" x={0} y={6} style={{ pointerEvents: 'none' }}>
102-
{Number(stationCode) < 10 ? `0${Number(stationCode)}` : stationCode}
103+
{Number.isInteger(stationCode) && Number(stationCode) < 10 ? `0${Number(stationCode)}` : stationCode}
103104
</text>
104105
<line
105106
x1={-5.5}

src/components/svgs/stations/chongqingrt2021-int.tsx

+12-15
Original file line numberDiff line numberDiff line change
@@ -330,21 +330,18 @@ const ChongqingRT2021IntAttrsComponent = (props: AttrsProps<ChongqingRT2021IntSt
330330
},
331331
minW: 'full',
332332
},
333-
...((attrs.isRapid
334-
? [
335-
{
336-
type: 'custom',
337-
label: t('color'),
338-
component: (
339-
<ColorField
340-
type={StationType.ChongqingRT2021Int}
341-
defaultTheme={defaultChongqingRT2021IntStationAttributes.rapidColor}
342-
colorKey="rapidColor"
343-
/>
344-
),
345-
},
346-
]
347-
: []) as RmgFieldsField[]),
333+
{
334+
type: 'custom',
335+
label: t('color'),
336+
component: (
337+
<ColorField
338+
type={StationType.ChongqingRT2021Int}
339+
defaultTheme={defaultChongqingRT2021IntStationAttributes.rapidColor}
340+
colorKey="rapidColor"
341+
/>
342+
),
343+
hidden: !attrs.isRapid,
344+
},
348345
{
349346
type: 'switch',
350347
label: t('panel.details.stations.chongqingRT2021Int.isWide'),

src/util/save.test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -571,15 +571,16 @@ describe('Unit tests for param upgrade function', () => {
571571
});
572572
it('44 -> 45', () => {
573573
// Bump save version to support Chongqing Rail Transit stations.
574+
// Add isLoop attributes to Chongqing Rail Transit Basic stations.
574575
// Prepare an empty save.
575576
const oldParam =
576-
'{"graph":{"options":{"type":"directed","multi":true,"allowSelfLoops":true},"attributes":{},"nodes":[],"edges":[]},"svgViewBoxZoom":100,"svgViewBoxMin":{"x":0,"y":0},"version":44}';
577+
'{"graph":{"options":{"type":"directed","multi":true,"allowSelfLoops":true},"attributes":{},"nodes":[{"key":"stn_6oAhcug7aq","attributes":{"visible":true,"zIndex":0,"x":-1161.8544148936169,"y":648.2702426050851,"type":"chongqingrt-basic","chongqingrt-basic":{"names":["铁山坪","Tieshanping"],"color":["chongqing","cq4","#DC8633","#fff"],"nameOffsetX":"middle","nameOffsetY":"top","textVertical":false}}}],"edges":[]},"svgViewBoxZoom":100,"svgViewBoxMin":{"x":0,"y":0},"version":44}';
577578
// Upgrade it with your newly added function.
578579
const newParam = UPGRADE_COLLECTION[44](oldParam);
579580
const graph = new MultiDirectedGraph() as MultiDirectedGraph<NodeAttributes, EdgeAttributes, GraphAttributes>;
580581
expect(() => graph.import(JSON.parse(newParam))).not.toThrow();
581582
const expectParam =
582-
'{"graph":{"options":{"type":"directed","multi":true,"allowSelfLoops":true},"attributes":{},"nodes":[],"edges":[]},"svgViewBoxZoom":100,"svgViewBoxMin":{"x":0,"y":0},"version":45}';
583+
'{"graph":{"options":{"type":"directed","multi":true,"allowSelfLoops":true},"attributes":{},"nodes":[{"key":"stn_6oAhcug7aq","attributes":{"visible":true,"zIndex":0,"x":-1161.8544148936169,"y":648.2702426050851,"type":"chongqingrt-basic","chongqingrt-basic":{"names":["铁山坪","Tieshanping"],"color":["chongqing","cq4","#DC8633","#fff"],"nameOffsetX":"middle","nameOffsetY":"top","textVertical":false,"isLoop":false}}}],"edges":[]},"svgViewBoxZoom":100,"svgViewBoxMin":{"x":0,"y":0},"version":45}';
583584
// And the updated save has only version field changed.
584585
expect(newParam).toEqual(expectParam);
585586
});

src/util/save.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,20 @@ export const UPGRADE_COLLECTION: { [version: number]: (param: string) => string
585585
43: param =>
586586
// Bump save version to support Chongqing Rail Transit stations.
587587
JSON.stringify({ ...JSON.parse(param), version: 44 }),
588-
44: param =>
588+
44: param => {
589589
// Bump save version to support Chongqing Rail Transit 2021 stations and facilities.
590-
JSON.stringify({ ...JSON.parse(param), version: 45 }),
590+
// Add isLoop attributes to Chongqing Rail Transit Basic stations.
591+
const p = JSON.parse(param);
592+
const graph = new MultiDirectedGraph() as MultiDirectedGraph<NodeAttributes, EdgeAttributes, GraphAttributes>;
593+
graph.import(p?.graph);
594+
graph
595+
.filterNodes((node, attr) => node.startsWith('stn') && attr.type === StationType.ChongqingRTBasic)
596+
.forEach(node => {
597+
const type = graph.getNodeAttribute(node, 'type');
598+
const attr = graph.getNodeAttribute(node, type) as any;
599+
attr.isLoop = false;
600+
graph.mergeNodeAttributes(node, { [type]: attr });
601+
});
602+
return JSON.stringify({ ...p, version: 45, graph: graph.export() });
603+
},
591604
};

0 commit comments

Comments
 (0)