1
0
Fork 0

chore (core) cleanup

This commit is contained in:
Enoch Riese 2023-04-18 18:47:49 -04:00
parent 13ec5e43e5
commit 8ccab0df21
8 changed files with 97 additions and 77 deletions

View file

@ -9,7 +9,7 @@ import { __loadPatternDefaults } from '../config.mjs'
import { PatternConfig } from './pattern-config.mjs'
import { PatternDrafter } from './pattern-drafter.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 cloneDeep from 'lodash.clonedeep'

View file

@ -6,25 +6,6 @@ export function PatternDrafter(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
*
@ -66,6 +47,10 @@ PatternDrafter.prototype.draft = function () {
}
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.__createPartForSet(partName, set)
@ -77,7 +62,8 @@ PatternDrafter.prototype.draftPartForSet = function (partName, set) {
return
}
this.activePart = partName
this.pattern.activePart = partName
this.activeStore.set('activePart', partName)
try {
this.pattern.__runHooks('prePartDraft')
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) {
this.activeSet = set
this.pattern.activeSet = set
this.activeSettings = this.pattern.settings[set]
this.activeStore = this.pattern.setStores[set]
}

View file

@ -36,38 +36,36 @@ PatternRenderer.prototype.getRenderProps = function () {
autoLayout: this.pattern.autoLayout,
settings: this.pattern.settings,
parts: [],
stacks: {},
}
for (const set of this.pattern.parts) {
const setParts = {}
for (let p in set) {
if (!set[p].hidden) {
setParts[p] = {
...set[p].asProps(),
store: this.pattern.setStores[set[p].set],
for (const partSet of this.pattern.parts) {
const setPartProps = {}
for (let partName in partSet) {
const part = partSet[partName]
if (!part.hidden) {
setPartProps[partName] = {
...partSet[partName].asProps(),
store: this.pattern.setStores[part.set],
}
} else if (this.pattern.setStores[set.set]) {
this.pattern.setStores[set.set].log.info(
`Part${p} is hidden in set ${set.set}. Not adding to render props`
} else if (this.pattern.setStores[part.set]) {
this.pattern.setStores[part.set].log.info(
`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) {
if (!this.pattern.__isStackHidden(s)) {
props.stacks[s] = this.pattern.stacks[s].asProps()
} else this.pattern.store.log.info(`Stack ${s} is hidden. Skipping in render props.`)
}
props.logs = {
pattern: this.pattern.store.logs,
sets: this.pattern.setStores.map((store) => ({
debug: store.logs.debug,
info: store.logs.info,
error: store.logs.error,
warning: store.logs.warning,
})),
sets: this.pattern.setStores.map((store) => store.logs),
}
this.svg.__runHooks('postRender')
@ -107,7 +105,7 @@ PatternRenderer.prototype.__stack = function () {
*/
PatternRenderer.prototype.__pack = function () {
this.pattern.__runHooks('preLayout')
const { settings, setStores, parts } = this.pattern
const { settings, setStores, activeSet } = this.pattern
for (const set in settings) {
if (setStores[set].logs.error.length > 0) {
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')
if (!this.pattern.__isStackHidden(key)) {
stack.home()
if (settings[0].layout === true)
if (settings[activeSet].layout === true)
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
let size = pack(bins, { inPlace: true, maxWidth: settings[0].maxWidth })
for (let bin of bins) {
this.autoLayout.stacks[bin.id] = { move: {} }
let stack = this.stacks[bin.id]
if (bin.x !== 0 || bin.y !== 0) {
stack.attr('transform', `translate(${bin.x}, ${bin.y})`)
}
this.autoLayout.stacks[bin.id].move = {
x: bin.x + stack.layout.move.x,
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)
}
const packedLayout =
typeof settings[activeSet].layout === 'object' ? settings[activeSet].layout : this.autoLayout
this.width = packedLayout.width
this.height = packedLayout.height
for (let stackId of Object.keys(packedLayout.stacks)) {
// Some parts are added by late-stage plugins
if (this.stacks[stackId]) {
let transforms = packedLayout.stacks[stackId]
this.stacks[stackId].generateTransform(transforms)
}
}