1
0
Fork 0

chore(plugin-annotations): Migrate bartack

This commit is contained in:
joostdecock 2023-09-05 20:36:45 +02:00
parent d95645b679
commit dae90a557d

View file

@ -1,3 +1,25 @@
import { getIds } from './utils.mjs'
/*
* Defaults for the bartack macro
*/
const macroDefaults = {
anchor: false,
angle: 0,
bartackAlong: false,
bartackFractionAlong: false,
classes: 'stroke-sm stroke-mark',
density: 3,
end: 1,
from: false,
id: 'bartack',
length: 15,
path: false,
start: 0,
to: false,
width: 3,
}
// Method that draws the actual bartack // Method that draws the actual bartack
const drawBartack = (pointList, { Path }) => { const drawBartack = (pointList, { Path }) => {
let path = new Path().move(pointList.path1[0]) let path = new Path().move(pointList.path1[0])
@ -10,9 +32,9 @@ const drawBartack = (pointList, { Path }) => {
} }
// Helper method to generate the points to draw on later // Helper method to generate the points to draw on later
const getPoints = (path, so) => { const getPoints = (path, mc) => {
let path1 = path.offset(so.width / 2) let path1 = path.offset(mc.width / 2)
let path2 = path.offset(so.width / -2) let path2 = path.offset(mc.width / -2)
let len = path1.length() let len = path1.length()
let len2 = path2.length() let len2 = path2.length()
@ -28,7 +50,7 @@ const getPoints = (path, so) => {
path1: [path1.start()], path1: [path1.start()],
path2: [path2.start()], path2: [path2.start()],
} }
let steps = Math.ceil((len / so.width) * so.density) let steps = Math.ceil((len / mc.width) * mc.density)
for (let i = 1; i < steps; i++) { for (let i = 1; i < steps; i++) {
points.path1.push(path1.shiftFractionAlong((1 / steps) * i)) points.path1.push(path1.shiftFractionAlong((1 / steps) * i))
points.path2.push(path2.shiftFractionAlong((1 / steps) * i)) points.path2.push(path2.shiftFractionAlong((1 / steps) * i))
@ -37,88 +59,138 @@ const getPoints = (path, so) => {
return points return points
} }
const bartackPath = (path, so, props) => (path ? drawBartack(getPoints(path, so), props) : null) const bartackPath = (path, mc, props) => (path ? drawBartack(getPoints(path, mc), props) : null)
function createBartack(so, props) { /*
const defaults = { * This method creates the actual bartack
width: 3, */
length: 15, function createBartack(config, props) {
density: 3, /*
angle: 0, * Don't add a bartack when complete is false, unless force is true
prefix: '', */
suffix: '', if (!props.complete && !config.force) return
anchor: false,
path: false,
from: false,
to: false,
start: 0,
end: 1,
bartackAlong: false,
bartackFractionAlong: false,
}
so = { ...defaults, ...so }
/*
* Merge macro defaults with user-provided config to create the macro config (mc)
*/
const mc = { ...macroDefaults, ...config }
/*
* Destructure what we need from props
*/
const { Path, paths } = props const { Path, paths } = props
// Handle negative angle /*
if (so.angle < 0) so.angle = 360 + (so.angle % -360) * Handle negative angle
*/
if (mc.angle < 0) mc.angle = 360 + (mc.angle % -360)
/*
* Construct the guide path
*/
let guide = false let guide = false
let name = 'bartack'
if (so.anchor) if (mc.anchor)
// Anchor + angle + length // Anchor + angle + length
guide = new Path().move(so.anchor).line(so.anchor.shift(so.angle, so.length)) guide = new Path().move(mc.anchor).line(mc.anchor.shift(mc.angle, mc.length))
else if (so.from && so.to) else if (mc.from && mc.to)
// From to // From to
guide = new Path().move(so.from).line(so.to) guide = new Path().move(mc.from).line(mc.to)
else if (so.path) { else if (mc.path) {
// Along path // Along path
let start = false let start = false
let end = false let end = false
if (so.bartackAlong) guide = so.path.clone() name = 'bartackalong'
else if (so.bartackFractionAlong) { if (mc.bartackAlong) guide = mc.path.clone()
if (so.start === so.end) return null else if (mc.bartackFractionAlong) {
if (so.start > so.end) { name = 'bartackfractionalong'
const newEnd = so.start if (mc.start === mc.end) return null
so.start = so.end if (mc.start > mc.end) {
so.end = newEnd const newEnd = mc.start
mc.start = mc.end
mc.end = newEnd
} }
if (so.start > 0) start = so.path.shiftFractionAlong(so.start) if (mc.start > 0) start = mc.path.shiftFractionAlong(mc.start)
if (so.end < 1) end = so.path.shiftFractionAlong(so.end) if (mc.end < 1) end = mc.path.shiftFractionAlong(mc.end)
if (start && end) guide = so.path.split(start).pop().split(end).shift() if (start && end) guide = mc.path.split(start).pop().split(end).shift()
else if (start) guide = so.path.split(start).pop() else if (start) guide = mc.path.split(start).pop()
else if (end) guide = so.path.split(end).shift() else if (end) guide = mc.path.split(end).shift()
else guide = so.path.clone() else guide = mc.path.clone()
} }
} }
paths[`${so.prefix}bartack${so.suffix}`] = bartackPath(guide, so, props).attr( /*
'class', * Get the list of IDs
'stroke-sm stroke-mark' */
const ids = getIds(['stitches'], mc.id, name)
paths[ids.stitches] = bartackPath(guide, mc, props).attr('class', mc.classes)
/*
* 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)
}
/*
* The method that will remove all macros
*/
const removeBartack = function (name = 'bartack', id = macroDefaults.id, { paths, store, part }) {
for (const pid of Object.values(
store.get(['parts', part.name, 'macros', name, 'ids', id, 'paths'], {})
))
delete paths[pid]
}
/*
* The rmbartackalong and rmbartackfractionalong macros just call rmbartack with the correct name
*/
const rmbartack = (id, props) => removeBartack('bartack', id, props)
const rmbartackAlong = (id, props) => removeBartack('bartackalong', id, props)
const rmbartackFractionAlong = (id, props) => removeBartack('bartackfractionalong', id, props)
/*
* The bartack macro
*/
const bartack = (config, props) => createBartack(config, props)
/*
* The bartackAlong macro
*/
const bartackAlong = (config, props) =>
createBartack(
{
...config,
bartackFractionAlong: false,
bartackAlong: true,
anchor: false,
from: false,
to: false,
},
props
) )
return true /*
} * The bartackFractionAlong macro
*/
const bartackFractionAlong = (config, props) =>
createBartack(
{
...config,
bartackFractionAlong: true,
bartackAlong: false,
anchor: false,
from: false,
to: false,
},
props
)
// Export macros // Export macros
export const bartackMacros = { export const bartackMacros = {
bartack: function (so, props) { bartack,
return createBartack(so, props) bartackAlong,
}, bartackFractionAlong,
bartackAlong: function (so, props) { rmbartack,
so.bartackFractionAlong = false rmbartackAlong,
so.bartackAlong = true rmbartackFractionAlong,
so.anchor = false
so.from = false
so.to = false
return createBartack(so, props)
},
bartackFractionAlong: function (so, props) {
so.bartackFractionAlong = true
so.bartackAlong = false
so.anchor = false
so.from = false
so.to = false
return createBartack(so, props)
},
} }