adder.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import '@flowgram.ai/fixed-layout-editor/index.css';
  6. import { FC } from 'react';
  7. import {
  8. FlowNodeEntity,
  9. FlowNodeJSON,
  10. FlowOperationService,
  11. usePlayground,
  12. useService,
  13. } from '@flowgram.ai/fixed-layout-editor';
  14. const useAddNode = () => {
  15. const playground = usePlayground();
  16. const flowOperationService = useService(FlowOperationService) as FlowOperationService;
  17. const handleAdd = (addProps: FlowNodeJSON, dropNode: FlowNodeEntity) => {
  18. const blocks = addProps.blocks ? addProps.blocks : undefined;
  19. const entity = flowOperationService.addFromNode(dropNode, {
  20. ...addProps,
  21. blocks,
  22. });
  23. setTimeout(() => {
  24. playground.scrollToView({
  25. bounds: entity.bounds,
  26. scrollToCenter: true,
  27. });
  28. }, 10);
  29. return entity;
  30. };
  31. const handleAddBranch = (addProps: FlowNodeJSON, dropNode: FlowNodeEntity) => {
  32. const index = dropNode.index + 1;
  33. const entity = flowOperationService.addBlock(dropNode.originParent!, addProps, {
  34. index,
  35. });
  36. return entity;
  37. };
  38. return {
  39. handleAdd,
  40. handleAddBranch,
  41. };
  42. };
  43. export const Adder: FC<{
  44. from: FlowNodeEntity;
  45. to?: FlowNodeEntity;
  46. hoverActivated: boolean;
  47. }> = ({ from, hoverActivated }) => {
  48. const playground = usePlayground();
  49. const { handleAdd } = useAddNode();
  50. if (playground.config.readonlyOrDisabled) return null;
  51. return (
  52. <div
  53. style={{
  54. width: hoverActivated ? 15 : 6,
  55. height: hoverActivated ? 15 : 6,
  56. backgroundColor: hoverActivated ? '#fff' : 'rgb(143, 149, 158)',
  57. color: '#fff',
  58. borderRadius: '50%',
  59. display: 'flex',
  60. alignItems: 'center',
  61. justifyContent: 'center',
  62. cursor: 'pointer',
  63. }}
  64. onClick={() => {
  65. handleAdd(
  66. {
  67. type: 'custom',
  68. id: `custom_${Date.now()}`,
  69. data: {
  70. title: 'New Custom Node',
  71. content: 'Custom Node Content',
  72. },
  73. },
  74. from
  75. );
  76. }}
  77. >
  78. {hoverActivated ? (
  79. <span
  80. style={{
  81. color: '#3370ff',
  82. fontSize: 12,
  83. }}
  84. >
  85. +
  86. </span>
  87. ) : null}
  88. </div>
  89. );
  90. };