1
0
Fork 0

chore(plugin-annotations): Migrate sewtogether

This commit is contained in:
joostdecock 2023-09-05 20:38:14 +02:00
parent 509e6a3b85
commit d33139c6d0

View file

@ -1,93 +1,130 @@
import { getIds } from './utils.mjs'
/*
* Defaults for the sewtogether macro
*/
const macroDefaults = {
id: 'sewtogether',
force: false,
classes: {
curve: 'dotted note stroke-sm',
hinge: 'note dotted stroke-sm',
text: 'center fill-note text-xs',
},
}
// Export defs
export const sewtogetherDefs = [
{
name: 'sewTogetherStart',
def: `
<marker id="sewTogetherStart" markerWidth="4" markerHeight="4" orient="auto" refX="0" refY="2">
<path class="note stroke-sm" d="M4,4 L0,2 4,0" />
<marker id="sewTogetherStart" markerWidth="10" markerHeight="6" orient="auto" refX="1" refY="2">
<path d="M 0,2 L 6,0 C 5,1 5,3 6,4 z" class="fill-note note" />
</marker>`,
},
{
name: 'sewTogetherEnd',
def: `
<marker id="sewTogetherEnd" markerWidth="4" markerHeight="4" orient="auto" refX="4" refY="2">
<path class="note stroke-sm" d="M0,0 L4,2 0,4" />
<marker id="sewTogetherEnd" markerWidth="10" markerHeight="6" orient="auto" refX="6" refY="2">
<path d="M 6,2 L 0,0 C 1,1 1,3 0,4 z" class="fill-note note" />
</marker>`,
},
{
name: 'sewTogetherCross',
def: `
<marker id="sewTogetherCross" markerWidth="4" markerHeight="4" orient="auto" refX="2" refY="2">
<path d="M 0,0 L 4,4 M 4,0 L 0,4" class="note stroke-sm"/>
<marker id="sewTogetherCross" markerWidth="5" markerHeight="5" orient="auto" refX="2.5" refY="2.5">
<path d="M 0,0 L 5,5 M 5,0 L 0,5" class="note"/>
</marker>`,
},
]
// Export macros
export const sewtogetherMacros = {
sewTogether: function (so, { points, paths, Path, complete, sa }) {
if (so === false) {
delete points.sewtogetherFrom
delete points.sewtogetherFromCp
delete points.sewtogetherMiddle
delete points.sewtogetherTo
delete points.sewtogetherHinge
delete points.sewtogetherToCp
delete paths.sewtogetherSewTogetherHinge
delete paths.sewtogetherSewTogether
return true
}
so = {
prefix: 'sewtogether',
...so,
}
if (complete) {
if (null == so.middle) {
so.middle = so.from.shiftFractionTowards(so.to, 0.5)
}
points[so.prefix + 'From'] = so.from
points[so.prefix + 'Middle'] = so.middle
points[so.prefix + 'To'] = so.to
points[so.prefix + 'FromCp'] = points[so.prefix + 'From'].shift(
points[so.prefix + 'From'].angle(points[so.prefix + 'Middle']) + 90,
points[so.prefix + 'From'].dist(points[so.prefix + 'Middle']) / 1.5
)
points[so.prefix + 'ToCp'] = points[so.prefix + 'To'].shift(
points[so.prefix + 'To'].angle(points[so.prefix + 'Middle']) - 90,
points[so.prefix + 'To'].dist(points[so.prefix + 'Middle']) / 1.5
)
if (so.hinge) {
points[so.prefix + 'Hinge'] = points[so.prefix + 'Middle'].shift(
points[so.prefix + 'Middle'].angle(points[so.prefix + 'To']) +
Math.abs(
points[so.prefix + 'Middle'].angle(points[so.prefix + 'From']) -
points[so.prefix + 'Middle'].angle(points[so.prefix + 'To'])
) /
2 +
(sa ? 180 : 0),
sa
? sa
: Math.min(
points[so.prefix + 'From'].dist(points[so.prefix + 'Middle']),
points[so.prefix + 'From'].dist(points[so.prefix + 'Middle'])
) / 4
)
paths[so.prefix + 'SewTogetherHinge'] = new Path()
.move(points[so.prefix + 'Middle'])
.line(points[so.prefix + 'Hinge'])
.attr('marker-start', 'url(#sewTogetherCross)')
.attr('class', 'dotted note stroke-sm')
}
paths[so.prefix + 'SewTogether'] = new Path()
.move(points[so.prefix + 'From'])
.curve(points[so.prefix + 'FromCp'], points[so.prefix + 'ToCp'], points[so.prefix + 'To'])
.attr('class', 'dotted note stroke-sm')
.attr('marker-start', 'url(#sewTogetherStart)')
.attr('marker-end', 'url(#sewTogetherEnd)')
.attr('data-text', 'sewTogether')
.attr('data-text-class', 'center fill-note text-xs')
}
},
/*
* The rmsewtogether macro
*/
const rmsewtogether = function (id = macroDefaults.id, { paths, store, part }) {
for (const pid of Object.values(
store.get(['parts', part.name, 'macros', 'sewtogether', 'ids', id, 'paths'], {})
))
delete paths[pid]
}
/*
* The sewtogether macro
*/
const sewtogether = function (config, { points, paths, Path, complete, sa, store, part }) {
/*
* Don't add a title when complete is false, unless force is true
*/
if (!complete && !config.force) return
/*
* Merge macro defaults with user-provided config to create the macro config (mc)
*/
const mc = {
...macroDefaults,
...config,
classes: macroDefaults.classes,
}
if (config.classes) mc.classes = { ...mc.classes, ...config.classes }
/*
* Make sure mc.from and mc.to are Point instances
*/
if (!mc.from || typeof mc.from.attr !== 'function') {
log.warn(`Sewtogether macro called without a valid from point. Using (0,0) for from.`)
mc.from = new Point(0, 0)
}
if (!mc.to || typeof mc.to.attr !== 'function') {
log.warn(`Sewtogether macro called without a valid to point. Using (666,666) for to.`)
mc.to = new Point(666, 666)
}
/*
* Ensure we have a middle point
*/
if (!mc.middle) mc.middle = mc.from.shiftFractionTowards(mc.to, 0.5)
/*
* Get the list of IDs
* Initialize the verticle cadence
*/
const ids = getIds(['curve', 'hinge'], mc.id, 'sewtogether')
/*
* Draw the curve
*/
const fromCp = mc.from.shift(mc.from.angle(mc.middle) + 90, mc.from.dist(mc.middle) / 1.5)
const toCp = mc.to.shift(mc.to.angle(mc.middle) - 90, mc.to.dist(mc.middle) / 1.5)
paths[ids.curve] = new Path()
.move(mc.from)
.curve(fromCp, toCp, mc.to)
.attr('class', mc.classes.curve)
.attr('marker-start', 'url(#sewTogetherStart)')
.attr('marker-end', 'url(#sewTogetherEnd)')
.addText('sewTogether', mc.classes.text)
/*
* Draw the hinge, if needed
*/
if (mc.hinge) {
const hinge = mc.middle.shift(
mc.middle.angle(mc.to) +
Math.abs(mc.middle.angle(mc.from) - mc.middle.angle(mc.to)) / 2 +
(sa ? 180 : 0),
sa ? sa : mc.from.dist(mc.middle) / 4
)
paths[ids.hinge] = new Path()
.move(mc.middle)
.line(hinge)
.attr('marker-start', 'url(#sewTogetherCross)')
.attr('class', mc.classes.hinge)
} else delete ids.hinge
/*
* 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)
}
// Export macros
export const sewtogetherMacros = { sewtogether, rmsewtogether }