2022-09-09 12:13:52 -05:00
|
|
|
import { name, version } from '../data.mjs'
|
2018-12-22 14:16:53 +01:00
|
|
|
|
2022-08-28 12:33:06 +02:00
|
|
|
export const plugin = {
|
|
|
|
name,
|
|
|
|
version,
|
2018-12-22 14:16:53 +01:00
|
|
|
macros: {
|
2022-09-25 16:47:04 +02:00
|
|
|
flip: function (so, { paths, points, snippets }) {
|
2022-01-19 16:23:40 +01:00
|
|
|
const axis = so?.axis === 'y' ? 'y' : 'x'
|
2019-08-03 15:03:33 +02:00
|
|
|
let flipped = null
|
2021-11-27 16:43:41 +01:00
|
|
|
const ops = ['from', 'to', 'cp1', 'cp2']
|
2022-09-25 16:47:04 +02:00
|
|
|
for (const id in points) {
|
2021-11-27 16:43:41 +01:00
|
|
|
// Keep track of the amount of flips
|
|
|
|
// (needed to allow flipping twice, but also avoid double flips in paths below)
|
2019-07-06 13:57:38 +02:00
|
|
|
if (flipped === null) {
|
2022-09-25 16:47:04 +02:00
|
|
|
flipped = points[id].attributes.get('flipped')
|
2019-08-03 15:03:33 +02:00
|
|
|
if (flipped === false) flipped = 1
|
|
|
|
else flipped += 1
|
2019-07-06 13:57:38 +02:00
|
|
|
}
|
2022-09-25 16:47:04 +02:00
|
|
|
points[id][axis] = points[id][axis] * -1
|
|
|
|
points[id].attributes.set('flipped', flipped)
|
2019-07-06 13:57:38 +02:00
|
|
|
}
|
2022-09-25 16:47:04 +02:00
|
|
|
for (let id of Object.keys(paths)) {
|
|
|
|
for (let op in paths[id].ops) {
|
2018-12-22 14:16:53 +01:00
|
|
|
for (let type of ops) {
|
2022-09-25 16:47:04 +02:00
|
|
|
if (typeof paths[id].ops[op][type] !== 'undefined') {
|
2019-07-06 13:57:38 +02:00
|
|
|
// Path ops can use points not listed in part.points. We should only flip those here
|
|
|
|
// and not double flip the points flipped above
|
2022-09-25 16:47:04 +02:00
|
|
|
let wasFlipped = paths[id].ops[op][type].attributes.get('flipped')
|
2019-08-03 15:03:33 +02:00
|
|
|
if (wasFlipped !== false) wasFlipped = parseInt(wasFlipped)
|
2019-07-06 13:57:38 +02:00
|
|
|
if (wasFlipped !== flipped) {
|
2022-09-25 16:47:04 +02:00
|
|
|
paths[id].ops[op][type][axis] = paths[id].ops[op][type][axis] * -1
|
|
|
|
paths[id].ops[op][type].attributes.set('flipped', flipped)
|
2019-07-06 13:57:38 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-22 14:16:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-25 16:47:04 +02:00
|
|
|
for (let id of Object.keys(snippets)) {
|
2019-07-06 13:57:38 +02:00
|
|
|
// Snippets use points not listed in part.points. We should only flip those here
|
|
|
|
// and not double flip the points flipped above
|
2022-09-25 16:47:04 +02:00
|
|
|
let wasFlipped = snippets[id].anchor.attributes.get('flipped')
|
2019-08-03 15:03:33 +02:00
|
|
|
if (wasFlipped !== false) wasFlipped = parseInt(wasFlipped)
|
2022-09-25 16:47:04 +02:00
|
|
|
if (wasFlipped !== flipped) snippets[id].anchor[axis] = snippets[id].anchor[axis] * -1
|
2019-07-06 13:57:38 +02:00
|
|
|
}
|
2022-01-19 16:23:40 +01:00
|
|
|
},
|
|
|
|
},
|
2019-08-03 15:03:33 +02:00
|
|
|
}
|
2022-08-28 12:33:06 +02:00
|
|
|
|
|
|
|
// More specifically named exports
|
|
|
|
export const flipPlugin = plugin
|
|
|
|
export const pluginFlip = plugin
|