1
0
Fork 0

chore(plugin-flip): Ported to v3

This commit is contained in:
Joost De Cock 2022-08-28 12:33:06 +02:00
parent e5d5ead92d
commit 6f8cc6e753
5 changed files with 21 additions and 19 deletions

View file

@ -0,0 +1,54 @@
import { name, version } from '../package.json' assert { type: 'json' }
export const plugin = {
name,
version,
macros: {
flip: function (so) {
const axis = so?.axis === 'y' ? 'y' : 'x'
let flipped = null
const ops = ['from', 'to', 'cp1', 'cp2']
for (const id in this.points) {
// Keep track of the amount of flips
// (needed to allow flipping twice, but also avoid double flips in paths below)
if (flipped === null) {
flipped = this.points[id].attributes.get('flipped')
if (flipped === false) flipped = 1
else flipped += 1
}
this.points[id][axis] = this.points[id][axis] * -1
this.points[id].attributes.set('flipped', flipped)
}
for (let id of Object.keys(this.paths)) {
for (let op in this.paths[id].ops) {
for (let type of ops) {
if (typeof this.paths[id].ops[op][type] !== 'undefined') {
// Path ops can use points not listed in part.points. We should only flip those here
// and not double flip the points flipped above
let wasFlipped = this.paths[id].ops[op][type].attributes.get('flipped')
if (wasFlipped !== false) wasFlipped = parseInt(wasFlipped)
if (wasFlipped !== flipped) {
this.paths[id].ops[op][type][axis] = this.paths[id].ops[op][type][axis] * -1
this.paths[id].ops[op][type].attributes.set('flipped', flipped)
}
}
}
}
}
for (let id of Object.keys(this.snippets)) {
// Snippets use points not listed in part.points. We should only flip those here
// and not double flip the points flipped above
let wasFlipped = this.snippets[id].anchor.attributes.get('flipped')
if (wasFlipped !== false) wasFlipped = parseInt(wasFlipped)
if (wasFlipped !== flipped)
this.snippets[id].anchor[axis] = this.snippets[id].anchor[axis] * -1
}
},
},
}
// More specifically named exports
export const flipPlugin = plugin
export const pluginFlip = plugin