awv3
Version:
⚡ AWV3 embedded CAD
171 lines (156 loc) • 5.85 kB
JavaScript
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _extends from "@babel/runtime/helpers/extends";
var _class, _temp;
import React from 'react';
import PropTypes from 'prop-types';
import Reconciler from 'react-reconciler';
import omit from 'lodash/omit';
import capitalize from 'lodash/capitalize';
import { actions as elementActions } from './store/elements';
import * as Elements from './elements';
var roots = new Map();
var emptyObject = {};
function applyProps(instance, newProps, oldProps) {
// Filter equals, events and reserved props
var sameProps = Object.keys(newProps).filter(function (key) {
return newProps[key] === oldProps[key];
});
var handlers = Object.keys(newProps).filter(function (key) {
return typeof newProps[key] === 'function' && key.startsWith('on');
});
var filteredProps = omit(newProps, sameProps.concat(handlers, ['children', 'key', 'ref']));
if (Object.keys(filteredProps).length > 0) {
// Set props
instance.session.dispatch(elementActions.update(instance.id, filteredProps)); // Set events
instance.unsubscribes = handlers.reduce(function (acc, key) {
var _extends2;
var name = key.charAt(2).toLowerCase() + key.substr(3); // Remove old events and return new unsubscribe
if (instance.unsubscribes && instance.unsubscribes[key]) instance.removeSubscription(instance.unsubscribes[key]);
return _extends({}, acc, (_extends2 = {}, _extends2[key] = instance.observe(function (state) {
return state[name];
}, function (state, old) {
return !instance.busy && newProps[key](state, old);
}), _extends2));
}, {});
}
}
var Renderer = Reconciler({
useSyncScheduling: true,
now: function now() {
return performance.now();
},
createInstance: function createInstance(type, props, rootContainerInstance, hostContext, internalInstanceHandle) {
var instance = new Elements[capitalize(type)](rootContainerInstance, {});
applyProps(instance, props, {});
return instance;
},
createTextInstance: function createTextInstance() {},
appendInitialChild: function appendInitialChild(parentInstance, child) {
if (child) parentInstance.children = parentInstance.children.concat([child.id]);
},
finalizeInitialChildren: function finalizeInitialChildren(instance, type, props, rootContainerInstance) {
return false;
},
prepareUpdate: function prepareUpdate(instance, type, oldProps, newProps, rootContainerInstance, hostContext) {
return emptyObject;
},
getRootHostContext: function getRootHostContext(rootContainerInstance) {
return emptyObject;
},
shouldDeprioritizeSubtree: function shouldDeprioritizeSubtree(type, props) {
return false;
},
getChildHostContext: function getChildHostContext(parentHostContext, type) {
return emptyObject;
},
getPublicInstance: function getPublicInstance(instance) {
return instance;
},
prepareForCommit: function prepareForCommit() {},
resetAfterCommit: function resetAfterCommit() {},
shouldSetTextContent: function shouldSetTextContent(props) {
return false;
},
mutation: {
appendChild: function appendChild(parentInstance, child) {
if (child) parentInstance.children = parentInstance.children.concat([child.id]);
},
appendChildToContainer: function appendChildToContainer(parentInstance, child) {
if (child) parentInstance.addElement(child);
},
insertBefore: function insertBefore(parentInstance, child, beforeChild) {
if (child) {
var index = parentInstance.children.indexOf(beforeChild.id);
parentInstance.children = parentInstance.children.slice(0, index).concat([child.id], parentInstance.children.slice(index));
}
},
removeChild: function removeChild(parentInstance, child) {
if (child) {
parentInstance.children = parentInstance.children.filter(function (id) {
return id !== child.id;
});
child.destroy();
child.unsubscribes = undefined;
}
},
removeChildFromContainer: function removeChildFromContainer(parentInstance, child) {
if (child) {
parentInstance.removeElement(child.id);
child.destroy();
child.unsubscribes = undefined;
}
},
commitUpdate: function commitUpdate(instance, updatePayload, type, oldProps, newProps) {
instance.busy = true;
applyProps(instance, newProps, oldProps);
instance.busy = false;
}
}
});
export var Provider = (_temp = _class =
/*#__PURE__*/
function (_React$Component) {
_inheritsLoose(Provider, _React$Component);
function Provider() {
return _React$Component.apply(this, arguments) || this;
}
var _proto = Provider.prototype;
_proto.getChildContext = function getChildContext() {
var plugin = this.props.plugin;
return {
plugin: plugin,
session: plugin.session,
store: plugin.session.store
};
};
_proto.render = function render() {
return this.props.children;
};
return Provider;
}(React.Component), Object.defineProperty(_class, "childContextTypes", {
configurable: true,
enumerable: true,
writable: true,
value: {
plugin: PropTypes.object,
session: PropTypes.object,
store: PropTypes.object
}
}), _temp);
export default {
render: function render(element, container) {
var root = roots.get(container);
if (!root) {
root = Renderer.createContainer(container);
roots.set(container, root);
}
Renderer.updateContainer(element, root, null, undefined);
return Renderer.getPublicRootInstance(root);
},
unmountComponentAtNode: function unmountComponentAtNode(container) {
var root = roots.get(container);
if (root) Renderer.updateContainer(null, root, null, function () {
return roots.delete(container);
});
}
};