Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeparticle committed Oct 3, 2023
1 parent c61ffe0 commit a9c57dd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
1 change: 0 additions & 1 deletion ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@

<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/logo.svg" />
<!-- hello -->
</head>
<body>
<div id="root"></div>
Expand Down
29 changes: 13 additions & 16 deletions ui/src/pages/Converter/CodeFormatter/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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";
Expand All @@ -11,7 +10,11 @@ import {
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";
import {
BEAUTIFY_FUNCTIONS,
INDENTATION_LEVEL,
INPUT_TYPE,
} from "./utils/constants";

const CodeFormatter: React.FC = () => {
const [inputCode, setInputCode] = useState("");
Expand All @@ -23,20 +26,14 @@ const CodeFormatter: React.FC = () => {

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),
});
}
const options = {
indent_size: Number(indentationLevel),
};

const selectedBeautifyFunction =
BEAUTIFY_FUNCTIONS[inputType] || BEAUTIFY_FUNCTIONS.default;

const formatted = selectedBeautifyFunction(inputCode, options);

setFormattedCode(formatted);
} catch (error) {
Expand Down
12 changes: 11 additions & 1 deletion ui/src/pages/Converter/CodeFormatter/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import beautify from "js-beautify";

const INDENTATION_LEVEL = [
{
value: "2",
Expand All @@ -21,4 +23,12 @@ const INPUT_TYPE = [
{ label: "YAML", value: "yaml" },
];

export { INDENTATION_LEVEL, INPUT_TYPE };
const BEAUTIFY_FUNCTIONS: {
[key: string]: (code: string, options: object) => string;
} = {
html: beautify.html_beautify,
css: beautify.css_beautify,
default: beautify.js_beautify,
};

export { INDENTATION_LEVEL, INPUT_TYPE, BEAUTIFY_FUNCTIONS };

0 comments on commit a9c57dd

Please sign in to comment.