chore (core) cleanup
This commit is contained in:
parent
13ec5e43e5
commit
8ccab0df21
8 changed files with 97 additions and 77 deletions
|
@ -9,7 +9,7 @@ import { __loadPatternDefaults } from '../config.mjs'
|
||||||
import { PatternConfig } from './pattern-config.mjs'
|
import { PatternConfig } from './pattern-config.mjs'
|
||||||
import { PatternDrafter } from './pattern-drafter.mjs'
|
import { PatternDrafter } from './pattern-drafter.mjs'
|
||||||
import { PatternSampler } from './pattern-sampler.mjs'
|
import { PatternSampler } from './pattern-sampler.mjs'
|
||||||
import { PatternPlugins, getPluginName } from './pattern-plugins.mjs'
|
import { PatternPlugins } from './pattern-plugins.mjs'
|
||||||
import { PatternRenderer } from './pattern-renderer.mjs'
|
import { PatternRenderer } from './pattern-renderer.mjs'
|
||||||
import cloneDeep from 'lodash.clonedeep'
|
import cloneDeep from 'lodash.clonedeep'
|
||||||
|
|
||||||
|
|
|
@ -6,25 +6,6 @@ export function PatternDrafter(pattern) {
|
||||||
this.pattern = pattern
|
this.pattern = pattern
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.defineProperty(PatternDrafter.prototype, 'activeSet', {
|
|
||||||
get: function () {
|
|
||||||
return this.pattern.activeSet
|
|
||||||
},
|
|
||||||
set: function (newVal) {
|
|
||||||
this.pattern.activeSet = newVal
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
Object.defineProperty(PatternDrafter.prototype, 'activePart', {
|
|
||||||
get: function () {
|
|
||||||
return this.pattern.activePart
|
|
||||||
},
|
|
||||||
set: function (newVal) {
|
|
||||||
this.pattern.activePart = newVal
|
|
||||||
this.activeStore.set('activePart', newVal)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Drafts this pattern, aka the raison d'etre of FreeSewing
|
* Drafts this pattern, aka the raison d'etre of FreeSewing
|
||||||
*
|
*
|
||||||
|
@ -66,6 +47,10 @@ PatternDrafter.prototype.draft = function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
PatternDrafter.prototype.draftPartForSet = function (partName, set) {
|
PatternDrafter.prototype.draftPartForSet = function (partName, set) {
|
||||||
|
// gotta protect against attacks
|
||||||
|
if (set === '__proto__') {
|
||||||
|
throw new Error('malicious attempt at altering Object.prototype. Stopping action')
|
||||||
|
}
|
||||||
this.__useSet(set)
|
this.__useSet(set)
|
||||||
this.__createPartForSet(partName, set)
|
this.__createPartForSet(partName, set)
|
||||||
|
|
||||||
|
@ -77,7 +62,8 @@ PatternDrafter.prototype.draftPartForSet = function (partName, set) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
this.activePart = partName
|
this.pattern.activePart = partName
|
||||||
|
this.activeStore.set('activePart', partName)
|
||||||
try {
|
try {
|
||||||
this.pattern.__runHooks('prePartDraft')
|
this.pattern.__runHooks('prePartDraft')
|
||||||
const result = configPart.draft(this.pattern.parts[set][partName].shorthand())
|
const result = configPart.draft(this.pattern.parts[set][partName].shorthand())
|
||||||
|
@ -222,7 +208,7 @@ PatternDrafter.prototype.__snappedPercentageOption = function (optionName, set)
|
||||||
}
|
}
|
||||||
|
|
||||||
PatternDrafter.prototype.__useSet = function (set = 0) {
|
PatternDrafter.prototype.__useSet = function (set = 0) {
|
||||||
this.activeSet = set
|
this.pattern.activeSet = set
|
||||||
this.activeSettings = this.pattern.settings[set]
|
this.activeSettings = this.pattern.settings[set]
|
||||||
this.activeStore = this.pattern.setStores[set]
|
this.activeStore = this.pattern.setStores[set]
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,38 +36,36 @@ PatternRenderer.prototype.getRenderProps = function () {
|
||||||
autoLayout: this.pattern.autoLayout,
|
autoLayout: this.pattern.autoLayout,
|
||||||
settings: this.pattern.settings,
|
settings: this.pattern.settings,
|
||||||
parts: [],
|
parts: [],
|
||||||
|
stacks: {},
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const set of this.pattern.parts) {
|
for (const partSet of this.pattern.parts) {
|
||||||
const setParts = {}
|
const setPartProps = {}
|
||||||
for (let p in set) {
|
for (let partName in partSet) {
|
||||||
if (!set[p].hidden) {
|
const part = partSet[partName]
|
||||||
setParts[p] = {
|
if (!part.hidden) {
|
||||||
...set[p].asProps(),
|
setPartProps[partName] = {
|
||||||
store: this.pattern.setStores[set[p].set],
|
...partSet[partName].asProps(),
|
||||||
|
store: this.pattern.setStores[part.set],
|
||||||
}
|
}
|
||||||
} else if (this.pattern.setStores[set.set]) {
|
} else if (this.pattern.setStores[part.set]) {
|
||||||
this.pattern.setStores[set.set].log.info(
|
this.pattern.setStores[part.set].log.info(
|
||||||
`Part${p} is hidden in set ${set.set}. Not adding to render props`
|
`Part ${partName} is hidden in set ${part.set}. Not adding to render props`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
props.parts.push(setParts)
|
props.parts.push(setPartProps)
|
||||||
}
|
}
|
||||||
props.stacks = {}
|
|
||||||
for (let s in this.pattern.stacks) {
|
for (let s in this.pattern.stacks) {
|
||||||
if (!this.pattern.__isStackHidden(s)) {
|
if (!this.pattern.__isStackHidden(s)) {
|
||||||
props.stacks[s] = this.pattern.stacks[s].asProps()
|
props.stacks[s] = this.pattern.stacks[s].asProps()
|
||||||
} else this.pattern.store.log.info(`Stack ${s} is hidden. Skipping in render props.`)
|
} else this.pattern.store.log.info(`Stack ${s} is hidden. Skipping in render props.`)
|
||||||
}
|
}
|
||||||
|
|
||||||
props.logs = {
|
props.logs = {
|
||||||
pattern: this.pattern.store.logs,
|
pattern: this.pattern.store.logs,
|
||||||
sets: this.pattern.setStores.map((store) => ({
|
sets: this.pattern.setStores.map((store) => store.logs),
|
||||||
debug: store.logs.debug,
|
|
||||||
info: store.logs.info,
|
|
||||||
error: store.logs.error,
|
|
||||||
warning: store.logs.warning,
|
|
||||||
})),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.svg.__runHooks('postRender')
|
this.svg.__runHooks('postRender')
|
||||||
|
@ -107,7 +105,7 @@ PatternRenderer.prototype.__stack = function () {
|
||||||
*/
|
*/
|
||||||
PatternRenderer.prototype.__pack = function () {
|
PatternRenderer.prototype.__pack = function () {
|
||||||
this.pattern.__runHooks('preLayout')
|
this.pattern.__runHooks('preLayout')
|
||||||
const { settings, setStores, parts } = this.pattern
|
const { settings, setStores, activeSet } = this.pattern
|
||||||
for (const set in settings) {
|
for (const set in settings) {
|
||||||
if (setStores[set].logs.error.length > 0) {
|
if (setStores[set].logs.error.length > 0) {
|
||||||
setStores[set].log.warning(`One or more errors occured. Not packing pattern parts`)
|
setStores[set].log.warning(`One or more errors occured. Not packing pattern parts`)
|
||||||
|
@ -123,39 +121,37 @@ PatternRenderer.prototype.__pack = function () {
|
||||||
stack.attributes.remove('transform')
|
stack.attributes.remove('transform')
|
||||||
if (!this.pattern.__isStackHidden(key)) {
|
if (!this.pattern.__isStackHidden(key)) {
|
||||||
stack.home()
|
stack.home()
|
||||||
if (settings[0].layout === true)
|
if (settings[activeSet].layout === true)
|
||||||
bins.push({ id: key, width: stack.width, height: stack.height })
|
bins.push({ id: key, width: stack.width, height: stack.height })
|
||||||
else {
|
}
|
||||||
if (this.width < stack.width) this.width = stack.width
|
}
|
||||||
if (this.height < stack.height) this.height = stack.height
|
if (settings[activeSet].layout === true) {
|
||||||
|
// some plugins will add a width constraint to the settings, but we can safely pass undefined if not
|
||||||
|
let size = pack(bins, { inPlace: true, maxWidth: settings[0].maxWidth })
|
||||||
|
this.autoLayout.width = size.width
|
||||||
|
this.autoLayout.height = size.height
|
||||||
|
|
||||||
|
for (let bin of bins) {
|
||||||
|
let stack = this.stacks[bin.id]
|
||||||
|
this.autoLayout.stacks[bin.id] = {
|
||||||
|
move: {
|
||||||
|
x: bin.x + stack.layout.move.x,
|
||||||
|
y: bin.y + stack.layout.move.y,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (settings[0].layout === true) {
|
|
||||||
// some plugins will add a width constraint to the settings, but we can safely pass undefined if not
|
const packedLayout =
|
||||||
let size = pack(bins, { inPlace: true, maxWidth: settings[0].maxWidth })
|
typeof settings[activeSet].layout === 'object' ? settings[activeSet].layout : this.autoLayout
|
||||||
for (let bin of bins) {
|
|
||||||
this.autoLayout.stacks[bin.id] = { move: {} }
|
this.width = packedLayout.width
|
||||||
let stack = this.stacks[bin.id]
|
this.height = packedLayout.height
|
||||||
if (bin.x !== 0 || bin.y !== 0) {
|
for (let stackId of Object.keys(packedLayout.stacks)) {
|
||||||
stack.attr('transform', `translate(${bin.x}, ${bin.y})`)
|
// Some parts are added by late-stage plugins
|
||||||
}
|
if (this.stacks[stackId]) {
|
||||||
this.autoLayout.stacks[bin.id].move = {
|
let transforms = packedLayout.stacks[stackId]
|
||||||
x: bin.x + stack.layout.move.x,
|
this.stacks[stackId].generateTransform(transforms)
|
||||||
y: bin.y + stack.layout.move.y,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.width = size.width
|
|
||||||
this.height = size.height
|
|
||||||
} else if (typeof settings[0].layout === 'object') {
|
|
||||||
this.width = settings[0].layout.width
|
|
||||||
this.height = settings[0].layout.height
|
|
||||||
for (let stackId of Object.keys(settings[0].layout.stacks)) {
|
|
||||||
// Some parts are added by late-stage plugins
|
|
||||||
if (this.stacks[stackId]) {
|
|
||||||
let transforms = settings[this.activeStack || 0].layout.stacks[stackId]
|
|
||||||
this.stacks[stackId].generateTransform(transforms)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -320,7 +320,7 @@ export const generateStackTransform = (
|
||||||
|
|
||||||
// add the scaling to the transforms
|
// add the scaling to the transforms
|
||||||
if (scaleX + scaleY < 2) {
|
if (scaleX + scaleY < 2) {
|
||||||
transforms.push(`scale(${scaleX} ${scaleY})`)
|
transforms.push(`scale(${scaleX}, ${scaleY})`)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rotate) {
|
if (rotate) {
|
||||||
|
@ -331,11 +331,11 @@ export const generateStackTransform = (
|
||||||
}
|
}
|
||||||
|
|
||||||
// add the rotation around the center to the transforms
|
// add the rotation around the center to the transforms
|
||||||
transforms.push(`rotate(${rotate} ${center.x} ${center.y})`)
|
transforms.push(`rotate(${rotate}, ${center.x}, ${center.y})`)
|
||||||
}
|
}
|
||||||
|
|
||||||
// put the translation before any other transforms to avoid having to make complex calculations once the matrix has been rotated or scaled
|
// put the translation before any other transforms to avoid having to make complex calculations once the matrix has been rotated or scaled
|
||||||
if (xTotal !== 0 || yTotal !== 0) transforms.unshift(`translate(${xTotal} ${yTotal})`)
|
if (xTotal !== 0 || yTotal !== 0) transforms.unshift(`translate(${xTotal}, ${yTotal})`)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
transform: transforms.join(' '),
|
transform: transforms.join(' '),
|
||||||
|
|
|
@ -159,7 +159,6 @@ describe('Pattern', () => {
|
||||||
expect(pattern.setStores[0].logs.error[0][0]).to.equal('Unable to draft part `test` (set 0)')
|
expect(pattern.setStores[0].logs.error[0][0]).to.equal('Unable to draft part `test` (set 0)')
|
||||||
})
|
})
|
||||||
|
|
||||||
// FIXME: Add assertions here
|
|
||||||
it('Handle layout object', () => {
|
it('Handle layout object', () => {
|
||||||
const part = {
|
const part = {
|
||||||
name: 'test',
|
name: 'test',
|
||||||
|
@ -174,7 +173,7 @@ describe('Pattern', () => {
|
||||||
layout: { stacks: { test: { flipX: true } }, width: 300, height: 400 },
|
layout: { stacks: { test: { flipX: true } }, width: 300, height: 400 },
|
||||||
})
|
})
|
||||||
const props = pattern.draft().getRenderProps()
|
const props = pattern.draft().getRenderProps()
|
||||||
expect(props.stacks.test.attributes.get('transform')).to.equal('scale(-1 1)')
|
expect(props.stacks.test.attributes.get('transform')).to.equal('scale(-1, 1)')
|
||||||
expect(props.width).to.equal(300)
|
expect(props.width).to.equal(300)
|
||||||
expect(props.height).to.equal(400)
|
expect(props.height).to.equal(400)
|
||||||
})
|
})
|
||||||
|
|
39
packages/core/tests/pattern-renderer.test.mjs
Normal file
39
packages/core/tests/pattern-renderer.test.mjs
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
import chai from 'chai'
|
||||||
|
import { Design } from '../src/index.mjs'
|
||||||
|
|
||||||
|
const expect = chai.expect
|
||||||
|
|
||||||
|
describe('Pattern Rendering', () => {
|
||||||
|
describe('Pattern.prototype.getRenderProps()', () => {
|
||||||
|
describe('Hidden parts and stacks', () => {
|
||||||
|
const part = {
|
||||||
|
name: 'test',
|
||||||
|
draft: ({ part }) => {
|
||||||
|
part.hide()
|
||||||
|
return part
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
const design = new Design({ parts: [part] })
|
||||||
|
const pattern = new design({})
|
||||||
|
const props = pattern.draft().getRenderProps()
|
||||||
|
|
||||||
|
it('Should not include hidden parts', () => {
|
||||||
|
expect(props.parts[0]).not.to.have.property('test')
|
||||||
|
})
|
||||||
|
it('Should log that it has skipped a hidden part', () => {
|
||||||
|
expect(props.logs.sets[0].info).to.include(
|
||||||
|
'Part test is hidden in set 0. Not adding to render props'
|
||||||
|
)
|
||||||
|
})
|
||||||
|
it('Should not include hidden stacks', () => {
|
||||||
|
expect(props.stacks).not.to.have.property('test')
|
||||||
|
})
|
||||||
|
it('Should log that it has skipped a hidden stack', () => {
|
||||||
|
expect(props.logs.pattern.info).to.include(
|
||||||
|
'Stack test is hidden. Skipping in render props.'
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
|
@ -170,7 +170,7 @@ describe('Stacks', () => {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
expect(pattern.stacks.test.attributes.list.transform.length).to.equal(1)
|
expect(pattern.stacks.test.attributes.list.transform.length).to.equal(1)
|
||||||
expect(pattern.stacks.test.attributes.list.transform[0]).to.equal('translate(10 20)')
|
expect(pattern.stacks.test.attributes.list.transform[0]).to.equal('translate(10, 20)')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -492,6 +492,6 @@ describe('Utils', () => {
|
||||||
const pattern = new design()
|
const pattern = new design()
|
||||||
const props = pattern.draft().getRenderProps()
|
const props = pattern.draft().getRenderProps()
|
||||||
const transform = generateStackTransform(30, 60, 90, true, true, props.stacks.test)
|
const transform = generateStackTransform(30, 60, 90, true, true, props.stacks.test)
|
||||||
expect(transform.transform).to.equal('translate(51 138) scale(-1 -1) rotate(90 10.5 39)')
|
expect(transform.transform).to.equal('translate(51, 138) scale(-1, -1) rotate(90, 10.5, 39)')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue