@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
84 lines • 3.7 kB
JavaScript
import { check4SiteTheme } from "../../common/commandStyles/ISiteThemeChoices";
/**
* Created from BoxStyles in order to create IReactCSSProperties from strings.
* @param key - purely for error feedback
* @param value
*/
export function getReactCSSFromString(key, value, fallback, ignoreColors) {
const result = {
errMessage: '',
string: value,
parsed: fallback,
};
if (!value || value === null || value === undefined || value === '') {
result.string = '';
}
else {
try {
if (value && value.length > 0) {
if (value.indexOf('{') !== 0) {
value = '{' + value;
}
if (value.lastIndexOf('}') !== value.length - 1) {
value += '}';
}
result.parsed = JSON.parse(value);
if (ignoreColors === true && result.parsed.color)
result.parsed.color = 'useSiteTheme';
if (ignoreColors === true && result.parsed.background)
result.parsed.background = 'useSiteTheme';
if (ignoreColors === true && result.parsed.backgroundColor)
result.parsed.backgroundColor = 'useSiteTheme';
}
}
catch (e) {
// errMessage = getHelpfullErrorV2( e, true, true, null ); //'BoxTilesWebpart.ts ~ boxStyles.' + key
result.errMessage = `${key} value is not correct JSON format - IReactCSSProperties type but without outer { "background":"red" }`;
console.log('Unable to understand this style string:', value + '');
}
}
return result;
}
/**
* Copied from banner/component.tsx origilly in ECStorage project
* @param styleString
* @param fallback
*/
export function createStyleFromString(styleString, fallback, themeChoice, traceString) {
let thisStyle = {};
if (!styleString || styleString === null || styleString === undefined) {
return fallback;
}
if (typeof styleString !== 'string') {
console.log('StringToReactCSS.ts ~ 62 detected unknown type');
return fallback;
}
styleString = styleString.trim() + ''; //Added to insure it never modifies the original
//Add leading and trailing curley as needed
if (styleString.indexOf('{') < 0) {
styleString = '{' + styleString;
}
if (styleString.lastIndexOf('}') !== styleString.length - 1) {
styleString = styleString + '}';
}
//replace single quotes with double because you sometimes have one or the other.
styleString = styleString.replace(/'/g, '"');
//replace all semi-colons with commas just to help when typing and make a typo
styleString = styleString.replace(/;/g, ',');
try {
thisStyle = JSON.parse(styleString);
// Added if style choice is based on the site theme
if (check4SiteTheme(themeChoice) === true)
thisStyle.color = null; // 2024-09-07: Set 'null as any' to remove typing warning
if (check4SiteTheme(themeChoice) === true)
thisStyle.background = null; // 2024-09-07: Set 'null as any' to remove typing warning
if (check4SiteTheme(themeChoice) === true)
thisStyle.backgroundColor = null; // 2024-09-07: Set 'null as any' to remove typing warning
}
catch (e) {
console.log('Unable to understand this style string:', styleString + ''); //doing + '' so that if you pause in console but later modify, it does not change
thisStyle = fallback;
}
return thisStyle;
}
//# sourceMappingURL=reactCSS.js.map