Skip to content

Commit c2f80a3

Browse files
authored
fix(workflow): resolve batch node copy error by using clipboard service (#2555)
1 parent 56edc4d commit c2f80a3

File tree

3 files changed

+44
-31
lines changed
  • frontend/packages
    • common/flowgram-adapter/free-layout-editor/src
    • workflow/playground/src

3 files changed

+44
-31
lines changed

frontend/packages/common/flowgram-adapter/free-layout-editor/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export {
6060
WorkflowResetLayoutService,
6161
WorkflowDocumentProvider,
6262
POINT_RADIUS,
63+
usePlaygroundContainer,
6364
WorkflowLineRenderData,
6465
usePlaygroundReadonlyState,
6566
type WorkflowEdgeJSON,

frontend/packages/workflow/playground/src/form-extensions/components/node-header/index.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import classnames from 'classnames';
3030
import {
3131
useEntityFromContext,
3232
useService,
33+
usePlaygroundContainer,
3334
} from '@flowgram-adapter/free-layout-editor';
3435
import {
3536
type WorkflowNodeEntity,
@@ -166,10 +167,21 @@ export const NodeHeader: React.FC<NodeHeaderProps> = ({
166167
}, 10);
167168
};
168169

169-
const handleCopy = (_, e: MouseEvent) => {
170-
e.stopPropagation();
171-
editService.copyNode(node);
172-
};
170+
const container = usePlaygroundContainer();
171+
const handleCopy = useCallback(
172+
async (_: unknown, e: MouseEvent) => {
173+
e.stopPropagation();
174+
const {
175+
WorkflowCopyShortcutsContribution,
176+
WorkflowPasteShortcutsContribution,
177+
} = await import('@/shortcuts');
178+
const copyService = container.get(WorkflowCopyShortcutsContribution);
179+
const pasteService = container.get(WorkflowPasteShortcutsContribution);
180+
const data = await copyService.toData([node]);
181+
pasteService.apply(data);
182+
},
183+
[container, node],
184+
);
173185

174186
const handleDecapsulate = (_, e: MouseEvent) => {
175187
e.stopPropagation();

frontend/packages/workflow/playground/src/shortcuts/contributions/paste/index.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,33 @@ export class WorkflowPasteShortcutsContribution
126126
});
127127
return nodes;
128128
}
129+
/** Apply clipboard data */
130+
public async apply(
131+
data: WorkflowClipboardData,
132+
): Promise<WorkflowNodeEntity[]> {
133+
const { source, json: rawJSON } = data;
134+
const json = generateUniqueWorkflow({
135+
json: rawJSON,
136+
isUniqueId: (id: string) => !this.entityManager.getEntityById(id),
137+
});
138+
139+
// You need to initialize the node data before rebuilding the node
140+
await this.saveService.initNodeData(json.nodes as WorkflowNodeJSON[]);
141+
142+
const titleCache: string[] = [];
143+
const offset = this.calcPasteOffset(data.bounds);
144+
const container = this.getSelectedContainer();
145+
const nodes = await this.render({
146+
json,
147+
source,
148+
titleCache,
149+
offset,
150+
parent: container,
151+
toContainer: container,
152+
});
153+
this.selectNodes(nodes);
154+
return nodes;
155+
}
129156
/** Handling replication events */
130157
private async handle(
131158
_event: KeyboardEvent,
@@ -161,33 +188,6 @@ export class WorkflowPasteShortcutsContribution
161188
return;
162189
}
163190
}
164-
/** Apply clipboard data */
165-
private async apply(
166-
data: WorkflowClipboardData,
167-
): Promise<WorkflowNodeEntity[]> {
168-
const { source, json: rawJSON } = data;
169-
const json = generateUniqueWorkflow({
170-
json: rawJSON,
171-
isUniqueId: (id: string) => !this.entityManager.getEntityById(id),
172-
});
173-
174-
// You need to initialize the node data before rebuilding the node
175-
await this.saveService.initNodeData(json.nodes as WorkflowNodeJSON[]);
176-
177-
const titleCache: string[] = [];
178-
const offset = this.calcPasteOffset(data.bounds);
179-
const container = this.getSelectedContainer();
180-
const nodes = await this.render({
181-
json,
182-
source,
183-
titleCache,
184-
offset,
185-
parent: container,
186-
toContainer: container,
187-
});
188-
this.selectNodes(nodes);
189-
return nodes;
190-
}
191191
/** Calculate Paste Offset */
192192
private calcPasteOffset(boundsData: WorkflowClipboardRect): IPoint {
193193
const { x, y, width, height } = boundsData;

0 commit comments

Comments
 (0)