@@ -6,7 +6,7 @@ import { FlowNode } from '../models/store';
66import { useStoreApi } from './useStore' ;
77import { useTemporalStore } from './useTemporalStore' ;
88import autoLayoutNodes from '../utils/autoLayoutNodes' ;
9- import { generateCopyNodes , uuid } from '../utils' ;
9+ import { generateCopyNodes , safeJSONParse , safeJsonStringify , uuid } from '../utils' ;
1010import { message } from 'antd' ;
1111
1212// useFlow 维护原则
@@ -113,6 +113,11 @@ export const useFlow = () => {
113113 storeApi . setState ( { copyTimeoutId : null } ) ;
114114 } , 30000 ) ;
115115
116+ navigator . clipboard
117+ . writeText ( safeJsonStringify ( copyNodes ) )
118+ . catch ( ( err ) => {
119+ message . error ( '复制失败~' ) ;
120+ } )
116121 storeApi . setState ( {
117122 copyNodes,
118123 copyTimeoutId : timeoutId ,
@@ -148,8 +153,9 @@ export const useFlow = () => {
148153 }
149154 } ) ;
150155
151- const pasteNodeSimple = useMemoizedFn ( ( ) => {
156+ const pasteNodeSimple = useMemoizedFn ( async ( ) => {
152157 const mousePosition = storeApi . getState ( ) . mousePosition ;
158+ const text = await navigator . clipboard . readText ( ) . catch ( ( ) => null )
153159 if ( storeApi . getState ( ) . copyNodes . length > 0 ) {
154160 // 清除超时定时器
155161 if ( storeApi . getState ( ) . copyTimeoutId ) {
@@ -178,7 +184,24 @@ export const useFlow = () => {
178184 copyTimeoutId : null ,
179185 } ) ;
180186 } ) ;
181- } else {
187+ } else if ( text ) {
188+ const copyNodeData = safeJSONParse ( text , [ ] )
189+ const flowPos = screenToFlowPosition ( {
190+ x : mousePosition . elementX ,
191+ y : mousePosition . elementY ,
192+ } ) ;
193+ const copyNodes = copyNodeData . map ( node => ( {
194+ ...node ,
195+ id :uuid ( ) ,
196+ position : {
197+ x : flowPos . x ,
198+ y : flowPos . y ,
199+ } ,
200+ } ) ) ;
201+ record ( ( ) => {
202+ storeApi . getState ( ) . addNodes ( copyNodes , false ) ;
203+ } ) ;
204+ } else {
182205 // message.warning('请先复制节点!');
183206 }
184207 } ) ;
0 commit comments