mermaid: print preview closes automatically after opening #959
Some checks failed
docs-build-deployment / Run deploy (push) Has been cancelled
docs-build / Run build (push) Has been cancelled

This commit is contained in:
Sören Weber 2024-12-04 20:10:20 +01:00
parent be85052efe
commit 25824cef5b
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D
2 changed files with 19 additions and 11 deletions

View file

@ -1 +1 @@
7.1.1+d3bf75bfc71ca7e3f5e86174309e9d491aacec65 7.1.1+be85052efea3a0aaef45ecb0126d390c1bbac760

View file

@ -2,6 +2,7 @@ window.relearn = window.relearn || {};
var theme = true; var theme = true;
var isPrint = document.querySelector('body').classList.contains('print'); var isPrint = document.querySelector('body').classList.contains('print');
var isPrintPreview = false;
var isRtl = document.querySelector('html').getAttribute('dir') == 'rtl'; var isRtl = document.querySelector('html').getAttribute('dir') == 'rtl';
var lang = document.querySelector('html').getAttribute('lang'); var lang = document.querySelector('html').getAttribute('lang');
@ -297,6 +298,7 @@ function initMermaid(update, attrs) {
window.addEventListener( window.addEventListener(
'beforeprint', 'beforeprint',
function () { function () {
isPrintPreview = true;
initMermaid(true, { initMermaid(true, {
theme: variants.getColorValue('PRINT-MERMAID-theme'), theme: variants.getColorValue('PRINT-MERMAID-theme'),
}); });
@ -305,6 +307,7 @@ function initMermaid(update, attrs) {
window.addEventListener( window.addEventListener(
'afterprint', 'afterprint',
function () { function () {
isPrintPreview = false;
initMermaid(true); initMermaid(true);
}.bind(this) }.bind(this)
); );
@ -384,27 +387,27 @@ function initOpenapi(update, attrs) {
window.addEventListener( window.addEventListener(
'beforeprint', 'beforeprint',
function () { function () {
initOpenapi(true, { isPrintPreview: true }); isPrintPreview = true;
initOpenapi(true);
}.bind(this) }.bind(this)
); );
window.addEventListener( window.addEventListener(
'afterprint', 'afterprint',
function () { function () {
initOpenapi(true, { isPrintPreview: false }); isPrintPreview = false;
initOpenapi(true);
}.bind(this) }.bind(this)
); );
} }
attrs = attrs || { attrs = attrs || {};
isPrintPreview: false,
};
function addFunctionToResizeEvent() {} function addFunctionToResizeEvent() {}
function getFirstAncestorByClass() {} function getFirstAncestorByClass() {}
function renderOpenAPI(oc) { function renderOpenAPI(oc) {
var relBasePath = window.relearn.relBasePath; var relBasePath = window.relearn.relBasePath;
var assetBuster = window.themeUseOpenapi.assetsBuster; var assetBuster = window.themeUseOpenapi.assetsBuster;
var print = isPrint || attrs.isPrintPreview ? 'PRINT-' : ''; var print = isPrint || isPrintPreview ? 'PRINT-' : '';
var theme = print ? `${relBasePath}/css/theme-relearn-light.css${assetBuster}` : document.querySelector('#R-variant-style').attributes.href.value; var theme = print ? `${relBasePath}/css/theme-relearn-light.css${assetBuster}` : document.querySelector('#R-variant-style').attributes.href.value;
var swagger_theme = variants.getColorValue(print + 'OPENAPI-theme'); var swagger_theme = variants.getColorValue(print + 'OPENAPI-theme');
var swagger_code_theme = variants.getColorValue(print + 'OPENAPI-CODE-theme'); var swagger_code_theme = variants.getColorValue(print + 'OPENAPI-CODE-theme');
@ -436,12 +439,12 @@ function initOpenapi(update, attrs) {
var options = { var options = {
defaultModelsExpandDepth: 2, defaultModelsExpandDepth: 2,
defaultModelExpandDepth: 2, defaultModelExpandDepth: 2,
docExpansion: isPrint || attrs.isPrintPreview ? 'full' : 'list', docExpansion: isPrint || isPrintPreview ? 'full' : 'list',
domNode: oi.contentWindow.document.getElementById(openapiId), domNode: oi.contentWindow.document.getElementById(openapiId),
filter: !(isPrint || attrs.isPrintPreview), filter: !(isPrint || isPrintPreview),
layout: 'BaseLayout', layout: 'BaseLayout',
onComplete: function () { onComplete: function () {
if (isPrint || attrs.isPrintPreview) { if (isPrint || isPrintPreview) {
oi.contentWindow.document.querySelectorAll('.model-container > .model-box > button[aria-expanded=false]').forEach(function (btn) { oi.contentWindow.document.querySelectorAll('.model-container > .model-box > button[aria-expanded=false]').forEach(function (btn) {
btn.click(); btn.click();
}); });
@ -505,7 +508,7 @@ function initOpenapi(update, attrs) {
} }
function setOpenAPIHeight(oi) { function setOpenAPIHeight(oi) {
// add empirical offset if in print preview (GC 103) // add empirical offset if in print preview (GC 103)
oi.style.height = oi.contentWindow.document.documentElement.getBoundingClientRect().height + (attrs.isPrintPreview ? 200 : 0) + 'px'; oi.style.height = oi.contentWindow.document.documentElement.getBoundingClientRect().height + (isPrintPreview ? 200 : 0) + 'px';
} }
function resizeOpenAPI() { function resizeOpenAPI() {
let divi = document.getElementsByClassName('sc-openapi-iframe'); let divi = document.getElementsByClassName('sc-openapi-iframe');
@ -1290,6 +1293,11 @@ function initHistory() {
function initScrollPositionSaver() { function initScrollPositionSaver() {
function savePosition(event) { function savePosition(event) {
// #959 if we fiddle around with the history during print preview
// GC will close the preview immediatley
if (isPrintPreview) {
return;
}
var state = window.history.state || {}; var state = window.history.state || {};
state = Object.assign({}, typeof state === 'object' ? state : {}); state = Object.assign({}, typeof state === 'object' ? state : {});
state.contentScrollTop = +elc.scrollTop; state.contentScrollTop = +elc.scrollTop;