Support for components in BitmapText

This commit is contained in:
SleepWalker 2016-10-01 11:06:47 +03:00
parent e61eaf8bd9
commit cadf330566
2 changed files with 21 additions and 11 deletions

View File

@ -100,7 +100,7 @@
"strict": ["warn", "never"], // babel все сделает за нас "strict": ["warn", "never"], // babel все сделает за нас
// variables // variables
"no-catch-shadow": "error", "no-catch-shadow": "off",
"no-delete-var": "error", "no-delete-var": "error",
"no-label-var": "error", "no-label-var": "error",
"no-shadow-restricted-names": "error", "no-shadow-restricted-names": "error",

View File

@ -5,16 +5,17 @@ import { FormattedMessage as Message } from 'react-intl';
export function BitmapText(props) { export function BitmapText(props) {
const parts = props.message.id.split('.'); const parts = props.message.id.split('.');
if (parts[0] !== 'emails') { if (parts[0] !== 'emails' && parts[0] !== 'components') {
throw new Error('Only src/emails subdirectories supported for now'); throw new Error('Only src/emails and src/components subdirectories supported for now');
} }
if (parts.length !== 3) { if (parts.length !== 3) {
throw new Error('The message.id must be contain 3 parts separated by dots'); throw new Error('The message.id must contain 3 parts separated by dots');
} }
let src; let src;
let size; let size;
try {
try { try {
src = require(`emails/${parts[1]}/images/${props.intl.locale}/${parts[2]}.png`); src = require(`emails/${parts[1]}/images/${props.intl.locale}/${parts[2]}.png`);
// TODO: we can improve this loader in future by adding an option to disable file emitting // TODO: we can improve this loader in future by adding an option to disable file emitting
@ -24,6 +25,15 @@ export function BitmapText(props) {
src = require(`emails/${parts[1]}/images/${props.intl.defaultLocale}/${parts[2]}.png`); src = require(`emails/${parts[1]}/images/${props.intl.defaultLocale}/${parts[2]}.png`);
size = require(`image-size!emails/${parts[1]}/images/${props.intl.defaultLocale}/${parts[2]}.png`); size = require(`image-size!emails/${parts[1]}/images/${props.intl.defaultLocale}/${parts[2]}.png`);
} }
} catch (err) { // try components
try {
src = require(`components/${parts[1]}/images/${props.intl.locale}/${parts[2]}.png`);
size = require(`image-size!components/${parts[1]}/images/${props.intl.locale}/${parts[2]}.png`);
} catch (err) { // fallback to default locale
src = require(`components/${parts[1]}/images/${props.intl.defaultLocale}/${parts[2]}.png`);
size = require(`image-size!components/${parts[1]}/images/${props.intl.defaultLocale}/${parts[2]}.png`);
}
}
const width = props.retina ? size.width / 2 : size.width; const width = props.retina ? size.width / 2 : size.width;
const height = props.retina ? size.height / 2 : size.height; const height = props.retina ? size.height / 2 : size.height;