Skip to content

Commit

Permalink
#40 Select for specific value (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
langonginc authored Nov 17, 2024
1 parent 1210986 commit 5b5dbbf
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
44 changes: 44 additions & 0 deletions src/components/panel/details-svgs-select.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
interface DetailsSvgsValue {
type: string;
key: string;
defaultValue: string;
options: Record<string | number, string>;
}

const detailsSvgsValue: DetailsSvgsValue[] = [
{
type: 'text',
key: 'className',
defaultValue: 'rmp-name__en',
options: {
'rmp-name__en': 'English',
'rmp-name__zh': 'Chinese',
'rmp-name__mtr__en': 'MTR English',
'rmp-name__mtr__zh': 'MTR Chinese',
'rmp-name__berlin': 'Berlin S/U Bahn',
'rmp-name__mrt': 'Singapore MRT',
'rmp-name__jreast_ja': 'JR East Japanese',
'rmp-name__jreast_en': 'JR East English',
'rmp-name__tokyo_en': 'Tokyo English',
'rmp-name__tube': 'London Tube',
},
},
{
type: 'text',
key: 'textAnchor',
defaultValue: 'start',
options: {
start: 'start',
middle: 'middle',
end: 'end',
},
},
];

export const getValueSelect = (type: string, key: string): DetailsSvgsValue | undefined =>
detailsSvgsValue.find(s => s.type === type && s.key === key);

export const getValueSelectValue = (elem: DetailsSvgsValue, value?: string): string => {
const e = Object.entries(elem.options).find(([key]) => key === value);
return e === undefined ? elem.defaultValue : e[0];
};
18 changes: 17 additions & 1 deletion src/components/panel/details-svgs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { supportsChildren } from '../../util/svgTagWithChildren';
import { MoveChildrenModal } from './details-svgs-move-children';
import svgs from '../svgs/module/svgs';
import { SvgsType } from '../../constants/svgs';
import { getValueSelect, getValueSelectValue } from './details-svgs-select';

interface AttrVarList {
id: string;
Expand Down Expand Up @@ -189,6 +190,9 @@ export function DetailsSvgs() {
];
const attrsField = Object.entries(s.attrs).map(([key, value]) => {
const mode = value.startsWith('1') ? 'value' : value.startsWith('2') ? 'var' : 'advanced';
const valueSelect = getValueSelect(s.type, key);
const valueSelectVal = valueSelect ? getValueSelectValue(valueSelect, value.slice(2, -1)) : '';
const valueSelectOptions = valueSelect ? valueSelect.options : {};
const handleChangeMode = (mode: AttrVarMode, value: string) => {
if (mode === 'value') {
return `1"${value.slice(1)}"`;
Expand Down Expand Up @@ -234,7 +238,19 @@ export function DetailsSvgs() {
...path,
i,
]),
hidden: mode === 'var',
hidden: mode === 'var' || (mode === 'value' && !!valueSelect),
},
{
label: t('panel.svgs.attrValue'),
type: 'select',
value: valueSelectVal,
options: valueSelectOptions,
onChange: v =>
handleSetValue(s.id, 'attrs', { ...s.attrs, [key]: handleChangeMode(mode, '_' + v) }, [
...path,
i,
]),
hidden: mode !== 'value' || !valueSelect,
},
{
label: t('panel.svgs.attrValue'),
Expand Down

0 comments on commit 5b5dbbf

Please sign in to comment.