Skip to content

Commit 6e13803

Browse files
authored
Merge pull request #1538 from vietdanh1899/fix-autoformat-codeeditor
Fix code editor auto format
2 parents c42c0e0 + 109bb8f commit 6e13803

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

client/packages/lowcoder/src/base/codeEditor/autoFormat.tsx

+15-13
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,31 @@ import type { CodeType } from "lowcoder-core";
22
import { relaxedJSONToJSON } from "lowcoder-core";
33
import { getDynamicStringSegments, isDynamicSegment } from "lowcoder-core";
44
import { format as formatSQL } from "sql-formatter";
5+
import estree from "prettier/plugins/estree";
56
import type { Language } from "./codeEditorTypes";
67

78
export async function cssFormatter(text: string) {
8-
const prettier = await require("prettier/standalone");
9-
const parserPlugin = await require("prettier/parser-postcss");
9+
const prettier = await import("prettier/standalone");
10+
const parserPlugin = await import("prettier/plugins/postcss");
1011
return (await prettier.format(text, { parser: "css", plugins: [parserPlugin], semi: false })).trim();
1112
}
1213

1314
export async function htmlFormatter(text: string) {
14-
const prettier = await require("prettier/standalone");
15-
const parserPlugin = await require("prettier/parser-html");
15+
const prettier = await import("prettier/standalone");
16+
const parserPlugin = await import("prettier/plugins/html");
1617
return (await prettier.format(text, { parser: "html", plugins: [parserPlugin], semi: false })).trim();
1718
}
1819

1920
async function getJavascriptFormatter() {
20-
const prettier = await require("prettier/standalone");
21-
const parserBabel = await require("prettier/parser-babel");
21+
const prettier = await import("prettier/standalone");
22+
const parserBabel = await import("prettier/plugins/babel");
2223
return async (text: string) =>
23-
(await prettier.format(text, { parser: "babel", plugins: [parserBabel], semi: false })).trim();
24+
(await prettier.format(text, { parser: "babel", plugins: [parserBabel, estree], semi: false })).trim();
2425
}
2526

2627
export async function getJsonFormatter() {
27-
const prettier = await require("prettier/standalone");
28-
const parserBabel = await require("prettier/parser-babel");
28+
const prettier = await import("prettier/standalone");
29+
const parserBabel = await import("prettier/plugins/babel");
2930
return async (text: string) => (await prettier.format(text, { parser: "json", plugins: [parserBabel] })).trim();
3031
}
3132

@@ -46,15 +47,16 @@ async function formatJsSegment(formatter: (text: string) => Promise<string>, scr
4647
async function getJsSegmentFormatter() {
4748
const formatter = await getJavascriptFormatter();
4849
return async (segment: string) => {
49-
return "{{" + formatJsSegment(formatter, segment.slice(2, -2)) + "}}";
50+
return "{{" + await formatJsSegment(formatter, segment.slice(2, -2)) + "}}";
5051
};
5152
}
5253

5354
export async function formatStringWithJsSnippets(text: string): Promise<string> {
5455
const jsSegmentFormatter = await getJsSegmentFormatter();
55-
return getDynamicStringSegments(text)
56-
.map((s) => (isDynamicSegment(s) ? jsSegmentFormatter(s) : s))
57-
.join("");
56+
const formatedSegments = await Promise.all(
57+
getDynamicStringSegments(text).map((s) => (isDynamicSegment(s) ? jsSegmentFormatter(s) : s))
58+
);
59+
return formatedSegments.join("");
5860
}
5961

6062
export async function formatSqlWithJsSnippets(text: string) {

0 commit comments

Comments
 (0)