Skip to content

Commit

Permalink
chore: updated version
Browse files Browse the repository at this point in the history
  • Loading branch information
wll8 committed Jul 27, 2023
1 parent 1dadc5b commit 60509fd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
42 changes: 36 additions & 6 deletions dist/try.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
;
(function (window, undefined) {
window.initTry = window.initTry || initTry;

/**
global variable:
window.initTry
window.initTry.cfg
window.initTry.renderPos
window.initTry.$operation
*/

function initTry(userCfg) {
loadScript("https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js").then(function () {
return loadScript("https://cdn.jsdelivr.net/npm/[email protected]/jquery.scrollTo.min.js");
Expand All @@ -28,7 +40,7 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
return loadScript("https://cdn.jsdelivr.net/npm/[email protected]/index.min.js");
}).then(function () {
var cfg = cfgHandle(userCfg);
window.cfg = cfg;
window.initTry.cfg = cfg;
if (cfg.onlySwagger) {
initSwagger(cfg.swaggerOptions);
$(".swaggerBox").addClass("onlySwagger");
Expand Down Expand Up @@ -150,6 +162,7 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
var $tryBtn = $(this);
$(".swaggerShadow").remove(); // First clear all temporary elements
var $operation = $tryBtn.parents("[data-section-id]").last(); // Get the outermost api box
window.initTry.$operation = $operation;
if ($operation.hasClass("try") === true) {
// If the current API is already in the try state, uninstall and exit the function
$(".swaggerBox").addClass("hide").removeClass("show");
Expand All @@ -162,7 +175,7 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
// The following 3 lines add class names to some necessary elements to facilitate acquisition or identification
$(".try>div>div:nth-child(2)").addClass("apiBlock");
$(".try .apiBlock>div:nth-child(1)").addClass("fullApiBox");
if (window.cfg.redocVersion !== 'next' && window.compareVersions.compare(window.cfg.redocVersion, "2.0.0-rc.32", "<=")) {
if (window.initTry.cfg.redocVersion !== 'next' && window.compareVersions.compare(window.initTry.cfg.redocVersion, "2.0.0-rc.32", "<=")) {
$(".try .apiBlock>div>div:nth-child(1)").addClass("fullApi");
} else {
$(".try .apiBlock>div>button").addClass("fullApi");
Expand Down Expand Up @@ -198,7 +211,7 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
var getShadowPos = function getShadowPos() {
// Get the position of swaggerShadow
var pos = {};
pos = getAbsolutePosition($(".try .swaggerShadow")[0]);
pos = getAbsolutePosition($(".try .swaggerShadow")[0]) || {};
pos = Object.keys(pos).reduce(function (prev, cur, index) {
// Add px to the number without unit, undefined when the number is 0
var val = pos[cur];
Expand Down Expand Up @@ -251,25 +264,42 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
}
renderPos();
setTimeout(renderPos, 0);
window.renderPos = renderPos;
window.initTry.renderPos = renderPos;
var observer = new MutationObserver(changeFn);
observer.disconnect();
observer.observe($opblock[0], {
attributes: true,
childList: true,
subtree: true
});
var redoc_dom = window.cfg.redocOptions[2];
var redoc_dom = window.initTry.cfg.redocOptions[2];
$(redoc_dom).off('click.redoc_dom').on('click.redoc_dom', function () {
renderPos();
if (isVisible(document.querySelector(".try .fullApiBox")) === false) {
$(".swaggerBox").addClass("hide").removeClass("show");
window.initTry.$operation.removeClass("try");
}
});
});

// When changing the browser window size, reset the state of swaggerBox
$(window).resize(debounce(function () {
window.renderPos();
window.initTry.renderPos();
}, 500));
}
function isVisible(element) {
var isVisible = true;
var parentElement = element;
while (parentElement) {
var parentStyle = getComputedStyle(parentElement);
if (false || parentStyle.display === 'none' || parentStyle.visibility === 'hidden' || parentStyle.opacity === '0' || parentStyle.opacity === '0.0') {
isVisible = false;
break;
}
parentElement = parentElement.offsetParent;
}
return isVisible;
}
function loadScript(src) {
return new Promise(function (resolve, reject) {
var script = document.createElement('script');
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
!window.initTryOptions.openApi && (delete window.initTryOptions.openApi);
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/bundles/redoc.standalone.min.js"> </script>
<script src="https://cdn.jsdelivr.net/gh/wll8/[email protected].7/dist/try.js"></script>
<script src="https://cdn.jsdelivr.net/gh/wll8/[email protected].8/dist/try.js"></script>
<script>
// initTry(`https://petstore.swagger.io/v2/swagger.json`)
initTry({
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redoc-try",
"version": "1.4.7",
"version": "1.4.8",
"description": "[👉 online preview](https://wll8.github.io/redoc-try/index.html)",
"main": "dist/try.js",
"env": {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Add `Try it out` function like [swagger](https://petstore.swagger.io/) on [redoc
<body>
<div id="redoc-container"></div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/bundles/redoc.standalone.min.js"> </script>
<script src="https://cdn.jsdelivr.net/gh/wll8/[email protected].7/dist/try.js"></script>
<script src="https://cdn.jsdelivr.net/gh/wll8/[email protected].8/dist/try.js"></script>
<script>
// initTry(`https://petstore.swagger.io/v2/swagger.json`)
initTry({
Expand Down

0 comments on commit 60509fd

Please sign in to comment.