UNPKG

@logicflow/react-node-registry

Version:

LogicFlow React Shape

80 lines 3.34 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import { createElement } from 'react'; import { createRoot } from 'react-dom/client'; import { HtmlNode } from '@logicflow/core'; import { Wrapper } from './wrapper'; import { Portal } from './portal'; import { createPortal } from 'react-dom'; var ReactNodeView = /** @class */ (function (_super) { __extends(ReactNodeView, _super); function ReactNodeView() { return _super !== null && _super.apply(this, arguments) || this; } ReactNodeView.prototype.targetId = function () { return "".concat(this.props.graphModel.flowId, ":").concat(this.props.model.id); }; ReactNodeView.prototype.componentWillUnmount = function () { _super.prototype.componentWillUnmount.call(this); this.unmount(); }; ReactNodeView.prototype.setHtml = function (rootEl) { var el = document.createElement('div'); el.className = 'custom-react-node-content'; this.renderReactComponent(el); rootEl.appendChild(el); }; // confirmUpdate(_rootEl: SVGForeignObjectElement) { // // TODO: 如有需要,可以先通过继承的方式,自定义该节点的更新逻辑;我们后续会根据实际需求,丰富该功能 // // console.log('_rootEl', _rootEl) // } ReactNodeView.prototype.renderReactComponent = function (container) { this.unmountReactComponent(); var _a = this.props, model = _a.model, graphModel = _a.graphModel; if (container) { // 基于自定义节点新建 React 元素 var elem = createElement(Wrapper, { node: model, graph: graphModel, }); if (Portal.isActive()) { // 使用 Portal var portal = createPortal(elem, container, model.id); Portal.connect(this.targetId(), portal); } else { // 创建 Root 元素 this.root = createRoot(container); this.root.render(elem); } } }; ReactNodeView.prototype.unmountReactComponent = function () { if (this.rootEl && this.root) { this.root.unmount(); this.root = undefined; this.rootEl.innerHTML = ''; } }; // DONE: 是否需要 unmount 或 destroy 方法,在销毁后做一些处理 ReactNodeView.prototype.unmount = function () { this.unmountReactComponent(); }; return ReactNodeView; }(HtmlNode)); export { ReactNodeView }; export default ReactNodeView; //# sourceMappingURL=view.js.map