1
0
Fork 0
freesewing/plugins/plugin-grainline/src/index.mjs

60 lines
1.9 KiB
JavaScript
Raw Normal View History

import { name, version } from '../data.mjs'
2018-08-10 18:42:20 +02:00
2022-08-28 12:40:19 +02:00
const markers = `
<marker orient="auto" refY="4.0" refX="10.0" id="grainlineFrom" style="overflow:visible;" markerWidth="12" markerHeight="8">
<path class="note fill-note" d="M 0,4 L 12,0 C 10,2 10,6 12,8 z" />
</marker>
<marker orient="auto" refY="4.0" refX="2.0" id="grainlineTo" style="overflow:visible;" markerWidth="12" markerHeight="8">
<path class="note fill-note" d="M 12,4 L 0,0 C 2,2 2,6 0,8 z" />
</marker>`
const dflts = { text: 'grainline' }
2022-08-28 12:40:19 +02:00
export const plugin = {
name,
version,
2018-08-10 18:42:20 +02:00
hooks: {
preRender: (svg) => {
2022-08-28 12:40:19 +02:00
if (svg.defs.indexOf(markers) === -1) svg.defs += markers
2021-04-24 10:16:31 +02:00
},
2018-08-10 18:42:20 +02:00
},
macros: {
grainline: function (so = {}, { points, paths, Path, complete, store }) {
if (so === false) {
delete points.grainlineFrom
delete points.grainlineTo
delete paths.grainline
2023-03-12 11:29:37 -05:00
if (typeof store.cutlist.setGrain === 'function') {
store.cutlist.setGrain(false) // Restoring default
}
2019-08-03 15:03:33 +02:00
return true
}
so = {
...dflts,
2022-01-19 16:23:40 +01:00
...so,
}
// setGrain relies on plugin-cutlist
2023-03-19 20:12:17 -05:00
if (typeof store.get('cutlist.setGrain') === 'function') {
2023-03-12 11:29:37 -05:00
store.cutlist.setGrain(so.from.angle(so.to))
}
2023-03-19 20:12:17 -05:00
if (complete) {
points.grainlineFrom = so.from.shiftFractionTowards(so.to, 0.05)
points.grainlineTo = so.to.shiftFractionTowards(so.from, 0.05)
paths.grainline = new Path()
.move(points.grainlineFrom)
.line(points.grainlineTo)
.attr('class', 'note')
.attr('marker-start', 'url(#grainlineFrom)')
.attr('marker-end', 'url(#grainlineTo)')
.attr('data-text', so.text)
.attr('data-text-class', 'center fill-note')
}
2021-04-24 10:16:31 +02:00
},
},
2019-08-03 15:03:33 +02:00
}
2022-08-28 12:40:19 +02:00
// More specifically named exports
export const grainlinePlugin = plugin
export const pluginGrainline = plugin