-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #344 from ashik-75/work
Add code formatter
- Loading branch information
Showing
10 changed files
with
279 additions
and
1 deletion.
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
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
18 changes: 18 additions & 0 deletions
18
ui/src/pages/Converter/CodeFormatter/CodeFormatter.module.scss
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,18 @@ | ||
.codeformatter { | ||
&__input { | ||
display: flex; | ||
flex-direction: column; | ||
height: 100%; | ||
} | ||
|
||
&__output { | ||
height: 100%; | ||
|
||
&_warning { | ||
min-height: 375px; | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: 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,112 @@ | ||
import React, { useState } from "react"; | ||
import beautify from "js-beautify"; | ||
import style from "./CodeFormatter.module.scss"; | ||
import PageGrid from "components/Layouts/PageGrid"; | ||
import { Card, Form, Space } from "antd"; | ||
import TextareaWithValidation from "components/General/TextareaWithValidation"; | ||
import { | ||
ResponsiveButton, | ||
ResponsiveSelectWithLabel, | ||
} from "components/General/FormComponents"; | ||
import CodeHighlightWithCopy from "components/General/CodeHighlightWithCopy"; | ||
import Warning from "components/General/Warning"; | ||
import InputGrid from "components/Layouts/InputGrid"; | ||
import { INDENTATION_LEVEL, INPUT_TYPE } from "./utils/constants"; | ||
|
||
const CodeFormatter: React.FC = () => { | ||
const [inputCode, setInputCode] = useState(""); | ||
const [formattedCode, setFormattedCode] = useState(""); | ||
const [indentationLevel, setIndentationLevel] = useState( | ||
INDENTATION_LEVEL[2].value | ||
); | ||
const [inputType, setInputType] = useState(INPUT_TYPE[3].value); | ||
|
||
const formatCode = () => { | ||
try { | ||
let formatted = ""; | ||
if (inputType === "html") { | ||
formatted = beautify.html_beautify(inputCode, { | ||
indent_size: Number(indentationLevel), | ||
}); | ||
} else if (inputType === "css") { | ||
formatted = beautify.css_beautify(inputCode, { | ||
indent_size: Number(indentationLevel), | ||
}); | ||
} else { | ||
formatted = beautify.js_beautify(inputCode, { | ||
indent_size: Number(indentationLevel), | ||
}); | ||
} | ||
|
||
setFormattedCode(formatted); | ||
} catch (error) { | ||
setFormattedCode( | ||
"Error formatting code. Check the console for details." | ||
); | ||
} | ||
}; | ||
|
||
return ( | ||
<PageGrid className={style.codeformatter}> | ||
<Card className={style.codeformatter__input}> | ||
<Form layout="vertical"> | ||
<InputGrid> | ||
<ResponsiveSelectWithLabel | ||
label="Input Type" | ||
value={inputType} | ||
defaultActiveFirstOption | ||
onSelect={(value) => { | ||
setInputType(value); | ||
}} | ||
options={INPUT_TYPE} | ||
/> | ||
<ResponsiveSelectWithLabel | ||
label="Indentation level" | ||
value={indentationLevel} | ||
defaultActiveFirstOption | ||
onSelect={(value) => { | ||
setIndentationLevel(value); | ||
}} | ||
options={INDENTATION_LEVEL} | ||
/> | ||
</InputGrid> | ||
<TextareaWithValidation | ||
value={inputCode} | ||
onChange={(e) => { | ||
setInputCode(e.target.value); | ||
setFormattedCode(""); | ||
}} | ||
label="Input Code" | ||
placeholder="Paste code for formatting" | ||
rows={12} | ||
status={status} | ||
/> | ||
|
||
<Space> | ||
<ResponsiveButton | ||
onClick={formatCode} | ||
disabled={inputCode.length === 0 || !inputType} | ||
> | ||
Format | ||
</ResponsiveButton> | ||
</Space> | ||
</Form> | ||
</Card> | ||
|
||
<Card className={style.codeformatter__output}> | ||
{formattedCode.toString().length > 0 ? ( | ||
<CodeHighlightWithCopy | ||
codeString={formattedCode.toString()} | ||
language={inputType} | ||
/> | ||
) : ( | ||
<div className={style.codeformatter__output_warning}> | ||
<Warning text="There is no data for the selected type, please provide data first." /> | ||
</div> | ||
)} | ||
</Card> | ||
</PageGrid> | ||
); | ||
}; | ||
|
||
export default CodeFormatter; |
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,24 @@ | ||
const INDENTATION_LEVEL = [ | ||
{ | ||
value: "2", | ||
label: "2 space per indent level", | ||
}, | ||
{ | ||
value: "3", | ||
label: "3 space per indent level", | ||
}, | ||
{ | ||
value: "4", | ||
label: "4 space per indent level", | ||
}, | ||
]; | ||
|
||
const INPUT_TYPE = [ | ||
{ label: "HTML", value: "html" }, | ||
{ label: "CSS", value: "css" }, | ||
{ label: "JSON", value: "json" }, | ||
{ label: "JavaScript", value: "javascript" }, | ||
{ label: "YAML", value: "yaml" }, | ||
]; | ||
|
||
export { INDENTATION_LEVEL, INPUT_TYPE }; |
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
Oops, something went wrong.