Skip to content

Commit

Permalink
Merge pull request #1 from alte0/master
Browse files Browse the repository at this point in the history
getting the fill attribute from the source svg for the sprite and update from upstream master
  • Loading branch information
artem-malko authored May 17, 2022
2 parents bb0010e + 98d89ee commit 0f51db3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
62 changes: 42 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var cheerio = require('cheerio'),
events = require('events'),
fs = require('fs'),
gutil = require('gulp-util'),
mkdirp = require('mkdirp'),
Expand Down Expand Up @@ -76,10 +77,14 @@ var spriteSVG = function(options) {
width: 0,
imgName: options.imgName
},
eventEmitter = new events.EventEmitter(),
self,
x = options.x,
y = options.y;

// When a template file is loaded, render it
eventEmitter.on("loadedTemplate", renderTemplate);

// Generate relative em/rem untis from pixels
function pxToRelative(value) {
return value / options.pixelBase;
Expand All @@ -98,7 +103,7 @@ var spriteSVG = function(options) {
dest: dest
};

renderTemplate(file, cb);
eventEmitter.emit("loadedTemplate", file, cb);
});
}

Expand All @@ -112,17 +117,23 @@ var spriteSVG = function(options) {

// For each sprite
for (var i in data.sprites) {
var sprite = data.sprites[i],
// Create, initialise and populate an SVG
$svg = $('<svg/>')
.attr({
'height': sprite.h,
'viewBox': sprite.viewBox,
'width': sprite.w,
'x': Math.ceil(sprite.fit.x)+options.padding,
'y': Math.ceil(sprite.fit.y)+options.padding
})
.append(sprite.file);
var sprite = data.sprites[i];
// Create, initialise and populate an SVG
var spriteAttr = {
'height': sprite.h,
'viewBox': sprite.viewBox,
'width': sprite.w,
'x': Math.ceil(sprite.fit.x)+options.padding,
'y': Math.ceil(sprite.fit.y)+options.padding
};

if (sprite.fill){
Object.assign(spriteAttr, {'fill': sprite.fill});
}

var $svg = $('<svg/>')
.attr(spriteAttr)
.append(sprite.file);

// Check and set parent SVG width
if(sprite.fit.x+sprite.w+options.padding>data.width) {
Expand Down Expand Up @@ -168,15 +179,21 @@ var spriteSVG = function(options) {
sprite.y = y+options.padding;

// Create, initialise and populate an SVG
var svgSpriteAttrs = {
'height': sprite.h,
'viewBox': sprite.viewBox,
'width': sprite.w,
'x': Math.ceil(sprite.x),
'y': Math.ceil(sprite.y)
};

if (sprite.fill) {
Object.assign(svgSpriteAttrs, {'fill': sprite.fill});
}

var $svg = $('<svg/>')
.attr({
'height': sprite.h,
'viewBox': sprite.viewBox,
'width': sprite.w,
'x': Math.ceil(sprite.x),
'y': Math.ceil(sprite.y)
})
.append(sprite.file);
.attr(svgSpriteAttrs)
.append(sprite.file);

// Round up coordinates
sprite.h = Math.ceil(sprite.h);
Expand Down Expand Up @@ -250,6 +267,10 @@ var spriteSVG = function(options) {
w: parseFloat(width)
};

if ($file.attr('fill') !== undefined) {
Object.assign(sprite, {fill: $file.attr('fill')});
}

// Add the sprite to our array
data.sprites.push(sprite);

Expand All @@ -275,6 +296,7 @@ var spriteSVG = function(options) {
// Render our template and then save the file
function renderTemplate(file, cb) {
var compiled = mustache.render(file.contents, file.data);

mkdirp(path.dirname(file.dest), function(){
fs.writeFile(file.dest, compiled, cb);
});
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gulp-svg-spritesheet",
"description": "Gulp SVG sprite sheet generator",
"version": "0.0.5",
"version": "0.0.6",
"author": "Darren Hall <[email protected]>",
"contributors": [
{
Expand Down

0 comments on commit 0f51db3

Please sign in to comment.