From 5c0c1d3068a53da21a86508512ec6449c8e96add Mon Sep 17 00:00:00 2001 From: PYC5635 <3653161641@qq.com> Date: Sun, 22 Nov 2026 15:50:48 +0800 Subject: [PATCH 1/4] feat: visual extension & addon editors (node graph <-> js) --- src/components/gui/gui.jsx | 4 + src/components/menu-bar/menu-bar.jsx | 34 +- src/components/visual-editor/node-canvas.jsx | 328 +++++ .../visual-editor/node-inspector.jsx | 140 ++ src/components/visual-editor/node-palette.jsx | 80 ++ .../visual-editor/visual-editor-modal.css | 373 ++++++ .../visual-editor/visual-editor-modal.jsx | 450 +++++++ src/containers/visual-addon-editor-modal.jsx | 41 + .../visual-extension-editor-modal.jsx | 41 + src/lib/tw-translations/local-messages.js | 4 + src/lib/visual-node-engine/compile.js | 312 +++++ src/lib/visual-node-engine/graph-model.js | 99 ++ src/lib/visual-node-engine/node-types.js | 1187 +++++++++++++++++ src/lib/visual-node-engine/parse.js | 211 +++ src/lib/visual-node-engine/templates.js | 67 + src/reducers/modals.js | 20 + 16 files changed, 3390 insertions(+), 1 deletion(-) create mode 100644 src/components/visual-editor/node-canvas.jsx create mode 100644 src/components/visual-editor/node-inspector.jsx create mode 100644 src/components/visual-editor/node-palette.jsx create mode 100644 src/components/visual-editor/visual-editor-modal.css create mode 100644 src/components/visual-editor/visual-editor-modal.jsx create mode 100644 src/containers/visual-addon-editor-modal.jsx create mode 100644 src/containers/visual-extension-editor-modal.jsx create mode 100644 src/lib/visual-node-engine/compile.js create mode 100644 src/lib/visual-node-engine/graph-model.js create mode 100644 src/lib/visual-node-engine/node-types.js create mode 100644 src/lib/visual-node-engine/parse.js create mode 100644 src/lib/visual-node-engine/templates.js diff --git a/src/components/gui/gui.jsx b/src/components/gui/gui.jsx index ba24710de85..9e6f12e2ba1 100644 --- a/src/components/gui/gui.jsx +++ b/src/components/gui/gui.jsx @@ -75,6 +75,8 @@ const FractchWorkspace = React.lazy(() => import('../mw-fractch-workspace/fractc // jszip). Lazy load it so it does not slow down the first editor load; it is // only mounted when the user actually opens the git panel. const TWGitModal = React.lazy(() => import('../../containers/mw-git-modal.jsx')); +const VisualExtensionEditor = React.lazy(() => import('../../containers/visual-extension-editor-modal.jsx')); +const VisualAddonEditor = React.lazy(() => import('../../containers/visual-addon-editor-modal.jsx')); // Heavy modal / panel components that are not shown on the initial editor // load. Lazy-loading them removes their code (and transitive dependencies) @@ -995,6 +997,8 @@ const GUIComponent = props => { + + {usernameModalVisible && ( diff --git a/src/components/menu-bar/menu-bar.jsx b/src/components/menu-bar/menu-bar.jsx index 67e92af2e49..51da04711bd 100644 --- a/src/components/menu-bar/menu-bar.jsx +++ b/src/components/menu-bar/menu-bar.jsx @@ -62,7 +62,9 @@ import { openGitModal, openExtensionManagerModal, openHelp, - openSimpleDialog + openSimpleDialog, + openVisualExtensionEditor, + openVisualAddonEditor } from '../../reducers/modals'; import {openCollaborationModal} from '../../reducers/collaboration'; import {setPlayer} from '../../reducers/mode'; @@ -2210,6 +2212,32 @@ class MenuBar extends React.Component { open={this.props.toolsMenuOpen} place={this.props.isRtl ? 'left' : 'right'} > + + { + this.props.onClickVisualExtensionEditor(); + this.props.onRequestCloseTools(); + }} + > + + + { + this.props.onClickVisualAddonEditor(); + this.props.onRequestCloseTools(); + }} + > + + + { @@ -2654,6 +2682,8 @@ MenuBar.propTypes = { onClickPreferencesModal: PropTypes.func, onClickGitModal: PropTypes.func, onClickHelp: PropTypes.func, + onClickVisualExtensionEditor: PropTypes.func, + onClickVisualAddonEditor: PropTypes.func, onOpenSettingsModal: PropTypes.func, onLogOut: PropTypes.func, @@ -2783,6 +2813,8 @@ const mapDispatchToProps = dispatch => ({ }, onClickHelp: () => dispatch(openHelp()), onOpenSettingsModal: () => dispatch(openSettingsModal()), + onClickVisualExtensionEditor: () => dispatch(openVisualExtensionEditor()), + onClickVisualAddonEditor: () => dispatch(openVisualAddonEditor()), onClickNew: needSave => { dispatch(setPlayer(false)); dispatch(requestNewProject(needSave)); diff --git a/src/components/visual-editor/node-canvas.jsx b/src/components/visual-editor/node-canvas.jsx new file mode 100644 index 00000000000..b34e3db0bb3 --- /dev/null +++ b/src/components/visual-editor/node-canvas.jsx @@ -0,0 +1,328 @@ +/** + * 可视化节点编辑器 · 画布 + * 平移(按住空白拖动 / 鼠标中键)、缩放(ctrl+滚轮)、节点拖动、 + * 端口拖拽连线、点选节点/连线、双击子图节点。 + */ +import React from 'react'; +import PropTypes from 'prop-types'; +import {getType} from '../../lib/visual-node-engine/node-types.js'; + +const PORT_COLORS_CLASS = kind => (kind === 'exec' ? 'exec' : 'expr'); + +// 节点配色 class +const nodeClass = typeDef => { + if (typeDef.isEvent) return 've-node-event'; + if (typeDef.isExpression) return 've-node-expr'; + if (typeDef.isFunction) return 've-node-func'; + if (typeDef.isExtensionBlock) return 've-node-block'; + if (typeDef.addonOnly) return 've-node-addon'; + return 've-node-flow'; +}; + +// 端口几何:行高 19px,节点标题 29px,顶部 padding 4px +const PORT_H = 19; +const TITLE_H = 29; + +class NodeCanvas extends React.Component { + constructor (props) { + super(props); + this.state = { + pan: {x: 60, y: 40}, + zoom: 1, + dragNode: null, // {id, dx, dy} + panning: false, + panStart: null, + linkFrom: null, // {nodeId, port, isExec, x, y}(世界坐标) + mouseWorld: null + }; + this.wrapRef = React.createRef(); + } + + // ---- 世界坐标换算 ---- + toWorld (clientX, clientY) { + const rect = this.wrapRef.current.getBoundingClientRect(); + return { + x: (clientX - rect.left - this.state.pan.x) / this.state.zoom, + y: (clientY - rect.top - this.state.pan.y) / this.state.zoom + }; + } + + portPos (node, portName, isInput, def) { + const inputs = (def.inputs || []); + const outputs = (def.outputs || []); + const inIdx = inputs.findIndex(p => p.name === portName); + const outIdx = outputs.findIndex(p => p.name === portName); + if (isInput && inIdx === -1) return null; + if (!isInput && outIdx === -1) return null; + const row = isInput ? inIdx : outIdx; + return { + x: node.x + (isInput ? 0 : node.width || 200), + y: node.y + TITLE_H + 4 + row * PORT_H + 8 + }; + } + + onWheel = e => { + e.preventDefault(); + if (e.ctrlKey || e.metaKey) { + const factor = e.deltaY > 0 ? 0.9 : 1.1; + this.setState(s => ({zoom: Math.min(2.5, Math.max(0.3, s.zoom * factor))})); + } else { + this.setState(s => ({pan: {x: s.pan.x - e.deltaX, y: s.pan.y - e.deltaY}})); + } + }; + + // ---- 画布空白:pan ---- + onCanvasMouseDown = e => { + if (e.target === e.currentTarget || e.target.classList.contains('ve-canvas-wrap')) { + this.setState({panning: true, panStart: {x: e.clientX, y: e.clientY, base: this.state.pan}}); + } + }; + + onMouseMove = e => { + if (this.state.panning && this.state.panStart) { + this.setState({ + pan: { + x: this.state.panStart.base.x + (e.clientX - this.state.panStart.x), + y: this.state.panStart.base.y + (e.clientY - this.state.panStart.y) + } + }); + return; + } + if (this.state.dragNode) { + const world = this.toWorld(e.clientX, e.clientY); + this.props.onMoveNode(this.state.dragNode.id, + world.x - this.state.dragNode.dx, world.y - this.state.dragNode.dy); + return; + } + if (this.state.linkFrom) { + this.setState({mouseWorld: this.toWorld(e.clientX, e.clientY)}); + } + }; + + onMouseUp = e => { + if (this.state.panning) this.setState({panning: false, panStart: null}); + if (this.state.dragNode) this.setState({dragNode: null}); + if (this.state.linkFrom) { + // 尝试在鼠标落点找端口:命中节点则由 onPortDrop 判定 + this.setState({linkFrom: null, mouseWorld: null}); + } + void e; + }; + + // ---- 节点拖动 ---- + onNodeTitleMouseDown = (e, node) => { + e.stopPropagation(); + const world = this.toWorld(e.clientX, e.clientY); + this.props.onSelectNode(node.id); + this.setState({dragNode: {id: node.id, dx: world.x - node.x, dy: world.y - node.y}}); + }; + + // ---- 端口拖拽连线 ---- + onPortMouseDown = (e, node, portName, isInput, def) => { + e.stopPropagation(); + const pos = this.portPos(node, portName, isInput, def); + this.props.onSelectNode(node.id); + this.setState({linkFrom: {nodeId: node.id, port: portName, isInput, x: pos.x, y: pos.y}}); + // mouseWorld 初始 + const w = this.toWorld(e.clientX, e.clientY); + this.setState({mouseWorld: w}); + // 松手在端口上:由 port onMouseUp 处理 + }; + + onPortMouseUp = (e, node, portName, isInput, def) => { + e.stopPropagation(); + const link = this.state.linkFrom; + if (!link) return; + // 方向归一:输出 → 输入 + let fromNode = link.nodeId; + let fromPort = link.port; + let toNode = node.id; + let toPort = portName; + if (link.isInput && !isInput) { + // 从输入端口拖到输出端口:反向 + fromNode = node.id; + fromPort = portName; + toNode = link.nodeId; + toPort = link.port; + } else if (link.isInput === isInput) { + // 同向端口:无效 + this.setState({linkFrom: null, mouseWorld: null}); + return; + } + this.props.onConnect(fromNode, fromPort, toNode, toPort); + this.setState({linkFrom: null, mouseWorld: null}); + void def; + }; + + // ---- 连线点击删除 ---- + onEdgeClick = (e, edge) => { + e.stopPropagation(); + this.props.onRemoveEdge(edge); + }; + + // ---- 节点点击(选中) ---- + onNodeMouseDown = (e, node) => { + e.stopPropagation(); + this.props.onSelectNode(node.id); + }; + + onNodeDoubleClick = (e, node) => { + e.stopPropagation(); + this.props.onOpenSubGraph(node.id); + }; + + renderPortDot (node, def, port, isInput) { + const key = `${node.id}:${isInput ? 'i' : 'o'}:${port.name}`; + return ( +
+ {isInput && ( +
this.onPortMouseDown(e, node, port.name, true, def)} + onMouseUp={e => this.onPortMouseUp(e, node, port.name, true, def)} + /> + )} + {port.label || port.name} + {!isInput && ( +
this.onPortMouseDown(e, node, port.name, false, def)} + onMouseUp={e => this.onPortMouseUp(e, node, port.name, false, def)} + /> + )} +
+ ); + } + + render () { + const {graph, selectedNodeId} = this.props; + const {pan, zoom, linkFrom, mouseWorld} = this.state; + // 节点宽度估:简单按节点类型固定 200~230 + const nodes = graph.nodes.map(n => { + const def = getType(n.type); + const width = def && (def.isExtensionBlock || def.isFunction) ? 230 : 200; + return {...n, width, __def: def}; + }); + + // 连线路径(世界坐标 → SVG 内直接用世界坐标,viewport transform 处理) + const edgePaths = graph.edges.map((edge, i) => { + const fromNode = nodes.find(n => n.id === edge.from.node); + const toNode = nodes.find(n => n.id === edge.to.node); + if (!fromNode || !toNode) return null; + const fromDef = fromNode.__def; + const toDef = toNode.__def; + if (!fromDef || !toDef) return null; + const fromPortDef = (fromDef.outputs || []).find(p => p.name === edge.from.port); + const toPortDef = (toDef.inputs || []).find(p => p.name === edge.to.port); + if (!fromPortDef || !toPortDef) return null; + const a = this.portPos(fromNode, edge.from.port, false, fromDef); + const b = this.portPos(toNode, edge.to.port, true, toDef); + if (!a || !b) return null; + const dx = Math.max(30, Math.abs(b.x - a.x) / 2); + const path = `M ${a.x} ${a.y} C ${a.x + dx} ${a.y}, ${b.x - dx} ${b.y}, ${b.x} ${b.y}`; + const cls = `ve-edge-path ${fromPortDef.kind === 'exec' ? 'exec' : ''}`; + return ( + this.onEdgeClick(e, edge)} + /> + ); + }); + + // 幽灵连线 + let ghost = null; + if (linkFrom && mouseWorld) { + const a = linkFrom.isInput ? mouseWorld : {x: linkFrom.x, y: linkFrom.y}; + const b = linkFrom.isInput ? {x: linkFrom.x, y: linkFrom.y} : mouseWorld; + const dx = Math.max(30, Math.abs(b.x - a.x) / 2); + ghost = (`M ${a.x} ${a.y} C ${a.x + dx} ${a.y}, ${b.x - dx} ${b.y}, ${b.x} ${b.y}`); + } + + return ( +
+
+ + {edgePaths} + {ghost ? : null} + + {nodes.length === 0 && ( +
+ 从左侧面板拖入节点开始搭建… +
+ )} + {nodes.map(node => { + const def = node.__def; + if (!def) return null; + return ( +
this.onNodeMouseDown(e, node)} + onDoubleClick={e => this.onNodeDoubleClick(e, node)} + onContextMenu={e => { + e.preventDefault(); + e.stopPropagation(); + this.props.onRemoveNode(node.id); + }} + > +
this.onNodeTitleMouseDown(e, node)} + > + {def.title} + {def.category} +
+
+ {(def.inputs || []).map(p => this.renderPortDot(node, def, p, true))} + {(def.outputs || []).map(p => this.renderPortDot(node, def, p, false))} + {(def.params || []).some(p => p.type === 'graph') && ( +
+ 双击编辑子图 +
+ )} +
+
+ ); + })} +
+
+ 拖动空白平移 · Ctrl+滚轮缩放 · 拖端口连线 · 右键节点删除 · 双击进入子图 +
+
+ ); + } +} + +NodeCanvas.propTypes = { + graph: PropTypes.object.isRequired, + selectedNodeId: PropTypes.string, + onMoveNode: PropTypes.func, + onSelectNode: PropTypes.func, + onConnect: PropTypes.func, + onRemoveEdge: PropTypes.func, + onRemoveNode: PropTypes.func, + onOpenSubGraph: PropTypes.func +}; + +export default NodeCanvas; diff --git a/src/components/visual-editor/node-inspector.jsx b/src/components/visual-editor/node-inspector.jsx new file mode 100644 index 00000000000..5c72db604fb --- /dev/null +++ b/src/components/visual-editor/node-inspector.jsx @@ -0,0 +1,140 @@ +/** + * 可视化节点编辑器 · 属性面板(右侧) + * 选中节点的参数编辑(text/number/bool/select/code/graph)。 + * graph 类型参数(子图)显示「双击节点进入编辑」提示 + 清空按钮。 + */ +import React from 'react'; +import PropTypes from 'prop-types'; +import {getType} from '../../lib/visual-node-engine/node-types.js'; + +class NodeInspector extends React.Component { + handleChange (name, value) { + this.props.onParamChange(this.props.node.id, name, value); + } + + renderParam (p, node) { + const value = node.params ? node.params[p.name] : null; + switch (p.type) { + case 'bool': + return ( +
+ +
+ this.handleChange(p.name, e.target.checked)} + /> + {value ? '开' : '关'} +
+
+ ); + case 'number': + return ( +
+ + this.handleChange(p.name, Number(e.target.value))} + /> +
+ ); + case 'select': + return ( +
+ + +
+ ); + case 'code': + return ( +
+ +