1
0
Fork 0

feat(shared): Added support for flags

This commit is contained in:
joostdecock 2023-09-07 10:29:19 +02:00
parent 7f55b3fb2a
commit 4528e1bc88
14 changed files with 232 additions and 24 deletions

View file

@ -30,7 +30,11 @@ export const cutlistHooks = {
* @param {boolean} so.ignoreOnFold should these cutting instructions ignore any cutOnFold information set by the part
*/
function addCut(store, so = {}) {
const { cut = 2, material = 'fabric', identical = false, bias = false, ignoreOnFold = false } = so
const { cut = 2, identical = false, bias = false, ignoreOnFold = false } = so
// Make 'from' an alias for material
let { material = 'fabric' } = so
if (so.from) material = so.from
const partName = store.get('activePart')
if (cut === false) {
if (material === false) store.unset(['cutlist', partName, 'materials'])

View file

@ -12,6 +12,7 @@ const macroDefaults = {
grainline: false,
margin: 0.05,
offset: 15,
reverse: false,
}
// Export defs
@ -90,15 +91,13 @@ const cutonfold = function (config, { points, paths, Path, complete, store, scal
/*
* Draw the path
*/
const from = mc.from.shiftFractionTowards(mc.to, mc.margin / 100)
const to = mc.to.shiftFractionTowards(mc.from, mc.margin / 100)
const from = mc.from.shiftFractionTowards(mc.to, mc.margin)
const to = mc.to.shiftFractionTowards(mc.from, mc.margin)
const via1 = from.shiftTowards(mc.from, mc.offset * scale).rotate(-90, from)
const via2 = to.shiftTowards(mc.to, mc.offset * scale).rotate(90, to)
paths[ids.line] = new Path()
.move(from)
.line(via1)
.line(via2)
.line(to)
paths[ids.line] = new Path().move(from).line(via1).line(via2).line(to)
if (mc.reverse) paths[ids.line] = paths[ids.line].reverse()
paths[ids.line] = paths[ids.line]
.attr('class', mc.classes.line)
.attr('marker-start', 'url(#cutonfoldFrom)')
.attr('marker-end', 'url(#cutonfoldTo)')

View file

@ -0,0 +1,76 @@
const storeRoot = ['plugins', 'plugin-annotations', 'flags']
// This is also the order in which they will be displayed
export const flagTypes = ['error', 'warn', 'note', 'info', 'tip', 'fixme']
export const flagStores = [
['flag.info', (store, data) => flag('info', store, data)],
['flag.tip', (store, data) => flag('tip', store, data)],
['flag.note', (store, data) => flag('note', store, data)],
['flag.warn', (store, data) => flag('warn', store, data)],
['flag.fixme', (store, data) => flag('fixme', store, data)],
['flag.error', (store, data) => flag('error', store, data)],
['flag.preset', (store, preset) => flag('preset', store, preset)],
['unflag.info', (store, id) => unflag('info', store, id)],
['unflag.tip', (store, id) => unflag('tip', store, id)],
['unflag.note', (store, id) => unflag('note', store, id)],
['unflag.warn', (store, id) => unflag('warn', store, id)],
['unflag.fixme', (store, id) => unflag('fixme', store, id)],
['unflag.error', (store, id) => unflag('error', store, id)],
['unflag.preset', (store, preset) => unflag('preset', store, preset)],
]
/*
* Method that adds a flag to the store
*
* @param {type} string - The type of flag
* @param {store} object - The pattern store
* @param {data} object - The flag data
*/
function flag(type, store, data) {
// Load presets
if (type === 'preset' && presets[data]) {
data = presets[data]
type = data.type
}
if (!data.id && !data.msg) {
store.log.warning(`store.flag.${type} called without an id or msg property`)
console.log(data)
return
}
store.set([...storeRoot, type, data.id ? data.id : data.msg], data)
}
/*
* Method that removes a flag from the store
*
* @param {type} string - The type of flag
* @param {store} object - The pattern store
* @param {id} string - The flag id to remove
*/
function unflag(type, store, id) {
if (type === 'preset' && presets[preset]) {
id = presets[preset].id || presets[preset].msg
type = presets[preset].type
}
store.unset([...storeRoot, type, id])
}
/*
* Available flag presets
*/
const presets = {
expand: {
type: 'tip',
msg: 'flag:expandIsOff',
suggest: {
text: 'flag:enable',
icon: 'expand',
update: {
settings: ['expand', 1],
},
},
},
}

View file

@ -19,6 +19,8 @@ import { dimensionsMacros, dimensionsDefs } from './dimensions.mjs'
import { grainlineMacros, grainlineDefs } from './grainline.mjs'
import { pleatMacros, pleatDefs } from './pleat.mjs'
import { sewtogetherMacros, sewtogetherDefs } from './sewtogether.mjs'
// Only stores
import { flagStores } from './flag.mjs'
export const plugin = {
name,
@ -59,7 +61,7 @@ export const plugin = {
...sewtogetherMacros,
...titleMacros,
},
store: [...cutlistStores],
store: [...cutlistStores, ...flagStores],
}
export const annotationsPlugin = plugin

View file

@ -71,7 +71,10 @@ const removeScaleAnnotation = function (id = false, { paths, points, store, part
/*
* The scalebox macro
*/
const scalebox = function (config, { store, points, paths, scale, Point, Path, complete, part }) {
const scalebox = function (
config,
{ store, points, paths, scale, Point, Path, complete, log, part }
) {
/*
* Don't add a title when complete is false, unless force is true
*/
@ -103,7 +106,7 @@ const scalebox = function (config, { store, points, paths, scale, Point, Path, c
*/
if (!mc.at || typeof mc.at.attr !== 'function') {
log.warn(`Scalebox macro called without a valid at point. Using (0,0) for at.`)
mc.from = new Point(0, 0)
mc.at = new Point(0, 0)
}
/*
@ -262,7 +265,10 @@ const scalebox = function (config, { store, points, paths, scale, Point, Path, c
/*
* The miniscale macro
*/
const miniscale = function (config, { points, paths, scale, Point, Path, part, complete, store }) {
const miniscale = function (
config,
{ points, paths, scale, Point, Path, part, complete, log, store }
) {
/*
* Don't add a title when complete is false, unless force is true
*/
@ -293,7 +299,7 @@ const miniscale = function (config, { points, paths, scale, Point, Path, part, c
*/
if (!mc.at || typeof mc.at.attr !== 'function') {
log.warn(`Scalebox macro called without a valid at point. Using (0,0) for at.`)
mc.from = new Point(0, 0)
mc.at = new Point(0, 0)
}
/*

View file

@ -199,6 +199,7 @@ const addTitleMacro = function (
.shift(-90, shift)
.attr('data-text', `(${store.data.for})`)
.attr('data-text-class', `${mc.classes.for} ${mc.align}`)
.attr('data-text-transform', transform)
shift += mc.dy
} else delete ids.for
@ -217,6 +218,7 @@ const addTitleMacro = function (
})
)
.attr('data-text-class', `${mc.classes.date} ${mc.align}`)
.attr('data-text-transform', transform)
/*
* Store all IDs in the store so we can remove this macro with rmtitle