2020-01-27 17:28:07 +01:00
|
|
|
import React, { useState } from 'react'
|
|
|
|
import Draft from '../../Draft'
|
2020-04-25 13:29:02 +02:00
|
|
|
import Zoombox from '../Zoombox'
|
2020-01-27 17:28:07 +01:00
|
|
|
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'
|
2020-04-25 13:29:02 +02:00
|
|
|
import IconButton from '@material-ui/core/IconButton'
|
|
|
|
import DesignIcon from '@material-ui/icons/Fingerprint'
|
|
|
|
import DumpIcon from '@material-ui/icons/LocalSee'
|
|
|
|
import ClearIcon from '@material-ui/icons/HighlightOff'
|
|
|
|
import AdvancedIcon from '@material-ui/icons/Policy'
|
|
|
|
import PaperlessIcon from '@material-ui/icons/Nature'
|
|
|
|
import CompleteIcon from '@material-ui/icons/Style'
|
|
|
|
import UnhideIcon from '@material-ui/icons/ChevronLeft'
|
|
|
|
import HideIcon from '@material-ui/icons/ChevronRight'
|
2019-05-03 19:54:46 +02:00
|
|
|
|
2020-04-25 13:29:02 +02:00
|
|
|
const DraftPattern = (props) => {
|
2020-01-27 17:28:07 +01:00
|
|
|
const [design, setDesign] = useState(true)
|
|
|
|
const [focus, setFocus] = useState(null)
|
2020-04-25 13:29:02 +02:00
|
|
|
const [viewBox, setViewBox] = useState(false)
|
|
|
|
const [hideAside, setHideAside] = useState(false)
|
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
|
|
|
|
2020-04-25 13:29:02 +02:00
|
|
|
const svgToFile = (svg) => {
|
2019-07-19 12:48:57 +02:00
|
|
|
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'
|
2020-04-25 13:29:02 +02:00
|
|
|
},
|
|
|
|
aside: {
|
|
|
|
maxWidth: '350px',
|
|
|
|
background: 'transparent',
|
|
|
|
border: 0,
|
|
|
|
fontSize: '90%',
|
|
|
|
boxShadow: '0 0 1px #cccc',
|
|
|
|
display: hideAside ? 'none' : 'block'
|
|
|
|
},
|
|
|
|
icon: {
|
|
|
|
margin: '0 0.25rem'
|
|
|
|
},
|
|
|
|
unhide: {
|
|
|
|
position: 'absolute',
|
|
|
|
top: '76px',
|
|
|
|
right: 0,
|
|
|
|
background: props.theme === 'dark' ? '#f8f9fa' : '#212529',
|
|
|
|
borderTopLeftRadius: '50%',
|
|
|
|
borderBottomLeftRadius: '50%',
|
|
|
|
width: '26px',
|
|
|
|
height: '30px'
|
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'
|
|
|
|
)
|
2020-04-25 13:29:02 +02:00
|
|
|
let iconProps = {
|
|
|
|
size: 'small',
|
|
|
|
style: styles.icon,
|
|
|
|
color: 'inherit'
|
|
|
|
}
|
|
|
|
const color = (check) => (check ? '#40c057' : '#fa5252')
|
2019-05-09 15:24:26 +02:00
|
|
|
|
2019-05-03 19:54:46 +02:00
|
|
|
return (
|
|
|
|
<div className="fs-sa">
|
2020-04-25 19:09:02 +02:00
|
|
|
<section style={{ margin: '1rem' }}>
|
2020-04-25 13:29:02 +02:00
|
|
|
<Draft
|
|
|
|
{...patternProps}
|
|
|
|
design={design}
|
|
|
|
focus={focus}
|
|
|
|
raiseEvent={raiseEvent}
|
|
|
|
viewBox={viewBox}
|
|
|
|
className="freesewing draft shadow"
|
|
|
|
/>
|
|
|
|
{hideAside && (
|
|
|
|
<div style={styles.unhide}>
|
|
|
|
<IconButton
|
|
|
|
onClick={() => setHideAside(false)}
|
|
|
|
title="Show sidebar"
|
|
|
|
{...iconProps}
|
|
|
|
style={{ margin: 0 }}
|
|
|
|
>
|
|
|
|
<span style={{ color: props.theme === 'dark' ? '#212529' : '#f8f9fa' }}>
|
|
|
|
<UnhideIcon />
|
|
|
|
</span>
|
|
|
|
</IconButton>
|
|
|
|
</div>
|
|
|
|
)}
|
2019-05-03 19:54:46 +02:00
|
|
|
</section>
|
|
|
|
|
2020-04-25 13:29:02 +02:00
|
|
|
<aside style={styles.aside}>
|
2019-05-03 19:54:46 +02:00
|
|
|
<div className="sticky">
|
2020-04-25 13:29:02 +02:00
|
|
|
<div style={{ padding: '5px' }}>
|
|
|
|
<Zoombox patternProps={patternProps} setViewBox={setViewBox} />
|
|
|
|
</div>
|
|
|
|
<div style={{ margin: '1rem auto 0', textAlign: 'center' }}>
|
|
|
|
<IconButton
|
|
|
|
onClick={() => setDesign(!design)}
|
|
|
|
title="Toggle design mode"
|
|
|
|
{...iconProps}
|
|
|
|
>
|
|
|
|
<span style={{ color: color(design) }}>
|
|
|
|
<DesignIcon />
|
|
|
|
</span>
|
|
|
|
</IconButton>
|
|
|
|
{design && (
|
|
|
|
<IconButton
|
|
|
|
onClick={() => raiseEvent('clearFocusAll', null)}
|
|
|
|
title="Clear design mode"
|
|
|
|
{...iconProps}
|
|
|
|
>
|
|
|
|
<ClearIcon color="primary" />
|
|
|
|
</IconButton>
|
|
|
|
)}
|
|
|
|
<IconButton
|
|
|
|
onClick={() => console.log(pattern)}
|
|
|
|
title="console.log(pattern)"
|
|
|
|
{...iconProps}
|
|
|
|
>
|
|
|
|
<DumpIcon color="primary" />
|
|
|
|
</IconButton>
|
|
|
|
|
|
|
|
|
<IconButton
|
|
|
|
onClick={() =>
|
|
|
|
props.updateGist(!props.gist.settings.advanced, 'settings', 'advanced')
|
|
|
|
}
|
|
|
|
title="Toggle advanced settings"
|
|
|
|
{...iconProps}
|
|
|
|
>
|
|
|
|
<span style={{ color: color(props.gist.settings.advanced) }}>
|
|
|
|
<AdvancedIcon />
|
|
|
|
</span>
|
|
|
|
</IconButton>
|
|
|
|
<IconButton
|
|
|
|
onClick={() =>
|
|
|
|
props.updateGist(!props.gist.settings.paperless, 'settings', 'paperless')
|
|
|
|
}
|
|
|
|
title="Toggle paperless"
|
|
|
|
{...iconProps}
|
|
|
|
>
|
|
|
|
<span style={{ color: color(props.gist.settings.paperless) }}>
|
|
|
|
<PaperlessIcon />
|
|
|
|
</span>
|
|
|
|
</IconButton>
|
|
|
|
<IconButton
|
|
|
|
onClick={() =>
|
|
|
|
props.updateGist(!props.gist.settings.complete, 'settings', 'complete')
|
|
|
|
}
|
|
|
|
title="Toggle complete"
|
|
|
|
{...iconProps}
|
|
|
|
>
|
|
|
|
<span style={{ color: color(props.gist.settings.complete) }}>
|
|
|
|
<CompleteIcon />
|
|
|
|
</span>
|
|
|
|
</IconButton>
|
|
|
|
<IconButton onClick={() => setHideAside(true)} title="Hide sidebar" {...iconProps}>
|
|
|
|
<HideIcon />
|
|
|
|
</IconButton>
|
|
|
|
</div>
|
|
|
|
{design && (
|
|
|
|
<Design
|
|
|
|
focus={focus}
|
|
|
|
design={design}
|
|
|
|
raiseEvent={raiseEvent}
|
|
|
|
parts={patternProps.parts}
|
|
|
|
/>
|
2019-05-09 15:24:26 +02:00
|
|
|
)}
|
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}
|
2020-03-04 17:53:55 +01:00
|
|
|
updatePatternData={props.updateGist}
|
2019-05-03 19:54:46 +02:00
|
|
|
raiseEvent={props.raiseEvent}
|
|
|
|
freesewing={props.freesewing}
|
2020-04-25 19:09:02 +02:00
|
|
|
units={props.units || 'metric'}
|
2019-05-03 19:54:46 +02:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</aside>
|
|
|
|
</div>
|
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
|