step-4.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import '@flowgram.ai/fixed-layout-editor/index.css';
  2. import { FC } from 'react';
  3. import { createMinimapPlugin } from '@flowgram.ai/minimap-plugin';
  4. import { defaultFixedSemiMaterials } from '@flowgram.ai/fixed-semi-materials';
  5. import {
  6. FixedLayoutEditorProvider,
  7. EditorRenderer,
  8. FlowNodeEntity,
  9. useNodeRender,
  10. FlowNodeJSON,
  11. FlowOperationService,
  12. usePlayground,
  13. useService,
  14. FlowRendererKey,
  15. useClientContext,
  16. } from '@flowgram.ai/fixed-layout-editor';
  17. export const NodeRender = ({ node }: { node: FlowNodeEntity }) => {
  18. const {
  19. onMouseEnter,
  20. onMouseLeave,
  21. startDrag,
  22. form,
  23. dragging,
  24. isBlockOrderIcon,
  25. isBlockIcon,
  26. activated,
  27. } = useNodeRender();
  28. const ctx = useClientContext();
  29. return (
  30. <div
  31. onMouseEnter={onMouseEnter}
  32. onMouseLeave={onMouseLeave}
  33. onMouseDown={(e) => {
  34. startDrag(e);
  35. e.stopPropagation();
  36. }}
  37. style={{
  38. width: 280,
  39. minHeight: 88,
  40. height: 'auto',
  41. background: '#fff',
  42. border: '1px solid rgba(6, 7, 9, 0.15)',
  43. borderColor: activated ? '#82a7fc' : 'rgba(6, 7, 9, 0.15)',
  44. borderRadius: 8,
  45. boxShadow: '0 2px 6px 0 rgba(0, 0, 0, 0.04), 0 4px 12px 0 rgba(0, 0, 0, 0.02)',
  46. display: 'flex',
  47. flexDirection: 'column',
  48. justifyContent: 'center',
  49. position: 'relative',
  50. padding: 12,
  51. cursor: 'move',
  52. opacity: dragging ? 0.3 : 1,
  53. ...(isBlockOrderIcon || isBlockIcon ? { width: 260 } : {}),
  54. }}
  55. >
  56. {form?.render()}
  57. {/* 删除按钮 */}
  58. <button
  59. onClick={(e) => {
  60. e.stopPropagation();
  61. ctx.operation.deleteNode(node);
  62. }}
  63. style={{
  64. position: 'absolute',
  65. top: 4,
  66. right: 4,
  67. width: 20,
  68. height: 20,
  69. border: 'none',
  70. borderRadius: '50%',
  71. background: '#fff',
  72. color: '#666',
  73. fontSize: 12,
  74. cursor: 'pointer',
  75. display: 'flex',
  76. alignItems: 'center',
  77. justifyContent: 'center',
  78. boxShadow: '0 1px 3px rgba(0,0,0,0.12)',
  79. transition: 'all 0.2s',
  80. }}
  81. >
  82. ×
  83. </button>
  84. </div>
  85. );
  86. };
  87. const useAddNode = () => {
  88. const playground = usePlayground();
  89. const flowOperationService = useService(FlowOperationService) as FlowOperationService;
  90. const handleAdd = (addProps: FlowNodeJSON, dropNode: FlowNodeEntity) => {
  91. const blocks = addProps.blocks ? addProps.blocks : undefined;
  92. const entity = flowOperationService.addFromNode(dropNode, {
  93. ...addProps,
  94. blocks,
  95. });
  96. setTimeout(() => {
  97. playground.scrollToView({
  98. bounds: entity.bounds,
  99. scrollToCenter: true,
  100. });
  101. }, 10);
  102. return entity;
  103. };
  104. const handleAddBranch = (addProps: FlowNodeJSON, dropNode: FlowNodeEntity) => {
  105. const index = dropNode.index + 1;
  106. const entity = flowOperationService.addBlock(dropNode.originParent!, addProps, {
  107. index,
  108. });
  109. return entity;
  110. };
  111. return {
  112. handleAdd,
  113. handleAddBranch,
  114. };
  115. };
  116. const Adder: FC<{
  117. from: FlowNodeEntity;
  118. to?: FlowNodeEntity;
  119. hoverActivated: boolean;
  120. }> = ({ from, hoverActivated }) => {
  121. const playground = usePlayground();
  122. const { handleAdd } = useAddNode();
  123. if (playground.config.readonlyOrDisabled) return null;
  124. return (
  125. <div
  126. style={{
  127. width: hoverActivated ? 15 : 6,
  128. height: hoverActivated ? 15 : 6,
  129. backgroundColor: hoverActivated ? '#fff' : 'rgb(143, 149, 158)',
  130. color: '#fff',
  131. borderRadius: '50%',
  132. display: 'flex',
  133. alignItems: 'center',
  134. justifyContent: 'center',
  135. cursor: 'pointer',
  136. }}
  137. onClick={() => {
  138. handleAdd({ type: 'custom', id: `custom_${Date.now()}` }, from);
  139. }}
  140. >
  141. {hoverActivated ? (
  142. <span
  143. style={{
  144. color: '#3370ff',
  145. fontSize: 12,
  146. }}
  147. >
  148. +
  149. </span>
  150. ) : null}
  151. </div>
  152. );
  153. };
  154. const FlowGramApp = () => (
  155. <FixedLayoutEditorProvider
  156. plugins={() => [
  157. createMinimapPlugin({
  158. enableDisplayAllNodes: true,
  159. }),
  160. ]}
  161. nodeRegistries={[
  162. {
  163. type: 'custom',
  164. },
  165. ]}
  166. initialData={{
  167. nodes: [
  168. {
  169. id: 'start_0',
  170. type: 'start',
  171. },
  172. {
  173. id: 'custom_1',
  174. type: 'custom',
  175. },
  176. {
  177. id: 'end_2',
  178. type: 'end',
  179. },
  180. ],
  181. }}
  182. onAllLayersRendered={(ctx) => {
  183. setTimeout(() => {
  184. ctx.playground.config.fitView(ctx.document.root.bounds.pad(30));
  185. }, 10);
  186. }}
  187. materials={{
  188. renderDefaultNode: NodeRender,
  189. components: {
  190. ...defaultFixedSemiMaterials,
  191. [FlowRendererKey.ADDER]: Adder,
  192. },
  193. }}
  194. >
  195. <EditorRenderer />
  196. </FixedLayoutEditorProvider>
  197. );
  198. export default FlowGramApp;