1
0
Fork 0

feat(core): Pass shorthand() to draft methods

This commit is contained in:
joostdecock 2022-09-10 19:13:43 +02:00
parent 3b510fc2fb
commit ac4d134075
6 changed files with 32 additions and 52 deletions

View file

@ -185,6 +185,7 @@ Part.prototype.shorthand = function () {
const paperless = this.context.settings?.paperless === true ? true : false
const sa = this.context.settings?.complete ? this.context.settings?.sa || 0 : 0
const shorthand = {
part: this,
sa,
scale: this.context.settings?.scale,
store: this.context.store,

View file

@ -237,10 +237,10 @@ Pattern.prototype.draft = function () {
// Draft part
if (typeof this.__parts?.[partName]?.draft === 'function') {
try {
this.parts[partName] = this.__parts[partName].draft(this.parts[partName])
if (typeof this.parts[partName] === 'undefined') {
const result = this.__parts[partName].draft(this.parts[partName].shorthand())
if (typeof result === 'undefined') {
this.store.log.error(`Result of drafting part ${partName} was undefined. Did you forget to return the part?`)
}
} else this.parts[partName] = result
} catch (err) {
this.store.log.error([`Unable to draft part \`${partName}\``, err])
}

View file

@ -7,8 +7,7 @@ describe('Path', () => {
it('Should offset a line', () => {
const part = {
name: 'test',
draft: part => {
const { paths, Path, Point } = part.shorthand()
draft: ({ paths, Path, Point, part}) => {
paths.line = new Path().move(new Point(0, 0)).line(new Point(0, 40))
paths.offset = paths.line.offset(10)
return part
@ -24,8 +23,7 @@ describe('Path', () => {
it('Should offset a curve', () => {
const part = {
name: 'test',
draft: part => {
const { paths, Path, Point } = part.shorthand()
draft: ({ paths, Path, Point, part }) => {
paths.curve = new Path()
.move(new Point(0, 0))
.curve(new Point(0, 40), new Point(123, 34), new Point(23, 4))
@ -43,8 +41,7 @@ describe('Path', () => {
it('Should offset a curve where cp1 = start', () => {
const part = {
name: 'test',
draft: part => {
const { paths, Path, Point } = part.shorthand()
draft: ({ paths, Path, Point, part }) => {
paths.curve = new Path()
.move(new Point(0, 0))
._curve(new Point(123, 34), new Point(23, 4))
@ -62,8 +59,7 @@ describe('Path', () => {
it('Should offset a curve where cp2 = end', () => {
const part = {
name: 'test',
draft: part => {
const { paths, Path, Point } = part.shorthand()
draft: ({ paths, Path, Point, part }) => {
paths.curve = new Path()
.move(new Point(0, 0))
.curve_(new Point(40, 0), new Point(123, 34))
@ -82,8 +78,7 @@ describe('Path', () => {
it('Should throw error when offsetting line that is no line', () => {
const part = {
name: 'test',
draft: part => {
const { paths, Path, Point } = part.shorthand()
draft: ({ paths, Path, Point }) => {
paths.line = new Path()
.move(new Point(0, 40))
.line(new Point(0, 40))
@ -102,8 +97,7 @@ describe('Path', () => {
it('Should return the length of a line', () => {
const part = {
name: 'test',
draft: part => {
const { paths, Path, Point } = part.shorthand()
draft: ({ paths, Path, Point, part }) => {
paths.line = new Path()
.move(new Point(0, 0))
.line(new Point(40, 0))
@ -119,8 +113,7 @@ describe('Path', () => {
it('Should return the length of a curve', () => {
const part = {
name: 'test',
draft: part => {
const { paths, Path, Point } = part.shorthand()
draft: ({ paths, Path, Point, part }) => {
paths.curve = new Path()
.move(new Point(0, 0))
.curve(new Point(0, 40), new Point(123, 34), new Point(23, 4))
@ -137,8 +130,7 @@ describe('Path', () => {
it('Should return the path start point', () => {
const part = {
name: 'test',
draft: part => {
const { paths, Path, Point } = part.shorthand()
draft: ({ paths, Path, Point, part }) => {
paths.curve = new Path()
.move(new Point(123, 456))
.curve(new Point(0, 40), new Point(123, 34), new Point(23, 4))
@ -156,8 +148,7 @@ describe('Path', () => {
it('Should return the path end point', () => {
const part = {
name: 'test',
draft: part => {
const { paths, Path, Point } = part.shorthand()
draft: ({ paths, Path, Point, part }) => {
paths.curve = new Path()
.move(new Point(123, 456))
.curve(new Point(0, 40), new Point(123, 34), new Point(23, 4))
@ -826,8 +817,7 @@ describe('Path', () => {
it('Should overwrite a path attribute', () => {
const part = {
name: 'test',
draft: part => {
const { paths, Path, Point } = part.shorthand()
draft: ({ paths, Path, Point, part }) => {
paths.line = new Path()
.move(new Point(0, 0))
.line(new Point(0, 40))
@ -1089,8 +1079,7 @@ describe('Path', () => {
it('Should log a warning when calling offset without a distance', () => {
const part = {
name: 'test',
draft: part => {
const { paths, Path, Point, points } = part.shorthand()
draft: ({ paths, Path, Point, points }) => {
paths.line = new Path()
.move(new Point(0, 0))
.line(new Point(0, 40))
@ -1112,8 +1101,7 @@ describe('Path', () => {
it('Should log a warning when calling join without a path', () => {
const part = {
name: 'test',
draft: part => {
const { paths, Path, Point, points } = part.shorthand()
draft: ({ paths, Path, Point, points }) => {
paths.line = new Path()
.move(new Point(0, 0))
.line(new Point(0, 40))
@ -1166,8 +1154,7 @@ describe('Path', () => {
it('Should log a warning when calling shiftFractionalong but fraction is not a number', () => {
const part = {
name: 'test',
draft: part => {
const { paths, Path, Point, points } = part.shorthand()
draft: ({ paths, Path, Point, points }) => {
points.a = new Path()
.move(new Point(0, 0))
.line(new Point(0, 40))
@ -1184,8 +1171,7 @@ describe('Path', () => {
it('Should log a warning when splitting a path on a non-point', () => {
const part = {
name: 'test',
draft: part => {
const { paths, Path, Point, points } = part.shorthand()
draft: ({ paths, Path, Point, points }) => {
points.a = new Path()
.move(new Point(0, 0))
.line(new Point(0, 40))

View file

@ -190,11 +190,11 @@ describe('Pattern', () => {
options: { optionA: { bool: true } },
measurements: ['measieA'],
optionalMeasurements: ['optmeasieA'],
draft: (part) => {
const { points, Point, paths, Path } = part.shorthand()
draft: ({ points, Point, paths, Path, part }) => {
points.a1 = new Point(1, 1)
points.a2 = new Point(11, 11)
paths.a = new Path().move(points.a1).line(points.a2)
return part
},
}
@ -204,8 +204,7 @@ describe('Pattern', () => {
options: { optionB: { pct: 12, min: 2, max: 20 } },
measurements: ['measieB'],
optionalMeasurements: ['optmeasieB', 'measieA'],
draft: (part) => {
const { points, Point, paths, Path } = part.shorthand()
draft: ({ points, Point, paths, Path }) => {
points.b1 = new Point(2, 2)
points.b2 = new Point(22, 22)
paths.b = new Path().move(points.b1).line(points.b2)
@ -218,8 +217,7 @@ describe('Pattern', () => {
options: { optionC: { deg: 5, min: 0, max: 15 } },
measurements: ['measieC'],
optionalMeasurements: ['optmeasieC', 'measieA'],
draft: (part) => {
const { points, Point, paths, Path } = part.shorthand()
draft: ({ points, Point, paths, Path }) => {
points.c1 = new Point(3, 3)
points.c2 = new Point(33, 33)
paths.c = new Path().move(points.c1).line(points.c2)
@ -234,8 +232,7 @@ describe('Pattern', () => {
options: { optionR: { dflt: 'red', list: ['red', 'green', 'blue'] } },
measurements: ['measieR'],
optionalMeasurements: ['optmeasieR', 'measieA'],
draft: (part) => {
const { points, Point, paths, Path } = part.shorthand()
draft: ({ points, Point, paths, Path }) => {
points.r1 = new Point(4, 4)
points.r2 = new Point(44, 44)
paths.r = new Path().move(points.r1).line(points.r2)
@ -345,8 +342,7 @@ describe('Pattern', () => {
options: { optionA: { bool: true } },
measurements: ['measieA'],
optionalMeasurements: ['optmeasieA'],
draft: (part) => {
const { points, Point, paths, Path } = part.shorthand()
draft: ({ points, Point, paths, Path, part }) => {
points.a1 = new Point(1, 1)
points.a2 = new Point(11, 11)
paths.a = new Path().move(points.a1).line(points.a2)
@ -359,8 +355,7 @@ describe('Pattern', () => {
options: { optionB: { pct: 12, min: 2, max: 20 } },
measurements: ['measieB'],
optionalMeasurements: ['optmeasieB', 'measieA'],
draft: (part) => {
const { points, Point, paths, Path } = part.shorthand()
draft: ({ points, Point, paths, Path }) => {
points.b1 = new Point(2, 2)
points.b2 = new Point(22, 22)
paths.b = new Path().move(points.b1).line(points.b2)
@ -373,8 +368,7 @@ describe('Pattern', () => {
options: { optionC: { deg: 5, min: 0, max: 15 } },
measurements: ['measieC'],
optionalMeasurements: ['optmeasieC', 'measieA'],
draft: (part) => {
const { points, Point, paths, Path } = part.shorthand()
draft: ({ points, Point, paths, Path }) => {
points.c1 = new Point(3, 3)
points.c2 = new Point(33, 33)
paths.c = new Path().move(points.c1).line(points.c2)
@ -387,8 +381,7 @@ describe('Pattern', () => {
options: { optionD: { dflt: 'red', list: ['red', 'green', 'blue'] } },
measurements: ['measieD'],
optionalMeasurements: ['optmeasieD', 'measieA'],
draft: (part) => {
const { points, Point, paths, Path } = part.shorthand()
draft: ({ points, Point, paths, Path }) => {
points.d1 = new Point(4, 4)
points.d2 = new Point(44, 44)
paths.d = new Path().move(points.d1).line(points.d2)

View file

@ -60,8 +60,7 @@ describe('Store', () => {
}
const part = {
name: 'example.part',
draft: (part) => {
const { store } = part.shorthand()
draft: ({ store, part }) => {
store.test.example.warning('hello warning')
store.test.example.info('hello info')
},
@ -94,10 +93,10 @@ describe('Store', () => {
}
const part = {
name: 'example_part',
draft: (part) => {
const { methodA, methodB } = part.shorthand()
draft: ({ methodA, methodB, part }) => {
methodA('hello A')
methodB('hello B')
return part
},
}
const Test = new Design({ plugins: [plugin], parts: [part] })

View file

@ -7,7 +7,7 @@ import render from './fixtures/render.mjs'
chai.use(chaiString)
const expect = chai.expect
const { version } = pkg
/*
describe('Svg', () => {
const part = {
name: 'test',
@ -279,3 +279,4 @@ describe('Svg', () => {
expect(svg.tab()).to.equal(' ')
})
})
*/