|
(() => { |
|
"use strict"; |
|
|
|
; |
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } |
|
function _regenerator() { var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); } |
|
function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { if (r) i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n;else { var o = function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); }; o("next", 0), o("throw", 1), o("return", 2); } }, _regeneratorDefine2(e, r, n, t); } |
|
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } |
|
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } |
|
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } |
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } |
|
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } |
|
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); } |
|
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; } |
|
function loadFragments() { |
|
return _loadFragments.apply(this, arguments); |
|
} |
|
function _loadFragments() { |
|
_loadFragments = _asyncToGenerator(_regenerator().m(function _callee7() { |
|
var fragmentElements, FetchQueue, fetchQueue, currentIndex, elements; |
|
return _regenerator().w(function (_context7) { |
|
while (1) switch (_context7.n) { |
|
case 0: |
|
|
|
fragmentElements = Array.from(document.querySelectorAll('[id^="fragment-"]')); |
|
FetchQueue = function () { |
|
function FetchQueue() { |
|
var maxConcurrent = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 3; |
|
_classCallCheck(this, FetchQueue); |
|
this.queue = []; |
|
this.maxConcurrent = maxConcurrent; |
|
this.activeFetches = 0; |
|
this.maxRetries = 3; |
|
this.baseDelay = 1000; |
|
} |
|
return _createClass(FetchQueue, [{ |
|
key: "sleep", |
|
value: function () { |
|
var _sleep = _asyncToGenerator(_regenerator().m(function _callee(ms) { |
|
return _regenerator().w(function (_context) { |
|
while (1) switch (_context.n) { |
|
case 0: |
|
return _context.a(2, new Promise(function (resolve) { |
|
return setTimeout(resolve, ms); |
|
})); |
|
} |
|
}, _callee); |
|
})); |
|
function sleep(_x) { |
|
return _sleep.apply(this, arguments); |
|
} |
|
return sleep; |
|
}() |
|
}, { |
|
key: "fetchWithRetry", |
|
value: function () { |
|
var _fetchWithRetry = _asyncToGenerator(_regenerator().m(function _callee2(fragmentPath) { |
|
var retryCount, |
|
response, |
|
delay, |
|
_args2 = arguments, |
|
_t; |
|
return _regenerator().w(function (_context2) { |
|
while (1) switch (_context2.n) { |
|
case 0: |
|
retryCount = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : 0; |
|
_context2.p = 1; |
|
_context2.n = 2; |
|
return fetch(fragmentPath); |
|
case 2: |
|
response = _context2.v; |
|
if (response.ok) { |
|
_context2.n = 3; |
|
break; |
|
} |
|
throw new Error("HTTP error! status: ".concat(response.status)); |
|
case 3: |
|
_context2.n = 4; |
|
return response.text(); |
|
case 4: |
|
return _context2.a(2, _context2.v); |
|
case 5: |
|
_context2.p = 5; |
|
_t = _context2.v; |
|
if (!(retryCount < this.maxRetries)) { |
|
_context2.n = 7; |
|
break; |
|
} |
|
|
|
delay = this.baseDelay * Math.pow(2, retryCount); |
|
console.warn("Retry ".concat(retryCount + 1, "/").concat(this.maxRetries, " for ").concat(fragmentPath, " after ").concat(delay, "ms")); |
|
_context2.n = 6; |
|
return this.sleep(delay); |
|
case 6: |
|
return _context2.a(2, this.fetchWithRetry(fragmentPath, retryCount + 1)); |
|
case 7: |
|
throw _t; |
|
case 8: |
|
return _context2.a(2); |
|
} |
|
}, _callee2, this, [[1, 5]]); |
|
})); |
|
function fetchWithRetry(_x2) { |
|
return _fetchWithRetry.apply(this, arguments); |
|
} |
|
return fetchWithRetry; |
|
}() |
|
}, { |
|
key: "addFetch", |
|
value: function () { |
|
var _addFetch = _asyncToGenerator(_regenerator().m(function _callee5(element) { |
|
var _this = this; |
|
var fragmentName, fragmentPath; |
|
return _regenerator().w(function (_context5) { |
|
while (1) switch (_context5.n) { |
|
case 0: |
|
fragmentName = element.id.replace('fragment-', ''); |
|
fragmentPath = "fragments/".concat(fragmentName, ".html"); |
|
return _context5.a(2, new Promise(function () { |
|
var _ref = _asyncToGenerator(_regenerator().m(function _callee4(resolve, reject) { |
|
var fetchPromise; |
|
return _regenerator().w(function (_context4) { |
|
while (1) switch (_context4.n) { |
|
case 0: |
|
try { |
|
fetchPromise = _asyncToGenerator(_regenerator().m(function _callee3() { |
|
var html, temp, scripts, _t2; |
|
return _regenerator().w(function (_context3) { |
|
while (1) switch (_context3.n) { |
|
case 0: |
|
_context3.p = 0; |
|
_context3.n = 1; |
|
return _this.fetchWithRetry(fragmentPath); |
|
case 1: |
|
html = _context3.v; |
|
|
|
temp = document.createElement('div'); |
|
temp.innerHTML = html; |
|
element.innerHTML = temp.innerHTML; |
|
|
|
|
|
scripts = temp.getElementsByTagName('script'); |
|
Array.from(scripts).forEach(function (oldScript) { |
|
var newScript = document.createElement('script'); |
|
Array.from(oldScript.attributes).forEach(function (attr) { |
|
newScript.setAttribute(attr.name, attr.value); |
|
}); |
|
newScript.textContent = oldScript.textContent; |
|
oldScript.parentNode.removeChild(oldScript); |
|
document.body.appendChild(newScript); |
|
}); |
|
_this.activeFetches--; |
|
resolve(); |
|
_context3.n = 3; |
|
break; |
|
case 2: |
|
_context3.p = 2; |
|
_t2 = _context3.v; |
|
console.error("Failed to load fragment ".concat(fragmentPath, " after ").concat(_this.maxRetries, " retries:"), _t2); |
|
_this.activeFetches--; |
|
reject(_t2); |
|
case 3: |
|
return _context3.a(2); |
|
} |
|
}, _callee3, null, [[0, 2]]); |
|
}))(); |
|
_this.queue.push(fetchPromise); |
|
_this.activeFetches++; |
|
} catch (error) { |
|
reject(error); |
|
} |
|
case 1: |
|
return _context4.a(2); |
|
} |
|
}, _callee4); |
|
})); |
|
return function (_x4, _x5) { |
|
return _ref.apply(this, arguments); |
|
}; |
|
}())); |
|
} |
|
}, _callee5); |
|
})); |
|
function addFetch(_x3) { |
|
return _addFetch.apply(this, arguments); |
|
} |
|
return addFetch; |
|
}() |
|
}, { |
|
key: "processNext", |
|
value: function () { |
|
var _processNext = _asyncToGenerator(_regenerator().m(function _callee6(element) { |
|
return _regenerator().w(function (_context6) { |
|
while (1) switch (_context6.n) { |
|
case 0: |
|
if (!(this.activeFetches < this.maxConcurrent && element)) { |
|
_context6.n = 1; |
|
break; |
|
} |
|
_context6.n = 1; |
|
return this.addFetch(element); |
|
case 1: |
|
return _context6.a(2); |
|
} |
|
}, _callee6, this); |
|
})); |
|
function processNext(_x6) { |
|
return _processNext.apply(this, arguments); |
|
} |
|
return processNext; |
|
}() |
|
}]); |
|
}(); |
|
fetchQueue = new FetchQueue(3); |
|
currentIndex = 0; |
|
elements = fragmentElements; |
|
|
|
case 1: |
|
if (!(currentIndex < elements.length && currentIndex < 3)) { |
|
_context7.n = 3; |
|
break; |
|
} |
|
_context7.n = 2; |
|
return fetchQueue.processNext(elements[currentIndex]); |
|
case 2: |
|
currentIndex++; |
|
_context7.n = 1; |
|
break; |
|
case 3: |
|
if (!(currentIndex < elements.length)) { |
|
_context7.n = 6; |
|
break; |
|
} |
|
_context7.n = 4; |
|
return Promise.race(fetchQueue.queue); |
|
case 4: |
|
|
|
fetchQueue.queue = fetchQueue.queue.filter(function (p) { |
|
return p.status === 'pending'; |
|
}); |
|
|
|
_context7.n = 5; |
|
return fetchQueue.processNext(elements[currentIndex]); |
|
case 5: |
|
currentIndex++; |
|
_context7.n = 3; |
|
break; |
|
case 6: |
|
_context7.n = 7; |
|
return Promise.all(fetchQueue.queue); |
|
case 7: |
|
return _context7.a(2); |
|
} |
|
}, _callee7); |
|
})); |
|
return _loadFragments.apply(this, arguments); |
|
} |
|
|
|
; |
|
var queryArg = "section"; |
|
function syncHFSpacesURLHash() { |
|
|
|
var hasExplicitRequest = handleExplicitSectionRequest(); |
|
|
|
|
|
updateHashBasedOnHashChange(); |
|
|
|
|
|
setupScrollMonitoring(); |
|
|
|
|
|
|
|
} |
|
function handleExplicitSectionRequest() { |
|
|
|
var urlParams = new URLSearchParams(window.location.search); |
|
var sectionId = urlParams.get(queryArg); |
|
|
|
|
|
if (sectionId) { |
|
var targetElement = document.getElementById(sectionId); |
|
if (targetElement) { |
|
|
|
setTimeout(function () { |
|
targetElement.scrollIntoView(); |
|
history.replaceState(null, null, "#".concat(sectionId)); |
|
}, 100); |
|
} |
|
return true; |
|
} |
|
|
|
|
|
return false; |
|
} |
|
function setupScrollMonitoring() { |
|
|
|
var isScrolling = false; |
|
var lastKnownScrollPosition = 0; |
|
var initialScroll = true; |
|
|
|
|
|
window.addEventListener('scroll', function () { |
|
lastKnownScrollPosition = window.scrollY; |
|
if (!isScrolling) { |
|
window.requestAnimationFrame(function () { |
|
|
|
|
|
if (initialScroll) { |
|
initialScroll = false; |
|
} else { |
|
updateHashBasedOnScroll(lastKnownScrollPosition); |
|
} |
|
isScrolling = false; |
|
}); |
|
} |
|
isScrolling = true; |
|
}); |
|
} |
|
|
|
|
|
function updateHashBasedOnScroll(scrollPosition) { |
|
var closestHeading = findClosestHeading(scrollPosition); |
|
|
|
|
|
if (closestHeading && closestHeading.id) { |
|
|
|
if (window.location.hash !== "#".concat(closestHeading.id)) { |
|
silentlyUpdateHash(closestHeading.id); |
|
postMessageToHFSpaces(closestHeading.id); |
|
} |
|
} |
|
} |
|
|
|
|
|
function findClosestHeading(scrollPosition) { |
|
|
|
var headingsWithIds = Array.from(document.querySelectorAll('h1[id], h2[id], h3[id], h4[id], h5[id], h6[id]')); |
|
|
|
|
|
if (headingsWithIds.length === 0) return null; |
|
|
|
|
|
var closestHeading = null; |
|
var closestDistance = Infinity; |
|
var viewportMiddle = scrollPosition + window.innerHeight / 2; |
|
|
|
|
|
headingsWithIds.forEach(function (heading) { |
|
var headingTop = heading.getBoundingClientRect().top + scrollPosition; |
|
var distance = Math.abs(headingTop - viewportMiddle); |
|
if (distance < closestDistance) { |
|
closestDistance = distance; |
|
closestHeading = heading; |
|
} |
|
}); |
|
return closestHeading; |
|
} |
|
|
|
|
|
function silentlyUpdateHash(id) { |
|
history.replaceState(null, null, "#".concat(id)); |
|
} |
|
function updateHashBasedOnHashChange() { |
|
window.addEventListener('hashchange', function () { |
|
var elementId = window.location.hash.slice(1); |
|
postMessageToHFSpaces(elementId); |
|
}); |
|
} |
|
function postMessageToHFSpaces(elementId) { |
|
var parentOrigin = "https://huggingface.co"; |
|
window.parent.postMessage({ |
|
queryString: "".concat(queryArg, "=").concat(elementId) |
|
}, parentOrigin); |
|
} |
|
|
|
; |
|
|
|
|
|
|
|
document.addEventListener("DOMContentLoaded", function () { |
|
console.log("DOMContentLoaded"); |
|
loadFragments(); |
|
init_memory_plot(); |
|
syncHFSpacesURLHash(); |
|
}, { |
|
once: true |
|
}); |
|
})() |
|
; |
|
|