Skip to content

Commit a347acc

Browse files
split dasgboard card dnd management
1 parent aabd612 commit a347acc

4 files changed

Lines changed: 103 additions & 67 deletions

File tree

editor-packages/editor-dashboard/components/dashboard-item-card-scene.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,21 @@ import {
88
DashboardItemCardProps,
99
} from "./dashboard-item-card";
1010

11+
type SceneMeta = {
12+
id: string;
13+
filekey: string;
14+
name: string;
15+
width: number;
16+
height: number;
17+
type: any;
18+
};
19+
1120
function SceneCardPreview({
1221
maxWidth,
1322
scene,
1423
}: {
1524
maxWidth: number;
16-
scene: ReflectSceneNode;
25+
scene: SceneMeta;
1726
}) {
1827
const visibilityRef = useRef();
1928

@@ -57,7 +66,7 @@ function SceneCardPreview({
5766

5867
export interface SceneCardProps
5968
extends Omit<DashboardItemCardProps, "icon" | "preview" | "label"> {
60-
scene: ReflectSceneNode;
69+
scene: SceneMeta;
6170
}
6271

6372
export const SceneCard = React.forwardRef(function (

editor-packages/editor-dashboard/core/initial.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export function initialHierarchy(
5858
type: "COMPONENT",
5959
path: "components" + "/" + c.id,
6060
$type: "component" as const,
61+
scene: c as any,
6162
}));
6263

6364
return {

editor-packages/editor-dashboard/core/state.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ReflectSceneNode } from "@design-sdk/figma-node";
2+
import type { ComponentNode } from "@design-sdk/figma";
23

34
export interface DashboardState {
45
selection: Array<string>;
@@ -51,7 +52,8 @@ export type GroupedSceneItem = {
5152
path: string;
5253
name: string;
5354
type: "FRAME";
54-
scenes: Array<FrameSceneItem>;
55+
scene: ReflectSceneNode;
56+
alias: Array<FrameSceneItem>;
5557
};
5658

5759
export type ComponentItem = {
@@ -60,6 +62,7 @@ export type ComponentItem = {
6062
name: string;
6163
path: string;
6264
type: "COMPONENT";
65+
scene: ComponentNode & { filekey: string };
6366
width: number;
6467
height: number;
6568
};

editor-packages/editor-dashboard/scaffold/editor-dashboard.tsx

Lines changed: 87 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React, { useCallback } from "react";
22
import styled from "@emotion/styled";
33
import {
4-
FolderCard,
5-
SceneCard,
4+
FolderCard as _FolderCard,
5+
SceneCard as _SceneCard,
66
SectionHeaderAction,
77
SectionHeader,
88
DashboardItemCardProps,
@@ -15,7 +15,12 @@ import {
1515
MenuItem,
1616
} from "@editor-ui/context-menu";
1717
import * as Collapsible from "@radix-ui/react-collapsible";
18-
import { useDashboard, DashboardItem } from "../core";
18+
import {
19+
useDashboard,
20+
DashboardItem,
21+
DashboardFolderItem,
22+
SceneItem,
23+
} from "../core";
1924

2025
export function Dashboard() {
2126
const {
@@ -107,12 +112,6 @@ export function Dashboard() {
107112
);
108113
}
109114

110-
interface SceneCardMeta {
111-
id: string;
112-
name: string;
113-
$type: unknown;
114-
}
115-
116115
function RootDirectory({
117116
label,
118117
contents,
@@ -222,16 +221,31 @@ type DndMetaItem<T = object> = T & {
222221
function DashboardItemCard(
223222
props: DashboardItem &
224223
Omit<DashboardItemCardProps, "label" | "preview" | "icon">
224+
) {
225+
switch (props.$type) {
226+
case "frame-scene":
227+
case "component": {
228+
return <SceneCard {...props} />;
229+
}
230+
case "folder": {
231+
return <FolderCard {...props} />;
232+
}
233+
default: {
234+
throw new Error(`Unknown item type ${props.$type}`);
235+
}
236+
}
237+
}
238+
239+
function SceneCard(
240+
props: SceneItem & Omit<DashboardItemCardProps, "label" | "preview" | "icon">
225241
) {
226242
const [{ isActive }, drop] = useDrop(() => ({
227-
accept: "frame-scene", // todo
243+
accept: "scene",
228244
collect: (monitor) => ({
229245
isActive: monitor.canDrop() && monitor.isOver(),
230246
}),
231247
canDrop(item: DndMetaItem, monitor) {
232-
if (item.$type === props.$type) {
233-
return item.id !== props.id;
234-
}
248+
return item.id !== props.id;
235249
},
236250
drop(item, monitor) {
237251
console.log("drop", item, monitor);
@@ -240,22 +254,58 @@ function DashboardItemCard(
240254
}));
241255

242256
const [{ opacity }, drag] = useDrag(() => {
243-
let item: unknown = props;
244-
switch (props.$type) {
245-
case "frame-scene": {
246-
item = props.scene;
247-
Object.assign(item, { $type: props.$type });
248-
break;
249-
}
250-
case "folder": {
251-
item = props;
252-
break;
253-
}
254-
}
257+
return {
258+
type: "scene",
259+
item: props.scene,
260+
collect: (monitor) => ({
261+
opacity: monitor.isDragging() ? 0.5 : 1,
262+
}),
263+
};
264+
}, []);
265+
266+
function attachRef(el) {
267+
drag(el);
268+
drop(el);
269+
}
270+
271+
const defaultprops = {
272+
isOver: isActive,
273+
style: { opacity },
274+
};
275+
276+
return (
277+
<_SceneCard
278+
// @ts-ignore
279+
scene={props.scene as any}
280+
ref={attachRef}
281+
{...defaultprops}
282+
{...props}
283+
/>
284+
);
285+
}
255286

287+
function FolderCard(
288+
props: DashboardFolderItem &
289+
Omit<DashboardItemCardProps, "label" | "preview" | "icon">
290+
) {
291+
const [{ isActive }, drop] = useDrop(() => ({
292+
accept: ["scene", "folder"],
293+
collect: (monitor) => ({
294+
isActive: monitor.canDrop() && monitor.isOver(),
295+
}),
296+
canDrop(item: DndMetaItem, monitor) {
297+
return item.id !== props.id;
298+
},
299+
drop(item, monitor) {
300+
console.log("drop", item, monitor);
301+
// todo:
302+
},
303+
}));
304+
305+
const [{ opacity }, drag] = useDrag(() => {
256306
return {
257307
type: props.$type,
258-
item: item,
308+
item: props,
259309
collect: (monitor) => ({
260310
opacity: monitor.isDragging() ? 0.5 : 1,
261311
}),
@@ -272,44 +322,17 @@ function DashboardItemCard(
272322
style: { opacity },
273323
};
274324

275-
switch (props.$type) {
276-
case "frame-scene": {
277-
return (
278-
<SceneCard
279-
scene={props.scene}
280-
ref={attachRef}
281-
{...defaultprops}
282-
{...props}
283-
/>
284-
);
285-
}
286-
case "component": {
287-
return (
288-
<SceneCard
289-
scene={props as any} // todo
290-
ref={attachRef}
291-
{...defaultprops}
292-
{...props}
293-
/>
294-
);
295-
}
296-
case "folder": {
297-
return (
298-
<FolderCard
299-
ref={attachRef}
300-
id={props.id}
301-
path={props.path}
302-
name={props.name}
303-
contents={props.contents}
304-
{...defaultprops}
305-
{...props}
306-
/>
307-
);
308-
}
309-
default: {
310-
throw new Error(`Unknown item type ${props.$type}`);
311-
}
312-
}
325+
return (
326+
<_FolderCard
327+
ref={attachRef}
328+
id={props.id}
329+
path={props.path}
330+
name={props.name}
331+
contents={props.contents}
332+
{...defaultprops}
333+
{...props}
334+
/>
335+
);
313336
}
314337

315338
const SceneGrid = styled.div`

0 commit comments

Comments
 (0)