1
0
Fork 0

chore(core): Use hide, not render

This commit is contained in:
Joost De Cock 2022-09-18 17:01:19 +02:00
parent 026560ef24
commit 5dc7401308
82 changed files with 337 additions and 293 deletions

View file

@ -30,8 +30,7 @@ export function Part() {
utils.__addNonEnumProp(this, 'hooks', new Hooks())
// Enumerable properties
this.render = true // FIXME: Replace render with hide
this.hide = false // FIXME: Replace render with hide
this.hidden = false
this.attributes = new Attributes()
this.points = {}
this.paths = {}
@ -72,6 +71,41 @@ Part.prototype.getId = function (prefix = '') {
return prefix + this.freeId
}
/**
* Hide the part
*
* @return {Part} part - The Part instance
*/
Part.prototype.hide = function () {
this.hidden = true
return this
}
/**
* Reveal the part - alias for part.unhide()
*
* @return {Part} part - The Part instance
*/
Part.prototype.reveal = function () {
this.unhide()
return this
}
/**
* Set the hidden attribute
*
* @param {boolean} hidden - The value to set the hidden property to
* @return {Part} this - The Part instance
*/
Part.prototype.setHidden = function (hidden = false) {
if (hidden) this.hidden = true
else this.hidden = false
return this
}
/** Returns an object with shorthand access for pattern design */
/**
* Returns an object that will be passed to draft method to be destructured
@ -83,19 +117,19 @@ 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 = {
complete,
hide: this.hide,
log: this.context.store.log,
macro: this.__macroClosure(),
paperless,
part: this,
reveal: this.reveal,
sa,
scale: this.context.settings?.scale,
store: this.context.store,
macro: this.__macroClosure(),
unhide: this.unhide,
units: this.__unitsClosure(),
utils: utils,
complete,
paperless,
events: this.context.events,
log: this.context.store.log,
addCut: this.addCut,
removeCut: this.removeCut,
}
// Add top-level store methods and add a part name parameter
const partName = this.name
@ -170,6 +204,17 @@ Part.prototype.shorthand = function () {
return shorthand
}
/**
* Unhide the part - alias for part.reveal()
*
* @return {Part} part - The Part instance
*/
Part.prototype.unhide = function () {
this.hidden = false
return this
}
/**
* Returns a value formatted for units set in settings
*
@ -198,7 +243,7 @@ Part.prototype.__boundary = function () {
for (let key in this.paths) {
try {
let path = this.paths[key].__boundary()
if (path.render) {
if (!path.hidden) {
if (path.topLeft.x < topLeft.x) topLeft.x = path.topLeft.x
if (path.topLeft.y < topLeft.y) topLeft.y = path.topLeft.y
if (path.bottomRight.x > bottomRight.x) bottomRight.x = path.bottomRight.x

View file

@ -24,12 +24,11 @@ import {
*/
export function Path() {
// Enumerable properties
this.hide = false
this.hidden = false
this.ops = []
this.attributes = new Attributes()
this.topLeft = false
this.bottomRight = false
this.render = true
return this
}
@ -158,7 +157,7 @@ Path.prototype.bbox = function () {
* @return {Path} clone - A clone of this Path instance
*/
Path.prototype.clone = function () {
let clone = new Path().__withLog(this.log).setRender(this.render)
let clone = new Path().__withLog(this.log).setHidden(this.hidden)
if (this.topLeft) clone.topLeft = this.topLeft.clone()
else clone.topLeft = false
if (this.bottomRight) clone.bottomRight = this.bottomRight.clone()
@ -313,6 +312,17 @@ Path.prototype.end = function () {
else return op.to
}
/**
* Hide the path
*
* @return {Path} path - The Path instance
*/
Path.prototype.hide = function () {
this.hidden = true
return this
}
/**
* Replace a noop operation with the ops from path
*
@ -521,6 +531,17 @@ Path.prototype.offset = function (distance) {
return __pathOffset(this, distance, this.log)
}
/**
* Reveal the path - alias for path.unhide()
*
* @return {Path} path - The Path instance
*/
Path.prototype.reveal = function () {
this.unhide()
return this
}
/**
* Returns a reversed version of this Path
*
@ -590,10 +611,15 @@ Path.prototype.setClass = function (className = false) {
return this
}
/** FIXME: This should go */
Path.prototype.setRender = function (render = true) {
if (render) this.render = true
else this.render = false
/**
* Set the hidden attribute
*
* @param {boolean} hidden - The value to set the hidden property to
* @return {object} this - The Path instance
*/
Path.prototype.setHidden = function (hidden = false) {
if (hidden) this.hidden = true
else this.hidden = false
return this
}
@ -833,6 +859,17 @@ Path.prototype.trim = function () {
return this
}
/**
* Unhide the path - alias for path.reveal()
*
* @return {Path} path - The Path instance
*/
Path.prototype.unhide = function () {
this.hidden = false
return this
}
//////////////////////////////////////////////
// PRIVATE METHODS //
//////////////////////////////////////////////
@ -956,9 +993,6 @@ Path.prototype.__withLog = function (log = false) {
return this
}
//////////////////////////////////////////////
// PUBLIC STATIC METHODS //
//////////////////////////////////////////////
@ -1217,8 +1251,9 @@ function shiftAlongBezier(distance, bezier, steps = false) {
.curve(
new Point(...Object.values(bezier.points[1])),
new Point(...Object.values(bezier.points[2])),
new Point(...Object.values(bezier.points[3])),
).roughLength()
new Point(...Object.values(bezier.points[3]))
)
.roughLength()
if (rlen < 2) steps = 20
else if (rlen < 10) steps = 40
else if (rlen < 100) steps = 100

View file

@ -128,19 +128,19 @@ Pattern.prototype.draft = function () {
}
} else this.stores[set].log.error(`Unable to draft pattern. Part.draft() is not callable`)
try {
this.parts[set][partName].render =
this.parts[set][partName].render === false ? false : this.__wants(partName, set)
this.parts[set][partName].hidden =
this.parts[set][partName].hidden === true ? true : !this.__wants(partName, set)
} catch (err) {
this.stores[set].log.error([
`Unable to set \`render\` property on part \`${partName}\``,
`Unable to set \`hidden\` property on part \`${partName}\``,
err,
])
}
} else {
this.stores[set].log.debug(
`Part \`${partName}\` is not needed. Skipping draft and setting render to \`false\``
`Part \`${partName}\` is not needed. Skipping draft and setting hidden to \`true\``
)
this.parts[set][partName].render = false
this.parts[set][partName].hidden = true
}
}
this.__runHooks('postDraft')
@ -149,6 +149,15 @@ Pattern.prototype.draft = function () {
return this
}
/**
* Return the initialized configuration
*
* @return {object} config - The initialized config
*/
Pattern.prototype.getConfig = function () {
return this.init().config
}
/** Returns props required to render this pattern through
* an external renderer (eg. a React component)
*
@ -176,7 +185,7 @@ Pattern.prototype.getRenderProps = function () {
}))
props.parts = {}
for (let p in this.parts) {
if (this.parts[p].render) {
if (!this.parts[p].hidden) {
props.parts[p] = {
paths: this.parts[p].paths,
points: this.parts[p].points,

View file

@ -57,16 +57,16 @@ Svg.prototype.render = function (pattern) {
this.tail = this.__renderTail()
this.svg = ''
this.layout = {} // Reset layout
for (let partId in pattern.parts) {
let part = pattern.parts[partId]
if (part.render) {
let partSvg = this.__renderPart(part)
this.layout[partId] = {
svg: partSvg,
transform: part.attributes.getAsArray('transform'),
for (let stackId in pattern.stacks) {
const stack = pattern.stacks[stackId]
if (!stack.hidden) {
const stackSvg = this.__renderStack(stack)
this.layout[stackId] = {
svg: stackSvg,
transform: stack.attributes.getAsArray('transform'),
}
this.svg += this.__openGroup(`${this.idPrefix}part-${partId}`, part.attributes)
this.svg += partSvg
this.svg += this.__openGroup(`${this.idPrefix}stack-${stackId}`, stack.attributes)
this.svg += stackSvg
this.svg += this.__closeGroup()
}
}
@ -279,11 +279,11 @@ Svg.prototype.__renderPathText = function (path) {
* @param {Part} part - The Part instance to render
* @return {string} svg - The SVG markup for the Part object
*/
Svg.prototype.__renderPart = function (part) {
let svg = ''
Svg.prototype.__renderPart = function (part, partId) {
let svg = this.__openGroup(`${this.idPrefix}part-${partId}`, part.attributes)
for (let key in part.paths) {
let path = part.paths[key]
if (path.render) svg += this.__renderPath(path)
if (!path.hidden) svg += this.__renderPath(path)
}
for (let key in part.points) {
if (part.points[key].attributes.get('data-text')) {
@ -297,6 +297,7 @@ Svg.prototype.__renderPart = function (part) {
let snippet = part.snippets[key]
svg += this.__renderSnippet(snippet, part)
}
svg += this.__closeGroup()
return svg
}
@ -346,6 +347,20 @@ Svg.prototype.__renderSnippet = function (snippet) {
return svg
}
/**
* Returns SVG markup for a Stack object
*
* @private
* @param {Stack} stack - The Stack instance to render
* @return {string} svg - The SVG markup for the Stack object
*/
Svg.prototype.__renderStack = function (stack) {
let svg = ''
for (const part of stack.parts) svg += this.__renderPart(part)
return svg
}
/**
* Returns SVG markup for the style block
*

View file

@ -689,9 +689,9 @@ describe('Path', () => {
expect(p1.log()).to.equal('hello')
})
it('Should set render to true/false', () => {
const p1 = new Path().setRender(false)
expect(p1.render).to.equal(false)
it('Should set hidden to true/false', () => {
const p1 = new Path().setHidden(true)
expect(p1.hidden).to.equal(true)
})
it('Should set class with setClass', () => {