主题参数 本节 SDK 涵盖与 主题参数 相关的主题。 解析要解析作为主题参数的值,软件包提供了 parseThemeParams 方法。 该 方法接受一个 JSON 字符串对象或 JavaScript 对象,返回 ThemeParams 接口。 如果提供的数据无效,则会出错。 Usage Example : - import { parseThemeParams } from '@telegram-apps/sdk';' e+ @! u) N* b7 A) c& s) _7 u
- $ [" z! t: |% w0 v! {
- parseThemeParams({% v! k9 }; s" H" @% u! Y3 J
- accent_text_color: "#6ab2f2",! q: l, W. ]" D
- bg_color: "#17212b",
1 m, Y/ L" k+ ]; h( Y. Z: O+ m - button_color: "#5288c1",% M: N9 l0 u& y( ^+ `
- button_text_color: "#ffffff",% u& O+ m0 `2 L% U/ Q
- destructive_text_color: "#ec3942",+ q i" h. d7 X2 v
- header_bg_color: "#17212b",
, o" k0 P2 c' C m2 M. n - hint_color: "#708499"," J7 I; k7 R1 [( k9 {
- link_color: "#6ab3f3",! f4 O% |. m ?& I v0 q
- secondary_bg_color: "#232e3c",2 L2 c7 q7 I- g5 @0 R
- section_bg_color: "#17212b",5 w% _2 F, R( O& ^! A0 k
- section_header_text_color: "#6ab3f3"," F2 M0 }$ W( ^: y$ L6 H/ d) F' k0 o6 v2 |
- subtitle_text_color: "#708499",9 |0 u6 i' Z3 |% l$ v
- text_color: "#f5f5f5"
( K! Q0 }% O- g5 g u - });
复制代码Expected Result : - const result = {
9 M& s+ N" ? d' K! O- u; O - accentTextColor: "#6ab2f2",
( z) H2 J7 e1 |3 O: q; h0 C2 | - bgColor: "#17212b", H5 L( b/ d( R8 T
- buttonColor: "#5288c1",
* e6 c/ B4 q% d- C7 j - buttonTextColor: "#ffffff",
0 G. m! p# V; d - destructiveTextColor: "#ec3942",. N1 d3 D; g0 f9 G4 x. h6 l R
- headerBgColor: "#17212b",
4 j- O: n h$ I5 _/ c - hintColor: "#708499",
$ ^5 y3 m' L4 l& ?8 O - linkColor: "#6ab3f3",6 x7 C- M7 A+ b- i6 c9 W
- secondaryBgColor: "#232e3c",5 H. y1 C( ?$ @: O, u; O e
- sectionBgColor: "#17212b",
" x9 [8 E2 E0 U6 E% q% x# M - sectionHeaderTextColor: "#6ab3f3",
: N& z7 E0 N! f- ]! R0 k2 V6 I/ H( P2 j - subtitleTextColor: "#708499",
3 C8 B" P$ @# s, E+ N' U+ \9 J' N- q - textColor: "#f5f5f5"% f1 j' j+ f" ?" r. P9 H
- };
复制代码假设每个属性都使用蛇形大小写,它会将其转换为驼峰形大小写。
$ A* o3 g( }, h! f$ O; f序列化要将主题参数对象表示转换为字符串,开发人员应使用 的 serializeThemeParams 函数: - import { serializeThemeParams } from '@telegram-apps/sdk';0 o( Z7 ~7 F$ \ a* S. K3 e
; ~) Q4 M" j e6 N- serializeThemeParams({
3 ?$ m4 S, x. t8 y( i4 Q+ D! ]" f! ` - accentTextColor: "#6ab2f2"," _9 r/ @* I w' N, Y$ t" a; e
- bgColor: "#17212b",
% _$ r' i/ o# K1 W d4 F - buttonColor: "#5288c1",9 e6 M0 p, W0 o3 Z" w [
- buttonTextColor: "#ffffff",
& E: S& f( p3 ^/ J - destructiveTextColor: "#ec3942",( B1 {& N7 _6 b/ O1 h6 m0 X
- headerBgColor: "#17212b",9 L, K2 [) ~& t5 |
- hintColor: "#708499",4 m: [7 x4 Y U
- linkColor: "#6ab3f3",
5 K1 s7 E* i* |. g3 {7 Z - secondaryBgColor: "#232e3c",, E: B; q0 Z3 H# c1 L& L- C+ S
- sectionBgColor: "#17212b",$ M6 S+ C& W' D: t9 C3 l2 T$ W" c
- sectionHeaderTextColor: "#6ab3f3",
# ]8 F0 T9 N k2 _% S. n @ - subtitleTextColor: "#708499",3 B# ^" ?9 v% ]! X& c
- textColor: "#f5f5f5"$ A- K- D$ T$ I# n0 E, C
- });
* a& z1 [+ M) J4 v; A, u- o9 y
Z( M$ G2 e4 m* B! n- A4 _. {- // Result: o8 I* H2 [; {! g- O' a' M
- // `{
& s3 T4 G T0 e! U }) A! { - // "accent_text_color":"#6ab2f2",
9 s# t, L. {. Z [; ?8 ?# ~! K - // "bg_color":"#17212b", K* l8 k/ P3 x% ]
- // "button_color":"#5288c1",
& k; `% T0 s; c d# P2 c( o - // "button_text_color":"#ffffff",
/ J; o- n" S0 G8 E. `- ~ - // "destructive_text_color":"#ec3942",, [" r. x- Y7 k5 ]- L
- // "header_bg_color":"#17212b",
0 K! N# l/ J1 I! Y4 d4 {3 v" ` - // "hint_color":"#708499",; X3 J- m: Q0 X* K( O- p$ E; s
- // "link_color":"#6ab3f3",
# B8 v( n6 h+ @: t# \5 _& M0 e2 j; ] - // "secondary_bg_color":"#232e3c",# n; B& l; W2 p/ U( i( b
- // "section_bg_color":"#17212b",2 m+ d2 D7 A; w
- // "section_header_text_color":"#6ab3f3",
z5 i6 v& A8 W& j - // "subtitle_text_color":"#708499",4 P# [4 m8 E8 f% B7 [+ J
- // "text_color":"#f5f5f5"+ v6 G! H2 G: W3 H6 p' y' k
- // }`
复制代码
w* J+ K' |( f: |' z( s; Y4 ^1 M7 {4 L
, c3 v. M8 Q+ F$ Y% `
|