rendering icon fetching
This commit is contained in:
@@ -8,6 +8,7 @@ cache:
|
|||||||
- node_modules
|
- node_modules
|
||||||
install:
|
install:
|
||||||
- npm install
|
- npm install
|
||||||
|
- node build.sh
|
||||||
script:
|
script:
|
||||||
- jest --silent --coverage --coverageReporters=text-lcov | coveralls
|
- jest --silent --coverage --coverageReporters=text-lcov | coveralls
|
||||||
- jshint ./src
|
- jshint ./src
|
||||||
@@ -86,7 +86,7 @@ svn.commands.checkout(FA_SVG_FOLDER_URL, svgDir, (err) => {
|
|||||||
|
|
||||||
svgs[type][name] = {
|
svgs[type][name] = {
|
||||||
path: match2[1],
|
path: match2[1],
|
||||||
width: match1[1]
|
width: parseInt(match1[1])
|
||||||
};
|
};
|
||||||
|
|
||||||
count++;
|
count++;
|
||||||
|
|||||||
+61
-2
@@ -7,6 +7,14 @@ try {
|
|||||||
console.error('fa-diagrams: SVG list could not be loaded', err);
|
console.error('fa-diagrams: SVG list could not be loaded', err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef Node2
|
||||||
|
* @property {string} name
|
||||||
|
* @property {number} x
|
||||||
|
* @property {number} y
|
||||||
|
* @property {string} icon
|
||||||
|
*/
|
||||||
|
|
||||||
const DEFAULT_OPTIONS = {
|
const DEFAULT_OPTIONS = {
|
||||||
'beautify': false
|
'beautify': false
|
||||||
};
|
};
|
||||||
@@ -15,6 +23,56 @@ module.exports = (options = DEFAULT_OPTIONS) => {
|
|||||||
const self = {
|
const self = {
|
||||||
defaultOptions: DEFAULT_OPTIONS,
|
defaultOptions: DEFAULT_OPTIONS,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find icon data from given name
|
||||||
|
* @param {string} name
|
||||||
|
* @returns {null|{path: string, width: number}}
|
||||||
|
*/
|
||||||
|
getIcon: (name) => {
|
||||||
|
if (!name || !name.trim())
|
||||||
|
return null;
|
||||||
|
|
||||||
|
let search = ['solid', 'regular', 'brands'];
|
||||||
|
const spl = name.trim().split(' ').map(t => t.indexOf('fa-') === 0 ? t.substr(3) : t);
|
||||||
|
|
||||||
|
const checkType = (type, keywords) => {
|
||||||
|
if (search.length > 1) // else it's already found
|
||||||
|
keywords.forEach(kw => {
|
||||||
|
const i = spl.indexOf(kw);
|
||||||
|
if (i >= 0) {
|
||||||
|
spl.splice(i, 1);
|
||||||
|
search = [type];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
checkType('solid', ['fas', 'solid']);
|
||||||
|
checkType('regular', ['far', 'regular']);
|
||||||
|
checkType('brands', ['fab', 'brands']);
|
||||||
|
|
||||||
|
name = spl[0];
|
||||||
|
|
||||||
|
for (let i = 0; i < search.length; i++) {
|
||||||
|
if (list[search[i]] && list[search[i]][name]) {
|
||||||
|
return list[search[i]][name];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Object<string,Node2>} nodes
|
||||||
|
* @returns {Object}
|
||||||
|
*/
|
||||||
|
renderNodes: (nodes) => {
|
||||||
|
const g = [];
|
||||||
|
Object.values(nodes).forEach(() => {
|
||||||
|
//TODO
|
||||||
|
});
|
||||||
|
return {'g': g};
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert xml-js data into correct svg xml string
|
* Convert xml-js data into correct svg xml string
|
||||||
* @param {Object} data
|
* @param {Object} data
|
||||||
@@ -40,8 +98,9 @@ module.exports = (options = DEFAULT_OPTIONS) => {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
compute: () => {
|
compute: (nodes) => {
|
||||||
return self.toXML({}, 0, 0); //TODO temporary
|
const data = self.renderNodes(nodes);
|
||||||
|
return self.toXML(data, 0, 0); //TODO temporary
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,87 @@
|
|||||||
/* jshint -W117 */
|
/* jshint -W117 */
|
||||||
const rendering = require('../src/rendering');
|
const rendering = require('../src/rendering');
|
||||||
|
|
||||||
|
describe('getIcon', () => {
|
||||||
|
const solidCirclePath = 'M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8z';
|
||||||
|
const regularCirclePath = 'M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8zm0 448c-110.5 0-200-89.5-200-200S145.5 56 256 56s200 89.5 200 200-89.5 200-200 200z';
|
||||||
|
|
||||||
|
test('no name', () => {
|
||||||
|
const res = rendering().getIcon(undefined);
|
||||||
|
expect(res).toBeNull();
|
||||||
|
});
|
||||||
|
test('empty name', () => {
|
||||||
|
const res = rendering().getIcon(' ');
|
||||||
|
expect(res).toBeNull();
|
||||||
|
});
|
||||||
|
test('invalid name', () => {
|
||||||
|
const res = rendering().getIcon('circle-2');
|
||||||
|
expect(res).toBeNull();
|
||||||
|
});
|
||||||
|
test('valid circle', () => {
|
||||||
|
const res = rendering().getIcon('circle');
|
||||||
|
expect(res).toEqual({
|
||||||
|
path: solidCirclePath,
|
||||||
|
width: 512
|
||||||
|
});
|
||||||
|
});
|
||||||
|
test('valid circle alt name', () => {
|
||||||
|
const res = rendering().getIcon('fa-circle');
|
||||||
|
expect(res).toEqual({
|
||||||
|
path: solidCirclePath,
|
||||||
|
width: 512
|
||||||
|
});
|
||||||
|
});
|
||||||
|
test('ignored other name', () => {
|
||||||
|
const res = rendering().getIcon('circle server');
|
||||||
|
expect(res).toEqual({
|
||||||
|
path: solidCirclePath,
|
||||||
|
width: 512
|
||||||
|
});
|
||||||
|
});
|
||||||
|
test('forcing regular', () => {
|
||||||
|
const res = rendering().getIcon('far circle');
|
||||||
|
expect(res).toEqual({
|
||||||
|
path: regularCirclePath,
|
||||||
|
width: 512
|
||||||
|
});
|
||||||
|
});
|
||||||
|
test('forcing regular 2', () => {
|
||||||
|
const res = rendering().getIcon('regular circle');
|
||||||
|
expect(res).toEqual({
|
||||||
|
path: regularCirclePath,
|
||||||
|
width: 512
|
||||||
|
});
|
||||||
|
});
|
||||||
|
test('forcing regular 3', () => {
|
||||||
|
const res = rendering().getIcon('circle far');
|
||||||
|
expect(res).toEqual({
|
||||||
|
path: regularCirclePath,
|
||||||
|
width: 512
|
||||||
|
});
|
||||||
|
});
|
||||||
|
test('double type', () => {
|
||||||
|
const res = rendering().getIcon('circle far solid');
|
||||||
|
expect(res).toEqual({
|
||||||
|
path: solidCirclePath,
|
||||||
|
width: 512
|
||||||
|
});
|
||||||
|
});
|
||||||
|
test('double type 2', () => {
|
||||||
|
const res = rendering().getIcon('circle solid far');
|
||||||
|
expect(res).toEqual({
|
||||||
|
path: solidCirclePath,
|
||||||
|
width: 512
|
||||||
|
});
|
||||||
|
});
|
||||||
|
test('forcing solid on brands icon', () => {
|
||||||
|
const res = rendering().getIcon('usb');
|
||||||
|
expect(res).not.toBeNull();
|
||||||
|
const res2 = rendering().getIcon('fas usb');
|
||||||
|
expect(res2).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
describe('toXML', () => {
|
describe('toXML', () => {
|
||||||
test('no data', () => {
|
test('no data', () => {
|
||||||
const res = rendering({beautify: false}).toXML({}, 0, 0);
|
const res = rendering({beautify: false}).toXML({}, 0, 0);
|
||||||
|
|||||||
Reference in New Issue
Block a user