adder.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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({ type: 'custom', id: `custom_${Date.now()}` }, from);
  66. }}
  67. >
  68. {hoverActivated ? (
  69. <span
  70. style={{
  71. color: '#3370ff',
  72. fontSize: 12,
  73. }}
  74. >
  75. +
  76. </span>
  77. ) : null}
  78. </div>
  79. );
  80. };