groups-layer.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import React from 'react';
  6. import { inject, injectable } from 'inversify';
  7. import { FlowRendererRegistry } from '@flowgram.ai/renderer';
  8. import {
  9. FlowDocument,
  10. FlowDocumentTransformerEntity,
  11. FlowGroupController,
  12. FlowGroupService,
  13. FlowNodeEntity,
  14. FlowNodeRenderData,
  15. FlowNodeTransformData,
  16. } from '@flowgram.ai/document';
  17. import { Layer, observeEntity, observeEntityDatas } from '@flowgram.ai/core';
  18. import { domUtils } from '@flowgram.ai/utils';
  19. import { GroupsLayerOptions, IGroupBox } from './type';
  20. import { GroupRenderer } from './constant';
  21. import { GroupBox } from './components';
  22. @injectable()
  23. export class GroupsLayer extends Layer<GroupsLayerOptions> {
  24. public readonly node: HTMLElement;
  25. @inject(FlowDocument) protected document: FlowDocument;
  26. @inject(FlowRendererRegistry)
  27. protected readonly rendererRegistry: FlowRendererRegistry;
  28. @inject(FlowGroupService)
  29. protected readonly groupService: FlowGroupService;
  30. @observeEntity(FlowDocumentTransformerEntity)
  31. readonly documentTransformer: FlowDocumentTransformerEntity;
  32. @observeEntityDatas(FlowNodeEntity, FlowNodeRenderData)
  33. renderStates: FlowNodeRenderData[];
  34. @observeEntityDatas(FlowNodeEntity, FlowNodeTransformData)
  35. transforms: FlowNodeTransformData[];
  36. private readonly className = 'gedit-groups-layer';
  37. constructor() {
  38. super();
  39. this.node = domUtils.createDivWithClass(this.className);
  40. this.node.style.zIndex = '0';
  41. }
  42. /** 缩放 */
  43. public onZoom(scale: number): void {
  44. this.node!.style.transform = `scale(${scale})`;
  45. }
  46. public render(): JSX.Element {
  47. if (this.documentTransformer.loading) return <></>;
  48. this.documentTransformer.refresh();
  49. return <>{this.renderGroups()}</>;
  50. }
  51. /** 渲染分组 */
  52. protected renderGroups(): JSX.Element {
  53. const Box = this.renderer || GroupBox;
  54. return (
  55. <>
  56. {this.groups.map(group => (
  57. <Box
  58. key={group.groupNode.id}
  59. groupNode={group.groupNode}
  60. backgroundStyle={this.options.groupBoxStyle}
  61. />
  62. ))}
  63. </>
  64. );
  65. }
  66. /** 所有分组 */
  67. protected get groups(): FlowGroupController[] {
  68. return this.groupService.getAllGroups();
  69. }
  70. protected get renderer(): IGroupBox {
  71. return this.rendererRegistry.tryToGetRendererComponent(GroupRenderer.GroupBox)
  72. ?.renderer as IGroupBox;
  73. }
  74. }