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 self = this
const method = function (key, args) { const method = function (key, args) {
const macro = utils.__macroName(key) 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) else if ('context' in self)
self.context.store.log.warn('Unknown macro `' + key + '` used in ' + self.name) 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 = { export const plugin = {
name, name,
version, version,
store,
hooks, hooks,
macros, macros,
store,
} }
// Specific named export // 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 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) store.set(['parts', part.name, 'macros', 'banner', 'ids', mc.id, 'paths'], ids)
return store.getMacroIds(config.id, 'banner')
} }
// Export macros // 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 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) store.set(['parts', part.name, 'macros', 'bannerbox', 'ids', mc.id, 'paths'], ids)
return store.getMacroIds(mc.id, 'bannerbox')
} }
export const bannerboxMacros = { bannerbox, rmbannerbox } 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] * 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) 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 * Just make sure to keep points and paths apart
*/ */
store.set(['parts', part.name, 'macros', 'title', 'ids', mc.id], ids) store.set(['parts', part.name, 'macros', 'title', 'ids', mc.id], ids)
return store.getMacroIds(mc.id, 'crossbox')
} }
// Export macros // 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 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) store.set(['parts', part.name, 'macros', 'cutonfold', 'ids', mc.id, 'paths'], ids)
return store.getMacroIds(mc.id, 'cutonfold')
} }
// Export macros // 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 * 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) 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 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) store.set(['parts', part.name, 'macros', 'grainline', 'ids', mc.id, 'paths'], ids)
return store.getMacroIds(mc.id, 'grainline')
} }
// Export macros // Export macros

View file

@ -21,6 +21,7 @@ import { pleatMacros, pleatDefs } from './pleat.mjs'
import { sewtogetherMacros, sewtogetherDefs } from './sewtogether.mjs' import { sewtogetherMacros, sewtogetherDefs } from './sewtogether.mjs'
// Only stores // Only stores
import { flagStores } from './flag.mjs' import { flagStores } from './flag.mjs'
import { utilsStores } from './utils.mjs'
export const plugin = { export const plugin = {
name, name,
@ -61,7 +62,7 @@ export const plugin = {
...sewtogetherMacros, ...sewtogetherMacros,
...titleMacros, ...titleMacros,
}, },
store: [...cutlistStores, ...flagStores], store: [...cutlistStores, ...flagStores, ...utilsStores],
} }
export const annotationsPlugin = plugin 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 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) store.set(['parts', part.name, 'macros', 'pleat', 'ids', mc.id, 'paths'], ids)
return store.getMacroIds(mc.id, 'pleat')
} }
// Export macros // Export macros

View file

@ -259,6 +259,8 @@ const scalebox = function (
imperial: ids.imperial, 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 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) store.set(['parts', part.name, 'macros', 'sewtogether', 'ids', mc.id, 'paths'], ids)
return store.getMacroIds(mc.id, 'sewtogether')
} }
// Export macros // 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 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) store.set(['parts', part.name, 'macros', 'title', 'ids', mc.id, 'points'], ids)
return store.getMacroIds(mc.id, 'title')
} }
// Export macros // Export macros

View file

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