icomoon-helper
Version:
Helper for icomoon custom font render
55 lines (54 loc) • 2.41 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createIconSet = void 0;
const react_1 = __importDefault(require("react"));
const renderIcon_1 = require("./renderIcon");
const logger_1 = require("./logger");
function createIconSet(config, renderIcon) {
const fontFamily = config.metadata.name;
const symbols = config.icons.map((item) => {
const codes = item.properties.codes || [item.properties.code];
const values = codes.map((code) => String.fromCharCode(code));
const colors = item.attrs.map((attr) => attr.fill || attr.stroke);
const opacities = item.attrs.map((attr) => attr.opacity);
return values.map((value, index) => ({
value,
color: colors[index],
opacity: opacities[index],
size: item.properties.prevSize,
}));
});
const iconDictionary = config.icons.reduce((prev, current, index) => {
prev[current.properties.name] = index;
return prev;
}, {});
return (_a) => {
var { children } = _a, props = __rest(_a, ["children"]);
const Component = renderIcon || renderIcon_1.renderIcon;
const symbolIndex = iconDictionary[children];
if (symbolIndex != undefined) {
const currentSymbols = symbols[symbolIndex];
logger_1.checkSymbolsCount(children, currentSymbols.length);
return (react_1.default.createElement(Component, Object.assign({ fontFamily: fontFamily, symbols: currentSymbols }, props)));
}
else {
logger_1.logger.error(`icon with name "${children}" not found`);
return null;
}
};
}
exports.createIconSet = createIconSet;