Add copy button to codeblocks

This commit is contained in:
Eduard Heimbuch
2023-01-25 15:55:00 +01:00
parent d74db7be2c
commit dc60c924ed
13 changed files with 460 additions and 133 deletions

View File

@@ -0,0 +1,64 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import * as React from "react";
import { storiesOf } from "@storybook/react";
import styled from "styled-components";
import PreformattedCodeBlock from "./PreformattedCodeBlock";
import { SubSubtitle } from "./layout";
const Wrapper = styled.div``;
const longContent =
"#!/bin/bash\n" +
"\n" +
"### For this hook to work you need the SCM CLI client (https://scm-manager.org/cli/)\n" +
"### installed and connected to your SCM Server.\n" +
"\n" +
"BRANCH_NAME=$(git symbolic-ref --short HEAD)\n" +
"COMMIT_MSG_FILE=`cat $1`\n" +
"\n" +
'scm repo commit-message-check aaa/ultimate-repo $BRANCH_NAME "$COMMIT_MSG_FILE"';
storiesOf("PreformattedCodeBlock", module)
.addDecorator((storyFn) => <Wrapper className="m-6">{storyFn()}</Wrapper>)
.add("Default", () => <PreformattedCodeBlock>git checkout main</PreformattedCodeBlock>)
.add("With scrollbar", () => (
<PreformattedCodeBlock>
git remote add origin
https://scm-manager-instance4.example.org:8081/scm/repo/my-new-namespace/LoremipsumdolorsitametconsetetursadipscingelitrseddiamnonumyeirmodtemporinviduntutlaboreetdoloremagnaaliquyameratseddiamvoluptuaAtveroeosetaccusametjustoduodoloresetearebumStetclitakasdgubergrennoseatakimatasanctusestLoremipsumdolorsitametLoremipsumdolorsitametconsetetursadipscingelitrseddiamnonumyeirmodtemporinviduntutlaboreetdoloremagnaaliquyameratseddiamvoluptuaAtveroeosetaccusametjustoduodoloresetearebumStetclitakasdgubergrennoseatakimatasanctusestLoremipsumdolorsitamet
</PreformattedCodeBlock>
))
.add("Long content", () => <PreformattedCodeBlock>{longContent}</PreformattedCodeBlock>)
.add("Combination", () => (
<div className="content">
<SubSubtitle>Clone the Repository</SubSubtitle>
<PreformattedCodeBlock>git clone https://fancy-scm.url/scm/repo/test/scmm\n cd scmm</PreformattedCodeBlock>
<SubSubtitle>Git hook for commit message validation</SubSubtitle>
<PreformattedCodeBlock>{longContent}</PreformattedCodeBlock>
<SubSubtitle>Get Remote Changes</SubSubtitle>
<PreformattedCodeBlock>git fetch</PreformattedCodeBlock>
<SubSubtitle>Switch Branch</SubSubtitle>
<PreformattedCodeBlock>git checkout feature/something</PreformattedCodeBlock>
</div>
));

View File

@@ -0,0 +1,67 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import * as React from "react";
import { FC, useState } from "react";
import { useTranslation } from "react-i18next";
import styled from "styled-components";
import Button from "./buttons/Button";
import copyToClipboard from "./CopyToClipboard";
type Props = {
children: string;
};
const TopRightButton = styled(Button)`
position: absolute;
display: none;
height: inherit;
top: 1.25em;
right: 1.5em;
`;
const Container = styled.div`
&:hover > ${TopRightButton} {
display: inline-block;
}
`;
const PreformattedCodeBlock: FC<Props> = ({ children }) => {
const [t] = useTranslation("repos");
const [copied, setCopied] = useState(false);
const copy = () => copyToClipboard(children).then(() => setCopied(true));
return (
<Container className="is-relative">
<pre>
<code>{children}</code>
</pre>
<TopRightButton className="is-small" title={t("syntaxHighlighting.copyButton")} action={copy}>
<i className={copied ? "fa fa-clipboard-check" : "fa fa-clipboard"} />
</TopRightButton>
</Container>
);
};
export default PreformattedCodeBlock;

View File

