2022-02-12 15:23:37 +01:00
|
|
|
import { useEffect } 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'
|
|
|
|
import theme from 'pkgs/plugin-theme/src/index.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'
|
|
|
|
|
|
|
|
const views = {
|
|
|
|
measurements: Measurements,
|
|
|
|
draft: LabDraft,
|
2022-02-13 15:45:27 +01:00
|
|
|
test: LabSample,
|
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
|
|
|
|
|
|
|
// Generates a default pattern gist to start from
|
2022-01-27 18:07:37 +01:00
|
|
|
const defaultGist = (pattern, locale='en') => {
|
2022-01-27 12:26:56 +01:00
|
|
|
const gist = {
|
2022-01-22 17:55:03 +01:00
|
|
|
design: pattern.config.name,
|
|
|
|
version: pattern.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
|
|
|
|
|
|
|
const hasRequiredMeasurements = (pattern, gist) => {
|
|
|
|
for (const m of pattern.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-02-12 14:31:17 +01:00
|
|
|
* keeping the gist state, which will trickly down
|
2022-01-22 17:55:03 +01:00
|
|
|
* to all workbench subcomponents
|
|
|
|
*/
|
|
|
|
const WorkbenchWrapper = ({ app, pattern }) => {
|
|
|
|
|
2022-02-12 14:31:17 +01:00
|
|
|
// State for gist
|
|
|
|
const [gist, setGist] = useLocalStorage(`${pattern.config.name}_gist`, defaultGist(pattern, app.locale))
|
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-02-12 14:31:17 +01:00
|
|
|
gist?._state?.view !== 'measurements'
|
2022-01-22 17:55:03 +01:00
|
|
|
&& !hasRequiredMeasurements(pattern, gist)
|
2022-02-12 14:31:17 +01:00
|
|
|
) updateGist(['_state', 'view'], 'measurements')
|
2022-01-22 17:55:03 +01:00
|
|
|
})
|
|
|
|
|
2022-02-12 14:31:17 +01:00
|
|
|
// Helper methods to manage the gist state
|
2022-01-22 17:55:03 +01:00
|
|
|
const updateGist = (path, content) => {
|
|
|
|
const newGist = {...gist}
|
|
|
|
set(newGist, path, content)
|
|
|
|
setGist(newGist)
|
|
|
|
}
|
2022-01-25 18:14:31 +01:00
|
|
|
const unsetGist = (path) => {
|
|
|
|
const newGist = {...gist}
|
|
|
|
unset(newGist, path)
|
|
|
|
setGist(newGist)
|
|
|
|
}
|
|
|
|
|
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-01-30 15:14:44 +01:00
|
|
|
draft = new pattern(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) {
|
|
|
|
console.log('Failed to draft pattern', error)
|
|
|
|
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
|
|
|
|
const componentProps = { app, pattern, gist, updateGist, unsetGist, setGist, draft }
|
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-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-02-12 14:31:17 +01:00
|
|
|
return <Layout {...layoutProps}>
|
|
|
|
<Component {...componentProps} />
|
|
|
|
</Layout>
|
2022-01-22 17:55:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default WorkbenchWrapper
|
|
|
|
|