Files
test-repo/website/playground/PrettierFormat.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

60 lines
1.1 KiB
JavaScript
Raw Normal View History

2025-02-26 15:47:40 +08:00
const { React } = window;
2018-04-11 22:28:50 -03:00
2018-04-17 17:29:44 -03:00
export default class PrettierFormat extends React.Component {
2018-04-11 22:28:50 -03:00
constructor() {
super();
2018-04-19 14:46:29 -03:00
this.state = { formatted: "", debug: {} };
2018-04-11 22:28:50 -03:00
}
componentDidMount() {
this.format();
}
componentDidUpdate(prevProps) {
for (const key of [
2022-07-23 22:46:16 +03:00
"enabled",
"code",
"options",
"debugAst",
2022-10-10 14:26:55 +03:00
"debugPreprocessedAst",
"debugDoc",
"debugComments",
"reformat",
"rethrowEmbedErrors",
]) {
2018-04-19 14:46:29 -03:00
if (prevProps[key] !== this.props[key]) {
this.format();
break;
}
2018-04-11 22:28:50 -03:00
}
}
async format() {
2018-04-19 14:46:29 -03:00
const {
worker,
code,
options,
debugAst: ast,
2022-10-10 14:26:55 +03:00
debugPreprocessedAst: preprocessedAst,
2018-04-19 14:46:29 -03:00
debugDoc: doc,
debugComments: comments,
2018-04-20 09:54:15 -03:00
reformat,
rethrowEmbedErrors,
2018-04-19 14:46:29 -03:00
} = this.props;
2018-04-17 16:09:37 -03:00
const result = await worker.format(code, options, {
ast,
2022-10-10 14:26:55 +03:00
preprocessedAst,
doc,
comments,
reformat,
rethrowEmbedErrors,
});
2022-08-01 11:27:34 +08:00
this.setState(result);
2018-04-11 22:28:50 -03:00
}
render() {
return this.props.children(this.state);
}
}