| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- /**
- * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
- * SPDX-License-Identifier: MIT
- */
- import { useMemo } from 'react';
- import { createMinimapPlugin } from '@flowgram.ai/minimap-plugin';
- import { createFreeSnapPlugin } from '@flowgram.ai/free-snap-plugin';
- import {
- FreeLayoutProps,
- WorkflowNodeProps,
- WorkflowNodeRenderer,
- Field,
- useNodeRender,
- FlowNodeMeta,
- } from '@flowgram.ai/free-layout-editor';
- import { createContainerNodePlugin } from '@flowgram.ai/free-container-plugin';
- import { nodeRegistries } from '../nodes';
- import { initialData } from '../initial-data';
- export const useEditorProps = () =>
- useMemo<FreeLayoutProps>(
- () => ({
- /**
- * Whether to enable the background
- */
- background: true,
- /**
- * Whether it is read-only or not, the node cannot be dragged in read-only mode
- */
- readonly: false,
- /**
- * Initial data
- * 初始化数据
- */
- initialData,
- /**
- * Node registries
- * 节点注册
- */
- nodeRegistries,
- /**
- * 节点数据转换, 由 ctx.document.fromJSON 调用
- * Node data transformation, called by ctx.document.fromJSON
- * @param node
- * @param json
- */
- fromNodeJSON(node, json) {
- return json;
- },
- /**
- * 节点数据转换, 由 ctx.document.toJSON 调用
- * Node data transformation, called by ctx.document.toJSON
- * @param node
- * @param json
- */
- toNodeJSON(node, json) {
- return json;
- },
- /**
- * Get the default node registry, which will be merged with the 'nodeRegistries'
- * 提供默认的节点注册,这个会和 nodeRegistries 做合并
- */
- getNodeDefaultRegistry(type) {
- return {
- type,
- meta: {
- defaultExpanded: true,
- },
- formMeta: {
- /**
- * Render form
- */
- render: () => (
- <>
- <Field<string> name="title">
- {({ field }) => <div className="demo-free-node-title">{field.value}</div>}
- </Field>
- <div className="demo-free-node-content">
- <Field<string> name="content">
- <input />
- </Field>
- </div>
- </>
- ),
- },
- };
- },
- materials: {
- /**
- * Render Node
- */
- renderDefaultNode: (props: WorkflowNodeProps) => {
- const { node, form } = useNodeRender();
- const meta = node.getNodeMeta<FlowNodeMeta>();
- return (
- <WorkflowNodeRenderer
- className="demo-free-node"
- node={props.node}
- style={meta.wrapperStyle}
- >
- {form?.render()}
- </WorkflowNodeRenderer>
- );
- },
- },
- /**
- * Content change
- */
- onContentChange(ctx, event) {
- console.log('Auto Save: ', event, ctx.document.toJSON());
- },
- canDeleteLine: (ctx, line) => {
- if (line.from?.flowNodeType === 'batch' && line.to?.flowNodeType === 'batch_function') {
- return false;
- }
- return true;
- },
- isHideArrowLine(ctx, line) {
- if (line.from?.flowNodeType === 'batch' && line.to?.flowNodeType === 'batch_function') {
- return true;
- }
- return false;
- },
- // /**
- // * Node engine enable, you can configure formMeta in the FlowNodeRegistry
- // */
- nodeEngine: {
- enable: true,
- },
- /**
- * Redo/Undo enable
- */
- history: {
- enable: true,
- enableChangeNode: true, // Listen Node engine data change
- },
- /**
- * Playground init
- */
- onInit: (ctx) => {},
- /**
- * Playground render
- */
- onAllLayersRendered(ctx) {
- // Fitview
- ctx.document.fitView(false);
- },
- /**
- * Playground dispose
- */
- onDispose() {
- console.log('---- Playground Dispose ----');
- },
- plugins: () => [
- /**
- * Minimap plugin
- * 缩略图插件
- */
- createMinimapPlugin({
- disableLayer: true,
- canvasStyle: {
- canvasWidth: 182,
- canvasHeight: 102,
- canvasPadding: 50,
- canvasBackground: 'rgba(245, 245, 245, 1)',
- canvasBorderRadius: 10,
- viewportBackground: 'rgba(235, 235, 235, 1)',
- viewportBorderRadius: 4,
- viewportBorderColor: 'rgba(201, 201, 201, 1)',
- viewportBorderWidth: 1,
- viewportBorderDashLength: 2,
- nodeColor: 'rgba(255, 255, 255, 1)',
- nodeBorderRadius: 2,
- nodeBorderWidth: 0.145,
- nodeBorderColor: 'rgba(6, 7, 9, 0.10)',
- overlayColor: 'rgba(255, 255, 255, 0)',
- },
- }),
- /**
- * Snap plugin
- * 自动对齐及辅助线插件
- */
- createFreeSnapPlugin({
- edgeColor: '#00B2B2',
- alignColor: '#00B2B2',
- edgeLineWidth: 1,
- alignLineWidth: 1,
- alignCrossWidth: 8,
- }),
- /**
- * This is used for the rendering of the loop node sub-canvas
- * 这个用于 loop 节点子画布的渲染
- */
- createContainerNodePlugin({}),
- ],
- }),
- []
- );
|