@@ -19277,6 +19277,235 @@ exports[`Storyshots Popover Link 1`] = `
</div>
`;
exports[`Storyshots PreformattedCodeBlock Combination 1`] = `
<div
className="PreformattedCodeBlockstories__Wrapper-sc-exgqpp-0 m-6"
>
<div
className="content"
>
<h3
className="is-size-5"
>
Clone the Repository
</h3>
<div
className="PreformattedCodeBlock__Container-sc-11slzrr-1 iISymu is-relative"
>
<pre>
<code>
git clone https://fancy-scm.url/scm/repo/test/scmm\\n cd scmm
</code>
</pre>
<button
className="button is-default PreformattedCodeBlock__TopRightButton-sc-11slzrr-0 dfZgVU is-small"
onClick={[Function]}
title="syntaxHighlighting.copyButton"
type="button"
>
<span>
<i
className="fa fa-clipboard"
/>
</span>
</button>
</div>
<h3
className="is-size-5"
>
Git hook for commit message validation
</h3>
<div
className="PreformattedCodeBlock__Container-sc-11slzrr-1 iISymu is-relative"
>
<pre>
<code>
#!/bin/bash
### For this hook to work you need the SCM CLI client (https://scm-manager.org/cli/)
### installed and connected to your SCM Server.
BRANCH_NAME=$(git symbolic-ref --short HEAD)
COMMIT_MSG_FILE=\`cat $1\`
scm repo commit-message-check aaa/ultimate-repo $BRANCH_NAME "$COMMIT_MSG_FILE"
</code>
</pre>
<button
className="button is-default PreformattedCodeBlock__TopRightButton-sc-11slzrr-0 dfZgVU is-small"
onClick={[Function]}
title="syntaxHighlighting.copyButton"
type="button"
>
<span>
<i
className="fa fa-clipboard"
/>
</span>
</button>
</div>
<h3
className="is-size-5"
>
Get Remote Changes
</h3>
<div
className="PreformattedCodeBlock__Container-sc-11slzrr-1 iISymu is-relative"
>
<pre>
<code>
git fetch
</code>
</pre>
<button
className="button is-default PreformattedCodeBlock__TopRightButton-sc-11slzrr-0 dfZgVU is-small"
onClick={[Function]}
title="syntaxHighlighting.copyButton"
type="button"
>
<span>
<i
className="fa fa-clipboard"
/>
</span>
</button>
</div>
<h3
className="is-size-5"
>
Switch Branch
</h3>
<div
className="PreformattedCodeBlock__Container-sc-11slzrr-1 iISymu is-relative"
>
<pre>
<code>
git checkout feature/something
</code>
</pre>
<button
className="button is-default PreformattedCodeBlock__TopRightButton-sc-11slzrr-0 dfZgVU is-small"
onClick={[Function]}
title="syntaxHighlighting.copyButton"
type="button"
>
<span>
<i
className="fa fa-clipboard"
/>
</span>
</button>
</div>
</div>
</div>
`;
exports[`Storyshots PreformattedCodeBlock Default 1`] = `
<div
className="PreformattedCodeBlockstories__Wrapper-sc-exgqpp-0 m-6"
>
<div
className="PreformattedCodeBlock__Container-sc-11slzrr-1 iISymu is-relative"
>
<pre>
<code>
git checkout main
</code>
</pre>
<button
className="button is-default PreformattedCodeBlock__TopRightButton-sc-11slzrr-0 dfZgVU is-small"
onClick={[Function]}
title="syntaxHighlighting.copyButton"
type="button"
>
<span>
<i
className="fa fa-clipboard"
/>
</span>
</button>
</div>
</div>
`;
exports[`Storyshots PreformattedCodeBlock Long content 1`] = `
<div
className="PreformattedCodeBlockstories__Wrapper-sc-exgqpp-0 m-6"
>
<div
className="PreformattedCodeBlock__Container-sc-11slzrr-1 iISymu is-relative"
>
<pre>
<code>
#!/bin/bash
### For this hook to work you need the SCM CLI client (https://scm-manager.org/cli/)
### installed and connected to your SCM Server.
BRANCH_NAME=$(git symbolic-ref --short HEAD)
COMMIT_MSG_FILE=\`cat $1\`
scm repo commit-message-check aaa/ultimate-repo $BRANCH_NAME "$COMMIT_MSG_FILE"
</code>
</pre>
<button
className="button is-default PreformattedCodeBlock__TopRightButton-sc-11slzrr-0 dfZgVU is-small"
onClick={[Function]}
title="syntaxHighlighting.copyButton"
type="button"
>
<span>
<i
className="fa fa-clipboard"
/>
</span>
</button>
</div>
</div>
`;
exports[`Storyshots PreformattedCodeBlock With scrollbar 1`] = `
<div
className="PreformattedCodeBlockstories__Wrapper-sc-exgqpp-0 m-6"
>
<div
className="PreformattedCodeBlock__Container-sc-11slzrr-1 iISymu is-relative"
>
<pre>
<code>
git remote add origin https://scm-manager-instance4.example.org:8081/scm/repo/my-new-namespace/LoremipsumdolorsitametconsetetursadipscingelitrseddiamnonumyeirmodtemporinviduntutlaboreetdoloremagnaaliquyameratseddiamvoluptuaAtveroeosetaccusametjustoduodoloresetearebumStetclitakasdgubergrennoseatakimatasanctusestLoremipsumdolorsitametLoremipsumdolorsitametconsetetursadipscingelitrseddiamnonumyeirmodtemporinviduntutlaboreetdoloremagnaaliquyameratseddiamvoluptuaAtveroeosetaccusametjustoduodoloresetearebumStetclitakasdgubergrennoseatakimatasanctusestLoremipsumdolorsitamet
</code>
</pre>
<button
className="button is-default PreformattedCodeBlock__TopRightButton-sc-11slzrr-0 dfZgVU is-small"
onClick={[Function]}
title="syntaxHighlighting.copyButton"
type="button"
>
<span>
<i
className="fa fa-clipboard"
/>
</span>
</button>
</div>
</div>
`;
exports[`Storyshots Repositories/Annotate Default 1`] = `
<div
className="Annotatestories__Wrapper-sc-1fdvl94-0 ldNvIm box"

View File

@@ -81,6 +81,7 @@ export { default as CreateTagModal } from "./modals/CreateTagModal";
export { default as CardColumn } from "./CardColumn";
export { default as CardColumnSmall } from "./CardColumnSmall";
export { default as CommaSeparatedList } from "./CommaSeparatedList";
export { default as PreformattedCodeBlock } from "./PreformattedCodeBlock";
export { SplitAndReplace, Replacement } from "@scm-manager/ui-text";
export { useShortcut } from "@scm-manager/ui-shortcuts";
export { regExpPattern as changesetShortLinkRegex } from "./markdown/remarkChangesetShortLinkParser";