@@ -53,15 +53,27 @@ import { useUndoableDispatch } from "@/hooks/useUndoableDispatch";
5353import { Layout } from "@/layout" ;
5454import { type RootState } from "@/store" ;
5555
56- export const HAUL_TYPE = "arc-element " ;
56+ export const HAUL_TYPE = "arc_element " ;
5757
58- const StageRenderer = ( {
58+ export type HaulItem = Haul . Item < typeof HAUL_TYPE , string , undefined > ;
59+
60+ export const createHaulItem = ( key : string ) : HaulItem => ( { type : HAUL_TYPE , key } ) ;
61+
62+ export const isHaulItem = ( item : Haul . Item ) : item is HaulItem =>
63+ item . type === HAUL_TYPE ;
64+
65+ export const filterHaulItems = ( items : Haul . Item [ ] ) : HaulItem [ ] =>
66+ items . filter ( isHaulItem ) ;
67+
68+ export const canDropHaulItem = Haul . canDropOfType < HaulItem > ( HAUL_TYPE ) ;
69+
70+ const NodeRenderer = ( {
5971 nodeKey,
6072 position,
6173 selected,
6274 draggable,
6375} : Diagram . NodeProps ) : ReactElement | null => {
64- const { layoutKey, dispatch } = useArcEditorContext ( "ArcEditor.StageRenderer " ) ;
76+ const { layoutKey, dispatch } = useArcEditorContext ( "ArcEditor.NodeRenderer " ) ;
6577 const props = useSelectNodeProps ( layoutKey , nodeKey ) ;
6678 const { key = "" , ...rest } = props ?? { } ;
6779 const handleChange = useCallback (
@@ -94,7 +106,7 @@ const StageRenderer = ({
94106} ;
95107
96108const ArcDiagram = Base . create ( {
97- node : Component . renderProp ( StageRenderer ) ,
109+ node : Component . renderProp ( NodeRenderer ) ,
98110} ) ;
99111
100112export const ContextMenu : Layout . ContextMenuRenderer = ( { layoutKey } ) => (
@@ -131,9 +143,15 @@ export const Editor: Layout.Renderer = ({ layoutKey, visible }) => {
131143 ) ;
132144
133145 const handleNodesChange = useCallback (
134- ( changes : Diagram . NodeChange [ ] ) =>
135- undoableDispatch ( applyNodeChanges ( { key : layoutKey , changes } ) ) ,
136- [ layoutKey , undoableDispatch ] ,
146+ ( changes : Diagram . NodeChange [ ] ) => {
147+ const dragging = changes . some (
148+ ( c ) => c . type === "position" && c . dragging === true ,
149+ ) ;
150+ const action = applyNodeChanges ( { key : layoutKey , changes } ) ;
151+ if ( dragging ) dispatch ( action ) ;
152+ else undoableDispatch ( action ) ;
153+ } ,
154+ [ layoutKey , dispatch , undoableDispatch ] ,
137155 ) ;
138156
139157 const handleEdgesChange = useCallback (
@@ -172,9 +190,9 @@ export const Editor: Layout.Renderer = ({ layoutKey, visible }) => {
172190
173191 const handleDrop = useCallback (
174192 ( { items, event } : Haul . OnDropProps ) : Haul . Item [ ] => {
175- const valid = Haul . filterByType ( HAUL_TYPE , items ) ;
193+ const valid = filterHaulItems ( items ) ;
176194 if ( ref . current == null || event == null ) return valid ;
177- valid . forEach ( ( { key, data } ) => {
195+ valid . forEach ( ( { key } ) => {
178196 const spec = Base . Stage . REGISTRY [ key ] ;
179197 if ( spec == null ) return ;
180198 const pos = xy . truncate ( calculateCursorPosition ( event ) , 0 ) ;
@@ -183,7 +201,7 @@ export const Editor: Layout.Renderer = ({ layoutKey, visible }) => {
183201 key : layoutKey ,
184202 elKey : id . create ( ) ,
185203 node : { position : pos , zIndex : spec . zIndex } ,
186- props : { key, ...spec . defaultProps ( theme ) , ... ( data ?? { } ) } ,
204+ props : { key, ...spec . defaultProps ( theme ) } ,
187205 } ) ,
188206 ) ;
189207 } ) ;
@@ -195,7 +213,7 @@ export const Editor: Layout.Renderer = ({ layoutKey, visible }) => {
195213 const dropProps = Haul . useDrop ( {
196214 type : "arc" ,
197215 key : layoutKey ,
198- canDrop : Haul . canDropOfType ( HAUL_TYPE ) ,
216+ canDrop : canDropHaulItem ,
199217 onDrop : handleDrop ,
200218 } ) ;
201219
0 commit comments