From 837d86b77f40b0643ca96361d3a229fadf17fd03 Mon Sep 17 00:00:00 2001 From: luka Date: Fri, 21 Jun 2024 00:14:04 -0400 Subject: [PATCH 1/3] feat(swagger-ui-react): add HTML preview functionality to response body component --- src/core/components/response-body.jsx | 9 ++-- .../components/HighlightCode.jsx | 48 +++++++++++++++---- src/style/_layout.scss | 32 +++++++++++++ 3 files changed, 75 insertions(+), 14 deletions(-) diff --git a/src/core/components/response-body.jsx b/src/core/components/response-body.jsx index 4b99f41f8bb..53a7fd4026d 100644 --- a/src/core/components/response-body.jsx +++ b/src/core/components/response-body.jsx @@ -117,12 +117,12 @@ export default class ResponseBody extends React.PureComponent { }) bodyEl = {body} - // HTML or Plain Text - } else if (toLower(contentType) === "text/html" || /text\/plain/.test(contentType)) { - bodyEl = {content} + // HTML + } else if (/text\/html/.test(contentType)) { + bodyEl = {content} // CSV - } else if (toLower(contentType) === "text/csv" || /text\/csv/.test(contentType)) { + } else if (/text\/csv/.test(contentType)) { bodyEl = {content} // Image @@ -136,6 +136,7 @@ export default class ResponseBody extends React.PureComponent { // Audio } else if (/^audio\//i.test(contentType)) { bodyEl =
+ // As Plain Text } else if (typeof content === "string") { bodyEl = {content} } else if ( content.size > 0 ) { diff --git a/src/core/plugins/syntax-highlighting/components/HighlightCode.jsx b/src/core/plugins/syntax-highlighting/components/HighlightCode.jsx index f389f0b95ef..b368a9a1b79 100644 --- a/src/core/plugins/syntax-highlighting/components/HighlightCode.jsx +++ b/src/core/plugins/syntax-highlighting/components/HighlightCode.jsx @@ -1,7 +1,7 @@ /** * @prettier */ -import React, { useRef, useEffect } from "react" +import React, { useRef, useEffect, useState } from "react" import PropTypes from "prop-types" import classNames from "classnames" import saveAs from "js-file-download" @@ -11,12 +11,15 @@ const HighlightCode = ({ fileName = "response.txt", className, downloadable, + previewable, getComponent, canCopy, language, children, }) => { const rootRef = useRef(null) + const [previewVisible, setPreviewVisible] = useState(false) + const [previewHeight, setPreviewHeight] = useState(screen.height); const SyntaxHighlighter = getComponent("SyntaxHighlighter", true) const handleDownload = () => { @@ -40,6 +43,14 @@ const HighlightCode = ({ } } + const handlePreviewToggle = () => { + previewable && setPreviewVisible(!previewVisible) + } + + const handlePreviewAutoHeight = (event) => { + setPreviewHeight(event.target?.contentWindow.document?.body?.scrollHeight ?? previewHeight) + } + useEffect(() => { const childNodes = Array.from(rootRef.current.childNodes).filter( (node) => !!node.nodeType && node.classList.contains("microlight") @@ -65,8 +76,15 @@ const HighlightCode = ({ } }, [children, className, language]) + // console.log(previewable, fileName, className, downloadable, language, canCopy, children) return (
+ {previewable && ( + + )} + {canCopy && (
@@ -81,15 +99,24 @@ const HighlightCode = ({ )} - ( - {children} - )} - > - {children} - + {previewVisible ? ( +