-
-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add svg & qrcode #310
Merged
Merged
add svg & qrcode #310
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Card, Form, Input } from "antd"; | ||
import CodeHighlightWithCopy from "components/General/CodeHighlightWithCopy"; | ||
import PageGrid from "components/Layouts/PageGrid"; | ||
import React, { ChangeEvent, useState } from "react"; | ||
import { combineSVGPaths } from "./utils/helper"; | ||
|
||
const { TextArea } = Input; | ||
|
||
const SvgFormatter: React.FC = () => { | ||
const [value, setValue] = useState(""); | ||
const [combinedSVG, setCombinedSVG] = useState<string>(""); | ||
|
||
const handleChange = (e: ChangeEvent<HTMLTextAreaElement>) => { | ||
const newValue = e.target.value; | ||
setValue(newValue); | ||
|
||
const combinedSvgOutput = combineSVGPaths(newValue); | ||
setCombinedSVG(combinedSvgOutput); | ||
}; | ||
return ( | ||
<PageGrid> | ||
<Card> | ||
<Form layout="vertical"> | ||
<Form.Item label="Svg Input"> | ||
<TextArea | ||
placeholder="Enter svg value" | ||
value={value} | ||
rows={10} | ||
onChange={handleChange} | ||
data-gramm={false} | ||
allowClear | ||
/> | ||
</Form.Item> | ||
</Form> | ||
</Card> | ||
<Card> | ||
<CodeHighlightWithCopy | ||
language="css" | ||
codeString={combinedSVG} | ||
/> | ||
</Card> | ||
</PageGrid> | ||
); | ||
}; | ||
|
||
export default SvgFormatter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
function combineSVGPaths(svgInput: string): string { | ||
try { | ||
const parser = new DOMParser(); | ||
const svgDocument = parser.parseFromString(svgInput, "image/svg+xml"); | ||
|
||
const pathElements = svgDocument.querySelectorAll("path"); | ||
|
||
if (pathElements.length === 0) { | ||
throw new Error("No <path> elements found in the SVG."); | ||
} | ||
|
||
const pathDataArray = Array.from(pathElements).map((path) => | ||
path.getAttribute("d") | ||
); | ||
|
||
const combinedPathData = pathDataArray.join("\n"); | ||
const svgElement = svgDocument.querySelector("svg"); | ||
|
||
if (svgElement) { | ||
svgElement.innerHTML = `<path d="${combinedPathData}" stroke="#F0F" />`; | ||
} else { | ||
throw new Error("<svg> element not found in the SVG."); | ||
} | ||
|
||
const serializer = new XMLSerializer(); | ||
const combinedSVGString = serializer.serializeToString(svgDocument); | ||
|
||
return combinedSVGString; | ||
} catch (error) { | ||
console.error(error); | ||
return "Error combining SVG paths."; | ||
} | ||
} | ||
|
||
export { combineSVGPaths }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
.qrcode { | ||
&__label { | ||
display: flex; | ||
width: 100vw; | ||
justify-content: space-between; | ||
} | ||
|
||
&__output { | ||
height: 266px; | ||
&__result, | ||
&__notfound { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
} | ||
|
||
&__notfound { | ||
height: 200px; | ||
text-align: center; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { Button, Card, Form, QRCode, Input, Badge } from "antd"; | ||
import PageGrid from "components/Layouts/PageGrid"; | ||
import React, { useEffect, useState } from "react"; | ||
import { detectQrData, downloadQRCode } from "./utils/helper"; | ||
import Icon from "components/General/Icon"; | ||
import style from "./QRcode.module.scss"; | ||
|
||
const { TextArea } = Input; | ||
|
||
const QRcode: React.FC = () => { | ||
const [value, setValue] = useState("Generate QR code ..."); | ||
const [dataType, setDataType] = useState(""); | ||
|
||
useEffect(() => { | ||
setDataType(detectQrData(value)); | ||
}, [value]); | ||
|
||
return ( | ||
<PageGrid> | ||
<Card> | ||
<Form layout="vertical"> | ||
<Form.Item | ||
label={ | ||
<div className={style.qrcode__label}> | ||
<p>{`Input data`}</p> | ||
<Badge | ||
text={`${dataType} detected`} | ||
color={ | ||
dataType === "No data" | ||
? "yellow" | ||
: "green" | ||
} | ||
/> | ||
</div> | ||
} | ||
> | ||
<TextArea | ||
value={value} | ||
rows={7} | ||
onChange={(e) => setValue(e.target.value)} | ||
data-gramm={false} | ||
allowClear | ||
/> | ||
</Form.Item> | ||
</Form> | ||
</Card> | ||
<Card className={style.qrcode__output}> | ||
{value.length > 0 ? ( | ||
<div id="myqrcode" className={style.qrcode__output__result}> | ||
<QRCode | ||
value={value} | ||
bgColor="#fff" | ||
style={{ marginBottom: 16 }} | ||
/> | ||
<Button onClick={downloadQRCode}>Download</Button> | ||
</div> | ||
) : ( | ||
<div className={style.qrcode__output__notfound}> | ||
<span> | ||
<Icon name="AlertTriangle" size={30} /> | ||
</span> | ||
|
||
<p> | ||
There is no data for generating QR code, please | ||
provide data first. | ||
</p> | ||
</div> | ||
)} | ||
</Card> | ||
</PageGrid> | ||
); | ||
}; | ||
|
||
export default QRcode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
function isValidUrl(url: string) { | ||
const pattern = new RegExp( | ||
"^(https?:\\/\\/)?" + | ||
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + | ||
Check failure Code scanning / CodeQL Inefficient regular expression
This part of the regular expression may cause exponential backtracking on strings starting with '0' and containing many repetitions of '0'.
|
||
"((\\d{1,3}\\.){3}\\d{1,3}))" + | ||
"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + | ||
"(\\?[;&a-z\\d%_.~+=-]*)?" + | ||
"(\\#[-a-z\\d_]*)?$", | ||
"i" | ||
); | ||
return pattern.test(url); | ||
} | ||
|
||
function detectQrData(data: string) { | ||
if (data.length === 0) { | ||
return "No data"; | ||
} | ||
|
||
if (isValidUrl(data)) return "Url"; | ||
|
||
return typeof data; | ||
} | ||
|
||
const downloadQRCode = () => { | ||
const canvas = document | ||
.getElementById("myqrcode") | ||
?.querySelector<HTMLCanvasElement>("canvas"); | ||
if (canvas) { | ||
const url = canvas.toDataURL(); | ||
const a = document.createElement("a"); | ||
a.download = "QRCode.png"; | ||
a.href = url; | ||
document.body.appendChild(a); | ||
a.click(); | ||
document.body.removeChild(a); | ||
} | ||
}; | ||
|
||
export { detectQrData, downloadQRCode }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
DOM text reinterpreted as HTML