UNPKG

@danilandreev/material-docs

Version:

material-docs - react framework for easy creating documentation site in material design style.

166 lines (132 loc) 9.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.displayName = void 0; var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _useGroups2 = _interopRequireWildcard(require("../../hooks/useGroups")); function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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 _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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure 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(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } 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; } function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } var displayName = "MatDocPagesGroup"; exports.displayName = displayName; var PagesGroup = /*#__PURE__*/_react.default.forwardRef(function PagesGroup(props, ref) { var name = props.name, children = props.children, getData = props.getData, other = _objectWithoutProperties(props, ["name", "children", "getData"]); if (getData && typeof getData !== "function") throw new TypeError("MaterialDocs: incorrect type of getData, expected function, got ".concat(_typeof(getData))); if (typeof name !== "string") throw new TypeError("MaterialDocs: incorrect type of required prop name, expected string, got ".concat(_typeof(name))); var _React$useState = _react.default.useState([]), _React$useState2 = _slicedToArray(_React$useState, 2), pages = _React$useState2[0], setPages = _React$useState2[1]; var _React$useState3 = _react.default.useState([]), _React$useState4 = _slicedToArray(_React$useState3, 2), groups = _React$useState4[0], setGroups = _React$useState4[1]; var _useGroups = (0, _useGroups2.default)(), addGroup = _useGroups.addGroup, deleteGroup = _useGroups.deleteGroup, parentPath = _useGroups.path; var groupPath = getData ? [] : [].concat(_toConsumableArray(parentPath || []), [name]); _react.default.useEffect(function () { var group = { name: name, pages: pages, groups: groups, path: groupPath }; !getData && addGroup(group); getData && getData(group); // return () => !getData && deleteGroup(group); }, [name, pages, groups]); function _addPage(page) { if (_typeof(page) !== "object") throw new TypeError("MaterialDocs: incorrect page type, expected {name: string, link: string | object}, got ".concat(_typeof(page))); if (typeof page.name !== "string") throw new TypeError("MaterialDocs: incorrect page.name type, expected string, got ".concat(_typeof(page.name))); if (typeof page.link !== "string") throw new TypeError("MaterialDocs: incorrect page.link type, expected string, got ".concat(_typeof(page.name))); setPages(function (prev) { var newPages = prev.map(function (item) { return item.name === page.name ? null : item; }).filter(function (item) { return item; }); newPages.push(page); return newPages; }); } function _deletePage(page) { if (!(_typeof(page) === "object" || typeof page === "string")) throw new TypeError("MaterialDocs: incorrect page type, expected {name: string, link?: string | object} | string, got ".concat(_typeof(page))); if (_typeof(page) === "object" && typeof page.name !== "string") throw new TypeError("MaterialDocs: incorrect page.name type, expected string, got ".concat(_typeof(page.name))); setPages(function (prev) { var name = _typeof(page) === "object" ? page.name : page; return prev.map(function (item) { return item.name === name ? item : null; }).filter(function (item) { return item; }); }); } function _addGroup(group) { if (_typeof(group) !== "object") throw new TypeError("MaterialDocs: incorrect group type, expected {name: string, pages: any[], groups: any[]}, got ".concat(_typeof(group))); if (typeof group.name !== "string") throw new TypeError("MaterialDocs: incorrect group.name type, expected string, got ".concat(_typeof(group.name))); if (!Array.isArray(group.pages)) throw new TypeError("MaterialDocs: incorrect group.pages type, expected any[], got ".concat(_typeof(group.pages))); if (!Array.isArray(group.groups)) throw new TypeError("MaterialDocs: incorrect group.groups type, expected any[], got ".concat(_typeof(group.groups))); setGroups(function (prev) { var newGroups = prev.map(function (item) { return item.name === group.name ? null : item; }).filter(function (item) { return item; }); newGroups.push(group); return newGroups; }); } function _deleteGroup(group) { if (!(_typeof(group) === "object" || typeof group === "string")) throw new TypeError("MaterialDocs: incorrect group type, expected {name: string, pages: any[], groups: any[]} | string, got ".concat(_typeof(group))); if (_typeof(group) === "object" && typeof group.name !== "string") throw new TypeError("MaterialDocs: incorrect group.name type, expected string, got ".concat(_typeof(group.name))); setGroups(function (prev) { var name = _typeof(group) === "object" ? group.name : group; return prev.map(function (item) { return item.name === name ? item : null; }).filter(function (item) { return item; }); }); } return /*#__PURE__*/_react.default.createElement(_useGroups2.GroupsContext.Provider, { value: { pages: pages, groups: groups, name: name, path: groupPath, addPage: _addPage, deletePage: _deletePage, addGroup: _addGroup, deleteGroup: _deleteGroup } }, children); }); PagesGroup.displayName = displayName; PagesGroup.propTypes = { // PagesGroupProps name: _propTypes.default.string, getData: _propTypes.default.func, // Containerable children: _propTypes.default.node }; var _default = PagesGroup; exports.default = _default;