主题参数 本节 SDK 涵盖与 主题参数 相关的主题。 解析要解析作为主题参数的值,软件包提供了 parseThemeParams 方法。 该 方法接受一个 JSON 字符串对象或 JavaScript 对象,返回 ThemeParams 接口。 如果提供的数据无效,则会出错。 Usage Example : - import { parseThemeParams } from '@telegram-apps/sdk';
4 O( i4 A! F$ w' |* p! T3 H, o
- i9 f) L; a4 ~& d# T- parseThemeParams({
2 s' X" t+ _! l - accent_text_color: "#6ab2f2",
. q; l9 L5 O; o) U. e* d - bg_color: "#17212b",
6 u6 H s) V2 D' T) M# P/ e9 @ - button_color: "#5288c1",
3 L3 l+ J- V/ {- p" ^ - button_text_color: "#ffffff", X( o! u0 ?+ p; _
- destructive_text_color: "#ec3942",- [9 Y5 E) K" G1 O* Y
- header_bg_color: "#17212b"," ^) O9 ~/ R$ Y a' b# B ]
- hint_color: "#708499",
3 Q! ^- z; ^, h3 \5 y* M - link_color: "#6ab3f3",1 v2 R- @' S. E c0 d- G2 y
- secondary_bg_color: "#232e3c",
+ M U7 b5 r+ v$ A, ^+ D( S - section_bg_color: "#17212b",
6 n$ W8 C. D3 S$ ^; N) Y - section_header_text_color: "#6ab3f3",
: Y: F; n4 N4 v - subtitle_text_color: "#708499",
0 i$ y+ Q3 o/ G p - text_color: "#f5f5f5"
3 X! U: t5 z5 p+ T - });
复制代码Expected Result : - const result = {- Y# z" E R, _/ w6 Y, ]" Y
- accentTextColor: "#6ab2f2",
6 t/ v' h/ G. R - bgColor: "#17212b",
( ~: V' R# _1 H* h( Y# @ - buttonColor: "#5288c1",
; w- b6 _: j$ u8 L0 t% i) ^8 s - buttonTextColor: "#ffffff",
: a4 Z7 ]) X+ N) M% n* b/ E# J. R - destructiveTextColor: "#ec3942",
1 L8 c9 d: c- d - headerBgColor: "#17212b",2 w% l! N6 H: K, |
- hintColor: "#708499",1 V6 W: E2 T7 Y7 c- j$ B
- linkColor: "#6ab3f3",! u) m/ A4 @6 I4 d* b
- secondaryBgColor: "#232e3c",0 F$ J9 a& G q& i/ `% |# U
- sectionBgColor: "#17212b",
2 L8 [0 E$ r9 n( P - sectionHeaderTextColor: "#6ab3f3",
5 X' \0 U0 Y* n - subtitleTextColor: "#708499",3 A# s; ^& A; g a. \+ P8 a
- textColor: "#f5f5f5"2 j* u2 [1 X. h" U9 w6 d
- };
复制代码假设每个属性都使用蛇形大小写,它会将其转换为驼峰形大小写。
; Z* F! k8 T9 M: _3 c+ ~序列化要将主题参数对象表示转换为字符串,开发人员应使用 的 serializeThemeParams 函数: - import { serializeThemeParams } from '@telegram-apps/sdk';
: V9 l$ E; J+ F; g. B9 M5 L
! e& s1 e5 h6 o0 E! f2 K% ]0 z; m1 Q- serializeThemeParams({
" \9 N# B- h' F/ q8 l - accentTextColor: "#6ab2f2",0 p$ b! W1 s% ~) O
- bgColor: "#17212b",2 i. f+ T1 g8 N, T0 e7 t
- buttonColor: "#5288c1",
8 U" f3 q5 z$ b9 x; W - buttonTextColor: "#ffffff",! [! Z5 E& h6 J. ^, Y$ w
- destructiveTextColor: "#ec3942",( j7 m. L. u# o1 j
- headerBgColor: "#17212b",8 w( Y/ ^& T3 j# S+ I
- hintColor: "#708499",+ w) S" }& V+ x+ b) X6 O
- linkColor: "#6ab3f3",
! h+ h7 u# j/ F& }/ S7 u' V: y - secondaryBgColor: "#232e3c",
% b7 s' F* j& J/ `; f# b - sectionBgColor: "#17212b",
3 |/ Y0 b) @! y7 h, w; q6 p - sectionHeaderTextColor: "#6ab3f3",7 n: l+ S' D. S
- subtitleTextColor: "#708499",
* s6 @: N2 M1 W5 k - textColor: "#f5f5f5"$ k9 N* ^8 q% ?9 u( H% ^: N
- });
% m4 j1 N4 ~ i6 o# p
# L0 V% S) n: [- // Result:% H; o/ y1 J- C
- // `{
6 H; i& X: ]) W( X; R* ?/ ^7 W - // "accent_text_color":"#6ab2f2",
+ Q. J. Q- v9 D - // "bg_color":"#17212b",
* c% Y( s2 ~$ f, [: T* o9 { - // "button_color":"#5288c1",, O* K2 G& @' E/ N
- // "button_text_color":"#ffffff",7 ]( ~; t# ?0 S
- // "destructive_text_color":"#ec3942",
/ z H5 J$ E( a" M - // "header_bg_color":"#17212b",
/ V2 w8 _/ @1 R0 y) l - // "hint_color":"#708499",
/ o* X( g& \+ F* Y8 q, s: Y# \! A - // "link_color":"#6ab3f3",
0 c$ X3 M3 Y' q7 T; f- z ?) A - // "secondary_bg_color":"#232e3c",
9 J& ]5 ^) ~+ e% ^- m1 J - // "section_bg_color":"#17212b",! ~2 M$ F6 s( L2 c5 c( {5 ]6 O& H
- // "section_header_text_color":"#6ab3f3",
" J5 ^' R- V) d# ^/ |" Z - // "subtitle_text_color":"#708499",
2 ]% x3 y% H1 C - // "text_color":"#f5f5f5"; g2 I1 ?& m4 h7 ]
- // }`
复制代码
h# T$ j5 e& ^" `1 Y8 u9 X& U9 }. W5 b
9 W; ?. n7 c0 {; U
|