chore(plugin-bartack): Changes for v3. See #2856
This commit is contained in:
parent
3c088be9b9
commit
89d491fb48
2 changed files with 73 additions and 46 deletions
|
@ -1,11 +1,11 @@
|
|||
import { version, name } from '../data.mjs'
|
||||
|
||||
// Method that draws the actual bartack
|
||||
const drawBartack = (points, self) => {
|
||||
let path = new self.Path().move(points.path1[0])
|
||||
for (const i in points.path1) {
|
||||
if (points.path1[i]) path = path.line(points.path1[i])
|
||||
if (points.path2[i]) path = path.line(points.path2[i])
|
||||
const drawBartack = (pointList, { Path }) => {
|
||||
let path = new Path().move(pointList.path1[0])
|
||||
for (const i in pointList.path1) {
|
||||
if (pointList.path1[i]) path = path.line(pointList.path1[i])
|
||||
if (pointList.path2[i]) path = path.line(pointList.path2[i])
|
||||
}
|
||||
|
||||
return path
|
||||
|
@ -39,9 +39,9 @@ const getPoints = (path, so) => {
|
|||
return points
|
||||
}
|
||||
|
||||
const bartackPath = (path, so, self) => (path ? drawBartack(getPoints(path, so), self) : null)
|
||||
const bartackPath = (path, so, props) => (path ? drawBartack(getPoints(path, so), props) : null)
|
||||
|
||||
export default function bartack(so, self) {
|
||||
function bartack(so, props) {
|
||||
const defaults = {
|
||||
width: 3,
|
||||
length: 15,
|
||||
|
@ -60,6 +60,8 @@ export default function bartack(so, self) {
|
|||
}
|
||||
so = { ...defaults, ...so }
|
||||
|
||||
const { Path, paths } = props
|
||||
|
||||
// Handle negative angle
|
||||
if (so.angle < 0) so.angle = 360 + (so.angle % -360)
|
||||
|
||||
|
@ -67,10 +69,10 @@ export default function bartack(so, self) {
|
|||
|
||||
if (so.anchor)
|
||||
// Anchor + angle + length
|
||||
guide = new self.Path().move(so.anchor).line(so.anchor.shift(so.angle, so.length))
|
||||
guide = new Path().move(so.anchor).line(so.anchor.shift(so.angle, so.length))
|
||||
else if (so.from && so.to)
|
||||
// From to
|
||||
guide = new self.Path().move(so.from).line(so.to)
|
||||
guide = new Path().move(so.from).line(so.to)
|
||||
else if (so.path) {
|
||||
// Along path
|
||||
let start = false
|
||||
|
@ -92,7 +94,7 @@ export default function bartack(so, self) {
|
|||
}
|
||||
}
|
||||
|
||||
self.paths[`${so.prefix}bartack${so.suffix}`] = bartackPath(guide, so, self).attr(
|
||||
paths[`${so.prefix}bartack${so.suffix}`] = bartackPath(guide, so, props).attr(
|
||||
'class',
|
||||
'stroke-sm stroke-mark'
|
||||
)
|
||||
|
@ -105,27 +107,24 @@ export const plugin = {
|
|||
name,
|
||||
version,
|
||||
macros: {
|
||||
bartack: function (so) {
|
||||
const self = this
|
||||
return bartack(so, self)
|
||||
bartack: function (so, props) {
|
||||
return bartack(so, props)
|
||||
},
|
||||
bartackAlong: function (so) {
|
||||
const self = this
|
||||
bartackAlong: function (so, props) {
|
||||
so.bartackFractionAlong = false
|
||||
so.bartackAlong = true
|
||||
so.anchor = false
|
||||
so.from = false
|
||||
so.to = false
|
||||
return bartack(so, self)
|
||||
return bartack(so, props)
|
||||
},
|
||||
bartackFractionAlong: function (so) {
|
||||
const self = this
|
||||
bartackFractionAlong: function (so, props) {
|
||||
so.bartackFractionAlong = true
|
||||
so.bartackAlong = false
|
||||
so.anchor = false
|
||||
so.from = false
|
||||
so.to = false
|
||||
return bartack(so, self)
|
||||
return bartack(so, props)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
|
@ -8,17 +8,21 @@ describe('Bartack plugin Tests', () => {
|
|||
it('draws a default bartack from a point', function () {
|
||||
const part = {
|
||||
name: 'test',
|
||||
draft: ({ Point, points, macro }) => {
|
||||
draft: ({ Point, points, macro, part }) => {
|
||||
points.from = new Point(10, 20)
|
||||
macro('bartack', {
|
||||
anchor: points.from,
|
||||
})
|
||||
|
||||
return part
|
||||
},
|
||||
plugins: [plugin],
|
||||
}
|
||||
const design = new Design({ parts: [part], plugins: [plugin] })
|
||||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft()
|
||||
const c = pattern.parts.test.paths.bartack
|
||||
|
||||
const c = pattern.parts[0].test.paths.bartack
|
||||
expect(c.attributes.get('class')).to.equal('stroke-sm stroke-mark')
|
||||
expect(c.ops[0].type).to.equal('move')
|
||||
expect(c.ops[0].to.x).to.equal(10)
|
||||
|
@ -38,18 +42,21 @@ describe('Bartack plugin Tests', () => {
|
|||
it('draws a bartack along a path', function () {
|
||||
const part = {
|
||||
name: 'test',
|
||||
draft: ({ Point, points, Path, macro }) => {
|
||||
draft: ({ Point, points, Path, macro, part }) => {
|
||||
points.from = new Point(10, 20)
|
||||
points.to = new Point(10, 30)
|
||||
macro('bartackAlong', {
|
||||
path: new Path().move(points.from).line(points.to),
|
||||
})
|
||||
|
||||
return part
|
||||
},
|
||||
plugins: [plugin],
|
||||
}
|
||||
const design = new Design({ parts: [part], plugins: [plugin] })
|
||||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft()
|
||||
const c = pattern.parts.test.paths.bartack
|
||||
const c = pattern.parts[0].test.paths.bartack
|
||||
expect(c.attributes.get('class')).to.equal('stroke-sm stroke-mark')
|
||||
expect(c.ops[0].type).to.equal('move')
|
||||
expect(c.ops[0].to.x).to.equal(8.5)
|
||||
|
@ -69,7 +76,7 @@ describe('Bartack plugin Tests', () => {
|
|||
it('can be called using the bartackFractionAlong syntax', function () {
|
||||
const part = {
|
||||
name: 'test',
|
||||
draft: ({ Point, points, Path, macro }) => {
|
||||
draft: ({ Point, points, Path, macro, part }) => {
|
||||
points.from = new Point(10, 20)
|
||||
points.to = new Point(10, 100)
|
||||
macro('bartackAlong', {
|
||||
|
@ -77,12 +84,15 @@ describe('Bartack plugin Tests', () => {
|
|||
start: 0.2,
|
||||
end: 0.8,
|
||||
})
|
||||
|
||||
return part
|
||||
},
|
||||
plugins: [plugin],
|
||||
}
|
||||
const design = new Design({ parts: [part], plugins: [plugin] })
|
||||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft()
|
||||
const c = pattern.parts.test.paths.bartack
|
||||
const c = pattern.parts[0].test.paths.bartack
|
||||
expect(c.attributes.get('class')).to.equal('stroke-sm stroke-mark')
|
||||
expect(c.ops[0].type).to.equal('move')
|
||||
expect(round(c.ops[0].to.x)).to.equal(8.5)
|
||||
|
@ -102,7 +112,7 @@ describe('Bartack plugin Tests', () => {
|
|||
it('can be called using the bartackFractionAlong syntax', function () {
|
||||
const part = {
|
||||
name: 'test',
|
||||
draft: ({ Point, points, Path, macro }) => {
|
||||
draft: ({ Point, points, Path, macro, part }) => {
|
||||
points.from = new Point(10, 20)
|
||||
points.to = new Point(10, 100)
|
||||
macro('bartackFractionAlong', {
|
||||
|
@ -110,12 +120,15 @@ describe('Bartack plugin Tests', () => {
|
|||
start: 0.2,
|
||||
end: 0.8,
|
||||
})
|
||||
|
||||
return part
|
||||
},
|
||||
plugins: [plugin],
|
||||
}
|
||||
const design = new Design({ parts: [part], plugins: [plugin] })
|
||||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft()
|
||||
const c = pattern.parts.test.paths.bartack
|
||||
const c = pattern.parts[0].test.paths.bartack
|
||||
expect(c.attributes.get('class')).to.equal('stroke-sm stroke-mark')
|
||||
expect(c.ops[0].type).to.equal('move')
|
||||
expect(round(c.ops[0].to.x)).to.equal(8.5)
|
||||
|
@ -135,18 +148,21 @@ describe('Bartack plugin Tests', () => {
|
|||
it('has configurable length', function () {
|
||||
const part = {
|
||||
name: 'test',
|
||||
draft: ({ Point, points, macro }) => {
|
||||
draft: ({ Point, points, macro, part }) => {
|
||||
points.from = new Point(10, 20)
|
||||
macro('bartack', {
|
||||
anchor: points.from,
|
||||
length: 20,
|
||||
})
|
||||
|
||||
return part
|
||||
},
|
||||
plugins: [plugin],
|
||||
}
|
||||
const design = new Design({ parts: [part], plugins: [plugin] })
|
||||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft()
|
||||
const c = pattern.parts.test.paths.bartack
|
||||
const c = pattern.parts[0].test.paths.bartack
|
||||
expect(c.attributes.get('class')).to.equal('stroke-sm stroke-mark')
|
||||
expect(c.ops[0].type).to.equal('move')
|
||||
expect(c.ops[0].to.x).to.equal(10)
|
||||
|
@ -166,18 +182,21 @@ describe('Bartack plugin Tests', () => {
|
|||
it('has configurable width', function () {
|
||||
const part = {
|
||||
name: 'test',
|
||||
draft: ({ Point, points, macro }) => {
|
||||
draft: ({ Point, points, macro, part }) => {
|
||||
points.from = new Point(10, 20)
|
||||
macro('bartack', {
|
||||
anchor: points.from,
|
||||
width: 5,
|
||||
})
|
||||
|
||||
return part
|
||||
},
|
||||
plugins: [plugin],
|
||||
}
|
||||
const design = new Design({ parts: [part], plugins: [plugin] })
|
||||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft()
|
||||
const c = pattern.parts.test.paths.bartack
|
||||
const c = pattern.parts[0].test.paths.bartack
|
||||
expect(c.attributes.get('class')).to.equal('stroke-sm stroke-mark')
|
||||
expect(c.ops[0].type).to.equal('move')
|
||||
expect(c.ops[0].to.x).to.equal(10)
|
||||
|
@ -197,18 +216,21 @@ describe('Bartack plugin Tests', () => {
|
|||
it('has configurable angle', function () {
|
||||
const part = {
|
||||
name: 'test',
|
||||
draft: ({ Point, points, macro }) => {
|
||||
draft: ({ Point, points, macro, part }) => {
|
||||
points.from = new Point(10, 20)
|
||||
macro('bartack', {
|
||||
anchor: points.from,
|
||||
angle: 45,
|
||||
})
|
||||
|
||||
return part
|
||||
},
|
||||
plugins: [plugin],
|
||||
}
|
||||
const design = new Design({ parts: [part], plugins: [plugin] })
|
||||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft()
|
||||
const c = pattern.parts.test.paths.bartack
|
||||
const c = pattern.parts[0].test.paths.bartack
|
||||
expect(c.attributes.get('class')).to.equal('stroke-sm stroke-mark')
|
||||
expect(c.ops[0].type).to.equal('move')
|
||||
expect(round(c.ops[0].to.x)).to.equal(11.06)
|
||||
|
@ -228,36 +250,42 @@ describe('Bartack plugin Tests', () => {
|
|||
it('has configurable suffix', function () {
|
||||
const part = {
|
||||
name: 'test',
|
||||
draft: ({ Point, points, macro }) => {
|
||||
draft: ({ Point, points, macro, part }) => {
|
||||
points.from = new Point(10, 20)
|
||||
macro('bartack', {
|
||||
anchor: points.from,
|
||||
suffix: 'foo',
|
||||
})
|
||||
|
||||
part
|
||||
},
|
||||
plugins: [plugin],
|
||||
}
|
||||
const design = new Design({ parts: [part], plugins: [plugin] })
|
||||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft()
|
||||
const c = pattern.parts.test.paths.bartackfoo
|
||||
const c = pattern.parts[0].test.paths.bartackfoo
|
||||
expect(c.attributes.get('class')).to.equal('stroke-sm stroke-mark')
|
||||
})
|
||||
|
||||
it('has configurable prefix', function () {
|
||||
const part = {
|
||||
name: 'test',
|
||||
draft: ({ Point, points, macro }) => {
|
||||
draft: ({ Point, points, macro, part }) => {
|
||||
points.from = new Point(10, 20)
|
||||
macro('bartack', {
|
||||
anchor: points.from,
|
||||
prefix: 'foo',
|
||||
})
|
||||
|
||||
return part
|
||||
},
|
||||
plugins: [plugin],
|
||||
}
|
||||
const design = new Design({ parts: [part], plugins: [plugin] })
|
||||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft()
|
||||
const c = pattern.parts.test.paths.foobartack
|
||||
const c = pattern.parts[0].test.paths.foobartack
|
||||
expect(c.attributes.get('class')).to.equal('stroke-sm stroke-mark')
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue