From ac0d2a3cc8e75a29e7d5a328300b4bc2c5390666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Gorej?= Date: Sat, 6 Apr 2024 00:16:00 +0200 Subject: [PATCH] refactor(syntax-highlighting): use component wrapping for syntax highlighting activation (#9784) --- src/core/components/curl.jsx | 26 ++++---- src/core/components/example.jsx | 8 +-- src/core/components/model-example.jsx | 5 +- src/core/components/param-body.jsx | 7 +- src/core/components/response-body.jsx | 15 ++--- src/core/components/response.jsx | 6 +- .../plugins/oas3/components/request-body.jsx | 9 +-- .../request-snippets/request-snippets.jsx | 64 +++++++++---------- .../components/HighlightCode.jsx | 37 ++++------- .../components/PlainTextViewer.jsx | 17 +++++ .../components/SyntaxHighlighter.jsx | 5 +- src/core/plugins/syntax-highlighting/index.js | 16 ++++- .../wrap-components/SyntaxHighlighter.jsx | 33 ++++++++++ src/style/_layout.scss | 5 +- test/unit/components/highlight-code.jsx | 2 +- 15 files changed, 143 insertions(+), 112 deletions(-) create mode 100644 src/core/plugins/syntax-highlighting/components/PlainTextViewer.jsx create mode 100644 src/core/plugins/syntax-highlighting/wrap-components/SyntaxHighlighter.jsx diff --git a/src/core/components/curl.jsx b/src/core/components/curl.jsx index 799099953a9..618b21b9e6b 100644 --- a/src/core/components/curl.jsx +++ b/src/core/components/curl.jsx @@ -1,31 +1,19 @@ import React from "react" import PropTypes from "prop-types" import { CopyToClipboard } from "react-copy-to-clipboard" -import get from "lodash/get" import { requestSnippetGenerator_curl_bash } from "../plugins/request-snippets/fn" export default class Curl extends React.Component { static propTypes = { - getConfigs: PropTypes.func.isRequired, getComponent: PropTypes.func.isRequired, request: PropTypes.object.isRequired } render() { - let { request, getConfigs, getComponent } = this.props - let curl = requestSnippetGenerator_curl_bash(request) - - const config = getConfigs() + const { request, getComponent } = this.props + const curl = requestSnippetGenerator_curl_bash(request) const SyntaxHighlighter = getComponent("SyntaxHighlighter", true) - const curlBlock = get(config, "syntaxHighlight.activated") ? ( - - {curl} - - ) : ( - - ) - return (

Curl

@@ -33,7 +21,15 @@ export default class Curl extends React.Component {
- {curlBlock} + ( + {children} + )} + > + {curl} +
) diff --git a/src/core/components/example.jsx b/src/core/components/example.jsx index 6cd637429d0..fc8ddbc7297 100644 --- a/src/core/components/example.jsx +++ b/src/core/components/example.jsx @@ -8,7 +8,7 @@ import ImPropTypes from "react-immutable-proptypes" import { stringify } from "core/utils" export default function Example(props) { - const { example, showValue, getComponent, getConfigs } = props + const { example, showValue, getComponent } = props const Markdown = getComponent("Markdown", true) const HighlightCode = getComponent("HighlightCode", true) @@ -28,10 +28,7 @@ export default function Example(props) { {showValue && example.has("value") ? (
Example Value
- + {stringify(example.get("value"))}
) : null} @@ -42,5 +39,4 @@ Example.propTypes = { example: ImPropTypes.map.isRequired, showValue: PropTypes.bool, getComponent: PropTypes.func.isRequired, - getConfigs: PropTypes.func.getConfigs, } diff --git a/src/core/components/model-example.jsx b/src/core/components/model-example.jsx index 6b942799cc9..abbdfcc97b1 100644 --- a/src/core/components/model-example.jsx +++ b/src/core/components/model-example.jsx @@ -113,10 +113,7 @@ const ModelExample = ({ {example ? ( example ) : ( - + (no example available )} )} diff --git a/src/core/components/param-body.jsx b/src/core/components/param-body.jsx index 966b48fb589..878b4546ab4 100644 --- a/src/core/components/param-body.jsx +++ b/src/core/components/param-body.jsx @@ -15,7 +15,6 @@ export default class ParamBody extends PureComponent { consumes: PropTypes.object, consumesValue: PropTypes.string, fn: PropTypes.object.isRequired, - getConfigs: PropTypes.func.isRequired, getComponent: PropTypes.func.isRequired, isExecute: PropTypes.bool, specSelectors: PropTypes.object.isRequired, @@ -98,7 +97,6 @@ export default class ParamBody extends PureComponent { isExecute, specSelectors, pathMethod, - getConfigs, getComponent, } = this.props @@ -127,10 +125,7 @@ export default class ParamBody extends PureComponent { { isEditBox && isExecute ? - ) + useEffect(() => { + const doIt = () => { + + } + doIt() + }, []) + + useEffect(() => { + const childNodes = Array + .from(rootRef.current.childNodes) + .filter(node => !!node.nodeType && node.classList?.contains("curl-command")) + // eslint-disable-next-line no-use-before-define + childNodes.forEach(node => node.addEventListener("mousewheel", handlePreventYScrollingBeyondElement, { passive: false })) + + return () => { + // eslint-disable-next-line no-use-before-define + childNodes.forEach(node => node.removeEventListener("mousewheel", handlePreventYScrollingBeyondElement)) + } + }, [request]) return (
@@ -142,7 +129,15 @@ const RequestSnippets = ({ request, requestSnippetsSelectors, getConfigs, getCom
- {SnippetComponent} + ( + {children} + )} + > + {snippet} +
} @@ -153,7 +148,6 @@ const RequestSnippets = ({ request, requestSnippetsSelectors, getConfigs, getCom RequestSnippets.propTypes = { request: PropTypes.object.isRequired, requestSnippetsSelectors: PropTypes.object.isRequired, - getConfigs: PropTypes.object.isRequired, getComponent: PropTypes.func.isRequired, requestSnippetsActions: PropTypes.object, } diff --git a/src/core/plugins/syntax-highlighting/components/HighlightCode.jsx b/src/core/plugins/syntax-highlighting/components/HighlightCode.jsx index aeb8a232940..f389f0b95ef 100644 --- a/src/core/plugins/syntax-highlighting/components/HighlightCode.jsx +++ b/src/core/plugins/syntax-highlighting/components/HighlightCode.jsx @@ -4,30 +4,23 @@ import React, { useRef, useEffect } from "react" import PropTypes from "prop-types" import classNames from "classnames" -import get from "lodash/get" import saveAs from "js-file-download" import { CopyToClipboard } from "react-copy-to-clipboard" const HighlightCode = ({ - value, fileName = "response.txt", className, downloadable, - getConfigs, getComponent, canCopy, language, + children, }) => { - const config = getConfigs() - const canSyntaxHighlight = - get(config, "syntaxHighlight") !== false && - get(config, "syntaxHighlight.activated", true) const rootRef = useRef(null) - const SyntaxHighlighter = getComponent("SyntaxHighlighter", true) const handleDownload = () => { - saveAs(value, fileName) + saveAs(children, fileName) } const handlePreventYScrollingBeyondElement = (e) => { @@ -70,13 +63,13 @@ const HighlightCode = ({ ) ) } - }, [value, className, language]) + }, [children, className, language]) return (
{canCopy && (
- +
@@ -88,29 +81,27 @@ const HighlightCode = ({ )} - {canSyntaxHighlight ? ( - - {value} - - ) : ( -
{value}
- )} + ( + {children} + )} + > + {children} +
) } HighlightCode.propTypes = { - value: PropTypes.string.isRequired, - getConfigs: PropTypes.func.isRequired, getComponent: PropTypes.func.isRequired, className: PropTypes.string, downloadable: PropTypes.bool, fileName: PropTypes.string, language: PropTypes.string, canCopy: PropTypes.bool, + children: PropTypes.string.isRequired, } export default HighlightCode diff --git a/src/core/plugins/syntax-highlighting/components/PlainTextViewer.jsx b/src/core/plugins/syntax-highlighting/components/PlainTextViewer.jsx new file mode 100644 index 00000000000..9438fb837a2 --- /dev/null +++ b/src/core/plugins/syntax-highlighting/components/PlainTextViewer.jsx @@ -0,0 +1,17 @@ +/** + * @prettier + */ +import React from "react" +import PropTypes from "prop-types" +import classNames from "classnames" + +const PlainTextViewer = ({ className = "", children }) => ( +
{children}
+) + +PlainTextViewer.propTypes = { + className: PropTypes.string, + children: PropTypes.string.isRequired, +} + +export default PlainTextViewer diff --git a/src/core/plugins/syntax-highlighting/components/SyntaxHighlighter.jsx b/src/core/plugins/syntax-highlighting/components/SyntaxHighlighter.jsx index af57010733e..43db2e2a70c 100644 --- a/src/core/plugins/syntax-highlighting/components/SyntaxHighlighter.jsx +++ b/src/core/plugins/syntax-highlighting/components/SyntaxHighlighter.jsx @@ -11,7 +11,7 @@ const SyntaxHighlighter = ({ className = "", getConfigs, syntaxHighlighting = {}, - children = null, + children = "", }) => { const configs = getConfigs() const theme = get(configs, "syntaxHighlight.theme") @@ -37,7 +37,8 @@ SyntaxHighlighter.propTypes = { styles: PropTypes.object, defaultStyle: PropTypes.object, }), - children: PropTypes.node, + renderPlainText: PropTypes.func, + children: PropTypes.string, } export default SyntaxHighlighter diff --git a/src/core/plugins/syntax-highlighting/index.js b/src/core/plugins/syntax-highlighting/index.js index 91eb51c2c13..180256ec521 100644 --- a/src/core/plugins/syntax-highlighting/index.js +++ b/src/core/plugins/syntax-highlighting/index.js @@ -5,8 +5,10 @@ import afterLoad from "./after-load" import { styles, defaultStyle } from "./root-injects" import SyntaxHighlighter from "./components/SyntaxHighlighter" import HighlightCode from "./components/HighlightCode" +import PlainTextViewer from "./components/PlainTextViewer" +import SyntaxHighlighterWrapper from "./wrap-components/SyntaxHighlighter" -const SyntaxHighlightingPlugin = () => ({ +const SyntaxHighlightingPlugin1 = () => ({ afterLoad, rootInjects: { syntaxHighlighting: { styles, defaultStyle }, @@ -14,7 +16,19 @@ const SyntaxHighlightingPlugin = () => ({ components: { SyntaxHighlighter, HighlightCode, + PlainTextViewer, }, }) +const SyntaxHighlightingPlugin2 = () => ({ + wrapComponents: { + SyntaxHighlighter: SyntaxHighlighterWrapper, + }, +}) + +const SyntaxHighlightingPlugin = () => [ + SyntaxHighlightingPlugin1, + SyntaxHighlightingPlugin2, +] + export default SyntaxHighlightingPlugin diff --git a/src/core/plugins/syntax-highlighting/wrap-components/SyntaxHighlighter.jsx b/src/core/plugins/syntax-highlighting/wrap-components/SyntaxHighlighter.jsx new file mode 100644 index 00000000000..a9e557209f0 --- /dev/null +++ b/src/core/plugins/syntax-highlighting/wrap-components/SyntaxHighlighter.jsx @@ -0,0 +1,33 @@ +/** + * @prettier + */ +import React from "react" +import PropTypes from "prop-types" +import get from "lodash/get" + +const SyntaxHighlighterWrapper = (Original, system) => { + const SyntaxHighlighter = ({ renderPlainText, children, ...rest }) => { + const configs = system.getConfigs() + const canSyntaxHighlight = !!get(configs, "syntaxHighlight.activated") + const PlainTextViewer = system.getComponent("PlainTextViewer") + + if (!canSyntaxHighlight && typeof renderPlainText === "function") { + return renderPlainText({ children, PlainTextViewer }) + } + if (!canSyntaxHighlight) { + return {children} + } + + return {children} + } + + SyntaxHighlighter.propTypes = { + ...Original.propTypes, + renderPlainText: PropTypes.func, + children: PropTypes.string.isRequired, + } + + return SyntaxHighlighter +} + +export default SyntaxHighlighterWrapper diff --git a/src/style/_layout.scss b/src/style/_layout.scss index f0645e9488d..b97083a2605 100644 --- a/src/style/_layout.scss +++ b/src/style/_layout.scss @@ -654,8 +654,9 @@ .curl { - white-space: normal; - max-height: 400px; + overflow-y: auto; + max-height: 400px; + min-height: 6em; } } diff --git a/test/unit/components/highlight-code.jsx b/test/unit/components/highlight-code.jsx index 99b0e23d54c..4186ab5caec 100644 --- a/test/unit/components/highlight-code.jsx +++ b/test/unit/components/highlight-code.jsx @@ -33,7 +33,7 @@ describe("", () => { it("should render values in a preformatted element", () => { const value = "test text" - const props = { value, getConfigs: fakeGetConfigs, getComponent: fakeGetComponent } + const props = { children: value , getConfigs: fakeGetConfigs, getComponent: fakeGetComponent } const wrapper = mount() const preTag = wrapper.find("pre")