1
0
Fork 0
freesewing/sites/shared/components/wrappers/workbench.js

183 lines
5.5 KiB
JavaScript
Raw Normal View History

import { useEffect, useState, useMemo,} from 'react'
import {useGist} from 'shared/hooks/useGist'
import Layout from 'shared/components/layouts/default'
2022-01-25 12:39:29 +01:00
import Menu from 'shared/components/workbench/menu/index.js'
2022-01-30 15:14:44 +01:00
import DraftError from 'shared/components/workbench/draft/error.js'
2022-06-17 12:02:09 +02:00
import theme from '@freesewing/plugin-theme'
import preloaders from 'shared/components/workbench/preload.js'
import Modal from 'shared/components/modal'
2022-01-27 12:26:56 +01:00
// Views
2022-02-12 15:23:37 +01:00
import Measurements from 'shared/components/workbench/measurements/index.js'
import LabDraft from 'shared/components/workbench/draft/index.js'
2022-02-13 15:45:27 +01:00
import LabSample from 'shared/components/workbench/sample.js'
2022-08-21 10:26:35 +01:00
import ExportDraft from 'shared/components/workbench/exporting/index'
import GistAsJson from 'shared/components/workbench/json.js'
import GistAsYaml from 'shared/components/workbench/yaml.js'
import DraftEvents from 'shared/components/workbench/events.js'
2022-02-20 18:46:21 +01:00
import CutLayout from 'shared/components/workbench/layout/cut'
2022-08-14 16:50:16 -05:00
import PrintingLayout from 'shared/components/workbench/layout/print'
import ErrorBoundary from 'shared/components/error/error-boundary';
2022-07-11 15:32:51 -05:00
const views = {
measurements: Measurements,
draft: LabDraft,
2022-02-13 15:45:27 +01:00
test: LabSample,
2022-08-14 16:50:16 -05:00
printingLayout: PrintingLayout,
2022-02-20 18:46:21 +01:00
cuttingLayout: CutLayout,
2022-02-19 17:01:28 +01:00
export: ExportDraft,
events: DraftEvents,
yaml: GistAsYaml,
json: GistAsJson,
welcome: () => <p>TODO</p>,
}
const hasRequiredMeasurementsMethod = (design, gist) => {
2022-07-05 18:37:29 -05:00
if (design.config.measurements.length && !gist.measurements) return false
for (const m of design.config.measurements || []) {
if (!gist.measurements[m]) return false
}
2022-01-25 10:29:47 +01:00
return true
}
const doPreload = async (preload, from, design, gist, setGist, setPreloaded) => {
const g = await preloaders[from](preload, design)
setPreloaded(preload)
setGist({ ...gist, ...g.settings })
}
/*
* This component wraps the workbench and is in charge of
2022-05-14 14:53:29 +02:00
* keeping the gist state, which will trickle down
* to all workbench subcomponents
*/
2022-06-17 12:02:09 +02:00
const WorkbenchWrapper = ({ app, design, preload=false, from=false, layout=false }) => {
// State for gist
const {gist, setGist, unsetGist, updateGist, gistReady, undoGist, resetGist} = useGist(design, app);
const [messages, setMessages] = useState([])
const [popup, setPopup] = useState(false)
const [preloaded, setPreloaded] = useState(false)
// We'll use this in more than one location
const hasRequiredMeasurements = hasRequiredMeasurementsMethod(design, gist)
// If we don't have the required measurements,
// force view to measurements
useEffect(() => {
if (!gistReady) return
2022-07-02 11:27:39 -05:00
if (gist._state?.view !== 'measurements'
&& !hasRequiredMeasurements
) updateGist(['_state', 'view'], 'measurements')
}, [gistReady, gist._state?.view, hasRequiredMeasurements])
// If we need to preload the gist, do so
useEffect(() => {
if (
preload &&
preload !== preloaded &&
from &&
preloaders[from]
) {
doPreload(preload, from, design, gist, setGist, setPreloaded)
}
}, [preload, preloaded, from, design])
// Helper methods to manage the gist state
const updateWBGist = useMemo(() => (path, value, closeNav=false, addToHistory=true) => {
updateGist(path, value, addToHistory)
// Force close of menu on mobile if it is open
if (closeNav && app.primaryMenu) app.setPrimaryMenu(false)
}, [app])
// Helper methods to handle messages
const feedback = {
add: msg => {
const newMsgs = [...messages]
if (Array.isArray(msg)) newMsgs.push(...msg)
else newMsgs.push(msg)
setMessages(newMsgs)
},
set: setMessages,
clear: () => setMessages([]),
}
// don't do anything until the gist is ready
if (!gistReady) {return null}
2022-08-14 16:50:16 -05:00
// Generate the draft here so we can pass it down to both the view and the options menu
2022-01-30 15:14:44 +01:00
let draft = false
if (['draft', 'events', 'test', 'printingLayout'].indexOf(gist._state?.view) !== -1) {
2022-08-14 16:50:16 -05:00
// get the appropriate layout for the view
const layout = gist.layouts?.[gist._state.view] || gist.layout || true
// hand it separately to the design
draft = new design({...gist, layout})
2022-08-14 16:50:16 -05:00
// add theme to svg renderer
2022-02-13 16:10:51 +01:00
if (gist.renderer === 'svg') draft.use(theme)
2022-08-14 16:50:16 -05:00
// draft it for draft and event views. Other views may add plugins, etc and we don't want to draft twice
2022-02-13 15:45:27 +01:00
try {
2022-08-09 17:22:19 -05:00
if (['draft', 'events'].indexOf(gist._state.view) > -1) draft.draft()
2022-02-13 15:45:27 +01:00
}
2022-01-30 15:14:44 +01:00
catch(error) {
2022-06-17 12:02:09 +02:00
console.log('Failed to draft design', error)
2022-01-30 15:14:44 +01:00
return <DraftError error={error} app={app} draft={draft} at={'draft'} />
}
}
// Props to pass down
const componentProps = {
app,
design,
gist,
2022-06-23 14:37:11 -05:00
updateGist: updateWBGist,
unsetGist,
setGist,
draft,
feedback,
gistReady,
showInfo: setPopup,
hasRequiredMeasurements,
}
// Required props for layout
const layoutProps = {
app: app,
noSearch: true,
workbench: true,
AltMenu: <Menu {...componentProps }/>,
showInfo: setPopup,
}
const errorProps = {
undoGist,
resetGist,
gist
}
2022-05-14 14:53:29 +02:00
// Layout to use
const LayoutComponent = layout
? layout
: Layout
2022-06-22 15:38:15 -05:00
const Component = views[gist._state?.view]
? views[gist._state.view]
: views.welcome
2022-01-30 15:14:44 +01:00
2022-05-14 14:53:29 +02:00
return <LayoutComponent {...layoutProps}>
{messages}
<ErrorBoundary {...errorProps}>
2022-07-11 15:32:51 -05:00
<Component {...componentProps} />
{popup && <Modal cancel={() => setPopup(false)}>{popup}</Modal>}
</ErrorBoundary>
2022-05-14 14:53:29 +02:00
</LayoutComponent>
}
export default WorkbenchWrapper