Skip to content

Commit 1fc22a4

Browse files
authored
fix: support quick set tid in SettingForm (#151)
* fix: support quick set tid * fix: shape return type of parseDndId
1 parent ffaa276 commit 1fc22a4

File tree

8 files changed

+58
-32
lines changed

8 files changed

+58
-32
lines changed

apps/storybook/src/setting-form.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function SettingFormDemo({ initValues, prototype }: SettingFormDemoProps) {
4444
prototype={prototype}
4545
showIdentifier={{
4646
identifierKey: 'tid',
47+
getIdentifier: () => `time${Date.now()}`,
4748
}}
4849
/>
4950
</Box>

packages/designer/src/helpers/dom.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export function getElementData(
7575
const display = getElementCSSDisplay(element);
7676
return {
7777
id: dnd.id,
78-
codeId: dnd.index,
78+
codeId: dnd.codeId,
7979
name: dnd.component || element.tagName.toLowerCase(),
8080
filename: dnd.filename,
8181
bounding,

packages/designer/src/setting-panel.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import { Box } from 'coral-system';
33
import { SettingForm, FormModel, SettingFormProps } from '@music163/tango-setting-form';
44
import { Panel } from '@music163/tango-ui';
5-
import { clone } from '@music163/tango-helpers';
5+
import { clone, parseDndId } from '@music163/tango-helpers';
66
import { observer, useDesigner, useWorkspace } from '@music163/tango-context';
77
import { registerBuiltinSetters } from './setters';
88

@@ -103,6 +103,11 @@ export const SettingPanel = observer((props: SettingPanelProps) => {
103103
prototype={prototype}
104104
showIdentifier={{
105105
identifierKey: 'tid',
106+
getIdentifier: () => {
107+
// 直接拿当前的虚拟 id 作为 tid
108+
const { codeId } = parseDndId(workspace.selectSource.first.id);
109+
return codeId;
110+
},
106111
}}
107112
{...props}
108113
/>

packages/designer/src/sidebar/outline-panel/components-tree.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ const OutlineTreeNode: React.FC<{ node: ITangoViewNodeData } & ComponentsTreePro
8181
const sandboxQuery = useSandboxQuery();
8282
const [visible, setVisible] = useState(true);
8383
const nodeLabel = (() => {
84-
const { index } = parseDndId(node.id);
85-
return index;
84+
const { codeId } = parseDndId(node.id);
85+
return codeId;
8686
})();
8787
const componentPrototype = workspace.componentPrototypes.get(node.component);
8888
const icon = componentPrototype?.icon || 'icon-placeholder';

packages/helpers/src/helpers/string.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,13 @@ export function parseDndTrackId(str: string) {
7575

7676
interface DndIdParsedType {
7777
/**
78-
* 完整的 ID
78+
* full id
7979
*/
8080
id?: string;
81+
/**
82+
* 组件代码中的 id,一般对应组件的 tid 属性
83+
*/
84+
codeId?: string;
8185
/**
8286
* 组件名
8387
*/
@@ -86,19 +90,11 @@ interface DndIdParsedType {
8690
* 文件名
8791
*/
8892
filename?: string;
89-
/**
90-
* @deprecated 使用 filename 代替
91-
*/
92-
module?: string;
93-
/**
94-
* 序号
95-
*/
96-
index?: string;
9793
}
9894

9995
/**
10096
* 解析 dnd id
101-
* @example Button:123 -> { component: "Button", id: "Button:123" }
97+
* @example Button:button123 -> { component: "Button", id: "Button:123" }
10298
* @example LocalBlock:Button:123 -> { filename: LocalBlock, component: "Button", id: "Button:123" }
10399
* @param str
104100
*/
@@ -109,19 +105,20 @@ export function parseDndId(str: string): DndIdParsedType {
109105

110106
const parts = str.split(':');
111107
if (parts.length === 2) {
108+
// e.g. Button:button123
112109
return {
113-
component: parts[0],
114-
index: parts[1],
115110
id: str,
111+
component: parts[0],
112+
codeId: parts[1],
116113
};
117114
} else if (parts.length >= 3) {
115+
// e.g. LocalBlock:Button:button123
118116
const filename = decodeURIComponent(parts[0]);
119117
return {
120-
module: filename,
118+
id: str,
121119
filename,
122120
component: parts[1],
123-
index: parts[2],
124-
id: str,
121+
codeId: parts[2],
125122
};
126123
}
127124
return {

packages/helpers/tests/helpers.test.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,21 @@ describe('string', () => {
5858

5959
it('parseDndId', () => {
6060
expect(parseDndId('123')).toEqual({ id: '123' });
61-
expect(parseDndId('Button:123')).toEqual({
61+
expect(parseDndId('Button:button123')).toEqual({
6262
component: 'Button',
63-
id: 'Button:123',
64-
index: '123',
63+
id: 'Button:button123',
64+
codeId: 'button123',
6565
});
66-
expect(parseDndId('Button.Group:123')).toEqual({
66+
expect(parseDndId('Button.Group:buttonGroup123')).toEqual({
6767
component: 'Button.Group',
68-
id: 'Button.Group:123',
69-
index: '123',
68+
id: 'Button.Group:buttonGroup123',
69+
codeId: 'buttonGroup123',
7070
});
71-
expect(parseDndId('LocalBlock:Button.Group:123')).toEqual({
71+
expect(parseDndId('LocalBlock:Button.Group:buttonGroup123')).toEqual({
72+
id: 'LocalBlock:Button.Group:buttonGroup123',
7273
filename: 'LocalBlock',
73-
module: 'LocalBlock',
7474
component: 'Button.Group',
75-
id: 'LocalBlock:Button.Group:123',
76-
index: '123',
75+
codeId: 'buttonGroup123',
7776
});
7877
});
7978

packages/setting-form/src/form.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ export interface SettingFormProps {
104104
showIdentifier?:
105105
| false
106106
| {
107-
identifierKey: string;
107+
identifierKey: string; // 唯一标识符属性key
108+
getIdentifier?: () => string; // 获取唯一标识符的方法
108109
};
109110
/**
110111
* 是否显示搜索框
@@ -222,6 +223,9 @@ export function SettingForm({
222223
<SettingFormItem
223224
noStyle
224225
setter="idSetter"
226+
setterProps={{
227+
getId: showIdentifier.getIdentifier,
228+
}}
225229
name={showIdentifier.identifierKey}
226230
/>
227231
}

packages/setting-form/src/setters/id-setter.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import React, { useEffect, useState } from 'react';
22
import { Input, InputProps, Tooltip } from 'antd';
33
import { css, Box } from 'coral-system';
44
import { FormItemComponentProps } from '../form-item';
5-
import { ExclamationCircleOutlined } from '@ant-design/icons';
5+
import { ExclamationCircleOutlined, ThunderboltOutlined } from '@ant-design/icons';
6+
import { Action } from '@music163/tango-ui';
67

78
const idInputStyle = css`
89
> .ant-input-borderless {
@@ -22,6 +23,7 @@ export function IdSetter({
2223
defaultValue,
2324
onChange,
2425
placeholder = '输入唯一组件ID',
26+
getId,
2527
...rest
2628
}: FormItemComponentProps<string>) {
2729
const [value, setValue] = useState(valueProp || defaultValue);
@@ -60,20 +62,38 @@ export function IdSetter({
6062
},
6163
};
6264

65+
const showQuickIdButton = !value || error; // 没有设置 id 或 id 不合法的时候显示快捷生成按钮
66+
6367
return (
6468
<Box css={idInputStyle}>
6569
<Input
6670
placeholder={placeholder}
6771
value={value}
6872
{...__props}
6973
{...rest}
70-
suffix={
74+
prefix={
7175
error ? (
7276
<Tooltip title={error} color="#ff4d4f">
7377
<ExclamationCircleOutlined style={{ color: 'red' }} />
7478
</Tooltip>
7579
) : null
7680
}
81+
suffix={
82+
showQuickIdButton ? (
83+
<Action
84+
size="small"
85+
icon={<ThunderboltOutlined />}
86+
tooltip="快捷设置组件ID"
87+
onClick={() => {
88+
const id = getId?.();
89+
if (id) {
90+
setValue(id);
91+
onChange?.(id);
92+
}
93+
}}
94+
/>
95+
) : null
96+
}
7797
/>
7898
</Box>
7999
);

0 commit comments

Comments
 (0)