2018-04-12 04:28:50 +03:00
|
|
|
import React from "react";
|
|
|
|
|
2018-04-17 22:09:37 +03:00
|
|
|
import { shallowEqual } from "./helpers";
|
|
|
|
|
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();
|
2018-04-19 20:46:29 +03:00
|
|
|
this.state = { formatted: "", debug: {} };
|
2018-04-12 04:28:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.format();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
2018-04-19 20:46:29 +03:00
|
|
|
for (const key of [
|
|
|
|
"code",
|
|
|
|
"options",
|
|
|
|
"debugAst",
|
|
|
|
"debugDoc",
|
|
|
|
"secondFormat"
|
|
|
|
]) {
|
|
|
|
if (prevProps[key] !== this.props[key]) {
|
|
|
|
this.format();
|
|
|
|
break;
|
|
|
|
}
|
2018-04-12 04:28:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
format() {
|
2018-04-19 20:46:29 +03:00
|
|
|
const {
|
|
|
|
worker,
|
|
|
|
code,
|
|
|
|
options,
|
|
|
|
debugAst: ast,
|
|
|
|
debugDoc: doc,
|
|
|
|
secondFormat: reformat
|
|
|
|
} = this.props;
|
2018-04-17 22:09:37 +03:00
|
|
|
|
2018-04-12 04:28:50 +03:00
|
|
|
worker
|
2018-04-19 20:46:29 +03:00
|
|
|
.format(code, options, { ast, doc, reformat })
|
2018-04-12 04:28:50 +03:00
|
|
|
.then(result => this.setState(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return this.props.children(this.state);
|
|
|
|
}
|
|
|
|
}
|