@logicflow/react-node-registry
Version:
LogicFlow React Shape
83 lines • 3.53 kB
JavaScript
;
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 __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReactNodeView = void 0;
var react_1 = require("react");
var client_1 = require("react-dom/client");
var core_1 = require("@logicflow/core");
var wrapper_1 = require("./wrapper");
var portal_1 = require("./portal");
var react_dom_1 = require("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 = (0, react_1.createElement)(wrapper_1.Wrapper, {
node: model,
graph: graphModel,
});
if (portal_1.Portal.isActive()) {
// 使用 Portal
var portal = (0, react_dom_1.createPortal)(elem, container, model.id);
portal_1.Portal.connect(this.targetId(), portal);
}
else {
// 创建 Root 元素
this.root = (0, client_1.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;
}(core_1.HtmlNode));
exports.ReactNodeView = ReactNodeView;
exports.default = ReactNodeView;
//# sourceMappingURL=view.js.map