Skip to content

Commit

Permalink
Add ability to download your Taskfile and improve README
Browse files Browse the repository at this point in the history
  • Loading branch information
rick-nu committed Dec 6, 2024
1 parent 4ca7234 commit 32e726a
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 63 deletions.
28 changes: 0 additions & 28 deletions src/components/Generator/GeneredTaskfile/Copy/Copy.tsx

This file was deleted.

27 changes: 0 additions & 27 deletions src/components/Generator/GeneredTaskfile/Copy/copy.module.css

This file was deleted.

1 change: 0 additions & 1 deletion src/components/Generator/GeneredTaskfile/Copy/index.tsx

This file was deleted.

14 changes: 7 additions & 7 deletions src/components/Generator/GeneredTaskfile/GeneratedTaskfile.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use client';

import { ReactElement } from 'react';
import { taskfile } from '@/components/Generator/GeneredTaskfile/taskfile';
import { useFormContext } from 'react-hook-form';
import { GeneratorSettings } from '@/components/Generator/Generator';
import CopyToClipboard from '@/components/Generator/GeneredTaskfile/Copy';
import { highlighter } from './Highlighter';
import {ReactElement} from 'react';
import {taskfile} from '@/components/Generator/GeneredTaskfile/taskfile';
import {useFormContext} from 'react-hook-form';
import {GeneratorSettings} from '@/components/Generator/Generator';
import SaveFile from './SaveFile';
import {highlighter} from './Highlighter';

const GeneratedTaskfile = (): ReactElement => {
const form = useFormContext<GeneratorSettings>();
Expand All @@ -16,7 +16,7 @@ const GeneratedTaskfile = (): ReactElement => {

return (
<>
<CopyToClipboard onCopy={() => navigator.clipboard.writeText(resultTaskfile)} />
<SaveFile content={resultTaskfile} />
<pre>{highlighter(resultTaskfile)}</pre>
</>
);
Expand Down
39 changes: 39 additions & 0 deletions src/components/Generator/GeneredTaskfile/SaveFile/SaveFile.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use client';

import {ReactElement, useState} from 'react';

import styles from './save-file.module.scss';

type CopyProps = {
content: string;
};

const CopyToClipboard = ({ content }: CopyProps): ReactElement => {
const [isCopied, setCopied] = useState(false);

const onClick = (): void => {
setCopied(true);
navigator.clipboard.writeText(content);

setTimeout(() => setCopied(false), 1000);
};

const download = (): void => {
const blob = new Blob([content], {type: 'text/x-shellscript'});
const elem = window.document.createElement('a');
elem.href = window.URL.createObjectURL(blob);
elem.download = 'Taskfile';
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
};

return (
<div className={styles.container}>
<button type="button" className={`${styles.download}`} onClick={download} title="Download Taskfile" />
<button type="button" className={`${styles.copy} ${isCopied && styles.copied}`} onClick={onClick} title="Copy Taskfile content" />
</div>
);
};

export default CopyToClipboard;
19 changes: 19 additions & 0 deletions src/components/Generator/GeneredTaskfile/SaveFile/clipboard.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions src/components/Generator/GeneredTaskfile/SaveFile/download.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './SaveFile';
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
.container {
position: sticky;
top: 0;
float: right;
transform: translateY(-1rem);
display: flex;
flex-direction: row;
gap: 0.5rem;
}

.copy,
.download {
background-color: #10001fc2;
color: rgb(255 255 255 / 0.5);
border: 0.1rem solid rgb(255 255 255 / 0.5);
border-radius: 0.2rem;
height: 2.6rem;
width: 2.6rem;
cursor: pointer;
font-size: 1rem;
background-size: 1.2rem;
background-repeat: no-repeat;
background-position: center;
border-radius: 0.2rem;
transition:
background-color 200ms ease-in-out;
}

.download {
background-image: url('./download.svg');
}

.copy {
background-image: url('./clipboard.svg');
background-size: 1.0rem;
}

.copy:hover, .download:hover {
color: #fff;
border-color: #fff;
}

.copy.copied,
.copy:hover.copied {
background-color: #2ac93faa;
}

0 comments on commit 32e726a

Please sign in to comment.