1
0
Fork 0

feat(core): Return from macros

This commit is contained in:
joostdecock 2023-09-28 13:26:32 +02:00
parent 3ed61f56e1
commit 18042c8f3d
17 changed files with 98 additions and 31 deletions

View file

@ -359,7 +359,7 @@ Part.prototype.__macroClosure = function (props) {
const self = this
const method = function (key, args) {
const macro = utils.__macroName(key)
if (typeof self[macro] === 'function') self[macro](args, props)
if (typeof self[macro] === 'function') return self[macro](args, props)
else if ('context' in self)
self.context.store.log.warn('Unknown macro `' + key + '` used in ' + self.name)
}

View file

@ -45,9 +45,9 @@ for (const plugin of bundledPlugins) {
export const plugin = {
name,
version,
store,
hooks,
macros,
store,
}
// Specific named export

View file

@ -58,6 +58,8 @@ const banner = function (config, { part, paths, store, complete }) {
* Store all IDs in the store so we can remove this macro with rmbanner
*/
store.set(['parts', part.name, 'macros', 'banner', 'ids', mc.id, 'paths'], ids)
return store.getMacroIds(config.id, 'banner')
}
// Export macros

View file

@ -100,6 +100,8 @@ const bannerbox = function (config, { Point, paths, Path, part, macro, log, stor
* Store all IDs in the store so we can remove this macro with rmtitle
*/
store.set(['parts', part.name, 'macros', 'bannerbox', 'ids', mc.id, 'paths'], ids)
return store.getMacroIds(mc.id, 'bannerbox')
}
export const bannerboxMacros = { bannerbox, rmbannerbox }

View file

@ -129,6 +129,8 @@ function createBartack(config, props) {
* Store all IDs in the store so we can remove this macro with rm[name]
*/
props.store.set(['parts', props.part.name, 'macros', name, 'ids', mc.id, 'paths'], ids)
return props.store.getMacroIds(mc.id, name)
}
/*

View file

@ -119,6 +119,8 @@ const crossbox = function (config, { points, Point, paths, Path, complete, store
* Just make sure to keep points and paths apart
*/
store.set(['parts', part.name, 'macros', 'title', 'ids', mc.id], ids)
return store.getMacroIds(mc.id, 'crossbox')
}
// Export macros

View file

@ -107,6 +107,8 @@ const cutonfold = function (config, { paths, Path, complete, store, scale, log,
* Store all IDs in the store so we can remove this macro with rmcutonfold
*/
store.set(['parts', part.name, 'macros', 'cutonfold', 'ids', mc.id, 'paths'], ids)
return store.getMacroIds(mc.id, 'cutonfold')
}
// Export macros

View file

@ -151,6 +151,8 @@ const addDimension = (config, props, type) => {
* Store all IDs in the store so we can remove this macro with rm variants
*/
props.store.set(['parts', props.part.name, 'macros', type, 'ids', mc.id, 'paths'], ids)
return props.store.getMacroIds(mc.id, type)
}
/*

View file

@ -99,6 +99,8 @@ const grainline = function (config = {}, { paths, Path, Point, complete, store,
* Store all IDs in the store so we can remove this macro with rmgrainline
*/
store.set(['parts', part.name, 'macros', 'grainline', 'ids', mc.id, 'paths'], ids)
return store.getMacroIds(mc.id, 'grainline')
}
// Export macros

View file

@ -21,6 +21,7 @@ import { pleatMacros, pleatDefs } from './pleat.mjs'
import { sewtogetherMacros, sewtogetherDefs } from './sewtogether.mjs'
// Only stores
import { flagStores } from './flag.mjs'
import { utilsStores } from './utils.mjs'
export const plugin = {
name,
@ -61,7 +62,7 @@ export const plugin = {
...sewtogetherMacros,
...titleMacros,
},
store: [...cutlistStores, ...flagStores],
store: [...cutlistStores, ...flagStores, ...utilsStores],
}
export const annotationsPlugin = plugin

View file

@ -110,6 +110,8 @@ const pleat = function (config, { paths, Path, log, Point, complete, scale, stor
* Store all IDs in the store so we can remove this macro with rmpleat
*/
store.set(['parts', part.name, 'macros', 'pleat', 'ids', mc.id, 'paths'], ids)
return store.getMacroIds(mc.id, 'pleat')
}
// Export macros

View file

@ -259,6 +259,8 @@ const scalebox = function (
imperial: ids.imperial,
},
})
return store.getMacroIds(mc.id, 'scalebox')
}
/*

View file

@ -125,6 +125,8 @@ const sewtogether = function (config, { paths, Path, log, Point, complete, sa, s
* Store all IDs in the store so we can remove this macro with rmsewtogether
*/
store.set(['parts', part.name, 'macros', 'sewtogether', 'ids', mc.id, 'paths'], ids)
return store.getMacroIds(mc.id, 'sewtogether')
}
// Export macros

