Skip to content

Commit

Permalink
fix: parse tid in code
Browse files Browse the repository at this point in the history
  • Loading branch information
wwsun committed Mar 5, 2024
1 parent 7470190 commit 91f0553
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 2,642 deletions.
8 changes: 4 additions & 4 deletions apps/playground/src/helpers/mock-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ class App extends React.Component {
render() {
return (
<Page title={tango.stores.app.title}>
<Section title="Section Title">
<Section tid="section1" title="Section Title">
</Section>
<Section>
<Space>
<Section tid="section2">
<Space tid="space1">
<LocalButton />
<Button>button</Button>
<Button tid="button1">button</Button>
<Input />
</Space>
</Section>
Expand Down
12 changes: 9 additions & 3 deletions packages/core/src/helpers/ast/traverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1279,9 +1279,11 @@ export function traverseViewFile(ast: t.File, idGenerator: IdGenerator) {
const attributes = getJSXElementAttributes(path.node);

// 获取组件的追踪属性
const trackId = attributes[SLOT.dnd];
const trackDnd = attributes[SLOT.dnd];
// 用户代码中的 id 标记
const codeId = attributes.tid;

let { component, id } = parseDndId(trackId);
let { component, id } = parseDndId(trackDnd);
component = component || getJSXElementName(path.node);
idGenerator.setItem(component);

Expand All @@ -1290,7 +1292,7 @@ export function traverseViewFile(ast: t.File, idGenerator: IdGenerator) {
}

// 如果没有 ID,生成组件的追踪 ID
if (!trackId) {
if (!trackDnd) {
id = idGenerator.generateId(component);
}

Expand All @@ -1301,16 +1303,20 @@ export function traverseViewFile(ast: t.File, idGenerator: IdGenerator) {

// parentId 用于追溯上下游关系
let parentId;
let parentCodeId;
const parentNode = path.findParent((p) => p.isJSXElement());

if (t.isJSXElement(parentNode?.node)) {
const parentAttributes = getJSXElementAttributes(parentNode.node);
parentId = parentAttributes[SLOT.dnd];
parentCodeId = parentAttributes.tid;
}

nodes.push({
id,
codeId,
parentId,
parentCodeId,
component,
rawNode: path.node,
});
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,18 @@ export interface ITangoViewNodeData<T = JSXElement> {
* 节点 ID
*/
id: string;
/**
* 代码中的 ID
*/
codeId?: string;
/**
* 父亲节点的 ID
*/
parentId: string;
/**
* 父节点中的 tid
*/
parentCodeId?: string;
/**
* 组件名
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ const OutlineTreeNode: React.FC<{ node: ITangoViewNodeData } & ComponentsTreePro
const workspace = useWorkspace();
const sandboxQuery = useSandboxQuery();
const [visible, setVisible] = useState(true);
const nodeLabel = useMemo(() => {
const { component, index } = parseDndId(node.id);
return `${component}:${index}`;
}, [node.id]);
const nodeLabel = (() => {
const { component } = parseDndId(node.id);
return `${component}${node.codeId ? `(${node.codeId})` : ''}`;
})();
const componentPrototype = workspace.componentPrototypes.get(node.component);
const icon = componentPrototype?.icon || 'icon-placeholder';

Expand Down
6 changes: 5 additions & 1 deletion packages/helpers/src/helpers/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export const SLOT = {
/**
* DND 追踪标记
* DND 追踪标记,通常值为 {{pagePath}}:{{componentName}}
*/
dnd: 'data-dnd',
/**
* ID 追踪标记
*/
id: 'data-id',
};
16 changes: 13 additions & 3 deletions packages/helpers/src/hoc/with-dnd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ interface IWithDndOptions<T> {
renderFooter?: (props: T) => React.ReactNode;
}

interface DraggableComponentProps {
/**
* 组件 ID
*/
tid?: string;
}

const renderEmpty = (): React.ReactNode => null;

export function withDnd(options: IWithDndOptions<any>) {
Expand All @@ -50,14 +57,17 @@ export function withDnd(options: IWithDndOptions<any>) {
const override = options.overrideProps || {};
const renderFooter = options.renderFooter || renderEmpty;

const Component = forwardRef<unknown, P>((props, refProp) => {
const dndId = props[SLOT.dnd];
const Component = forwardRef<unknown, P & DraggableComponentProps>((props, refProp) => {
const dndProps = {
[SLOT.dnd]: dndId,
[SLOT.id]: props.tid, // id 作为唯一标记
[SLOT.dnd]: props[SLOT.dnd], // dnd 作为追踪标记
draggable: draggable ? true : undefined,
};

const propsCopy = { ...props };
delete propsCopy[SLOT.dnd];
delete propsCopy.tid;

const ref = isFC ? undefined : refProp;

if (!hasWrapper) {
Expand Down
Loading

0 comments on commit 91f0553

Please sign in to comment.