Skip to content

Commit e510129

Browse files
chore: prettify preview
1 parent 9d9aa72 commit e510129

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/components/molecules/StaticEditor.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useEffect, useRef, useState, type ReactElement } from 'react';
22
import { useTheme } from '../ThemeProvider';
3+
import prettify from '@/lib/utils.ts';
34

45
const SANDBOX_THEMES = {
56
light: 'sandbox',
@@ -55,7 +56,11 @@ export function StaticEditor({ code }: { code: string }): ReactElement {
5556
}, []);
5657

5758
useEffect(() => {
58-
if (code !== sandboxRef.current?.getText?.()) sandboxRef.current?.setText?.(code);
59+
prettify(code)
60+
.then(prettyCode => {
61+
sandboxRef.current?.setText?.(prettyCode);
62+
})
63+
.catch(() => sandboxRef.current?.setText?.(code));
5964
}, [code]);
6065

6166
useEffect(() => {

src/lib/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
import { type ClassValue, clsx } from 'clsx';
22
import { twMerge } from 'tailwind-merge';
33

4+
import * as prettier from 'prettier/standalone';
5+
import * as estreePlugin from 'prettier/plugins/estree';
6+
import * as tsPlugin from 'prettier/plugins/typescript';
7+
8+
const prettierConfig = {
9+
singleQuote: true,
10+
trailingComma: 'es5',
11+
printWidth: 120,
12+
arrowParens: 'avoid',
13+
} as const;
14+
15+
const plugins = [estreePlugin, tsPlugin];
16+
17+
export default async function prettify(file: string, path = 'default.tsx'): Promise<string> {
18+
return prettier.format(file, {
19+
...prettierConfig,
20+
parser: 'typescript',
21+
plugins,
22+
filepath: path,
23+
});
24+
}
25+
426
export function cn(...inputs: ClassValue[]) {
527
return twMerge(clsx(inputs));
628
}

0 commit comments

Comments
 (0)