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 19, 2023
1 parent abb4d32 commit 67ff447
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 43 deletions.
84 changes: 44 additions & 40 deletions dist/try.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,51 +189,54 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
_fullApi$match2 = _slicedToArray(_fullApi$match, 3),
method = _fullApi$match2[1],
api = _fullApi$match2[2];

// Get the position of swaggerShadow
var pos = {};
window.getAbsolutePosition = getAbsolutePosition;
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];
return _objectSpread(_objectSpread({}, prev), {}, _defineProperty({}, cur, _typeof(val) === "number" ? val > 0 ? "".concat(val, "px") : undefined : val));
}, {});
var oldHeight = pos.height ? "".concat(pos.height) : undefined;

// Move swagger to the position of swaggerShadow
var getSwaggerBoxHeight = function getSwaggerBoxHeight() {
return getAbsolutePosition($(".swaggerBox")[0]).height + "px";
};
$(".swaggerBox").css({
left: "".concat(pos.left),
top: "".concat(pos.top),
width: "".concat(pos.width),
height: oldHeight
}).removeClass("hide").addClass('show');

// Synchronize the size of swaggerShadow to make it as big as swaggerBox
$(".swaggerShadow").css({
height: getSwaggerBoxHeight()
});

// scroll the swagger view to the same api position
var selStr = ".opblock-summary-".concat(method, " [data-path=\"").concat(api, "\"]");
var $swaggerApiDom = $(selStr);
var $opblock = $swaggerApiDom.parents(".opblock"); // Get the currently clicked swagger api, and it is not an expanded element
var getSwaggerBoxHeight = function getSwaggerBoxHeight() {
return getAbsolutePosition($(".swaggerBox")[0]).height + "px";
};
var getShadowPos = function getShadowPos() {
// Get the position of swaggerShadow
var pos = {};
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];
return _objectSpread(_objectSpread({}, prev), {}, _defineProperty({}, cur, _typeof(val) === "number" ? val > 0 ? "".concat(val, "px") : undefined : val));
}, {});
return pos;
};
var pos = getShadowPos();
var oldHeight = pos.height ? "".concat(pos.height) : undefined;
if ($opblock.hasClass("open") === false) {
$swaggerApiDom.click(); // turn on
}

$opblock.addClass("open");
console.log("selStr", selStr);
$(".swaggerBox").scrollTo($swaggerApiDom.parent());
function renderPos() {
var pos = getShadowPos();
// Move swagger to the position of swaggerShadow
$(".swaggerBox").css({
left: "".concat(pos.left),
top: "".concat(pos.top),
width: "".concat(pos.width),
height: oldHeight
}).removeClass("hide").addClass('show');

// Synchronize the size of swaggerShadow to make it as big as swaggerBox
$(".swaggerShadow").css({
height: getSwaggerBoxHeight()
});
// scroll the swagger view to the same api position
$(".swaggerBox").scrollTo($swaggerApiDom.parent());
changeFn();
}
function changeFn() {
var pos = getAbsolutePosition($opblock[0]);
if (pos.height === 0) {
var opblockPos = getAbsolutePosition($opblock[0]);
if (opblockPos.height === 0) {
return false; // The height is 0, no processing
} else {
var newHeight = "".concat(pos.height, "px");
var newHeight = "".concat(opblockPos.height, "px");
if (oldHeight !== newHeight) {
$(".swaggerBox").scrollTo($swaggerApiDom.parent());
$(".swaggerBox").css({
Expand All @@ -246,23 +249,24 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
}
}
}
changeFn();
renderPos();
window.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];
$(redoc_dom).off('click.redoc_dom').on('click.redoc_dom', function () {
renderPos();
});
});

// When changing the browser window size, reset the state of swaggerBox
$(window).resize(debounce(function () {
$(".swaggerBox").addClass("hide").removeClass("show").css({
left: 0,
top: 0
});
$("[data-section-id^=\"operation/\"]").removeClass("try");
window.renderPos();
}, 500));
}
function loadScript(src) {
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].4/dist/try.js"></script>
<script src="https://cdn.jsdelivr.net/gh/wll8/[email protected].5/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.4",
"version": "1.4.5",
"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].4/dist/try.js"></script>
<script src="https://cdn.jsdelivr.net/gh/wll8/[email protected].5/dist/try.js"></script>
<script>
// initTry(`https://petstore.swagger.io/v2/swagger.json`)
initTry({
Expand Down

0 comments on commit 67ff447

Please sign in to comment.