UNPKG

react-pdf-builder

Version:
98 lines (97 loc) 4.84 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ThemeBuilder = void 0; const color_1 = __importDefault(require("color")); const util_1 = require("../util/util"); class ThemeBuilder { constructor(colorScheme) { this.colorScheme = colorScheme; } /** * Creates the theme for the provided scale relative to the A4 page size (8.5 x 11", 595.28 by 841.89pt). * @param config Contains a scale multiplier for the theme, with a value of `1` being relative to a standard 8.5 x 11" A4 page, or 595.28 x 841.89pt, a value of `2` being relative to A2 at 1190.55 x 1683.78pt, etc. All scalable values for the theme (such as padding, margin, and font sizes) are multiplied by this value. Can also contain an override theme to customize the theme further. * @returns The scaled theme. */ build(config) { var _a; const themeOverride = Object.assign({}, config === null || config === void 0 ? void 0 : config.override); const scale = (_a = config === null || config === void 0 ? void 0 : config.scale) !== null && _a !== void 0 ? _a : 1; const colorScheme = ThemeBuilder.overrideColorScheme(this.colorScheme, themeOverride === null || themeOverride === void 0 ? void 0 : themeOverride.colorScheme); const theme = this.doBuild(scale, colorScheme); const mergedTheme = ThemeBuilder.overrideTheme(theme, themeOverride); mergedTheme.colorScheme = colorScheme; return mergedTheme; } // === Static === === === === === === === === static overrideTheme(theme, override = {}) { // Combine class name strings, if overridden const mergeClassNames = (key, a, b) => { if (key === 'className' && typeof a === 'string' && typeof b === 'string') { const combinedClassNames = `${a} ${b}`; return { success: true, value: combinedClassNames }; } else { return { success: false, value: undefined }; } }; return (0, util_1.deepMerge)(theme, override, mergeClassNames); } static overrideColorScheme(colorScheme, overrides = {}) { return (0, util_1.deepMerge)(colorScheme, overrides); } static getSwatchColor(swatch, colorScheme) { var _a, _b; return ((_b = (_a = colorScheme === null || colorScheme === void 0 ? void 0 : colorScheme.theme[swatch]) !== null && _a !== void 0 ? _a : colorScheme === null || colorScheme === void 0 ? void 0 : colorScheme.colors[swatch]) !== null && _b !== void 0 ? _b : colorScheme === null || colorScheme === void 0 ? void 0 : colorScheme.greyscale[swatch]); } static getContrastColor(swatch, colorScheme) { return colorScheme === null || colorScheme === void 0 ? void 0 : colorScheme.contrast[swatch]; } /** * Mixes white with the color provided by the specified ratio amount (value from 0 to 1). * @param color The color to lighten. * @param ratio Ratio from 0 to 1. * @returns The lightened color as a hex string. */ static lighten(color, ratio) { return new color_1.default(color).lighten(ratio).hex(); } /** * Mixes black with the color provided by the specified ratio amount (value from 0 to 1). * @param color The color to darken. * @param ratio Ratio from 0 to 1. * @returns The darkened color as a hex string. */ static darken(color, ratio) { return new color_1.default(color).darken(ratio).hex(); } /** * Converts a decimal from 0-255 to a two character long hexadecimal string. * @param decimal The value from 0 to 255. * @returns A two character long hex string. */ static decimalToHex(decimal) { return Math.round(Math.min(1, Math.max(0, decimal)) * 255) .toString(16) .padStart(2, '0') .toUpperCase(); } /** * Returns a Style object containing all styles for the class name string. Class name string can contain * one or more class names, separated by spaces. * * @param className The class name to get the Style object for. * @param classNames The style definitions for all available class names. * @returns A merged Style object containing styles for all classes in the class name string. */ static getStylesForClassName(className, classNames) { return (className !== null && className !== void 0 ? className : '') .split(' ') .map((c) => c.trim()) .filter((c) => c) .reduce((p, c) => (Object.assign(Object.assign({}, p), classNames[c])), {}); } } exports.ThemeBuilder = ThemeBuilder;