2022-09-17 10:30:21 +02:00
|
|
|
import { useEffect, useState, useMemo } from 'react'
|
|
|
|
import { useGist } from 'shared/hooks/useGist'
|
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-30 15:14:44 +01:00
|
|
|
import DraftError from 'shared/components/workbench/draft/error.js'
|
2022-11-14 16:33:57 -06:00
|
|
|
import { pluginTheme } from '@freesewing/plugin-theme'
|
2022-03-17 19:04:55 +01:00
|
|
|
import preloaders from 'shared/components/workbench/preload.js'
|
2022-06-18 12:15:05 +02:00
|
|
|
import Modal from 'shared/components/modal'
|
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-08-24 16:02:52 -05:00
|
|
|
import ExportDraft from 'shared/components/workbench/exporting/index.js'
|
2022-08-29 17:44:50 +02:00
|
|
|
import GistAsJson from 'shared/components/workbench/gist-as-json.js'
|
2022-02-12 14:31:17 +01:00
|
|
|
import GistAsYaml from 'shared/components/workbench/yaml.js'
|
2022-09-17 10:30:21 +02:00
|
|
|
import DraftLogs from 'shared/components/workbench/logs.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'
|
2022-02-12 14:31:17 +01:00
|
|
|
|
2022-09-17 10:30:21 +02:00
|
|
|
import ErrorBoundary from 'shared/components/error/error-boundary'
|
2022-07-11 15:32:51 -05:00
|
|
|
|
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-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,
|
2022-09-17 10:30:21 +02:00
|
|
|
logs: DraftLogs,
|
2022-02-12 14:31:17 +01:00
|
|
|
yaml: GistAsYaml,
|
|
|
|
json: GistAsJson,
|
|
|
|
welcome: () => <p>TODO</p>,
|
|
|
|
}
|
2022-01-22 17:55:03 +01:00
|
|
|
|
2022-06-23 10:11:41 +02:00
|
|
|
const hasRequiredMeasurementsMethod = (design, gist) => {
|
2022-10-08 04:25:39 +02:00
|
|
|
if (design.patternConfig?.measurements?.length > 0 && !gist.measurements) return false
|
2022-07-16 13:20:57 -04:00
|
|
|
|
2022-10-08 04:25:39 +02:00
|
|
|
for (const m of design.patternConfig?.measurements || []) {
|
2022-07-16 13:04:00 -04: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
|
|
|
}
|
|
|
|
|
2022-07-09 15:58:49 +02:00
|
|
|
const doPreload = async (preload, from, design, gist, setGist, setPreloaded) => {
|
|
|
|
const g = await preloaders[from](preload, design)
|
|
|
|
setPreloaded(preload)
|
|
|
|
setGist({ ...gist, ...g.settings })
|
|
|
|
}
|
|
|
|
|
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-09-17 10:30:21 +02:00
|
|
|
const WorkbenchWrapper = ({ app, design, preload = false, from = false, layout = false }) => {
|
2022-02-12 14:31:17 +01:00
|
|
|
// State for gist
|
2022-09-17 10:30:21 +02:00
|
|
|
const { gist, setGist, unsetGist, updateGist, gistReady, undoGist, resetGist } = useGist(
|
2022-10-06 22:22:41 +02:00
|
|
|
design.designConfig?.data?.name,
|
2022-09-17 10:30:21 +02:00
|
|
|
app
|
|
|
|
)
|
2022-03-18 19:11:00 +01:00
|
|
|
const [messages, setMessages] = useState([])
|
2022-06-18 12:15:05 +02:00
|
|
|
const [popup, setPopup] = useState(false)
|
2022-07-09 15:58:49 +02:00
|
|
|
const [preloaded, setPreloaded] = useState(false)
|
2022-01-22 17:55:03 +01:00
|
|
|
|
2022-06-23 10:11:41 +02:00
|
|
|
// We'll use this in more than one location
|
|
|
|
const hasRequiredMeasurements = hasRequiredMeasurementsMethod(design, gist)
|
|
|
|
|
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(() => {
|
2022-07-01 18:41:07 -05:00
|
|
|
if (!gistReady) return
|
2022-09-17 10:30:21 +02:00
|
|
|
if (gist._state?.view !== 'measurements' && !hasRequiredMeasurements)
|
|
|
|
updateGist(['_state', 'view'], 'measurements')
|
2022-07-01 18:41:07 -05:00
|
|
|
}, [gistReady, gist._state?.view, hasRequiredMeasurements])
|
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(() => {
|
2022-09-17 10:30:21 +02:00
|
|
|
if (preload && preload !== preloaded && from && preloaders[from]) {
|
|
|
|
doPreload(preload, from, design, gist, setGist, setPreloaded)
|
2022-03-17 19:04:55 +01:00
|
|
|
}
|
2022-07-09 15:58:49 +02:00
|
|
|
}, [preload, preloaded, from, design])
|
2022-03-17 19:04:55 +01:00
|
|
|
|
2022-02-12 14:31:17 +01:00
|
|
|
// Helper methods to manage the gist state
|
2022-09-17 10:30:21 +02:00
|
|
|
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]
|
|
|
|
)
|
2022-06-22 15:19:07 -05:00
|
|
|
|
2022-03-18 19:11:00 +01:00
|
|
|
// Helper methods to handle messages
|
|
|
|
const feedback = {
|
2022-09-17 10:30:21 +02:00
|
|
|
add: (msg) => {
|
2022-03-18 19:11:00 +01:00
|
|
|
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-07-16 13:04:00 -04:00
|
|
|
// don't do anything until the gist is ready
|
2022-09-17 10:30:21 +02:00
|
|
|
if (!gistReady) {
|
|
|
|
return null
|
|
|
|
}
|
2022-07-16 13:04:00 -04:00
|
|
|
|
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
|
2022-09-17 10:30:21 +02:00
|
|
|
if (['draft', 'logs', 'test', 'printingLayout'].indexOf(gist._state?.view) !== -1) {
|
2022-08-24 16:02:52 -05:00
|
|
|
gist.embed = true
|
2022-08-14 16:50:16 -05:00
|
|
|
// get the appropriate layout for the view
|
2022-10-29 00:34:42 -05:00
|
|
|
const layout = gist.layouts?.[gist._state.view] || gist.layout || true
|
2022-08-14 16:50:16 -05:00
|
|
|
// hand it separately to the design
|
2022-10-29 00:34:42 -05:00
|
|
|
draft = new design({ ...gist, layout })
|
2022-10-08 04:25:39 +02:00
|
|
|
//draft.__init()
|
2022-08-14 16:50:16 -05:00
|
|
|
|
|
|
|
// add theme to svg renderer
|
2022-12-13 11:55:50 -06:00
|
|
|
if (gist.renderer === 'svg') draft.use(pluginTheme, { skipGrid: ['pages'] })
|
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-09-17 10:30:21 +02:00
|
|
|
if (['draft', 'logs'].indexOf(gist._state.view) > -1) draft.draft()
|
|
|
|
} catch (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-23 10:11:41 +02:00
|
|
|
const componentProps = {
|
|
|
|
app,
|
|
|
|
design,
|
|
|
|
gist,
|
2022-06-23 14:37:11 -05:00
|
|
|
updateGist: updateWBGist,
|
2022-06-23 10:11:41 +02:00
|
|
|
unsetGist,
|
|
|
|
setGist,
|
|
|
|
draft,
|
|
|
|
feedback,
|
|
|
|
gistReady,
|
|
|
|
showInfo: setPopup,
|
|
|
|
hasRequiredMeasurements,
|
|
|
|
}
|
2022-01-22 17:55:03 +01:00
|
|
|
// Required props for layout
|
|
|
|
const layoutProps = {
|
|
|
|
app: app,
|
|
|
|
noSearch: true,
|
|
|
|
workbench: true,
|
2022-09-17 10:30:21 +02:00
|
|
|
AltMenu: <Menu {...componentProps} />,
|
2022-06-18 12:15:05 +02:00
|
|
|
showInfo: setPopup,
|
2022-01-22 17:55:03 +01:00
|
|
|
}
|
|
|
|
|
2022-07-12 17:32:47 -05:00
|
|
|
const errorProps = {
|
|
|
|
undoGist,
|
|
|
|
resetGist,
|
2022-09-17 10:30:21 +02:00
|
|
|
gist,
|
2022-07-12 17:32:47 -05:00
|
|
|
}
|
|
|
|
|
2022-05-14 14:53:29 +02:00
|
|
|
// Layout to use
|
2022-09-17 10:30:21 +02:00
|
|
|
const LayoutComponent = layout ? layout : Layout
|
|
|
|
|
|
|
|
const Component = views[gist._state?.view] ? views[gist._state.view] : views.welcome
|
|
|
|
|
|
|
|
return (
|
|
|
|
<LayoutComponent {...layoutProps}>
|
|
|
|
{messages}
|
|
|
|
<ErrorBoundary {...errorProps}>
|
|
|
|
<Component {...componentProps} />
|
|
|
|
{popup && <Modal cancel={() => setPopup(false)}>{popup}</Modal>}
|
|
|
|
</ErrorBoundary>
|
|
|
|
</LayoutComponent>
|
|
|
|
)
|
2022-01-22 17:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default WorkbenchWrapper
|