Skip to content

Commit 576667a

Browse files
authored
Merge pull request #664 from code0-tech/feat/#663
Adding formatter to Editor props
2 parents 6d5fbca + 5b2ddca commit 576667a

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

src/components/editor/Editor.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export interface EditorInputProps extends Omit<Code0Component<HTMLDivElement>, '
6666
readonly?: boolean
6767
showTooltips?: boolean
6868
showValidation?: boolean,
69+
formatter?: (value: string) => Promise<string>
6970
basicSetup?: BasicSetupOptions
7071
}
7172

@@ -136,6 +137,7 @@ export const Editor: React.FC<EditorInputProps> = (props) => {
136137
suggestions,
137138
onChange,
138139
extensions = [],
140+
formatter,
139141
initialValue,
140142
formValidation,
141143
disabled,
@@ -157,14 +159,18 @@ export const Editor: React.FC<EditorInputProps> = (props) => {
157159
} | null>(null)
158160
const containerRef = React.useRef<HTMLDivElement>(null)
159161

160-
language === "json" && React.useEffect(() => {
162+
const jsonFormatter = (value: string) => {
163+
return prettier.format(value, {
164+
parser: "json",
165+
plugins: [parserBabel, parserEstree],
166+
printWidth: 1
167+
})
168+
}
169+
170+
React.useEffect(() => {
161171
(async () => {
162172
try {
163-
const pretty = await prettier.format(JSON.stringify(initialValue) ?? "", {
164-
parser: language,
165-
plugins: [parserBabel, parserEstree],
166-
printWidth: 1
167-
})
173+
const pretty = formatter ? await formatter(initialValue) : language === "json" ? await jsonFormatter(initialValue) : initialValue
168174
setFormatted(pretty)
169175
} catch (e) {
170176
setFormatted(JSON.stringify(initialValue) ?? "")

0 commit comments

Comments
 (0)