View file

@ -230,6 +230,8 @@ const addTitleMacro = function (
* Store all IDs in the store so we can remove this macro with rmtitle
*/
store.set(['parts', part.name, 'macros', 'title', 'ids', mc.id, 'points'], ids)
return store.getMacroIds(mc.id, 'title')
}
// Export macros

View file

@ -7,3 +7,21 @@ export const getIds = (keys, id, macroName) => {
return ids
}
/*
* Helper method to get an existing macro id
*/
const getIdsFromStore = (store, id, macroName, partName = false) => {
if (!partName) partName = store.activePart
const data = store.get(['parts', partName, 'macros', macroName, 'ids', id])
return data ? data : false
}
/*
* Add these to the store
*/
export const utilsStores = [
['createMacroIds', (store, keys, id, macroName) => getIds(keys, id, macroName)],
['getMacroIds', getIdsFromStore],
]

View file

@ -1,39 +1,56 @@
import { name, version } from '../data.mjs'
import { getIds } from './utils.mjs'
/*
* These are the keys for macro IDs
*/
const pointKeys = ['start', 'cp1', 'cp2', 'end']
const pathKeys = ['path']
export const plugin = {
name,
version,
macros: {
round: function (so) {
round: function (mc, { points, paths, Point, Path, store, part }) {
const C = 0.55191502449
const { hide = true } = so
// Find angle between points
let from = so.from
let to = so.to
let via = so.via
let radius = so.radius
let prefix = so.prefix || 'round'
//let angle1 = from.angle(via)
//let angle2 = via.angle(to)
//if ((Math.round(angle1) - Math.round(angle2)) % 90 !== 0)
// console.log('Warning: The round macro only handles 90 degree angles correctly.')
let fd = from.dist(via)
let td = to.dist(via)
const {
from = new Point(0, 0),
to = new Point(666, 666),
via = new Point(666, 0),
id = 'round',
classes = '',
hide = true,
} = mc
let { radius = 66.6 } = mc
const ids = getIds([...pointKeys, ...pathKeys], id, name)
const fd = from.dist(via)
const td = to.dist(via)
if (radius > fd || radius > td || typeof radius === 'undefined') radius = fd > td ? td : fd
this.points[prefix + 'Start'] = via.shiftTowards(from, radius)
this.points[prefix + 'Cp1'] = via.shiftTowards(from, radius * (1 - C))
this.points[prefix + 'Cp2'] = via.shiftTowards(to, radius * (1 - C))
this.points[prefix + 'End'] = via.shiftTowards(to, radius)
this.paths[prefix + 'Rounded'] = new this.Path()
.move(this.points[prefix + 'Start'])
.curve(
this.points[prefix + 'Cp1'],
this.points[prefix + 'Cp2'],
this.points[prefix + 'End']
points[ids.start] = via.shiftTowards(from, radius)
points[ids.cp1] = via.shiftTowards(from, radius * (1 - C))
points[ids.cp2] = via.shiftTowards(to, radius * (1 - C))
points[ids.end] = via.shiftTowards(to, radius)
paths[ids.path] = new Path()
.move(this.points[ids.start])
.curve(points[ids.cp1], points[ids.cp2], points[ids.end])
.addClass(classes)
if (hide) paths[ids.path].hide()
else paths[ids.path].unhide()
/*
* Store all IDs in the store so we can remove this macro with rmtitle
*/
store.set(
['parts', part.name, 'macros', 'round', 'ids', mc.id, 'points'],
getIds(pointKeys, id, name)
)
.attr('class', so.class ? so.class : '')
if (hide) this.paths[prefix + 'Rounded'].hide()
else this.paths[prefix + 'Rounded'].unhide()
store.set(
['parts', part.name, 'macros', 'round', 'ids', mc.id, 'paths'],
getIds(pathKeys, id, name)
)
return store.getMacroIds(id, 'round')
},
},
}

View file

@ -0,0 +1,9 @@
/*
* Helper method to get the various IDs for a macro
*/
export const getIds = (keys, id, macroName) => {
const ids = {}
for (const key of keys) ids[key] = `__macro_${macroName}_${id}_${key}`
return ids
}