UNPKG

@roderickhsiao/react-i13n

Version:

[Experiment] React I13n provides a performant and scalable solution to application instrumentation.

117 lines (97 loc) 4.57 kB
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } /** * Copyright 2015 - Present, Yahoo Inc. * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. */ import { useEffect, useState } from 'react'; import { findDOMNode } from 'react-dom'; import { listen } from 'subscribe-ui-event'; import { IS_DEBUG_MODE } from '../utils/variables'; import clickHandler from '../libs/clickHandler'; import DebugDashboard from '../libs/DebugDashboard'; import I13nNode from '../core/I13nNode'; var DEFAULT_SCAN_TAGS = ['a', 'button']; /** * Scan matched tags and * 1. create i13n node * 2. bind the click event * 3. fire created event * 4. (if enabled) create debug node for it */ var useScanLinks = (_ref) => { var { enabled, executeEvent, i13nInstance, i13nNode: parentI13nNode, node, shouldFollowLink, tags = DEFAULT_SCAN_TAGS } = _ref; var [subI13nComponents, setSubI13nComponents] = useState([]); if (!enabled) { return; } useEffect(() => { var DOMNode = findDOMNode(node); if (!DOMNode) { return; } var foundElements = []; // find all links tags.forEach(tagName => { var collections = DOMNode.getElementsByTagName(tagName); if (collections) { foundElements = foundElements.concat(_toConsumableArray(collections)); } }); // for each link // 1. create a i13n node // 2. bind the click event // 3. fire created event // 4. (if enabled) create debug node for it var newSubI13nComponents = foundElements.map(element => { var I13nNodeClass = i13nInstance.getI13nNodeClass() || I13nNode; var subI13nNode = new I13nNodeClass(parentI13nNode, {}, true, i13nInstance.isViewportEnabled()); subI13nNode.setDOMNode(element); var handleClick = e => { clickHandler(e, { executeEvent, i13nNode: subI13nNode, props: { href: element.href, follow: true }, shouldFollowLink }); }; i13nInstance.execute('created', { i13nNode: subI13nNode }); return { componentClickListener: listen(element, 'click', handleClick), debugDashboard: IS_DEBUG_MODE ? new DebugDashboard(subI13nNode) : null, domElement: element, i13nNode: subI13nNode }; }); if (newSubI13nComponents) { setSubI13nComponents(newSubI13nComponents); } return () => { subI13nComponents.forEach(subI13nComponent => { var _subI13nComponent$com, _subI13nComponent$vie, _subI13nComponent$deb; subI13nComponent === null || subI13nComponent === void 0 ? void 0 : (_subI13nComponent$com = subI13nComponent.componentClickListener) === null || _subI13nComponent$com === void 0 ? void 0 : _subI13nComponent$com.remove(); subI13nComponent === null || subI13nComponent === void 0 ? void 0 : (_subI13nComponent$vie = subI13nComponent.viewportDetector) === null || _subI13nComponent$vie === void 0 ? void 0 : _subI13nComponent$vie.unsubscribeAll(); subI13nComponent === null || subI13nComponent === void 0 ? void 0 : (_subI13nComponent$deb = subI13nComponent.debugDashboard) === null || _subI13nComponent$deb === void 0 ? void 0 : _subI13nComponent$deb.destroy(); }); }; }, [tags, node, parentI13nNode, shouldFollowLink]); return { subI13nComponents }; }; export default useScanLinks;