2019-05-09 15:24:26 +02:00
|
|
|
import React, { useState } from "react";
|
2019-05-03 19:54:46 +02:00
|
|
|
import PropTypes from "prop-types";
|
2019-05-05 17:06:22 +02:00
|
|
|
import Draft from "../../Draft";
|
2019-05-09 15:24:26 +02:00
|
|
|
import Design from "../Design";
|
2019-05-03 19:54:46 +02:00
|
|
|
import DraftConfigurator from "../../DraftConfigurator";
|
|
|
|
import { FormattedMessage } from "react-intl";
|
2019-05-09 15:24:26 +02:00
|
|
|
import Prism from "prismjs";
|
2019-05-03 19:54:46 +02:00
|
|
|
|
|
|
|
const DraftPattern = props => {
|
2019-05-09 15:24:26 +02:00
|
|
|
const [design, setDesign] = useState(true);
|
|
|
|
const [focus, setFocus] = useState(null);
|
|
|
|
|
|
|
|
const raiseEvent = (type, data) => {
|
|
|
|
if (type === "clearFocusAll") return setFocus(null);
|
|
|
|
let f = {};
|
|
|
|
if (focus !== null) f = { ...focus };
|
|
|
|
if (typeof f[data.part] === "undefined")
|
|
|
|
f[data.part] = { paths: [], points: [], coords: [] };
|
|
|
|
if (type === "point") f[data.part].points.push(data.name);
|
|
|
|
else if (type === "path") f[data.part].paths.push(data.name);
|
|
|
|
else if (type === "coords") f[data.part].coords.push(data.coords);
|
|
|
|
else if (type === "clearFocus") {
|
|
|
|
let i = focus[data.part][data.type].indexOf(data.name);
|
|
|
|
f[data.part][data.type].splice(i, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
setFocus(f);
|
|
|
|
};
|
|
|
|
|
|
|
|
const styles = {
|
|
|
|
paragraph: {
|
|
|
|
padding: "0 1rem"
|
|
|
|
}
|
|
|
|
};
|
2019-05-05 17:06:22 +02:00
|
|
|
let pattern = new props.Pattern(props.gist.settings);
|
2019-05-03 19:54:46 +02:00
|
|
|
pattern.draft();
|
2019-05-09 15:24:26 +02:00
|
|
|
let patternProps = pattern.getRenderProps();
|
|
|
|
let focusCount = 0;
|
|
|
|
if (focus !== null) {
|
|
|
|
for (let p of Object.keys(focus)) {
|
|
|
|
for (let i in focus[p].points) focusCount++;
|
|
|
|
for (let i in focus[p].paths) focusCount++;
|
|
|
|
for (let i in focus[p].coords) focusCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let gist = Prism.highlight(
|
|
|
|
JSON.stringify(props.gist, null, 2),
|
|
|
|
Prism.languages.javascript,
|
|
|
|
"javascript"
|
|
|
|
);
|
|
|
|
|
2019-05-03 19:54:46 +02:00
|
|
|
return (
|
|
|
|
<div className="fs-sa">
|
|
|
|
<section>
|
|
|
|
<h2>
|
|
|
|
<FormattedMessage id="app.pattern" />
|
|
|
|
</h2>
|
2019-05-09 15:24:26 +02:00
|
|
|
<Draft
|
|
|
|
{...patternProps}
|
|
|
|
design={design}
|
|
|
|
focus={focus}
|
|
|
|
raiseEvent={raiseEvent}
|
|
|
|
/>
|
2019-05-03 19:54:46 +02:00
|
|
|
<h2>gist</h2>
|
2019-05-10 09:48:27 +02:00
|
|
|
<div className="gatsby-highlight">
|
2019-05-09 15:24:26 +02:00
|
|
|
<pre
|
|
|
|
className="language-json"
|
|
|
|
dangerouslySetInnerHTML={{ __html: gist }}
|
|
|
|
/>
|
|
|
|
</div>
|
2019-05-03 19:54:46 +02:00
|
|
|
</section>
|
|
|
|
|
|
|
|
<aside>
|
|
|
|
<div className="sticky">
|
2019-05-09 15:24:26 +02:00
|
|
|
{design ? (
|
|
|
|
<React.Fragment>
|
|
|
|
<p style={styles.paragraph}>
|
|
|
|
<FormattedMessage id="cfp.designModeIsOn" />
|
|
|
|
(
|
|
|
|
<a href="#logo" onClick={() => setDesign(false)}>
|
|
|
|
<FormattedMessage id="cfp.turnOff" />
|
|
|
|
</a>
|
|
|
|
)
|
|
|
|
{focusCount > 0 ? (
|
|
|
|
<React.Fragment>
|
|
|
|
 (
|
|
|
|
<a
|
|
|
|
href="#logo"
|
|
|
|
onClick={() => raiseEvent("clearFocusAll", null)}
|
|
|
|
>
|
|
|
|
<FormattedMessage id="app.reset" />
|
|
|
|
</a>
|
|
|
|
)
|
|
|
|
</React.Fragment>
|
|
|
|
) : null}
|
|
|
|
</p>
|
|
|
|
<Design
|
|
|
|
focus={focus}
|
|
|
|
design={design}
|
|
|
|
raiseEvent={raiseEvent}
|
|
|
|
parts={patternProps.parts}
|
|
|
|
/>
|
|
|
|
</React.Fragment>
|
|
|
|
) : (
|
|
|
|
<p style={styles.paragraph}>
|
|
|
|
<FormattedMessage id="cfp.designModeIsOff" />
|
|
|
|
(
|
|
|
|
<a href="#logo" onClick={() => setDesign(true)}>
|
|
|
|
<FormattedMessage id="cfp.turnOn" />
|
|
|
|
</a>
|
|
|
|
)
|
|
|
|
</p>
|
|
|
|
)}
|
2019-05-03 19:54:46 +02:00
|
|
|
<DraftConfigurator
|
2019-05-10 10:53:34 +02:00
|
|
|
noDocs
|
2019-05-03 19:54:46 +02:00
|
|
|
config={props.config}
|
|
|
|
gist={props.gist}
|
|
|
|
updateGist={props.updateGist}
|
|
|
|
raiseEvent={props.raiseEvent}
|
|
|
|
freesewing={props.freesewing}
|
|
|
|
units={props.units}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</aside>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
DraftPattern.propTypes = {
|
|
|
|
gist: PropTypes.object.isRequired,
|
|
|
|
updateGist: PropTypes.func.isRequired,
|
|
|
|
config: PropTypes.object.isRequired,
|
|
|
|
raiseEvent: PropTypes.func.isRequired,
|
|
|
|
Pattern: PropTypes.func.isRequired,
|
|
|
|
units: PropTypes.oneOf(["metric", "imperial"])
|
|
|
|
};
|
|
|
|
|
|
|
|
DraftPattern.defaultProps = {
|
|
|
|
units: "metric",
|
|
|
|
pointInfo: null
|
|
|
|
};
|
|
|
|
|
|
|
|
export default DraftPattern;
|