feat(free-group-plugin): add initGroupJSON config (#394)

* docs: update readme

* feat(free-group-plugin): add initGroupJSON config
This commit is contained in:
xiamidaxia 2025-06-20 19:00:38 +08:00 committed by GitHub
parent e385211484
commit 2ed104ee95
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 17 additions and 9 deletions

View File

@ -10,7 +10,7 @@ our name.
<div align="center">
[![License](https://img.shields.io/github/license/bytedance/flowgram.ai)](https://github.com/bytedance/flowgram.ai/blob/main/LICENSE)
[![@flowgram.ai/editor](https://img.shields.io/npm/dw/%40flowgram.ai%2Fcore
[![@flowgram.ai/editor](https://img.shields.io/npm/dm/%40flowgram.ai%2Fcore
)](https://www.npmjs.com/package/@flowgram.ai/editor)
</div>
@ -119,9 +119,6 @@ After that, you can start to develop projects inside this repository.
Enjoy it!
## Stats
![Repo Stats](https://repobeats.axiom.co/api/embed/2b88f04ae1568daf1e49f77de99bb9d2fbe0d296.svg)
## 🌟 Contributors
[![FlowGram.AI Contributors](https://contrib.rocks/image?repo=bytedance/flowgram.ai)](https://github.com/bytedance/flowgram.ai/graphs/contributors)

View File

@ -11,8 +11,9 @@ import { GroupNodeRegistry } from './group-node';
export const createFreeGroupPlugin = definePluginCreator<WorkflowGroupPluginOptions, PluginContext>(
{
onBind({ bind, rebind }) {
onBind({ bind, rebind }, opts) {
bind(WorkflowGroupService).toSelf().inSingletonScope();
bind(WorkflowGroupPluginOptions).toConstantValue(opts);
rebind(FlowGroupService).toService(WorkflowGroupService);
},
onInit(

View File

@ -1,7 +1,12 @@
import { FC } from 'react';
import { WorkflowNodeEntity, WorkflowNodeJSON } from '@flowgram.ai/free-layout-core';
export interface WorkflowGroupPluginOptions {
groupNodeRender: FC;
disableGroupShortcuts?: boolean;
disableGroupNodeRegister?: boolean;
initGroupJSON?: (json: WorkflowNodeJSON, nodes: WorkflowNodeEntity[]) => WorkflowNodeJSON;
}
export const WorkflowGroupPluginOptions = Symbol('WorkflowGroupPluginOptions');

View File

@ -16,6 +16,7 @@ import { FlowGroupService, FlowNodeBaseType } from '@flowgram.ai/document';
import { TransformData } from '@flowgram.ai/core';
import { WorkflowGroupUtils } from './utils';
import { WorkflowGroupPluginOptions } from './type';
@injectable()
/** 分组服务 */
@ -28,6 +29,8 @@ export class WorkflowGroupService extends FlowGroupService {
@inject(NodeIntoContainerService) private nodeIntoContainerService: NodeIntoContainerService;
@inject(WorkflowGroupPluginOptions) private opts: WorkflowGroupPluginOptions;
private toDispose = new DisposableCollection();
public ready(): void {
@ -44,10 +47,9 @@ export class WorkflowGroupService extends FlowGroupService {
return;
}
const parent = nodes[0].parent ?? this.document.root;
const groupId = `group_${nanoid(5)}`;
const groupJSON: WorkflowNodeJSON = {
let groupJSON: WorkflowNodeJSON = {
type: FlowNodeBaseType.GROUP,
id: groupId,
id: `group_${nanoid(5)}`,
meta: {
position: {
x: 0,
@ -56,6 +58,9 @@ export class WorkflowGroupService extends FlowGroupService {
},
data: {},
};
if (this.opts.initGroupJSON) {
groupJSON = this.opts.initGroupJSON(groupJSON, nodes);
}
this.historyService.startTransaction();
this.document.createWorkflowNodeByType(
FlowNodeBaseType.GROUP,
@ -68,7 +73,7 @@ export class WorkflowGroupService extends FlowGroupService {
);
nodes.forEach((node) => {
this.freeOperationService.moveNode(node, {
parent: groupId,
parent: groupJSON.id,
});
});
this.historyService.endTransaction();