UNPKG

react-reparenting

Version:
9 lines (8 loc) 8.96 kB
/** * React-reparenting v0.6.1 * https://paol-imi.github.io/react-reparenting * Copyright (c) 2020-present, Paol-imi * https://github.com/Paol-imi/react-reparenting/blob/master/LICENSE * @license MIT */ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),n=require("@babel/runtime/helpers/classCallCheck"),t=require("@babel/runtime/helpers/assertThisInitialized"),r=require("@babel/runtime/helpers/inherits"),i=require("@babel/runtime/helpers/possibleConstructorReturn"),u=require("@babel/runtime/helpers/getPrototypeOf"),l=require("@babel/runtime/helpers/wrapNativeSuper"),o=require("@babel/runtime/helpers/defineProperty"),a=require("@babel/runtime/helpers/createClass"),f=require("@babel/runtime/helpers/typeof");function c(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var s=c(e),d=c(n),p=c(t),b=c(r),h=c(i),v=c(u),m=c(l),y=c(o),g=c(a),F=c(f),C={appendChildToContainer:function(e,n){e.appendChild(n)},insertInContainerBefore:function(e,n,t){e.insertBefore(n,t)},removeChildFromContainer:function(e,n){e.removeChild(n)},isElement:function(e,n){return n instanceof Element}};function x(e,n){var t=e.child;if(null===t)return null;if(-1===n)for(;t.sibling;)t=t.sibling;else for(;t.sibling&&n>0;)n-=1,t=t.sibling;return t}function R(e,n){var t=e.child;if(null===t)return null;if(t.key===n)return e;for(var r=t.sibling;r;){if(r.key===n)return t;r=(t=r).sibling}return null}function k(e,n){for(var t=e.sibling;t&&t.key!==n;)t=t.sibling;return t}function N(e,n,t){if(-1===t)return I(e,n);if(0===t)return _(e,n);var r=x(e,t-1);return null===r?_(e,n):P(r,n)}function E(e,n,t){var r=R(e,t);return null===r?I(e,n):r===e?_(e,n):P(r,n)}function I(e,n){var t=x(e,-1);return null===t?_(e,n):P(t,n)}function P(e,n){var t=e.sibling,r=e.index+1;return e.sibling=n,n.return=e.return,n.sibling=t,r}function _(e,n){var t=e.child;return e.child=n,n.sibling=t,n.return=e,0}var w=s.default.version.startsWith("16"),q={nodePrefix:w?"__reactInternalInstance$":"__reactFiber$",componentAttribute:w?"_reactInternalFiber":"_reactInternals"};function D(e){var n=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(e){return!1}}();return function(){var t,r=v.default(e);if(n){var i=v.default(this).constructor;t=Reflect.construct(r,arguments,i)}else t=r.apply(this,arguments);return h.default(this,t)}}var S=function(e){b.default(t,e);var n=D(t);function t(){var e;d.default(this,t);for(var r=arguments.length,i=new Array(r),u=0;u<r;u++)i[u]=arguments[u];return e=n.call.apply(n,[this].concat(i)),y.default(p.default(e),"name","Invariant"),e}return t}(m.default(Error));function A(e,n){if(!e)throw new S("Invariant failed")}function T(e,n,t){for(;e;){if(t(e))return e;e=n(e)}return null}function j(e){if(null===e.alternate)return e;for(var n=e;null!==n.return;)n=n.return;return A(null!==n.stateNode&&"current"in n.stateNode),n.stateNode.current===n?e:e.alternate}function M(e){var n=Object.keys(e).find((function(e){return e.startsWith(q.nodePrefix)}));return A("string"==typeof n),e[n]}function O(e){return A(q.componentAttribute in e),e[q.componentAttribute]}function W(e,n){for(;e;)e.index=n,e=e.sibling,n+=1;return n-1}function B(e,n,t,r){var i;if(A("number"!=typeof t||t>=-1),W(n,i="number"==typeof t?N(e,n,t):E(e,n,t)),null===n.alternate||null===e.alternate?null!==n.alternate&&(n.alternate.return=null,n.alternate.sibling=null):("number"==typeof t?N(e.alternate,n.alternate,t):E(e.alternate,n.alternate,t),W(n.alternate,i)),r)return i;var u=T(e,(function(e){return e.return}),(function(e){return C.isElement(e.elementType,e.stateNode)})),l=T(n,(function(e){return e.child}),(function(e){return C.isElement(e.elementType,e.stateNode)}));if(null===u)return i;if(null===l)return i;var o=u.stateNode,a=l.stateNode;if(null===n.sibling)C.appendChildToContainer(o,a);else{var f=T(n.sibling,(function(e){return e.child}),(function(e){return C.isElement(e.elementType,e.stateNode)}));if(null!==f){var c=f.stateNode;C.insertInContainerBefore(o,a,c)}}return i}function U(e,n){if(0===n)return z(e);var t=x(e,n-1);return null===t?null:G(t)}function $(e,n){var t=R(e,n);return null===t?null:t===e?z(e):G(t)}function z(e){var n=e.child;return null===n?null:(e.child=n.sibling,n)}function G(e){var n=e.sibling;return null===n?null:(e.sibling=n.sibling,n)}function H(e,n,t){A("number"!=typeof n||n>=0);var r=null;if(null===(r="number"==typeof n?U(e,n):$(e,n)))return null;if(null!==r.sibling&&W(r.sibling,r.index),null!==r.alternate&&null!==e.alternate){var i=null;A(null!==(i="number"==typeof n?U(e.alternate,n):$(e.alternate,n))),null!==i.sibling&&W(i.sibling,i.index)}if(t)return r;var u=T(e,(function(e){return e.return}),(function(e){return C.isElement(e.elementType,e.stateNode)})),l=T(r,(function(e){return e.child}),(function(e){return C.isElement(e.elementType,e.stateNode)}));if(null===u)return r;if(null===l)return r;var o=u.stateNode,a=l.stateNode;return C.removeChildFromContainer(o,a),r}var J=function(){function e(){d.default(this,e),y.default(this,"fiber",null),y.default(this,"findFiber",void 0)}return g.default(e,[{key:"setFiber",value:function(e){this.fiber=e}},{key:"setFinder",value:function(e){this.findFiber=e}},{key:"getCurrent",value:function(){A(null!==this.fiber);var e=j(this.fiber);return"function"==typeof this.findFiber?this.findFiber(e):e}},{key:"addChild",value:function(e,n,t){return B(this.getCurrent(),e,n,t)}},{key:"removeChild",value:function(e,n){return H(this.getCurrent(),e,n)}},{key:"sendChild",value:function(e,n,t,r){var i=this.removeChild(n,r);return null===i?-1:e.addChild(i,t,r)}},{key:"clear",value:function(){this.fiber=null}}]),e}();function K(e){var n=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(e){return!1}}();return function(){var t,r=v.default(e);if(n){var i=v.default(this).constructor;t=Reflect.construct(r,arguments,i)}else t=r.apply(this,arguments);return h.default(this,t)}}var L=function(e){b.default(t,e);var n=K(t);function t(){var e;d.default(this,t);for(var r=arguments.length,i=new Array(r),u=0;u<r;u++)i[u]=arguments[u];return e=n.call.apply(n,[this].concat(i)),y.default(p.default(e),"parent",new J),e}return g.default(t,[{key:"componentDidMount",value:function(){var e=this.props,n=e.parentRef,t=e.findFiber,r=O(this);A(null!==n&&("function"==typeof n||"object"===F.default(n))),this.parent.setFiber(r),this.parent.setFinder(t),"function"==typeof n&&n(this.parent),"object"===F.default(n)&&null!==n&&(n.current=this.parent)}},{key:"componentDidUpdate",value:function(){var e=this.props.findFiber;this.parent.setFinder(e)}},{key:"componentWillUnmount",value:function(){this.parent.clear()}},{key:"render",value:function(){return this.props.children}}]),t}(e.Component);exports.Env=C,exports.Int=q,exports.Parent=L,exports.ParentFiber=J,exports.addChild=B,exports.addChildFiberAt=N,exports.addChildFiberBefore=E,exports.addSiblingFiber=P,exports.appendChildFiber=I,exports.configure=function(e){return Object.assign(C,e)},exports.createParent=function(e,n){var t=new J,r=e.componentDidMount,i=e.componentWillUnmount;return e.componentDidMount=function(){var i=O(e);t.setFiber(i),t.setFinder(n),"function"==typeof r&&r.call(this)},e.componentWillUnmount=function(){"function"==typeof i&&i.call(this),t.clear()},t},exports.createReparentableSpace=function(){var n=new Map;return{Reparentable:function(t){var r=t.id,i=t.children,u=t.findFiber,l=e.useRef(null);return e.useEffect((function(){return A("string"==typeof r),A(null!==l.current),n.set(r,l.current),function(){n.delete(r)}}),[]),s.default.createElement(L,{parentRef:l,findFiber:u},i)},sendReparentableChild:function(e,t,r,i,u){var l=n.get(e),o=n.get(t);return void 0===l||void 0===o?-1:l.sendChild(o,r,i,u)},ReparentableMap:n}},exports.findChildFiber=function(e,n){var t=e.child;return null===t?null:t.key===n?t:k(t,n)},exports.findChildFiberAt=x,exports.findPreviousFiber=R,exports.findSiblingFiber=k,exports.getCurrentFiber=j,exports.getFiberFromClassInstance=O,exports.getFiberFromElementInstance=M,exports.getFiberFromPath=T,exports.prependChildFiber=_,exports.removeChild=H,exports.removeChildFiber=$,exports.removeChildFiberAt=U,exports.removeFirstChildFiber=z,exports.removeSiblingFiber=G,exports.updateFiberDebugFields=function(e,n){var t;t=n.child===e?null===e.sibling?n:e.sibling:n.child||n,e._debugOwner=t._debugOwner,e._debugSource=t._debugSource},exports.updateFibersIndex=W,exports.useParent=function(n,t){var r=e.useRef(null);null===r.current&&(r.current=new J);var i=r.current;return i.setFinder(t),e.useEffect((function(){return A(null!==n.current&&void 0!==n.current),i.setFiber(M(n.current)),function(){i.clear()}}),[]),i};