react-native-image-filter-kit
Version:
Various image filters for iOS & Android
142 lines • 5.83 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)
t[p[i]] = s[p[i]];
return t;
};
import React from 'react';
import { processColor, Platform } from 'react-native';
import invariant from 'invariant';
import { distance, scalar, color, colorVector, image, placeholder, position, distanceVector, area } from './inputs';
import { ShapeRegistry } from './shape-registry';
import { id } from './util';
import { swapComposition } from './swap-composition';
import { ImagePlaceholder } from './image-placeholder';
const mainImageName = Platform.OS === 'android' ? 'image' : 'generatedImage';
const anyToString = (n) => `${n}`;
const convertDistances = (distances) => distances.map(anyToString);
const convertPosition = ({ x, y }) => ({ x: `${x}`, y: `${y}` });
const convertColor = processColor;
const convertColors = (cs) => cs.map(convertColor);
const convertArea = (rect) => ({
x: `${rect.x}`,
y: `${rect.y}`,
width: `${rect.width}`,
height: `${rect.height}`
});
const paramConvertMap = {
[position]: convertPosition,
[distance]: anyToString,
[distanceVector]: convertDistances,
[scalar]: anyToString,
[color]: convertColor,
[colorVector]: convertColors,
[area]: convertArea
};
const srcResizeMode = 'srcResizeMode';
const srcAnchor = 'srcAnchor';
const srcPosition = 'srcPosition';
const dstResizeMode = 'dstResizeMode';
const dstAnchor = 'dstAnchor';
const dstPosition = 'dstPosition';
const iosKeyConvertMap = {
inputImageResizeMode: srcResizeMode,
inputImageAnchor: srcAnchor,
inputImagePosition: srcPosition,
inputBackgroundImageResizeMode: dstResizeMode,
inputBackgroundImageAnchor: dstAnchor,
inputBackgroundImagePosition: dstPosition,
inputMaskResizeMode: dstResizeMode,
inputMaskAnchor: dstAnchor,
inputMaskPosition: dstPosition,
inputGradientImageResizeMode: dstResizeMode,
inputGradientImageAnchor: dstAnchor,
inputGradientImagePosition: dstPosition,
inputTargetImageResizeMode: dstResizeMode,
inputTargetImageAnchor: dstAnchor,
inputTargetImagePosition: dstPosition
};
const srcImage = 'srcImage';
const dstImage = 'dstImage';
const iosMatchMap = {
inputImage: srcImage,
inputGradientImage: dstImage,
inputBackgroundImage: dstImage,
inputMask: dstImage,
inputTargetImage: dstImage
};
const requiredValueInvariant = (filterName, value, key) => {
invariant(value !== undefined, `ImageFilterKit: ${filterName} filter should specify '${key}' value.`);
};
const convertKey = Platform.select({
android: id,
ios: (key) => iosKeyConvertMap[key] || key
});
const convertValue = Platform.select({
android: id,
ios: (value) => (typeof value.match === 'string'
? { match: iosMatchMap[value.match] || value.match }
: value)
});
export const finalizeConfig = (_a) => {
var { name } = _a, values = __rest(_a, ["name"]);
const shape = ShapeRegistry.shape(name);
return (Object.assign({ name }, (Object.keys(shape).reduce((acc, k) => {
const inputType = shape[k] === placeholder ? image : shape[k];
const key = shape[k] === placeholder ? mainImageName : k;
const inputValue = values[key];
if (inputValue !== undefined) {
const convert = paramConvertMap[inputType] ||
(inputType === image && typeof inputValue !== 'number' ? finalizeConfig : id);
acc[convertKey(key)] = { [inputType]: convertValue(convert(values[key])) };
}
return acc;
}, {}))));
};
const patchComposition = (config) => (Platform.select({
ios: config['resizeCanvasTo'] === 'dstImage' ? swapComposition(config, 'srcImage') : config,
android: config['resizeCanvasTo'] === 'srcImage' ? swapComposition(config, 'dstImage') : config
}));
export const extractConfigAndImages = (filterProps) => {
const images = [];
const parseFilter = (filter) => {
if (filter.type && !filter.type.isImageFilter) {
const idx = images.length;
const elem = filter;
images.push(elem);
return idx;
}
const _a = filter.props ? (filter.props.config || filter.props) : filter, { name: n = (filter.type && filter.type.displayName) } = _a, values = __rest(_a, ["name"]);
let prevConfig;
let nextConfig = Object.assign({ name: n }, values);
do {
prevConfig = nextConfig;
nextConfig = ShapeRegistry.transform(prevConfig.name)(patchComposition(prevConfig));
} while (nextConfig.name !== prevConfig.name);
const { name } = nextConfig, rest = __rest(nextConfig, ["name"]);
const shape = ShapeRegistry.shape(name);
return (Object.assign({ name }, (Object.keys(shape).reduce((acc, key) => {
const inputType = shape[key];
const inputValue = rest[key];
if (inputType === image) {
requiredValueInvariant(name, inputValue, key);
acc[key] = parseFilter(inputValue);
}
else if (inputType === placeholder) {
const idx = images.length;
images.push(inputValue || <ImagePlaceholder key={`ifk_placeholder_${idx}`}/>);
acc[mainImageName] = idx;
}
else if (inputValue !== undefined) {
acc[key] = inputValue;
}
return acc;
}, {}))));
};
const config = parseFilter(filterProps);
return { config, images };
};
//# sourceMappingURL=config.js.map