1
0
Fork 0

feat(core): Added support for custom sample styles

This commit is contained in:
Joost De Cock 2020-07-11 15:15:02 +02:00
parent e415816f66
commit 7d3815939b
7 changed files with 66 additions and 110 deletions

View file

@ -24,6 +24,8 @@ const Defs = (props) => {
<Snippets /> <Snippets />
<Grid units={props.units} /> <Grid units={props.units} />
{paperlessGrids} {paperlessGrids}
{props.extraDefs || '<!-- no extras -->'}
<g></g>
</defs> </defs>
) )
} }

View file

@ -20,6 +20,7 @@ const Draft = (props) => (
parts={props.parts} parts={props.parts}
paperless={props.settings.paperless} paperless={props.settings.paperless}
design={props.design || false} design={props.design || false}
extraDefs={props.extraDefs}
/> />
<g> <g>
{Object.keys(props.parts).map((name) => ( {Object.keys(props.parts).map((name) => (

View file

@ -6,7 +6,6 @@ export default function Hooks() {
postSample: [], postSample: [],
preRender: [], preRender: [],
postRender: [], postRender: [],
insertText: [], insertText: []
debug: []
} }
} }

View file

@ -32,24 +32,7 @@ Part.prototype.macroClosure = function(args) {
let self = this let self = this
let method = function (key, args) { let method = function (key, args) {
let macro = utils.macroName(key) let macro = utils.macroName(key)
if (typeof self[macro] === 'function') { if (typeof self[macro] === 'function') self[macro](args)
self[macro](args)
} else {
self.debug({
type: 'warning',
label: '🚨 Macro not found',
msg: `Macro ${key} is not registered`
})
}
}
return method
}
Part.prototype.debugClosure = function() {
let self = this
let method = function(data) {
self.debug(data)
} }
return method return method
@ -65,11 +48,6 @@ Part.prototype.runHooks = function(hookName, data = false) {
} }
} }
/** Debug method */
Part.prototype.debug = function(data) {
this.runHooks('debug', data)
}
/** Returns an unused ID */ /** Returns an unused ID */
Part.prototype.getId = function () { Part.prototype.getId = function () {
this.freeId += 1 this.freeId += 1
@ -149,7 +127,7 @@ Part.prototype.attr = function(name, value, overwrite = false) {
/** Copies point/path/snippet data from part orig into this */ /** Copies point/path/snippet data from part orig into this */
Part.prototype.inject = function (orig) { Part.prototype.inject = function (orig) {
const findBasePoint = p => { const findBasePoint = (p) => {
for (let i in orig.points) { for (let i in orig.points) {
if (orig.points[i] === p) return i if (orig.points[i] === p) return i
} }
@ -206,8 +184,7 @@ Part.prototype.shorthand = function() {
Path: this.Path, Path: this.Path,
Snippet: this.Snippet, Snippet: this.Snippet,
complete, complete,
paperless, paperless
debug: this.debugClosure()
} }
} }

View file

@ -182,7 +182,18 @@ Pattern.prototype.sampleRun = function (parts, anchors, run, runs, extraClass =
for (let j in this.parts[i].paths) { for (let j in this.parts[i].paths) {
parts[i].paths[j + '_' + run] = this.parts[i].paths[j] parts[i].paths[j + '_' + run] = this.parts[i].paths[j]
.clone() .clone()
.attr('style', sampleStyle(run, runs, this.settings.sample.styles)) .attr(
'style',
extraClass === 'sample-focus'
? this.settings.sample
? this.settings.sample.focusStyle || sampleStyle(run, runs)
: sampleStyle(run, runs)
: sampleStyle(
run,
runs,
this.settings.sample ? this.settings.sample.styles || false : false
)
)
.attr('data-sample-run', run) .attr('data-sample-run', run)
.attr('data-sample-runs', runs) .attr('data-sample-runs', runs)
if (this.parts[i].points.anchor) if (this.parts[i].points.anchor)
@ -216,11 +227,6 @@ Pattern.prototype.sampleOption = function (optionName) {
step = (option.max / factor - val) / 9 step = (option.max / factor - val) / 9
for (let run = 1; run < 11; run++) { for (let run = 1; run < 11; run++) {
this.settings.options[optionName] = val this.settings.options[optionName] = val
this.debug({
type: 'info',
label: '🏃🏿‍♀️ Sample run',
msg: `Sampling option ${optionName} with value ${round(val)}`
})
this.sampleRun(parts, anchors, run, 10) this.sampleRun(parts, anchors, run, 10)
val += step val += step
} }
@ -238,11 +244,6 @@ Pattern.prototype.sampleListOption = function (optionName) {
let runs = option.list.length let runs = option.list.length
for (let val of option.list) { for (let val of option.list) {
this.settings.options[optionName] = val this.settings.options[optionName] = val
this.debug({
type: 'info',
label: '🏃🏿‍♀️ Sample run',
msg: `Sampling option ${optionName} with value ${round(val)}`
})
this.sampleRun(parts, anchors, run, runs) this.sampleRun(parts, anchors, run, runs)
run++ run++
} }
@ -266,11 +267,6 @@ Pattern.prototype.sampleMeasurement = function (measurementName) {
val = val * 0.9 val = val * 0.9
for (let run = 1; run < 11; run++) { for (let run = 1; run < 11; run++) {
this.settings.measurements[measurementName] = val this.settings.measurements[measurementName] = val
this.debug({
type: 'info',
label: '🏃🏿‍♀️ Sample run',
msg: `Sampling option ${measurementName} with value ${round(val)}`
})
this.sampleRun(parts, anchors, run, 10) this.sampleRun(parts, anchors, run, 10)
val += step val += step
} }
@ -288,18 +284,18 @@ Pattern.prototype.sampleModels = function (models, focus = false) {
this.runHooks('preSample') this.runHooks('preSample')
let anchors = {} let anchors = {}
let parts = this.sampleParts() let parts = this.sampleParts()
let run = 0 // If there's a focus, do it first so it's at the bottom of the SVG
if (focus) {
this.settings.measurements = models[focus]
this.sampleRun(parts, anchors, -1, -1, 'sample-focus')
delete models[focus]
}
let run = -1
let runs = Object.keys(models).length let runs = Object.keys(models).length
for (let l in models) { for (let l in models) {
run++ run++
this.settings.measurements = models[l] this.settings.measurements = models[l]
this.debug({ this.sampleRun(parts, anchors, run, runs)
type: 'info',
label: '🏃🏿‍♀️ Sample run',
msg: `Sampling model ${l}`
})
let className = l === focus ? 'sample-focus' : ''
this.sampleRun(parts, anchors, run, runs, className)
} }
this.parts = parts this.parts = parts
this.runHooks('postSample') this.runHooks('postSample')
@ -307,11 +303,6 @@ Pattern.prototype.sampleModels = function (models, focus = false) {
return this return this
} }
/** Debug method, exposes debug hook */
Pattern.prototype.debug = function (data) {
this.runHooks('debug', data)
}
Pattern.prototype.render = function () { Pattern.prototype.render = function () {
this.svg = new Svg(this) this.svg = new Svg(this)
this.svg.hooks = this.hooks this.svg.hooks = this.hooks
@ -324,11 +315,6 @@ Pattern.prototype.on = function (hook, method, data) {
} }
Pattern.prototype.use = function (plugin, data) { Pattern.prototype.use = function (plugin, data) {
this.debug({
type: 'success',
label: '🔌 Plugin loaded',
msg: `${plugin.name} v${plugin.version}`
})
if (plugin.hooks) this.loadPluginHooks(plugin, data) if (plugin.hooks) this.loadPluginHooks(plugin, data)
if (plugin.macros) this.loadPluginMacros(plugin) if (plugin.macros) this.loadPluginMacros(plugin)

View file

@ -41,9 +41,6 @@ Svg.prototype.insertText = function(text) {
return text return text
} }
/** Debug method, exposes debug hook */
Svg.prototype.debug = function() {}
/** Renders a draft object as SVG */ /** Renders a draft object as SVG */
Svg.prototype.render = function (pattern) { Svg.prototype.render = function (pattern) {
this.idPrefix = pattern.settings.idPrefix this.idPrefix = pattern.settings.idPrefix
@ -181,9 +178,9 @@ Svg.prototype.renderPathText = function(path) {
else if (align && align.indexOf('right') > -1) offset = ' startOffset="100%" ' else if (align && align.indexOf('right') > -1) offset = ' startOffset="100%" '
let svg = this.nl() + '<text>' let svg = this.nl() + '<text>'
this.indent() this.indent()
svg += `<textPath xlink:href="#${path.attributes.get('id')}" ${offset}><tspan ${attributes}>${ svg += `<textPath xlink:href="#${path.attributes.get(
this.escapeText(this.text) 'id'
}</tspan></textPath>` )}" ${offset}><tspan ${attributes}>${this.escapeText(this.text)}</tspan></textPath>`
this.outdent() this.outdent()
svg += this.nl() + '</text>' svg += this.nl() + '</text>'
@ -214,8 +211,7 @@ Svg.prototype.renderText = function(point) {
svg += `<tspan x="${point.x}" dy="${lineHeight}">${line}</tspan>` svg += `<tspan x="${point.x}" dy="${lineHeight}">${line}</tspan>`
} }
} else { } else {
svg += `<tspan>${this.escapeText(this.text) svg += `<tspan>${this.escapeText(this.text)}</tspan>`
}</tspan>`
} }
this.outdent() this.outdent()
svg += this.nl() + '</text>' svg += this.nl() + '</text>'

View file

@ -53,11 +53,6 @@ svg.freesewing.draft {
stroke: none !important; stroke: none !important;
fill: none !important; fill: none !important;
} }
/* sample */
path.sample-focus {
stroke-width: 0;
fill-opacity: 0.2;
}
/* Text */ /* Text */
text { text {