@redocly/theme
Version:
Shared UI components lib
55 lines • 3.21 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadAndNavigate = loadAndNavigate;
const get_navbar_element_1 = require("./get-navbar-element");
const with_load_progress_1 = require("./with-load-progress");
let lastNavigatedPath;
// this is copy from portal/src/client/app/utils/loadAndNavigate.ts, for case when we need to run Redoc without Realm
function loadAndNavigate(_a) {
return __awaiter(this, arguments, void 0, function* ({ navigate, to, origin = 'browser', options, }) {
var _b, _c;
lastNavigatedPath = to;
const { pathname, hash, search } = new URL(to, window.location.origin + window.location.pathname);
// use window-shared loader instead of importing to prevent circular import issue
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
const loader = window['__LOADER'];
const result = yield (0, with_load_progress_1.withLoadProgress)(loader.tryLoad(pathname));
// @ts-ignore
if (result === null || result === void 0 ? void 0 : result.redirectTo) {
// @ts-ignore
return loadAndNavigate({ navigate, to: result.redirectTo, origin, options });
}
if (result && lastNavigatedPath === to) {
if (pathname !== window.location.pathname || search !== window.location.search) {
// origin is used to prevent infinite loop when navigating from post message (from wysiwyg)
navigate({ pathname, search, hash }, Object.assign(Object.assign({}, options), { state: { origin }, unstable_flushSync: true }));
}
// @ts-ignore
if ((_b = result.props) === null || _b === void 0 ? void 0 : _b.disableAutoScroll)
return;
if (hash) {
const el = document.getElementById(hash.slice(1));
if (el) {
const navbar = (0, get_navbar_element_1.getNavbarElement)();
const navbarHeight = (_c = navbar === null || navbar === void 0 ? void 0 : navbar.offsetHeight) !== null && _c !== void 0 ? _c : 0;
const elementTop = el.getBoundingClientRect().top + window.scrollY;
const scrollPosition = elementTop - navbarHeight;
window.scrollTo({ top: scrollPosition, behavior: 'smooth' });
}
}
else {
window.scrollTo(0, 0);
}
}
});
}
//# sourceMappingURL=load-and-navigate.js.map