prettier/website/playground/PrettierFormat.js

51 lines
958 B
JavaScript
Raw Normal View History

2018-04-12 04:28:50 +03:00
import React from "react";
export default class extends React.Component {
constructor() {
super();
this.state = { formatted: "" };
}
componentDidMount() {
this.format();
}
componentDidUpdate(prevProps) {
2018-04-17 19:40:55 +03:00
const { code, options, debugAst, debugDoc, secondFormat } = this.props;
2018-04-12 04:28:50 +03:00
if (
prevProps.code !== code ||
prevProps.options !== options ||
2018-04-12 17:51:49 +03:00
prevProps.debugAst !== debugAst ||
2018-04-17 19:40:55 +03:00
prevProps.debugDoc !== debugDoc ||
prevProps.secondFormat !== secondFormat
2018-04-12 04:28:50 +03:00
) {
this.format();
}
}
format() {
2018-04-17 19:40:55 +03:00
const {
code,
options,
worker,
debugAst,
debugDoc,
secondFormat
} = this.props;
2018-04-12 04:28:50 +03:00
worker
2018-04-17 19:40:55 +03:00
.postMessage({
type: "format",
code,
options,
debugAst,
debugDoc,
secondFormat
})
2018-04-12 04:28:50 +03:00
.then(result => this.setState(result));
}
render() {
return this.props.children(this.state);
}
}