diff --git a/src/core/components/curl.jsx b/src/core/components/curl.jsx index 44f3b15e..eefce029 100644 --- a/src/core/components/curl.jsx +++ b/src/core/components/curl.jsx @@ -21,7 +21,6 @@ export default class Curl extends React.Component { ? {curl} diff --git a/src/core/plugins/request-snippets/index.js b/src/core/plugins/request-snippets/index.js index cbda08f9..08bc8cc4 100644 --- a/src/core/plugins/request-snippets/index.js +++ b/src/core/plugins/request-snippets/index.js @@ -1,6 +1,6 @@ import * as fn from "./fn" import * as selectors from "./selectors" -import { RequestSnippets } from "./request-snippets" +import RequestSnippets from "./request-snippets" export default () => { return { components: { diff --git a/src/core/plugins/request-snippets/request-snippets.jsx b/src/core/plugins/request-snippets/request-snippets.jsx index 22b2ffe1..0410c035 100644 --- a/src/core/plugins/request-snippets/request-snippets.jsx +++ b/src/core/plugins/request-snippets/request-snippets.jsx @@ -1,127 +1,160 @@ -import React from "react" -import { CopyToClipboard } from "react-copy-to-clipboard" +import React, { useRef, useEffect, useState } from "react" import PropTypes from "prop-types" import get from "lodash/get" -import {SyntaxHighlighter, getStyle} from "core/syntax-highlighting" +import isFunction from "lodash/isFunction" +import { CopyToClipboard } from "react-copy-to-clipboard" +import { SyntaxHighlighter, getStyle } from "core/syntax-highlighting" -export class RequestSnippets extends React.Component { - constructor() { - super() - this.state = { - activeLanguage: this.props?.requestSnippetsSelectors?.getSnippetGenerators()?.keySeq().first(), - expanded: this.props?.requestSnippetsSelectors?.getDefaultExpanded(), +const style = { + cursor: "pointer", + lineHeight: 1, + display: "inline-flex", + backgroundColor: "rgb(250, 250, 250)", + paddingBottom: "0", + paddingTop: "0", + border: "1px solid rgb(51, 51, 51)", + borderRadius: "4px 4px 0 0", + boxShadow: "none", + borderBottom: "none" +} + +const activeStyle = { + cursor: "pointer", + lineHeight: 1, + display: "inline-flex", + backgroundColor: "rgb(51, 51, 51)", + boxShadow: "none", + border: "1px solid rgb(51, 51, 51)", + paddingBottom: "0", + paddingTop: "0", + borderRadius: "4px 4px 0 0", + marginTop: "-5px", + marginRight: "-5px", + marginLeft: "-5px", + zIndex: "9999", + borderBottom: "none" +} + +const RequestSnippets = ({ request, requestSnippetsSelectors, getConfigs }) => { + const config = isFunction(getConfigs) ? getConfigs() : null + const canSyntaxHighlight = get(config, "syntaxHighlight") !== false && get(config, "syntaxHighlight.activated", true) + const rootRef = useRef(null) + + const [activeLanguage, setActiveLanguage] = useState(requestSnippetsSelectors.getSnippetGenerators()?.keySeq().first()) + const [isExpanded, setIsExpanded] = useState(requestSnippetsSelectors?.getDefaultExpanded()) + 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]) + + const snippetGenerators = requestSnippetsSelectors.getSnippetGenerators() + const activeGenerator = snippetGenerators.get(activeLanguage) + const snippet = activeGenerator.get("fn")(request) + + const handleGenChange = (key) => { + const needsChange = activeLanguage !== key + if (needsChange) { + setActiveLanguage(key) } } - static propTypes = { - request: PropTypes.object.isRequired, - requestSnippetsSelectors: PropTypes.object.isRequired, - getConfigs: PropTypes.object.isRequired, - requestSnippetsActions: PropTypes.object.isRequired, + const handleSetIsExpanded = () => { + setIsExpanded(!isExpanded) } - render() { - const {request, getConfigs, requestSnippetsSelectors } = this.props - const snippetGenerators = requestSnippetsSelectors.getSnippetGenerators() - const activeLanguage = this.state.activeLanguage || snippetGenerators.keySeq().first() - const activeGenerator = snippetGenerators.get(activeLanguage) - const snippet = activeGenerator.get("fn")(request) - const onGenChange = (key) => { - const needsChange = activeLanguage !== key - if(needsChange) { - this.setState({ - activeLanguage: key - }) - } - } - const style = { - cursor: "pointer", - lineHeight: 1, - display: "inline-flex", - backgroundColor: "rgb(250, 250, 250)", - paddingBottom: "0", - paddingTop: "0", - border: "1px solid rgb(51, 51, 51)", - borderRadius: "4px 4px 0 0", - boxShadow: "none", - borderBottom: "none" - } - const activeStyle = { - cursor: "pointer", - lineHeight: 1, - display: "inline-flex", - backgroundColor: "rgb(51, 51, 51)", - boxShadow: "none", - border: "1px solid rgb(51, 51, 51)", - paddingBottom: "0", - paddingTop: "0", - borderRadius: "4px 4px 0 0", - marginTop: "-5px", - marginRight: "-5px", - marginLeft: "-5px", - zIndex: "9999", - borderBottom: "none" - } - const getBtnStyle = (key) => { - if (key === activeLanguage) { - return activeStyle - } - return style - } - const config = getConfigs() - const SnippetComponent = config?.syntaxHighlight?.activated - ? { + if (key === activeLanguage) { + return activeStyle + } + return style + } + + const handlePreventYScrollingBeyondElement = (e) => { + const { target, deltaY } = e + const { scrollHeight: contentHeight, offsetHeight: visibleHeight, scrollTop } = target + const scrollOffset = visibleHeight + scrollTop + const isElementScrollable = contentHeight > visibleHeight + const isScrollingPastTop = scrollTop === 0 && deltaY < 0 + const isScrollingPastBottom = scrollOffset >= contentHeight && deltaY > 0 + + if (isElementScrollable && (isScrollingPastTop || isScrollingPastBottom)) { + e.preventDefault() + } + } + + const SnippetComponent = canSyntaxHighlight + ? + {snippet} + + : + + + return ( +
+ Just a test +
+

handleSetIsExpanded()} + style={{ cursor: "pointer" }} + >Snippets

+ + + + + +
+ { + isExpanded &&
+
+ { + snippetGenerators.entrySeq().map(([key, gen]) => { + return (
handleGenChange(key)}> +

{gen.get("title")}

+
) + }) + } +
+
+ +
+
+ {SnippetComponent}
- { - expanded &&
-
- { - snippetGenerators.entrySeq().map(([key, gen]) => { - return (
onGenChange(key)}> -

{gen.get("title")}

-
) - }) - } -
-
- -
-
- {SnippetComponent} -
-
- }
- - ) - } + } +
+ ) } + +RequestSnippets.propTypes = { + request: PropTypes.object.isRequired, + requestSnippetsSelectors: PropTypes.object.isRequired, + getConfigs: PropTypes.object.isRequired, + requestSnippetsActions: PropTypes.object, +} + +export default RequestSnippets diff --git a/test/mocha/components/live-response.jsx b/test/mocha/components/live-response.jsx index b740ea94..bac8ff0f 100644 --- a/test/mocha/components/live-response.jsx +++ b/test/mocha/components/live-response.jsx @@ -6,7 +6,7 @@ import expect from "expect" import { shallow } from "enzyme" import LiveResponse from "components/live-response" import ResponseBody from "components/response-body" -import { RequestSnippets } from "core/plugins/request-snippets/request-snippets" +import RequestSnippets from "core/plugins/request-snippets/request-snippets" describe("", function () { let request = fromJSOrdered({