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'
|
import { version, name } from '../data.mjs'
|
||||||
|
|
||||||
// Method that draws the actual bartack
|
// Method that draws the actual bartack
|
||||||
const drawBartack = (points, self) => {
|
const drawBartack = (pointList, { Path }) => {
|
||||||
let path = new self.Path().move(points.path1[0])
|
let path = new Path().move(pointList.path1[0])
|
||||||
for (const i in points.path1) {
|
for (const i in pointList.path1) {
|
||||||
if (points.path1[i]) path = path.line(points.path1[i])
|
if (pointList.path1[i]) path = path.line(pointList.path1[i])
|
||||||
if (points.path2[i]) path = path.line(points.path2[i])
|
if (pointList.path2[i]) path = path.line(pointList.path2[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
return path
|
return path
|
||||||
|
@ -39,9 +39,9 @@ const getPoints = (path, so) => {
|
||||||
return points
|
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 = {
|
const defaults = {
|
||||||
width: 3,
|
width: 3,
|
||||||
length: 15,
|
length: 15,
|
||||||
|
@ -60,6 +60,8 @@ export default function bartack(so, self) {
|
||||||
}
|
}
|
||||||
so = { ...defaults, ...so }
|
so = { ...defaults, ...so }
|
||||||
|
|
||||||
|
const { Path, paths } = props
|
||||||
|
|
||||||
// Handle negative angle
|
// Handle negative angle
|
||||||
if (so.angle < 0) so.angle = 360 + (so.angle % -360)
|
if (so.angle < 0) so.angle = 360 + (so.angle % -360)
|
||||||
|
|
||||||
|
@ -67,10 +69,10 @@ export default function bartack(so, self) {
|
||||||
|
|
||||||
if (so.anchor)
|
if (so.anchor)
|
||||||
// Anchor + angle + length
|
// 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)
|
else if (so.from && so.to)
|
||||||
// From 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) {
|
else if (so.path) {
|
||||||
// Along path
|
// Along path
|
||||||
let start = false
|
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',
|
'class',
|
||||||
'stroke-sm stroke-mark'
|
'stroke-sm stroke-mark'
|
||||||
)
|
)
|
||||||
|
@ -105,27 +107,24 @@ export const plugin = {
|
||||||
name,
|
name,
|
||||||
version,
|
version,
|
||||||
macros: {
|
macros: {
|
||||||
bartack: function (so) {
|
bartack: function (so, props) {
|
||||||
const self = this
|
return bartack(so, props)
|
||||||
return bartack(so, self)
|
|
||||||
},
|
},
|
||||||
bartackAlong: function (so) {
|
bartackAlong: function (so, props) {
|
||||||
const self = this
|
|
||||||
so.bartackFractionAlong = false
|
so.bartackFractionAlong = false
|
||||||
so.bartackAlong = true
|
so.bartackAlong = true
|
||||||
so.anchor = false
|
so.anchor = false
|
||||||
so.from = false
|
so.from = false
|
||||||
so.to = false
|
so.to = false
|
||||||
return bartack(so, self)
|
return bartack(so, props)
|
||||||
},
|
},
|
||||||
bartackFractionAlong: function (so) {
|
bartackFractionAlong: function (so, props) {
|
||||||
const self = this
|
|
||||||
so.bartackFractionAlong = true
|
so.bartackFractionAlong = true
|
||||||
so.bartackAlong = false
|
so.bartackAlong = false
|
||||||
so.anchor = false
|
so.anchor = false
|
||||||
so.from = false
|
so.from = false
|
||||||
so.to = 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 () {
|
it('draws a default bartack from a point', function () {
|
||||||
const part = {
|
const part = {
|
||||||
name: 'test',
|
name: 'test',
|
||||||
draft: ({ Point, points, macro }) => {
|
draft: ({ Point, points, macro, part }) => {
|
||||||
points.from = new Point(10, 20)
|
points.from = new Point(10, 20)
|
||||||
macro('bartack', {
|
macro('bartack', {
|
||||||
anchor: points.from,
|
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()
|
const pattern = new design()
|
||||||
pattern.draft()
|
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.attributes.get('class')).to.equal('stroke-sm stroke-mark')
|
||||||
expect(c.ops[0].type).to.equal('move')
|
expect(c.ops[0].type).to.equal('move')
|
||||||
expect(c.ops[0].to.x).to.equal(10)
|
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 () {
|
it('draws a bartack along a path', function () {
|
||||||
const part = {
|
const part = {
|
||||||
name: 'test',
|
name: 'test',
|
||||||
draft: ({ Point, points, Path, macro }) => {
|
draft: ({ Point, points, Path, macro, part }) => {
|
||||||
points.from = new Point(10, 20)
|
points.from = new Point(10, 20)
|
||||||
points.to = new Point(10, 30)
|
points.to = new Point(10, 30)
|
||||||
macro('bartackAlong', {
|
macro('bartackAlong', {
|
||||||
path: new Path().move(points.from).line(points.to),
|
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()
|
const pattern = new design()
|
||||||
pattern.draft()
|
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.attributes.get('class')).to.equal('stroke-sm stroke-mark')
|
||||||
expect(c.ops[0].type).to.equal('move')
|
expect(c.ops[0].type).to.equal('move')
|
||||||
expect(c.ops[0].to.x).to.equal(8.5)
|
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 () {
|
it('can be called using the bartackFractionAlong syntax', function () {
|
||||||
const part = {
|
const part = {
|
||||||
name: 'test',
|
name: 'test',
|
||||||
draft: ({ Point, points, Path, macro }) => {
|
draft: ({ Point, points, Path, macro, part }) => {
|
||||||
points.from = new Point(10, 20)
|
points.from = new Point(10, 20)
|
||||||
points.to = new Point(10, 100)
|
points.to = new Point(10, 100)
|
||||||
macro('bartackAlong', {
|
macro('bartackAlong', {
|
||||||
|
@ -77,12 +84,15 @@ describe('Bartack plugin Tests', () => {
|
||||||
start: 0.2,
|
start: 0.2,
|
||||||
end: 0.8,
|
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()
|
const pattern = new design()
|
||||||
pattern.draft()
|
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.attributes.get('class')).to.equal('stroke-sm stroke-mark')
|
||||||
expect(c.ops[0].type).to.equal('move')
|
expect(c.ops[0].type).to.equal('move')
|
||||||
expect(round(c.ops[0].to.x)).to.equal(8.5)
|
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 () {
|
it('can be called using the bartackFractionAlong syntax', function () {
|
||||||
const part = {
|
const part = {
|
||||||
name: 'test',
|
name: 'test',
|
||||||
draft: ({ Point, points, Path, macro }) => {
|
draft: ({ Point, points, Path, macro, part }) => {
|
||||||
points.from = new Point(10, 20)
|
points.from = new Point(10, 20)
|
||||||
points.to = new Point(10, 100)
|
points.to = new Point(10, 100)
|
||||||
macro('bartackFractionAlong', {
|
macro('bartackFractionAlong', {
|
||||||
|
@ -110,12 +120,15 @@ describe('Bartack plugin Tests', () => {
|
||||||
start: 0.2,
|
start: 0.2,
|
||||||
end: 0.8,
|
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()
|
const pattern = new design()
|
||||||
pattern.draft()
|
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.attributes.get('class')).to.equal('stroke-sm stroke-mark')
|
||||||
expect(c.ops[0].type).to.equal('move')
|
expect(c.ops[0].type).to.equal('move')
|
||||||
expect(round(c.ops[0].to.x)).to.equal(8.5)
|
expect(round(c.ops[0].to.x)).to.equal(8.5)
|
||||||
|
@ -135,18 +148,21 @@ describe('Bartack plugin Tests', () => {
|
||||||
it('has configurable length', function () {
|
it('has configurable length', function () {
|
||||||
const part = {
|
const part = {
|
||||||
name: 'test',
|
name: 'test',
|
||||||
draft: ({ Point, points, macro }) => {
|
draft: ({ Point, points, macro, part }) => {
|
||||||
points.from = new Point(10, 20)
|
points.from = new Point(10, 20)
|
||||||
macro('bartack', {
|
macro('bartack', {
|
||||||
anchor: points.from,
|
anchor: points.from,
|
||||||
length: 20,
|
length: 20,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return part
|
||||||
},
|
},
|
||||||
|
plugins: [plugin],
|
||||||
}
|
}
|
||||||
const design = new Design({ parts: [part], plugins: [plugin] })
|
const design = new Design({ parts: [part] })
|
||||||
const pattern = new design()
|
const pattern = new design()
|
||||||
pattern.draft()
|
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.attributes.get('class')).to.equal('stroke-sm stroke-mark')
|
||||||
expect(c.ops[0].type).to.equal('move')
|
expect(c.ops[0].type).to.equal('move')
|
||||||
expect(c.ops[0].to.x).to.equal(10)
|
expect(c.ops[0].to.x).to.equal(10)
|
||||||
|
@ -166,18 +182,21 @@ describe('Bartack plugin Tests', () => {
|
||||||
it('has configurable width', function () {
|
it('has configurable width', function () {
|
||||||
const part = {
|
const part = {
|
||||||
name: 'test',
|
name: 'test',
|
||||||
draft: ({ Point, points, macro }) => {
|
draft: ({ Point, points, macro, part }) => {
|
||||||
points.from = new Point(10, 20)
|
points.from = new Point(10, 20)
|
||||||
macro('bartack', {
|
macro('bartack', {
|
||||||
anchor: points.from,
|
anchor: points.from,
|
||||||
width: 5,
|
width: 5,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return part
|
||||||
},
|
},
|
||||||
|
plugins: [plugin],
|
||||||
}
|
}
|
||||||
const design = new Design({ parts: [part], plugins: [plugin] })
|
const design = new Design({ parts: [part] })
|
||||||
const pattern = new design()
|
const pattern = new design()
|
||||||
pattern.draft()
|
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.attributes.get('class')).to.equal('stroke-sm stroke-mark')
|
||||||
expect(c.ops[0].type).to.equal('move')
|
expect(c.ops[0].type).to.equal('move')
|
||||||
expect(c.ops[0].to.x).to.equal(10)
|
expect(c.ops[0].to.x).to.equal(10)
|
||||||
|
@ -197,18 +216,21 @@ describe('Bartack plugin Tests', () => {
|
||||||
it('has configurable angle', function () {
|
it('has configurable angle', function () {
|
||||||
const part = {
|
const part = {
|
||||||
name: 'test',
|
name: 'test',
|
||||||
draft: ({ Point, points, macro }) => {
|
draft: ({ Point, points, macro, part }) => {
|
||||||
points.from = new Point(10, 20)
|
points.from = new Point(10, 20)
|
||||||
macro('bartack', {
|
macro('bartack', {
|
||||||
anchor: points.from,
|
anchor: points.from,
|
||||||
angle: 45,
|
angle: 45,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return part
|
||||||
},
|
},
|
||||||
|
plugins: [plugin],
|
||||||
}
|
}
|
||||||
const design = new Design({ parts: [part], plugins: [plugin] })
|
const design = new Design({ parts: [part] })
|
||||||
const pattern = new design()
|
const pattern = new design()
|
||||||
pattern.draft()
|
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.attributes.get('class')).to.equal('stroke-sm stroke-mark')
|
||||||
expect(c.ops[0].type).to.equal('move')
|
expect(c.ops[0].type).to.equal('move')
|
||||||
expect(round(c.ops[0].to.x)).to.equal(11.06)
|
expect(round(c.ops[0].to.x)).to.equal(11.06)
|
||||||
|
@ -228,36 +250,42 @@ describe('Bartack plugin Tests', () => {
|
||||||
it('has configurable suffix', function () {
|
it('has configurable suffix', function () {
|
||||||
const part = {
|
const part = {
|
||||||
name: 'test',
|
name: 'test',
|
||||||
draft: ({ Point, points, macro }) => {
|
draft: ({ Point, points, macro, part }) => {
|
||||||
points.from = new Point(10, 20)
|
points.from = new Point(10, 20)
|
||||||
macro('bartack', {
|
macro('bartack', {
|
||||||
anchor: points.from,
|
anchor: points.from,
|
||||||
suffix: 'foo',
|
suffix: 'foo',
|
||||||
})
|
})
|
||||||
|
|
||||||
|
part
|
||||||
},
|
},
|
||||||
|
plugins: [plugin],
|
||||||
}
|
}
|
||||||
const design = new Design({ parts: [part], plugins: [plugin] })
|
const design = new Design({ parts: [part] })
|
||||||
const pattern = new design()
|
const pattern = new design()
|
||||||
pattern.draft()
|
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')
|
expect(c.attributes.get('class')).to.equal('stroke-sm stroke-mark')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('has configurable prefix', function () {
|
it('has configurable prefix', function () {
|
||||||
const part = {
|
const part = {
|
||||||
name: 'test',
|
name: 'test',
|
||||||
draft: ({ Point, points, macro }) => {
|
draft: ({ Point, points, macro, part }) => {
|
||||||
points.from = new Point(10, 20)
|
points.from = new Point(10, 20)
|
||||||
macro('bartack', {
|
macro('bartack', {
|
||||||
anchor: points.from,
|
anchor: points.from,
|
||||||
prefix: 'foo',
|
prefix: 'foo',
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return part
|
||||||
},
|
},
|
||||||
|
plugins: [plugin],
|
||||||
}
|
}
|
||||||
const design = new Design({ parts: [part], plugins: [plugin] })
|
const design = new Design({ parts: [part] })
|
||||||
const pattern = new design()
|
const pattern = new design()
|
||||||
pattern.draft()
|
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')
|
expect(c.attributes.get('class')).to.equal('stroke-sm stroke-mark')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue