create-dan
Version:
Dan frameworks
1 lines • 4.15 kB
JSON
{"ast":null,"code":"\"use strict\";\n\nvar _classCallCheck = require(\"@babel/runtime-corejs2/helpers/classCallCheck\");\n\nvar _createClass = require(\"@babel/runtime-corejs2/helpers/createClass\");\n\nvar _interopRequireDefault = require(\"@babel/runtime-corejs2/helpers/interopRequireDefault\");\n\nexports.__esModule = true;\nexports[\"default\"] = void 0;\n\nvar _promise = _interopRequireDefault(require(\"@babel/runtime-corejs2/core-js/promise\"));\n\nvar DOMAttributeNames = {\n acceptCharset: 'accept-charset',\n className: 'class',\n htmlFor: 'for',\n httpEquiv: 'http-equiv'\n};\n\nvar HeadManager =\n/*#__PURE__*/\nfunction () {\n function HeadManager() {\n var _this = this;\n\n _classCallCheck(this, HeadManager);\n\n this.updateHead = function (head) {\n var promise = _this.updatePromise = _promise[\"default\"].resolve().then(function () {\n if (promise !== _this.updatePromise) return;\n _this.updatePromise = null;\n\n _this.doUpdateHead(head);\n });\n };\n\n this.updatePromise = null;\n }\n\n _createClass(HeadManager, [{\n key: \"doUpdateHead\",\n value: function doUpdateHead(head) {\n var _this2 = this;\n\n var tags = {};\n head.forEach(function (h) {\n var components = tags[h.type] || [];\n components.push(h);\n tags[h.type] = components;\n });\n this.updateTitle(tags.title ? tags.title[0] : null);\n var types = ['meta', 'base', 'link', 'style', 'script'];\n types.forEach(function (type) {\n _this2.updateElements(type, tags[type] || []);\n });\n }\n }, {\n key: \"updateTitle\",\n value: function updateTitle(component) {\n var title = '';\n\n if (component) {\n var children = component.props.children;\n title = typeof children === 'string' ? children : children.join('');\n }\n\n if (title !== document.title) document.title = title;\n }\n }, {\n key: \"updateElements\",\n value: function updateElements(type, components) {\n var headEl = document.getElementsByTagName('head')[0];\n var headCountEl = headEl.querySelector('meta[name=next-head-count]');\n\n if (false) {\n if (!headCountEl) {\n console.error('Warning: next-head-count is missing. https://err.sh/next.js/next-head-count-missing');\n return;\n }\n }\n\n var headCount = Number(headCountEl.content);\n var oldTags = [];\n\n for (var i = 0, j = headCountEl.previousElementSibling; i < headCount; i++, j = j.previousElementSibling) {\n if (j.tagName.toLowerCase() === type) {\n oldTags.push(j);\n }\n }\n\n var newTags = components.map(reactElementToDOM).filter(function (newTag) {\n for (var k = 0, len = oldTags.length; k < len; k++) {\n var oldTag = oldTags[k];\n\n if (oldTag.isEqualNode(newTag)) {\n oldTags.splice(k, 1);\n return false;\n }\n }\n\n return true;\n });\n oldTags.forEach(function (t) {\n return t.parentNode.removeChild(t);\n });\n newTags.forEach(function (t) {\n return headEl.insertBefore(t, headCountEl);\n });\n headCountEl.content = (headCount - oldTags.length + newTags.length).toString();\n }\n }]);\n\n return HeadManager;\n}();\n\nexports[\"default\"] = HeadManager;\n\nfunction reactElementToDOM(_ref) {\n var type = _ref.type,\n props = _ref.props;\n var el = document.createElement(type);\n\n for (var p in props) {\n if (!props.hasOwnProperty(p)) continue;\n if (p === 'children' || p === 'dangerouslySetInnerHTML') continue;\n var attr = DOMAttributeNames[p] || p.toLowerCase();\n el.setAttribute(attr, props[p]);\n }\n\n var children = props.children,\n dangerouslySetInnerHTML = props.dangerouslySetInnerHTML;\n\n if (dangerouslySetInnerHTML) {\n el.innerHTML = dangerouslySetInnerHTML.__html || '';\n } else if (children) {\n el.textContent = typeof children === 'string' ? children : children.join('');\n }\n\n return el;\n}","map":null,"metadata":{},"sourceType":"script"}