mirror of
https://github.com/ritz078/transform.git
synced 2026-11-21 00:32:56 +03:00
added all files
This commit is contained in:
1 parent
abf42b23b0
commit
15df256047
3 files changed
+55
-46
No files matched your search
@@ -91,17 +91,27 @@ export default class ConversionPanel extends PureComponent {
|
||||
value: "",
|
||||
splitValue: "",
|
||||
info: "",
|
||||
infoType: ""
|
||||
infoType: "",
|
||||
isPrettierAvailable: false
|
||||
};
|
||||
|
||||
componentDidMount() {
|
||||
const { defaultText, splitValue } = this.props;
|
||||
|
||||
this.onChange(defaultText, splitValue);
|
||||
|
||||
const MyWorker = require('worker-loader!../utils/worker.js')
|
||||
this.worker = new MyWorker()
|
||||
|
||||
this.worker.onmessage = this.setPrettyResult
|
||||
}
|
||||
|
||||
isPrettierAvailable() {
|
||||
return isBrowser && !!window.prettier;
|
||||
setPrettyResult = ({ data }) => {
|
||||
const {available, prettyCode, section} = data
|
||||
this.setState({
|
||||
isPrettierAvailable: available,
|
||||
[section]: prettyCode
|
||||
})
|
||||
}
|
||||
|
||||
fetchJSON = async () => {
|
||||
@@ -119,7 +129,13 @@ export default class ConversionPanel extends PureComponent {
|
||||
}
|
||||
};
|
||||
|
||||
prettify = (code, mode, section) => {
|
||||
this.worker.postMessage({ code, mode, section })
|
||||
}
|
||||
|
||||
onChange = async (newValue, leftSplitValue) => {
|
||||
const { prettifyRightPanel } = this.props
|
||||
|
||||
const nValue = newValue || this.state.value;
|
||||
const splitValue =
|
||||
leftSplitValue && typeof leftSplitValue === "string"
|
||||
@@ -128,8 +144,18 @@ export default class ConversionPanel extends PureComponent {
|
||||
|
||||
try {
|
||||
const code = await this.props.getTransformedValue(nValue, splitValue);
|
||||
if (prettifyRightPanel) {
|
||||
this.worker.postMessage({
|
||||
code,
|
||||
mode: this.props.rightMode,
|
||||
section: 'resultValue'
|
||||
})
|
||||
} else {
|
||||
this.setState({
|
||||
resultValue: code
|
||||
})
|
||||
}
|
||||
this.setState({
|
||||
resultValue: code,
|
||||
info: "",
|
||||
infoType: ""
|
||||
});
|
||||
@@ -155,46 +181,35 @@ export default class ConversionPanel extends PureComponent {
|
||||
}
|
||||
};
|
||||
|
||||
setResult = result => {
|
||||
this.setState({
|
||||
value: result
|
||||
});
|
||||
};
|
||||
|
||||
setSplitValue = splitValue => this.setState({ splitValue });
|
||||
|
||||
prettifyCode = () => {
|
||||
if (!this.isPrettierAvailable()) return;
|
||||
if (!this.state.isPrettierAvailable) return;
|
||||
const { leftMode } = this.props;
|
||||
const { value } = this.state;
|
||||
|
||||
this.setResult(
|
||||
window.prettier.format(value, { parser: prettierParsers[leftMode] })
|
||||
);
|
||||
this.worker.postMessage({
|
||||
code: value,
|
||||
mode: leftMode,
|
||||
section: 'value'
|
||||
})
|
||||
};
|
||||
|
||||
prettifySplitCode = () => {
|
||||
if (!this.isPrettierAvailable()) return;
|
||||
if (!this.state.isPrettierAvailable) return;
|
||||
const { splitMode } = this.props;
|
||||
const { splitValue } = this.state;
|
||||
|
||||
this.setSplitValue(
|
||||
window.prettier.format(splitValue, { parser: prettierParsers[splitMode] })
|
||||
);
|
||||
this.worker.postMessage({
|
||||
code: splitValue,
|
||||
mode: splitMode,
|
||||
section: 'splitValue'
|
||||
})
|
||||
};
|
||||
|
||||
copyCode = () => {
|
||||
const { rightMode } = this.props;
|
||||
const { resultValue } = this.state;
|
||||
const parser = prettierParsers[rightMode];
|
||||
|
||||
copy(
|
||||
parser
|
||||
? window.prettier.format(resultValue, {
|
||||
parser
|
||||
})
|
||||
: resultValue
|
||||
);
|
||||
copy(resultValue);
|
||||
|
||||
this.setState({
|
||||
info: "Code copied to clipboard.",
|
||||
infoType: "success"
|
||||
@@ -203,7 +218,7 @@ export default class ConversionPanel extends PureComponent {
|
||||
|
||||
getPrettifyClass = () => {
|
||||
return cn("btn", {
|
||||
disabled: !this.isPrettierAvailable()
|
||||
disabled: !this.state.isPrettierAvailable
|
||||
})
|
||||
}
|
||||
|
||||
@@ -216,7 +231,6 @@ export default class ConversionPanel extends PureComponent {
|
||||
initialCheckboxValue,
|
||||
leftTitle,
|
||||
rightTitle,
|
||||
prettifyRightPanel,
|
||||
splitLeft,
|
||||
splitTitle,
|
||||
splitMode,
|
||||
@@ -251,7 +265,6 @@ export default class ConversionPanel extends PureComponent {
|
||||
|
||||
return (
|
||||
<div className="wrapper">
|
||||
<script src="https://bundle.run/prettier@1.6.1" />
|
||||
<style jsx>{`
|
||||
.wrapper {
|
||||
display: flex;
|
||||
@@ -498,19 +511,7 @@ export default class ConversionPanel extends PureComponent {
|
||||
</div>
|
||||
{isBrowser && (
|
||||
<CodeMirror
|
||||
value={
|
||||
this.isPrettierAvailable() &&
|
||||
prettifyRightPanel &&
|
||||
prettierParsers[rightMode] &&
|
||||
resultValue ? (
|
||||
window.prettier.format(resultValue, {
|
||||
parser: prettierParsers[rightMode],
|
||||
printWidth: 70
|
||||
})
|
||||
) : (
|
||||
resultValue
|
||||
)
|
||||
}
|
||||
value={resultValue}
|
||||
options={{
|
||||
readOnly: true,
|
||||
mode: modeMapping[rightMode] || rightMode,
|
||||
|
||||
+2
-1
@@ -66,6 +66,7 @@
|
||||
"prettier": "^1.6.1",
|
||||
"style-loader": "^0.18.2",
|
||||
"sw-precache-webpack-plugin": "^0.11.4",
|
||||
"webpack-bundle-analyzer": "^2.9.0"
|
||||
"webpack-bundle-analyzer": "^2.9.0",
|
||||
"worker-loader": "^0.8.1"
|
||||
}
|
||||
}
|
||||
@@ -5791,6 +5791,13 @@ wordwrap@0.0.2:
|
||||
version "0.0.2"
|
||||
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.2.tgz#b79669bb42ecb409f83d583cad52ca17eaa1643f"
|
||||
|
||||
worker-loader@^0.8.1:
|
||||
version "0.8.1"
|
||||
resolved "https://registry.yarnpkg.com/worker-loader/-/worker-loader-0.8.1.tgz#e8e995331ea34df5bf68296824bfb7f0ad578d43"
|
||||
dependencies:
|
||||
loader-utils "^1.0.2"
|
||||
schema-utils "^0.3.0"
|
||||
|
||||
wrap-ansi@^2.0.0:
|
||||
version "2.1.0"
|
||||
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-2.1.0.tgz#d8fc3d284dd05794fe84973caecdd1cf824fdd85"
|
||||
|
||||
Reference in new issue
Block a user