From 153bff0536878fda5369ce84c276c148302699e9 Mon Sep 17 00:00:00 2001 From: Joost De Cock Date: Sun, 30 Jan 2022 15:14:44 +0100 Subject: [PATCH] wip(lab): Moved draft to wrapper --- .../components/workbench/draft/error.js | 2 +- .../components/workbench/draft/index.js | 41 +++---- .../components/workbench/draft/part/index.js | 12 +- .../components/workbench/draft/path/index.js | 4 +- .../components/workbench/draft/point/index.js | 107 ++++++++++++++++-- .../workbench/menu/xray/attributes.js | 9 +- .../components/workbench/menu/xray/list.js | 15 ++- .../workbench/menu/xray/path-ops.js | 2 +- .../components/workbench/menu/xray/path.js | 44 ++++--- .../components/workbench/menu/xray/point.js | 33 +++--- .../components/wrappers/workbench.js | 19 ++++ 11 files changed, 208 insertions(+), 80 deletions(-) diff --git a/packages/freesewing.shared/components/workbench/draft/error.js b/packages/freesewing.shared/components/workbench/draft/error.js index 787ed8382fb..ff05f20ea30 100644 --- a/packages/freesewing.shared/components/workbench/draft/error.js +++ b/packages/freesewing.shared/components/workbench/draft/error.js @@ -7,7 +7,7 @@ const Error = props => (

{props.app.t('errors.something')}

- + ) diff --git a/packages/freesewing.shared/components/workbench/draft/index.js b/packages/freesewing.shared/components/workbench/draft/index.js index 05a0d07ff1e..cb4549d10f8 100644 --- a/packages/freesewing.shared/components/workbench/draft/index.js +++ b/packages/freesewing.shared/components/workbench/draft/index.js @@ -1,7 +1,5 @@ import React, { useState } from 'react' import SvgWrapper from './svg-wrapper' -import theme from 'pkgs/plugin-theme/src/index.js' -import Robot from 'shared/components/robot/index.js' import Error from './error.js' import Events from './events.js' import Json from 'shared/components/json.js' @@ -17,37 +15,28 @@ const tabClasses = active => ` const Wrap = props =>
{props.children}
const LabDraft = props => { - const { app, pattern, gist, updateGist, unsetGist } = props + const { app, draft, pattern, gist, updateGist, unsetGist } = props + if (!draft) return null const [tab, setTab] = useState(props.pattern.config.name) - const patternInstance = new pattern(gist) - const eprops = { ...props, patternInstance } - if (gist?.renderer === 'svg') patternInstance.use(theme) - // Catch errors - try { patternInstance.draft() } - catch(error) { - console.log('Failed to draft pattern', error) - return - } - - // Render as SVG - let svg - try { svg = patternInstance.render() } - catch(error) { - console.log('Failed to render pattern', error) - return - } - - if (gist?.renderer === 'svg') + if (gist?.renderer === 'svg') { + // Render as SVG + let svg + try { svg = draft.render() } + catch(error) { + console.log('Failed to render pattern', error) + return + } return
+ } // Render as React let patternProps = {} - try { patternProps = patternInstance.draft().getRenderProps() } + try { patternProps = draft.draft().getRenderProps() } catch(error) { console.log('Failed to get render props for pattern', error) - return + return } return ( @@ -59,11 +48,11 @@ const LabDraft = props => { className={tabClasses(tab === name)} >{name})}
- {tab === 'events' && } + {tab === 'events' && } {tab === 'json' && {JSON.stringify(props.gist, null, 2)}} {tab === 'yaml' && } {tab === props.pattern.config.name && { {props.gist?.xray?.reveal?.[partName] && } {Object.keys(part.paths).map((pathName) => ( { {...props} /> ))} - {Object.keys(props.part.points).map((name) => ( + {Object.keys(props.part.points).map((pointName) => ( { ))} {Object.keys(props.part.snippets).map((snippetName) => ( ( props.updateGist( ['xray', 'parts', props.partName, 'paths', props.pathName], - props.path + 1 )} /> diff --git a/packages/freesewing.shared/components/workbench/draft/point/index.js b/packages/freesewing.shared/components/workbench/draft/point/index.js index 5a9b1372bc5..267a5040560 100644 --- a/packages/freesewing.shared/components/workbench/draft/point/index.js +++ b/packages/freesewing.shared/components/workbench/draft/point/index.js @@ -1,5 +1,6 @@ import Text from '../text' import Circle from '../circle' +import { round, formatMm } from 'shared/utils.js' const RevealPoint = props => { const r = 15 * props.gist.scale @@ -22,9 +23,89 @@ const RevealPoint = props => { m ${2*r} 0 L ${bottomRight.x} ${y}`} className={classes} /> ) - } +} -const XrayPoint = props => ( +// Length for the indicators +const lead = 20 +// Length for the text on indicators +// this is longer to prevent text from being cropped +const longLead = 40 + +const Coord = ({id, val, pointName}) => ( + + + + {round(val)} + + + + + {pointName} + + + +) + +const PosX = ({ id, point, scale, pointName }) => ( + + + + + +) + + +const PosY = ({ id, point, scale, pointName }) => ( + + + + + +) + + +const ActiveXrayPoint = props => { + const id = `${props.partName}_${props.pointName}_xray_point` + const r = 15 * props.gist.scale + const { x, y } = props.point + const { topLeft, bottomRight } = props.part + const i = Object.keys(props.gist.xray.parts[props.partName].points).indexOf(props.pointName)%10 + const classes = `stroke-sm stroke-color-${i} stroke-dashed` + const posProps = { + id, + point: props.point, + pointName: props.pointName, + scale: props.gist.scale, + } + + return +} + +const PassiveXrayPoint = props => ( ( cy={props.point.y} r={7.5 * props.gist.scale} className="opacity-0 stroke-lining fill-lining hover:opacity-25 hover:cursor-pointer" - onClick={() => props.updateGist( - ['xray', 'parts', props.partName, 'points', props.pointName], - props.point - )} + onClick={props.gist?.xray?.parts?.[props.partName]?.points?.[props.pointName] + ? () => props.unsetGist( + ['xray', 'parts', props.partName, 'points', props.pointName] + ) + : () => props.updateGist( + ['xray', 'parts', props.partName, 'points', props.pointName], + 1 + ) + } /> ) @@ -48,11 +134,18 @@ const XrayPoint = props => ( const Point = props => { const { point, pointName, partName, gist } = props const output = [] - if (gist.xray) output.push() + // Passive indication for points + if (gist.xray) output.push() + // Active indication for points (point that have been clicked on) + if (gist.xray?.parts?.[partName]?.points?.[pointName]) + output.push() + // Reveal (based on clicking the seach icon in sidebar if (gist.xray?.reveal?.[partName]?.points?.[pointName]) output.push() + // Render text if (point.attributes && point.attributes.get('data-text')) output.push() + // Render circle if (point.attributes && point.attributes.get('data-circle')) output.push() diff --git a/packages/freesewing.shared/components/workbench/menu/xray/attributes.js b/packages/freesewing.shared/components/workbench/menu/xray/attributes.js index 19c23298615..92a1608f2aa 100644 --- a/packages/freesewing.shared/components/workbench/menu/xray/attributes.js +++ b/packages/freesewing.shared/components/workbench/menu/xray/attributes.js @@ -2,7 +2,7 @@ import { Chevron } from 'shared/components/navigation/primary.js' import { Ul, Li, Details, Summary, SumDiv, NoSumDiv, Deg } from 'shared/components/workbench/menu' import { round } from 'shared/utils.js' -const XrayAttributes = ({ attr=false }) => { +const XrayAttributes = ({ attr=false, t }) => { if (!attr || !attr.list || Object.keys(attr.list).length < 1) return null return ( @@ -17,7 +17,7 @@ const XrayAttributes = ({ attr=false }) => {
    {Object.keys(attr.list).map(at => ( -
  • +
  • @@ -31,7 +31,10 @@ const XrayAttributes = ({ attr=false }) => {
  • - {val} + {val === true + ? t('app.yes') + : val + }
  • ))} diff --git a/packages/freesewing.shared/components/workbench/menu/xray/list.js b/packages/freesewing.shared/components/workbench/menu/xray/list.js index 3fdcd1f4936..aa67a5e96d2 100644 --- a/packages/freesewing.shared/components/workbench/menu/xray/list.js +++ b/packages/freesewing.shared/components/workbench/menu/xray/list.js @@ -107,8 +107,19 @@ const XrayList = props => {
    - {type === 'paths' && } - {type === 'points' && } + {type === 'paths' && } + {type === 'points' && }
  • )) diff --git a/packages/freesewing.shared/components/workbench/menu/xray/path-ops.js b/packages/freesewing.shared/components/workbench/menu/xray/path-ops.js index c217e96e212..2b0399b404b 100644 --- a/packages/freesewing.shared/components/workbench/menu/xray/path-ops.js +++ b/packages/freesewing.shared/components/workbench/menu/xray/path-ops.js @@ -61,7 +61,7 @@ const XrayPathOps = ({ ops=false }) => { - PathOps + path.ops diff --git a/packages/freesewing.shared/components/workbench/menu/xray/path.js b/packages/freesewing.shared/components/workbench/menu/xray/path.js index cba4e803ef3..53a86518683 100644 --- a/packages/freesewing.shared/components/workbench/menu/xray/path.js +++ b/packages/freesewing.shared/components/workbench/menu/xray/path.js @@ -1,25 +1,33 @@ import { Ul, Li, Details, Summary, NoSumDiv, Deg } from 'shared/components/workbench/menu' +import { formatMm } from 'shared/utils.js' import Attributes from './attributes.js' import Ops from './path-ops.js' -/* - * Things to add - * - * ops - */ +const XrayPath = ({ pathName, partName, draft, t, units }) => { + const path = draft?.parts?.[partName]?.paths?.[pathName] -const XrayPath = ({ path }) => ( -
      -
    • - - - Render = - {JSON.stringify(path.render)} - -
    • - - -
    -) + return ( +
      + +
    • + + + path.render = + {JSON.stringify(path.render)} + +
    • +
    • + + + path.length() = + + +
    • + +
    + ) +} export default XrayPath diff --git a/packages/freesewing.shared/components/workbench/menu/xray/point.js b/packages/freesewing.shared/components/workbench/menu/xray/point.js index a1ec440ab70..4b6c6345b77 100644 --- a/packages/freesewing.shared/components/workbench/menu/xray/point.js +++ b/packages/freesewing.shared/components/workbench/menu/xray/point.js @@ -2,19 +2,24 @@ import { Ul, Li, Details, Summary, NoSumDiv, Deg } from 'shared/components/workb import { round } from 'shared/utils.js' import Attributes from './attributes.js' -const XrayPoint = ({ point }) => ( -
      - {['x', 'y'].map(coord => ( -
    • - - - {coord} = - {round(point[coord])} - -
    • - ))} - -
    -) +const XrayPoint = ({ pointName, partName, draft, t }) => { + const point = draft?.parts?.[partName]?.points?.[pointName] + + return point + ? ( +
      + {['x', 'y'].map(coord => ( +
    • + + + {coord} = + {round(point[coord])} + +
    • + ))} + +
    + ) : null +} export default XrayPoint diff --git a/packages/freesewing.shared/components/wrappers/workbench.js b/packages/freesewing.shared/components/wrappers/workbench.js index 1153bf3323b..940ea0e64b3 100644 --- a/packages/freesewing.shared/components/wrappers/workbench.js +++ b/packages/freesewing.shared/components/wrappers/workbench.js @@ -7,6 +7,8 @@ import LabDraft from 'shared/components/workbench/draft/index.js' import set from 'lodash.set' import unset from 'lodash.unset' import defaultSettings from 'shared/components/workbench/default-settings.js' +import DraftError from 'shared/components/workbench/draft/error.js' +import theme from 'pkgs/plugin-theme/src/index.js' // Generates a default pattern gist to start from @@ -66,6 +68,18 @@ const WorkbenchWrapper = ({ app, pattern }) => { setGist(newGist) } + // Generate the draft here so we can pass it to both Menu + // and LabDraft + let draft = false + if (mode === 'draft') { + draft = new pattern(gist) + if (gist?.renderer === 'svg') patternInstance.use(theme) + try { draft.draft() } + catch(error) { + console.log('Failed to draft pattern', error) + return + } + } // Required props for layout const layoutProps = { @@ -81,9 +95,12 @@ const WorkbenchWrapper = ({ app, pattern }) => { updateGist={updateGist} unsetGist={unsetGist} setGist={setGist} + draft={draft} /> } + + return ( {mode === 'measurements' && ( @@ -98,8 +115,10 @@ const WorkbenchWrapper = ({ app, pattern }) => { )}