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) {
|
2021-01-27 12:30:10 +02:00
|
|
|
for (const key of [
|
2022-07-23 22:46:16 +03:00
|
|
|
"enabled",
|
2021-01-27 12:30:10 +02:00
|
|
|
"code",
|
|
|
|
|
"options",
|
|
|
|
|
"debugAst",
|
2022-10-10 14:26:55 +03:00
|
|
|
"debugPreprocessedAst",
|
2021-01-27 12:30:10 +02:00
|
|
|
"debugDoc",
|
|
|
|
|
"debugComments",
|
|
|
|
|
"reformat",
|
2022-08-05 17:05:50 +03:00
|
|
|
"rethrowEmbedErrors",
|
2021-01-27 12:30:10 +02:00
|
|
|
]) {
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 14:22:24 +08: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,
|
2021-01-27 12:30:10 +02:00
|
|
|
debugComments: comments,
|
2018-04-20 09:54:15 -03:00
|
|
|
reformat,
|
2022-08-05 17:05:50 +03:00
|
|
|
rethrowEmbedErrors,
|
2018-04-19 14:46:29 -03:00
|
|
|
} = this.props;
|
2018-04-17 16:09:37 -03:00
|
|
|
|
2022-04-29 14:22:24 +08:00
|
|
|
const result = await worker.format(code, options, {
|
|
|
|
|
ast,
|
2022-10-10 14:26:55 +03:00
|
|
|
preprocessedAst,
|
2022-04-29 14:22:24 +08:00
|
|
|
doc,
|
|
|
|
|
comments,
|
|
|
|
|
reformat,
|
2022-08-06 11:42:33 +08:00
|
|
|
rethrowEmbedErrors,
|
2022-04-29 14:22:24 +08:00
|
|
|
});
|
2022-08-01 11:27:34 +08:00
|
|
|
|
2022-04-29 14:22:24 +08:00
|
|
|
this.setState(result);
|
2018-04-11 22:28:50 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
return this.props.children(this.state);
|
|
|
|
|
}
|
|
|
|
|
}
|