[vercel skip] chore (core) document and test Stack.home
This commit is contained in:
parent
444d82c527
commit
042d4e71a9
2 changed files with 58 additions and 14 deletions
|
@ -52,30 +52,27 @@ Stack.prototype.home = function () {
|
||||||
for (const part of this.getPartList()) {
|
for (const part of this.getPartList()) {
|
||||||
part.__boundary()
|
part.__boundary()
|
||||||
|
|
||||||
const transforms = part.attributes.get('transform')
|
// get all corners of the part's bounds
|
||||||
let tl = part.topLeft || this.topLeft
|
let tl = part.topLeft || this.topLeft
|
||||||
let br = part.bottomRight || this.bottomRight
|
let br = part.bottomRight || this.bottomRight
|
||||||
let tr = new Point(br.x, tl.y)
|
let tr = new Point(br.x, tl.y)
|
||||||
let bl = new Point(tl.x, br.y)
|
let bl = new Point(tl.x, br.y)
|
||||||
|
|
||||||
|
// if there are transforms on the part, apply them to the corners so that we have the correct bounds
|
||||||
|
const transforms = part.attributes.getAsArray('transform')
|
||||||
if (transforms) {
|
if (transforms) {
|
||||||
const combinedTransform =
|
const combinedTransform = utils.combineTransforms(transforms)
|
||||||
typeof transforms === 'string' ? transforms : utils.combineTransforms(transforms)
|
|
||||||
|
|
||||||
tl = utils.applyTransformToPoint(combinedTransform, part.topLeft.copy())
|
tl = utils.applyTransformToPoint(combinedTransform, tl.copy())
|
||||||
br = utils.applyTransformToPoint(combinedTransform, part.bottomRight.copy())
|
br = utils.applyTransformToPoint(combinedTransform, br.copy())
|
||||||
bl = utils.applyTransformToPoint(
|
tr = utils.applyTransformToPoint(combinedTransform, tr.copy())
|
||||||
combinedTransform,
|
bl = utils.applyTransformToPoint(combinedTransform, bl.copy())
|
||||||
new Point(part.topLeft.x, part.bottomRight.y)
|
|
||||||
)
|
|
||||||
tr = utils.applyTransformToPoint(
|
|
||||||
combinedTransform,
|
|
||||||
new Point(part.bottomRight.x, part.topLeft.y)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get the top left, the minimum x and y values of any corner
|
||||||
this.topLeft.x = Math.min(this.topLeft.x, tl.x, br.x, bl.x, tr.x)
|
this.topLeft.x = Math.min(this.topLeft.x, tl.x, br.x, bl.x, tr.x)
|
||||||
this.topLeft.y = Math.min(this.topLeft.y, tl.y, br.y, bl.y, tr.y)
|
this.topLeft.y = Math.min(this.topLeft.y, tl.y, br.y, bl.y, tr.y)
|
||||||
|
// get the bottom right, the maximum x and y values of any corner
|
||||||
this.bottomRight.x = Math.max(this.bottomRight.x, tl.x, br.x, bl.x, tr.x)
|
this.bottomRight.x = Math.max(this.bottomRight.x, tl.x, br.x, bl.x, tr.x)
|
||||||
this.bottomRight.y = Math.max(this.bottomRight.y, tl.y, br.y, bl.y, tr.y)
|
this.bottomRight.y = Math.max(this.bottomRight.y, tl.y, br.y, bl.y, tr.y)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import chai from 'chai'
|
import chai from 'chai'
|
||||||
import { Design } from '../src/index.mjs'
|
import { Design, Point, Attributes } from '../src/index.mjs'
|
||||||
|
import { Stack } from '../src/stack.mjs'
|
||||||
|
|
||||||
const expect = chai.expect
|
const expect = chai.expect
|
||||||
|
|
||||||
|
@ -173,6 +174,52 @@ describe('Stacks', () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('Stack.prototype.home()', () => {
|
||||||
|
function dummyPart(tl, br) {
|
||||||
|
return {
|
||||||
|
__boundary: () => null,
|
||||||
|
topLeft: new Point(tl.x, tl.y),
|
||||||
|
bottomRight: new Point(br.x, br.y),
|
||||||
|
attributes: new Attributes(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
it('Should calculate the bounds using all part bounds', () => {
|
||||||
|
const stack = new Stack('test')
|
||||||
|
stack.context = { settings: [{ margin: 0 }] }
|
||||||
|
const expectedTlX = -1
|
||||||
|
const expectedTlY = -2
|
||||||
|
const expectedBrX = 20
|
||||||
|
const expectedBrY = 20
|
||||||
|
|
||||||
|
stack.addPart(dummyPart({ x: 0, y: expectedTlY }, { x: expectedBrX, y: 10 }))
|
||||||
|
|
||||||
|
stack.addPart(dummyPart({ x: expectedTlX, y: 0 }, { x: expectedBrX, y: expectedBrY }))
|
||||||
|
|
||||||
|
stack.home()
|
||||||
|
expect(stack.topLeft.x).to.equal(expectedTlX)
|
||||||
|
expect(stack.topLeft.y).to.equal(expectedTlY)
|
||||||
|
expect(stack.bottomRight.x).to.equal(expectedBrX)
|
||||||
|
expect(stack.bottomRight.y).to.equal(expectedBrY)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Should calculate the bounds using all transformed part bounds', () => {
|
||||||
|
const stack = new Stack('test')
|
||||||
|
stack.context = { settings: [{ margin: 0 }] }
|
||||||
|
|
||||||
|
const part1 = dummyPart({ x: 0, y: 0 }, { x: 10, y: 10 })
|
||||||
|
part1.attributes.add('transform', 'scale(1, -1)')
|
||||||
|
part1.attributes.add('transform', 'rotate(45)')
|
||||||
|
stack.addPart(part1)
|
||||||
|
|
||||||
|
stack.home()
|
||||||
|
expect(stack.topLeft.x).to.equal(-7.071067811865475)
|
||||||
|
expect(stack.topLeft.y).to.equal(-14.142135623730951)
|
||||||
|
expect(stack.bottomRight.x).to.equal(7.0710678118654755)
|
||||||
|
expect(stack.bottomRight.y).to.equal(0)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
it('Should get the anchor for the stack', () => {
|
it('Should get the anchor for the stack', () => {
|
||||||
const part = {
|
const part = {
|
||||||
name: 'test',
|
name: 'test',
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue