prettier/website/playground/PrettierFormat.js

41 lines
763 B
JavaScript
Raw Normal View History

2018-04-12 04:28:50 +03:00
import React from "react";
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-20 17:13:09 +03:00
for (const key of ["code", "options", "debugAst", "debugDoc", "reformat"]) {
2018-04-19 20:46:29 +03:00
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,
2018-04-20 15:54:15 +03:00
reformat
2018-04-19 20:46:29 +03:00
} = 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);
}
}