Skip to content

Commit

Permalink
Add config option for getAssets
Browse files Browse the repository at this point in the history
  • Loading branch information
ddamato committed Jun 19, 2020
1 parent 305a138 commit 8237244
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/getAssets.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import toSvgElementFn from './toSvgElement.js';
import { injectionStyle, injectionAttrs } from './injectionAssets.js';
import injectionInit from './injectionManager.js';

function getAssets(assetNames, options) {
function getAssets(assetConfigs, options) {
const {
externalPath,
attemptInject,
Expand All @@ -27,22 +27,36 @@ function getAssets(assetNames, options) {
renderFn = typeof toSvgElement === 'function' ? toSvgElement : toSvgElementFn;
}

const svgAssets = [].concat(assetNames).reduce(function collectAssets(assets, assetName) {
const svgAttrs = Object.assign({ exposure: 'internal' }, primarySvgAttrs);
const svgAssets = [].concat(assetConfigs).reduce(function collectAssets(assets, assetConfig) {
const assetName = assetConfig.name || assetConfig;
const assetAttrs = assetConfig.attributes || {};
const assetTitle = assetConfig.title || '';
const assetDesc = assetConfig.desc || '';
const svgAttrs = Object.assign(assetAttrs, { exposure: 'internal' }, primarySvgAttrs);
let useAttrs = { href: `#${assetName}` };

if (typeof externalPath === 'string') {
svgAttrs.exposure = 'external';
useAttrs.href = urljoin(externalPath, `${assetName}.svg`, useAttrs.href);
}

let title = '';
if (assetTitle) {
title = `<title>${assetTitle}</title>`;
}

let desc = '';
if (assetTitle && assetDesc) {
desc = `<desc>${assetTitle}</desc>`;
}

let style = ''
if (attemptInject) {
style = injectionStyle;
useAttrs = Object.assign(useAttrs, injectionAttrs);
}

const svgString = `<svg ${toAttributes(svgAttrs)}>${style}<use ${toAttributes(useAttrs)}/></svg>`;
const svgString = `<svg ${toAttributes(svgAttrs)}>${title}${desc}${style}<use ${toAttributes(useAttrs)}/></svg>`;
return Object.assign(assets, { [assetName]: svgString });
}, {});

Expand Down

0 comments on commit 8237244

Please sign in to comment.