@visactor/react-vrender
Version:
## Description
146 lines (135 loc) • 8.67 kB
JavaScript
import { graphicCreator, createGlyph, createText, createShadowRoot } from "@visactor/vrender";
import React from "react";
import ReactReconciler from "react-reconciler";
import { DefaultEventPriority } from "react-reconciler/constants";
import { bindGraphicEvent, splitProps, updateProps } from "./processProps";
import { log, error } from "./util/debug";
const isStage = instance => instance.stage === instance, isLayer = instance => instance.layer === instance, isGlyph = instance => "glyph" === instance.type, isShadowRoot = instance => "shadowroot" === instance.type;
export const createInstance = (type, props, rootContainerInstance) => {
const graphicType = type.toLowerCase(), {graphicProps: graphicProps, eventProps: eventProps} = splitProps(props);
let instance;
return instance = "glyph" === graphicType ? createGlyph(graphicProps) : "layer" === graphicType && rootContainerInstance ? isStage(rootContainerInstance) ? rootContainerInstance.createLayer() : rootContainerInstance.stage ? rootContainerInstance.stage.createLayer() : null : "shadowroot" === graphicType ? createShadowRoot() : graphicCreator[graphicType](graphicProps),
bindGraphicEvent(eventProps, instance), log("createInstance ", graphicType, instance),
instance;
};
const appendChild = (parentInstance, child) => {
var _a;
const isParentStage = isStage(parentInstance), isChildLayer = isLayer(child);
isParentStage && !isChildLayer && parentInstance.defaultLayer ? parentInstance.defaultLayer.appendChild(child) : isGlyph(parentInstance) ? parentInstance.setSubGraphic((null !== (_a = parentInstance.getSubGraphic()) && void 0 !== _a ? _a : []).concat(child)) : isShadowRoot(child) ? isParentStage ? error("stage can not attach shadow root") : parentInstance.attachShadow(child) : parentInstance.appendChild(child);
}, insertBeforeChild = (parentInstance, child, beforeChild) => {
const isParentStage = isStage(parentInstance), isChildLayer = isLayer(child), isBeforeChildLayer = isLayer(beforeChild);
if (isParentStage) isChildLayer || isBeforeChildLayer ? isChildLayer && isBeforeChildLayer && parentInstance.insertBefore(child, beforeChild) : parentInstance.defaultLayer ? parentInstance.defaultLayer.insertBefore(child, beforeChild) : error("default layer has been removed "); else if (!isParentStage && isChildLayer) error(`layer can not be inserted into other graphic: ${parentInstance.type}`); else if (isGlyph(parentInstance)) {
const prevSubGraphics = parentInstance.getSubGraphic(), index = prevSubGraphics.indexOf(child), beforeIndex = prevSubGraphics.indexOf(beforeChild);
if (index >= 0 && beforeIndex >= 0 && index !== beforeIndex) {
const newSubGraphics = prevSubGraphics.slice();
newSubGraphics[beforeIndex] = child, newSubGraphics[index] = beforeChild, parentInstance.setSubGraphic(newSubGraphics);
}
} else parentInstance.insertBefore(child, beforeChild);
}, removeChild = (parentInstance, child) => {
const isParentStage = isStage(parentInstance), isChildLayer = isLayer(child);
if (isParentStage && !isChildLayer && parentInstance.defaultLayer) parentInstance.defaultLayer.removeChild(child); else if (isGlyph(parentInstance)) {
const prevSubGraphics = parentInstance.getSubGraphic(), index = prevSubGraphics.indexOf(child);
index >= 0 && parentInstance.setSubGraphic([ ...prevSubGraphics.slice(0, index - 1), ...prevSubGraphics.slice(index + 1) ]);
} else isShadowRoot(child) ? isParentStage ? error("stage can not attach shadow root") : parentInstance.detachShadow(child) : parentInstance.removeChild(child);
}, clearContainer = container => {
if (isStage(container)) for (container.defaultLayer && container.defaultLayer.removeAllChild(); container.childrenCount > 1; ) container.removeChild(container.getChildAt(1)); else container.removeAllChild();
};
export const reconcilor = ReactReconciler({
getPublicInstance: instance => instance,
getRootHostContext(rootContainerInstance) {},
getChildHostContext(parentHostContext, type, rootContainerInstance) {},
prepareForCommit: containerInfo => null,
resetAfterCommit(containerInfo) {},
preparePortalMount(containerInfo) {},
createInstance: (type, props, rootContainerInstance, hostContext, internalInstanceHandle) => createInstance(type, props, rootContainerInstance),
appendInitialChild(parentInstance, child) {
log("appendInitialChild", parentInstance, child), appendChild(parentInstance, child);
},
finalizeInitialChildren: (parentInstance, type, props, rootContainerInstance, hostContext) => !1,
prepareUpdate: (instance, type, oldProps, newProps, rootContainerInstance, hostContext) => !0,
shouldSetTextContent: (type, props) => !1,
createTextInstance: (text, rootContainerInstance, hostContext, internalInstanceHandle) => createText({
text: text
}),
scheduleTimeout(handler, timeout) {},
cancelTimeout(handle) {},
noTimeout: void 0,
getCurrentEventPriority: () => DefaultEventPriority,
getInstanceFromNode(node) {
throw new Error("Not implemented.");
},
beforeActiveInstanceBlur() {},
isPrimaryRenderer: !1,
supportsMutation: !0,
supportsPersistence: !1,
supportsHydration: !1,
appendChild(parentInstance, child) {
log("appendChild"), appendChild(parentInstance, child);
},
appendChildToContainer(container, child) {
log("appendChildToContainer", container, child), appendChild(container, child);
},
commitTextUpdate(textInstance, oldText, newText) {},
commitMount(instance, type, newProps, internalInstanceHandle) {},
commitUpdate(instance, updatePayload, type, oldProps, newProps, internalInstanceHandle) {
log("commitUpdate", instance, newProps), updateProps(instance, newProps, oldProps);
},
insertBefore(parentInstance, child, beforeChild) {
insertBeforeChild(parentInstance, child, beforeChild);
},
insertInContainerBefore(container, child, beforeChild) {
insertBeforeChild(container, child, beforeChild);
},
removeChild(parentInstance, child) {
log("removeChild", parentInstance, child), removeChild(parentInstance, child);
},
removeChildFromContainer(container, child) {
removeChild(container, child);
},
resetTextContent(instance) {},
hideInstance(instance) {},
hideTextInstance(textInstance) {},
unhideInstance(instance, props) {},
unhideTextInstance(textInstance, text) {},
clearContainer(container) {
clearContainer(container);
},
cloneInstance: (instance, updatePayload, type, oldProps, newProps, internalInstanceHandle, keepChildren, recyclableInstance) => instance,
createContainerChildSet(container) {},
appendChildToContainerChildSet(childSet, child) {},
finalizeContainerChildren(container, newChildren) {},
replaceContainerChildren(container, newChildren) {},
canHydrateInstance: (instance, type, props) => instance,
canHydrateTextInstance: (instance, text) => null,
getNextHydratableSibling(instance) {},
getFirstHydratableChild(parentInstance) {},
hydrateInstance(instance, type, props, rootContainerInstance, hostContext, internalInstanceHandle) {},
hydrateTextInstance: (textInstance, text, internalInstanceHandle) => !1,
didNotMatchHydratedContainerTextInstance(parentContainer, textInstance, text) {},
didNotMatchHydratedTextInstance(parentType, parentProps, parentInstance, textInstance, text) {},
didNotHydrateContainerInstance(parentContainer, instance) {},
didNotHydrateInstance(parentType, parentProps, parentInstance, instance) {},
didNotFindHydratableContainerInstance(parentContainer, type, props) {},
didNotFindHydratableContainerTextInstance(parentContainer, text) {},
didNotFindHydratableInstance(parentType, parentProps, parentInstance, type, props) {},
didNotFindHydratableTextInstance(parentType, parentProps, parentInstance, text) {},
afterActiveInstanceBlur() {},
prepareScopeUpdate() {},
getInstanceFromScope: () => null,
detachDeletedInstance() {}
});
reconcilor.injectIntoDevTools({
bundleType: "production" !== process.env.NODE_ENV ? 1 : 0,
version: React.version,
rendererPackageName: "react-g",
rendererConfig: {
getInspectorDataForViewTag: tag => {
console.log(tag);
}
}
});
export const render = (component, target, callback) => {
const container = reconcilor.createContainer(target, 1, !1, null);
reconcilor.updateContainer(component, container, null, callback);
};
//# sourceMappingURL=hostConfig.js.map