2022-03-18 19:11:00 +01:00
|
|
|
import { useEffect, useState } from 'react'
|
2022-01-25 10:39:30 +01:00
|
|
|
import useLocalStorage from 'shared/hooks/useLocalStorage.js'
|
2022-01-22 17:55:03 +01:00
|
|
|
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-22 17:55:03 +01:00
|
|
|
import set from 'lodash.set'
|
2022-01-25 18:14:31 +01:00
|
|
|
import unset from 'lodash.unset'
|
2022-01-27 12:26:56 +01:00
|
|
|
import defaultSettings from 'shared/components/workbench/default-settings.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'
|
2022-03-17 19:04:55 +01:00
|
|
|
import preloaders from 'shared/components/workbench/preload.js'
|
2022-01-27 12:26:56 +01:00
|
|
|
|
2022-02-12 14:31:17 +01:00
|
|
|
// Views
|
2022-02-12 15:23:37 +01:00
|
|
|
import Measurements from 'shared/components/workbench/measurements/index.js'
|
2022-02-12 14:31:17 +01:00
|
|
|
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-02-19 17:01:28 +01:00
|
|
|
import ExportDraft from 'shared/components/workbench/export.js'
|
2022-02-12 14:31:17 +01:00
|
|
|
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'
|
|
|
|
import PrintLayout from 'shared/components/workbench/layout/print'
|
2022-02-12 14:31:17 +01:00
|
|
|
|
|
|
|
const views = {
|
|
|
|
measurements: Measurements,
|
|
|
|
draft: LabDraft,
|
2022-02-13 15:45:27 +01:00
|
|
|
test: LabSample,
|
2022-02-20 18:46:21 +01:00
|
|
|
printingLayout: PrintLayout,
|
|
|
|
cuttingLayout: CutLayout,
|
2022-02-19 17:01:28 +01:00
|
|
|
export: ExportDraft,
|
2022-02-12 14:31:17 +01:00
|
|
|
events: DraftEvents,
|
|
|
|
yaml: GistAsYaml,
|
|
|
|
json: GistAsJson,
|
|
|
|
welcome: () => <p>TODO</p>,
|
|
|
|
}
|
2022-01-22 17:55:03 +01:00
|
|
|
|
2022-06-17 12:02:09 +02:00
|
|
|
// Generates a default design gist to start from
|
|
|
|
const defaultGist = (design, locale='en') => {
|
2022-01-27 12:26:56 +01:00
|
|
|
const gist = {
|
2022-06-17 12:02:09 +02:00
|
|
|
design: design.config.name,
|
|
|
|
version: design.config.version,
|
2022-01-27 18:07:37 +01:00
|
|
|
...defaultSettings
|
2022-01-22 17:55:03 +01:00
|
|
|
}
|
2022-01-27 18:07:37 +01:00
|
|
|
if (locale) gist.locale = locale
|
2022-01-27 12:26:56 +01:00
|
|
|
|
|
|
|
return gist
|
|
|
|
}
|
2022-01-22 17:55:03 +01:00
|
|
|
|
2022-06-17 12:02:09 +02:00
|
|
|
const hasRequiredMeasurements = (design, gist) => {
|
|
|
|
for (const m of design.config.measurements) {
|
2022-01-25 10:29:47 +01:00
|
|
|
if (!gist?.measurements?.[m]) return false
|
2022-01-22 17:55:03 +01:00
|
|
|
}
|
2022-01-25 10:29:47 +01:00
|
|
|
|
|
|
|
return true
|
2022-01-22 17:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2022-01-22 17:55:03 +01:00
|
|
|
* to all workbench subcomponents
|
|
|
|
*/
|
2022-06-17 12:02:09 +02:00
|
|
|
const WorkbenchWrapper = ({ app, design, preload=false, from=false, layout=false }) => {
|
2022-01-22 17:55:03 +01:00
|
|
|
|
2022-02-12 14:31:17 +01:00
|
|
|
// State for gist
|
2022-06-17 12:02:09 +02:00
|
|
|
const [gist, setGist, ready] = useLocalStorage(`${design.config.name}_gist`, defaultGist(design, app.locale))
|
2022-03-18 19:11:00 +01:00
|
|
|
const [messages, setMessages] = useState([])
|
2022-01-22 17:55:03 +01:00
|
|
|
|
2022-02-12 14:31:17 +01:00
|
|
|
// If we don't have the required measurements,
|
|
|
|
// force view to measurements
|
2022-01-22 17:55:03 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (
|
2022-05-31 15:24:39 -04:00
|
|
|
ready && gist?._state?.view !== 'measurements'
|
2022-06-17 12:02:09 +02:00
|
|
|
&& !hasRequiredMeasurements(design, gist)
|
2022-02-12 14:31:17 +01:00
|
|
|
) updateGist(['_state', 'view'], 'measurements')
|
2022-05-31 15:24:39 -04:00
|
|
|
}, [ready])
|
2022-01-22 17:55:03 +01:00
|
|
|
|
2022-03-17 19:04:55 +01:00
|
|
|
// If we need to preload the gist, do so
|
2022-05-31 15:24:39 -04:00
|
|
|
useEffect(() => {
|
|
|
|
const doPreload = async () => {
|
|
|
|
if (preload && from && preloaders[from]) {
|
2022-06-17 12:02:09 +02:00
|
|
|
const g = await preloaders[from](preload, design)
|
2022-05-31 15:24:39 -04:00
|
|
|
setGist({ ...gist, ...g.settings })
|
|
|
|
}
|
2022-03-17 19:04:55 +01:00
|
|
|
}
|
2022-05-31 15:24:39 -04:00
|
|
|
doPreload();
|
2022-03-17 19:04:55 +01:00
|
|
|
}, [preload, from])
|
|
|
|
|
2022-02-12 14:31:17 +01:00
|
|
|
// Helper methods to manage the gist state
|
2022-05-15 14:46:03 +02:00
|
|
|
const updateGist = (path, content, closeNav=false) => {
|
2022-01-22 17:55:03 +01:00
|
|
|
const newGist = {...gist}
|
|
|
|
set(newGist, path, content)
|
|
|
|
setGist(newGist)
|
2022-05-14 19:52:16 +02:00
|
|
|
// Force close of menu on mobile if it is open
|
2022-05-15 14:46:03 +02:00
|
|
|
if (closeNav && app.primaryMenu) app.setPrimaryMenu(false)
|
2022-01-22 17:55:03 +01:00
|
|
|
}
|
2022-01-25 18:14:31 +01:00
|
|
|
const unsetGist = (path) => {
|
|
|
|
const newGist = {...gist}
|
|
|
|
unset(newGist, path)
|
|
|
|
setGist(newGist)
|
|
|
|
}
|
2022-03-18 19:11:00 +01:00
|
|
|
// 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([]),
|
|
|
|
}
|
2022-01-25 18:14:31 +01:00
|
|
|
|
2022-02-12 14:31:17 +01:00
|
|
|
// Generate the draft here so we can pass it down
|
2022-01-30 15:14:44 +01:00
|
|
|
let draft = false
|
2022-02-13 15:45:27 +01:00
|
|
|
if (['draft', 'events', 'test'].indexOf(gist?._state?.view) !== -1) {
|
2022-06-17 12:02:09 +02:00
|
|
|
draft = new design(gist)
|
2022-02-13 16:10:51 +01:00
|
|
|
if (gist.renderer === 'svg') draft.use(theme)
|
2022-02-13 15:45:27 +01:00
|
|
|
try {
|
|
|
|
if (gist._state.view !== 'test') draft.draft()
|
|
|
|
}
|
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'} />
|
|
|
|
}
|
|
|
|
}
|
2022-01-22 17:55:03 +01:00
|
|
|
|
2022-02-12 14:31:17 +01:00
|
|
|
// Props to pass down
|
2022-06-17 12:02:09 +02:00
|
|
|
const componentProps = { app, design, gist, updateGist, unsetGist, setGist, draft, feedback }
|
2022-01-22 17:55:03 +01:00
|
|
|
// Required props for layout
|
|
|
|
const layoutProps = {
|
|
|
|
app: app,
|
|
|
|
noSearch: true,
|
|
|
|
workbench: true,
|
2022-02-12 14:31:17 +01:00
|
|
|
AltMenu: <Menu {...componentProps }/>
|
2022-01-22 17:55:03 +01:00
|
|
|
}
|
|
|
|
|
2022-05-14 14:53:29 +02:00
|
|
|
// Layout to use
|
|
|
|
const LayoutComponent = layout
|
|
|
|
? layout
|
|
|
|
: Layout
|
|
|
|
|
2022-02-12 14:31:17 +01: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}>
|
2022-03-18 19:11:00 +01:00
|
|
|
{messages}
|
2022-02-12 14:31:17 +01:00
|
|
|
<Component {...componentProps} />
|
2022-05-14 14:53:29 +02:00
|
|
|
</LayoutComponent>
|
2022-01-22 17:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default WorkbenchWrapper
|
|
|
|
|