Fix macro nesting and introduce nameFormat function
This commit is contained in:
parent
8e768cf640
commit
b6d7c78bab
2 changed files with 91 additions and 44 deletions
|
@ -18,6 +18,10 @@ export const mirrorGen = (start, end) => {
|
|||
}
|
||||
}
|
||||
|
||||
function capFirst(string) {
|
||||
return string.charAt(0).toUpperCase() + string.slice(1)
|
||||
}
|
||||
|
||||
export default {
|
||||
name: name,
|
||||
version: version,
|
||||
|
@ -27,7 +31,6 @@ export default {
|
|||
svg.attributes.set('freesewing:plugin-mirror', version)
|
||||
}
|
||||
},
|
||||
macros: {
|
||||
macros: {
|
||||
mirror: function ({
|
||||
mirror,
|
||||
|
@ -35,16 +38,15 @@ export default {
|
|||
points = null,
|
||||
paths = null,
|
||||
prefix = 'mirrored',
|
||||
nameFormat // unimplemented
|
||||
nameFormat = undefined
|
||||
}) {
|
||||
const [start, end] = mirror
|
||||
const mirrorPoint = mirrorGen(start, end)
|
||||
const ops = ['from', 'to', 'cp1', 'cp2']
|
||||
|
||||
if (paths !== null) {
|
||||
paths.forEach((path) => {
|
||||
// find existing path id
|
||||
// Find point name from path by looking in the list of all points?
|
||||
paths.forEach((path, i) => {
|
||||
// Try to find point name from path by looking in list of all points
|
||||
let foundId = null
|
||||
for (let id of Object.keys(this.paths)) {
|
||||
if (this.paths[id] === path) {
|
||||
|
@ -53,8 +55,12 @@ export default {
|
|||
}
|
||||
}
|
||||
path = clone ? path.clone() : path
|
||||
if (clone && foundId !== null) {
|
||||
this.paths[`${prefix}${foundId}`] = path
|
||||
if (clone) {
|
||||
if (foundId === null && typeof nameFormat == 'function') {
|
||||
this.paths[nameFormat(path)] = path
|
||||
} else {
|
||||
this.paths[`${prefix}${capFirst(foundId)}`] = path
|
||||
}
|
||||
}
|
||||
for (let op in path.ops) {
|
||||
for (let type of ops) {
|
||||
|
@ -68,10 +74,25 @@ export default {
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (points !== null) {
|
||||
points.forEach((point) => {
|
||||
let foundId = null
|
||||
for (let id of Object.keys(this.points)) {
|
||||
if (this.points[id] === point) {
|
||||
foundId = id
|
||||
break
|
||||
}
|
||||
}
|
||||
if (clone) {
|
||||
point = point.clone()
|
||||
if (clone) {
|
||||
if (foundId === null && typeof nameFormat == 'function') {
|
||||
this.points[nameFormat(point)] = point
|
||||
} else {
|
||||
this.points[`${prefix}${capFirst(foundId)}`] = point
|
||||
}
|
||||
}
|
||||
}
|
||||
;[point.x, point.y] = mirrorPoint(point)
|
||||
point.attributes.set('mirrored', true)
|
||||
|
@ -79,5 +100,4 @@ export default {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,39 @@
|
|||
// These tests haven't been run, stub to fix when plugin testing is added
|
||||
import freesewing from 'freesewing'
|
||||
import { version } from '../package.json'
|
||||
import reflect, { lineValues, mirrorGen } from '../src/reflect'
|
||||
let chai = require('chai')
|
||||
let expect = chai.expect
|
||||
chai.use(require('chai-string'))
|
||||
let plugin = require('../dist/index.js')
|
||||
let plugin,
|
||||
{ lineValues, mirrorGen } = require('../dist/index.js')
|
||||
|
||||
it('Should set the plugin name:version attribute', () => {
|
||||
let pattern = new freesewing.Pattern().with(plugin)
|
||||
pattern.render()
|
||||
expect(pattern.svg.attributes.get('freesewing:plugin-mirror')).to.equal(version)
|
||||
})
|
||||
|
||||
describe('mirrorGen(start, end)', () => {
|
||||
it('Should reflect points', () => {
|
||||
const reflectedPoint1 = new Point(2, 2)
|
||||
reflectedPoint1.attributes.set('reflected', true)
|
||||
|
||||
const reflectedPoint2 = new Point(-32, 20)
|
||||
reflectedPoint2.attributes.set('reflected', true)
|
||||
|
||||
// Should reflect point along 45deg line
|
||||
const gen = mirrorGen(new Point(-1, 1))
|
||||
expect([gen(new Point(-2, -2)), gen(new Point(32, -20))]).to.equal([
|
||||
reflectedPoint1,
|
||||
reflectedPoint2
|
||||
])
|
||||
})
|
||||
|
||||
it('lineValues should find the correct values for the line equation', () => {
|
||||
expect(lineValues(new Point(-1, 1), new Point(1, -1))).to.equal([-2, -2, 0])
|
||||
expect(lineValues(new Point(-2, 1), new Point(2, -1))).to.equal([-4, -2, 0])
|
||||
expect(lineValues(new Point(-1, 4), new Point(4, -1))).to.equal([-5, -5, 15])
|
||||
expect(lineValues(new Point(-12, -13), new Point(-1, 3))).to.equal([-11, 16, 49])
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue