2020-01-27 17:28:07 +01:00
|
|
|
import React, { useState } from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import Draft from '../../Draft'
|
|
|
|
import Design from '../Design'
|
|
|
|
import DraftConfigurator from '../../DraftConfigurator'
|
|
|
|
import { FormattedMessage } from 'react-intl'
|
|
|
|
import Prism from 'prismjs'
|
|
|
|
import fileSaver from 'file-saver'
|
|
|
|
import theme from '@freesewing/plugin-theme'
|
2019-05-03 19:54:46 +02:00
|
|
|
|
|
|
|
const DraftPattern = props => {
|
2020-01-27 17:28:07 +01:00
|
|
|
const [design, setDesign] = useState(true)
|
|
|
|
const [focus, setFocus] = useState(null)
|
2019-05-09 15:24:26 +02:00
|
|
|
|
|
|
|
const raiseEvent = (type, data) => {
|
2020-01-27 17:28:07 +01:00
|
|
|
if (type === 'clearFocusAll') {
|
|
|
|
props.updateGist(false, 'settings', 'only')
|
|
|
|
return setFocus(null)
|
2019-05-30 21:19:41 +02:00
|
|
|
}
|
2020-01-27 17:28:07 +01:00
|
|
|
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)
|
|
|
|
} else if (type === 'part') props.updateGist(data, 'settings', 'only')
|
2019-05-09 15:24:26 +02:00
|
|
|
|
2020-01-27 17:28:07 +01:00
|
|
|
setFocus(f)
|
|
|
|
}
|
2019-05-09 15:24:26 +02:00
|
|
|
|
2019-07-19 12:48:57 +02:00
|
|
|
const svgToFile = svg => {
|
|
|
|
const blob = new Blob([svg], {
|
2020-01-27 17:28:07 +01:00
|
|
|
type: 'image/svg+xml;charset=utf-8'
|
|
|
|
})
|
|
|
|
fileSaver.saveAs(blob, 'freesewing-' + props.config.name + '.svg')
|
|
|
|
}
|
2019-07-19 12:48:57 +02:00
|
|
|
|
|
|
|
if (props.svgExport) {
|
|
|
|
svgToFile(
|
2019-09-17 10:41:57 +02:00
|
|
|
new props.Pattern({
|
|
|
|
...props.gist.settings,
|
|
|
|
embed: false
|
|
|
|
})
|
2019-07-19 12:48:57 +02:00
|
|
|
.use(theme)
|
|
|
|
.draft()
|
|
|
|
.render()
|
2020-01-27 17:28:07 +01:00
|
|
|
)
|
|
|
|
props.setSvgExport(false)
|
2019-07-19 12:48:57 +02:00
|
|
|
}
|
|
|
|
|
2019-05-09 15:24:26 +02:00
|
|
|
const styles = {
|
|
|
|
paragraph: {
|
2020-01-27 17:28:07 +01:00
|
|
|
padding: '0 1rem'
|
2019-05-09 15:24:26 +02:00
|
|
|
}
|
2020-01-27 17:28:07 +01:00
|
|
|
}
|
|
|
|
let pattern = new props.Pattern(props.gist.settings)
|
|
|
|
pattern.draft()
|
|
|
|
let patternProps = pattern.getRenderProps()
|
|
|
|
let focusCount = 0
|
2019-05-09 15:24:26 +02:00
|
|
|
if (focus !== null) {
|
|
|
|
for (let p of Object.keys(focus)) {
|
2020-01-27 17:28:07 +01:00
|
|
|
for (let i in focus[p].points) focusCount++
|
|
|
|
for (let i in focus[p].paths) focusCount++
|
|
|
|
for (let i in focus[p].coords) focusCount++
|
2019-05-09 15:24:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let gist = Prism.highlight(
|
|
|
|
JSON.stringify(props.gist, null, 2),
|
|
|
|
Prism.languages.javascript,
|
2020-01-27 17:28:07 +01:00
|
|
|
'javascript'
|
|
|
|
)
|
2019-05-09 15:24:26 +02:00
|
|
|
|
2019-05-03 19:54:46 +02:00
|
|
|
return (
|
|
|
|
<div className="fs-sa">
|
|
|
|
<section>
|
|
|
|
<h2>
|
|
|
|
<FormattedMessage id="app.pattern" />
|
|
|
|
</h2>
|
2020-01-27 17:28:07 +01: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">
|
2020-01-27 17:28:07 +01:00
|
|
|
<pre className="language-json" dangerouslySetInnerHTML={{ __html: gist }} />
|
2019-05-09 15:24:26 +02:00
|
|
|
</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>
|
|
|
|
 (
|
2020-01-27 17:28:07 +01:00
|
|
|
<a href="#logo" onClick={() => raiseEvent('clearFocusAll', null)}>
|
2019-05-09 15:24:26 +02:00
|
|
|
<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}
|
2020-01-27 17:28:07 +01:00
|
|
|
data={props.gist}
|
|
|
|
updateData={props.updateGist}
|
2019-05-03 19:54:46 +02:00
|
|
|
raiseEvent={props.raiseEvent}
|
|
|
|
freesewing={props.freesewing}
|
|
|
|
units={props.units}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</aside>
|
|
|
|
</div>
|
2020-01-27 17:28:07 +01:00
|
|
|
)
|
|
|
|
}
|
2019-05-03 19:54:46 +02:00
|
|
|
|
|
|
|
DraftPattern.propTypes = {
|
|
|
|
gist: PropTypes.object.isRequired,
|
|
|
|
updateGist: PropTypes.func.isRequired,
|
|
|
|
config: PropTypes.object.isRequired,
|
|
|
|
raiseEvent: PropTypes.func.isRequired,
|
|
|
|
Pattern: PropTypes.func.isRequired,
|
2020-01-27 17:28:07 +01:00
|
|
|
units: PropTypes.oneOf(['metric', 'imperial'])
|
|
|
|
}
|
2019-05-03 19:54:46 +02:00
|
|
|
|
|
|
|
DraftPattern.defaultProps = {
|
2020-01-27 17:28:07 +01:00
|
|
|
units: 'metric',
|
2019-05-03 19:54:46 +02:00
|
|
|
pointInfo: null
|
2020-01-27 17:28:07 +01:00
|
|
|
}
|
2019-05-03 19:54:46 +02:00
|
|
|
|
2020-01-27 17:28:07 +01:00
|
|
|
export default DraftPattern
|