UNPKG

@visactor/react-vtable

Version:
150 lines (135 loc) 6.63 kB
import { application, REACT_TO_CANOPUS_EVENTS, Group, Text } from "@visactor/vtable/es/vrender"; import { isFunction, isNumber, merge } from "@visactor/vutils"; import React from "react"; import ReactReconciler from "react-reconciler"; import { ConcurrentRoot, DefaultEventPriority, LegacyRoot } from "react-reconciler/constants.js"; import { createVRenderComponent } from "../../components/vrender-components/component-creater"; const rendererPackageName = "react-vtable", rendererVersion = "string" == typeof React.version && React.version.length > 0 ? React.version : "0.0.0"; let currentUpdatePriority = DefaultEventPriority; export const reconcilor = ReactReconciler({ supportsMutation: !0, supportsPersistence: !1, rendererVersion: rendererVersion, rendererPackageName: "react-vtable", createInstance: (type, props, instance) => { const graphic = createGraphic(type, props); if (graphic) return bindEventsToGraphic(graphic, props), graphic; }, createTextInstance: (text, instance) => {}, appendInitialChild: (parentInstance, childInstance) => { parentInstance.add(childInstance); }, finalizeInitialChildren: () => !1, prepareUpdate: () => !0, shouldSetTextContent: () => !1, getRootHostContext: () => ({}), getChildHostContext: () => ({}), getPublicInstance: instance => instance, prepareForCommit: () => null, resetAfterCommit: () => {}, preparePortalMount: () => null, scheduleTimeout: setTimeout, cancelTimeout: clearTimeout, supportsMicrotasks: !0, scheduleMicrotask: callback => { Promise.resolve().then(callback); }, noTimeout: -1, isPrimaryRenderer: !1, setCurrentUpdatePriority: newPriority => { currentUpdatePriority = newPriority; }, getCurrentUpdatePriority: () => currentUpdatePriority, resolveUpdatePriority: () => currentUpdatePriority, trackSchedulerEvent: () => {}, resolveEventType: () => null, resolveEventTimeStamp: () => Date.now(), shouldAttemptEagerTransition: () => !1, getCurrentEventPriority: () => DefaultEventPriority, getInstanceFromNode: node => null, beforeActiveInstanceBlur: () => {}, afterActiveInstanceBlur: () => {}, prepareScopeUpdate: () => {}, getInstanceFromScope: () => {}, detachDeletedInstance: () => {}, supportsHydration: !1, maySuspendCommit: () => !1, preloadInstance: () => null, startSuspendingCommit: () => null, suspendInstance: () => {}, waitForCommitToBeReady: () => null, appendChild: (parentInstance, child) => { parentInstance.add(child); }, appendChildToContainer: (container, child) => { container.add(child); }, insertBefore: (parentInstance, child, beforeChild) => { parentInstance.insertBefore(child, beforeChild); }, insertInContainerBefore: (parentInstance, child, beforeChild) => { parentInstance.insertBefore(child, beforeChild); }, removeChild: (parentInstance, child) => { child.delete(); }, removeChildFromContainer: (parentInstance, child) => { child.delete(); }, commitUpdate: (...args) => { const instance = args[0]; if ("string" == typeof args[1]) { const oldProps = args[2]; return void updateGraphicProps(instance, args[3], oldProps); } const oldProps = args[3]; updateGraphicProps(instance, args[4], oldProps); }, hideInstance: instance => { instance.setAttribute("visible", !1); }, unhideInstance: (instance, props) => { instance.setAttribute("visible", !0); }, clearContainer: container => { container.removeAllChild(); }, commitTextUpdate: (textInstance, oldText, newText) => {} }); const injectIntoDevTools = reconcilor.injectIntoDevTools; "function" == typeof injectIntoDevTools && (0 === injectIntoDevTools.length ? injectIntoDevTools() : injectIntoDevTools({ bundleType: 0, version: rendererVersion, rendererPackageName: "react-vtable" })); export const createReconcilerContainer = (container, identifierPrefix = "custom", reportError) => { var _a; const createContainer = reconcilor.createContainer, noop = () => {}, onUncaughtError = reportError ? error => reportError("uncaught", error) : noop, onCaughtError = reportError ? error => reportError("caught", error) : noop, onRecoverableError = reportError ? error => reportError("recoverable", error) : noop, majorVersion = Number.parseInt(String(null !== (_a = React.version) && void 0 !== _a ? _a : "0").split(".")[0], 10); return createContainer(container, Number.isFinite(majorVersion) && majorVersion >= 19 ? ConcurrentRoot : LegacyRoot, null, !1, !1, identifierPrefix, onUncaughtError, onCaughtError, onRecoverableError, null); }; function createGraphic(type, props) { const component = createVRenderComponent(type, props); if (component) return component; if ("group" === type) return new Group(props.attribute ? props.attribute : props); if ("text" === type) return new Text(props.attribute ? props.attribute : props); if (!application.graphicService.creator[type]) return; return application.graphicService.creator[type](props.attribute); } function isEventProp(key, props) { return key.startsWith("on") && isFunction(props[key]); } function bindEventsToGraphic(graphic, props) { for (const key in props) isEventProp(key, props) && REACT_TO_CANOPUS_EVENTS[key] && graphic.addEventListener(REACT_TO_CANOPUS_EVENTS[key], props[key]); } function updateGraphicProps(graphic, newProps, oldProps) { var _a; for (const propKey in oldProps) isEventProp(propKey, oldProps) && oldProps[propKey] !== newProps[propKey] && graphic.removeEventListener(REACT_TO_CANOPUS_EVENTS[propKey], oldProps[propKey]); for (const propKey in newProps) isEventProp(propKey, newProps) && oldProps[propKey] !== newProps[propKey] && graphic.addEventListener(REACT_TO_CANOPUS_EVENTS[propKey], newProps[propKey]); const attribute = null !== (_a = newProps.attribute) && void 0 !== _a ? _a : merge({}, newProps); if (graphic.initAttributes(attribute), "image" === graphic.type && graphic.loadImage(attribute.image), "flex" === attribute.display && void 0 === attribute.width && void 0 === attribute.height && isNumber(oldProps.attribute.width) && isNumber(oldProps.attribute.height)) { const plugin = graphic.stage.pluginService.findPluginsByName("FlexLayoutPlugin")[0]; plugin && plugin.tryLayoutChildren(graphic); } } //# sourceMappingURL=reconciler.js.map