UNPKG

4nm

Version:

TypeScript reimplementation of Telegram's official library for communicating with Telegram Web Apps.

101 lines (100 loc) 3.01 kB
import { createJSONStructParser, parseJSONParamAsOptRGBExt } from '../parsing'; /** * Extracts theme information from specified JSON. */ const extractFromJSON = createJSONStructParser({ backgroundColor: ['bg_color', parseJSONParamAsOptRGBExt], buttonColor: ['button_color', parseJSONParamAsOptRGBExt], buttonTextColor: ['button_text_color', parseJSONParamAsOptRGBExt], hintColor: ['hint_color', parseJSONParamAsOptRGBExt], linkColor: ['link_color', parseJSONParamAsOptRGBExt], secondaryBackgroundColor: ['secondary_bg_color', parseJSONParamAsOptRGBExt], textColor: ['text_color', parseJSONParamAsOptRGBExt], }); export class ThemeParams { raw; unsafe; params; /** * Returns empty instance of ThemeParams. */ static empty() { return new ThemeParams('', {}, {}); } /** * Creates new ThemeParams instance from raw representation of theme params. * @param jString - raw representation of theme params (JSON string). */ static fromJSONString(jString) { return new ThemeParams(jString, JSON.parse(jString), extractFromJSON(jString)); } /** * Creates new ThemeParams instance from raw representation of theme params. * @param json - raw representation of theme params (JSON). */ static fromJSON(json) { return new ThemeParams(JSON.stringify(json), json, extractFromJSON(json)); } constructor( /** * Raw representation of parsed string. */ raw, /** * Value which is unsafe to use. May contain values not specified in * current instance. This value is useful in case, Telegram native * application was updated, but this library is not up-to-date. */ unsafe, /** * Parameters parsed from special init param. */ params) { this.raw = raw; this.unsafe = unsafe; this.params = params; } /** * Background color in the #RRGGBB format. */ get backgroundColor() { return this.params.backgroundColor; } /** * Button color in the #RRGGBB format. */ get buttonColor() { return this.params.buttonColor; } /** * Button text color in the #RRGGBB format. */ get buttonTextColor() { return this.params.buttonTextColor; } /** * Hint text color in the #RRGGBB format. */ get hintColor() { return this.params.hintColor; } /** * Link color in the #RRGGBB format. */ get linkColor() { return this.params.linkColor; } /** * Secondary background color in the #RRGGBB format. * @since WebApp version 6.1+ */ get secondaryBackgroundColor() { return this.params.secondaryBackgroundColor; } /** * Main text color in the #RRGGBB format. */ get textColor() { return this.params.textColor; } }