prettier/website/playground/PrettierFormat.js

40 lines
838 B
JavaScript
Raw Normal View History

2018-04-12 04:28:50 +03:00
import React from "react";
2018-04-17 22:09:37 +03:00
import { shallowEqual } from "./helpers";
function getFormatProps(props) {
const { code, options, debugAst, debugDoc, secondFormat } = props;
return { code, options, debugAst, debugDoc, secondFormat };
}
2018-04-17 23:29:44 +03:00
export default class PrettierFormat extends React.Component {
2018-04-12 04:28:50 +03:00
constructor() {
super();
this.state = { formatted: "" };
}
componentDidMount() {
this.format();
}
componentDidUpdate(prevProps) {
2018-04-17 22:09:37 +03:00
if (!shallowEqual(getFormatProps(prevProps), getFormatProps(this.props))) {
2018-04-12 04:28:50 +03:00
this.format();
}
}
format() {
2018-04-17 22:09:37 +03:00
const { worker } = this.props;
2018-04-12 04:28:50 +03:00
worker
2018-04-17 22:09:37 +03:00
.postMessage(
Object.assign({ type: "format" }, getFormatProps(this.props))
)
2018-04-12 04:28:50 +03:00
.then(result => this.setState(result));
}
render() {
return this.props.children(this.state);
}
}