@@ -27,62 +27,13 @@ export type Data<
2727 }
2828}
2929
30- export const errorId = 'root_error'
31- export const rootId = 'root'
32- const dataId = '__MPPX_DATA__'
30+ export { attrs , classNames , ids , params } from './constants.js'
31+ import { attrs , classNames , ids } from './constants.js'
3332
34- export const serviceWorkerParam = '__mppx_worker'
35-
36- const challengeIdAttr = 'data-mppx-challenge-id'
37- const remainingAttr = 'data-remaining'
38-
39- export function getData <
40- method extends Method . Method = Method . Method ,
41- config extends Record < string , unknown > = { } ,
42- > ( methodName : method [ 'name' ] ) : Data < method , config > {
43- const el = document . getElementById ( dataId ) !
44- const map = Json . parse ( el . textContent ) as Record < string , Data < method , config > >
45- const remaining = el . getAttribute ( remainingAttr )
46- if ( ! remaining || Number ( remaining ) <= 1 ) el . remove ( )
47- else el . setAttribute ( remainingAttr , String ( Number ( remaining ) - 1 ) )
48- const script = document . currentScript
49- const challengeId = script ?. getAttribute ( challengeIdAttr )
50- if ( challengeId ) {
51- script ! . removeAttribute ( challengeIdAttr )
52- return map [ challengeId ] !
53- }
54- return Object . values ( map ) . find ( ( d ) => d . challenge . method === methodName ) !
55- }
56-
57- export function showError ( message : string ) {
58- const existing = document . getElementById ( errorId )
59- if ( existing ) {
60- existing . textContent = message
61- return
62- }
63- const el = document . createElement ( 'p' )
64- el . id = errorId
65- el . className = classNames . error
66- el . role = 'alert'
67- el . textContent = message
68- document . getElementById ( rootId ) ?. after ( el )
69- }
70-
71- const classNames = {
72- error : 'mppx-error' ,
73- header : 'mppx-header' ,
74- logo : 'mppx-logo' ,
75- logoColorScheme : ( colorScheme : string ) =>
76- colorScheme === 'dark' || colorScheme === 'light'
77- ? `${ classNames . logo } --${ colorScheme } `
78- : undefined ,
79- summary : 'mppx-summary' ,
80- summaryAmount : 'mppx-summary-amount' ,
81- summaryDescription : 'mppx-summary-description' ,
82- summaryExpires : 'mppx-summary-expires' ,
83- tab : 'mppx-tab' ,
84- tabList : 'mppx-tablist' ,
85- tabPanel : 'mppx-tabpanel' ,
33+ function logoColorScheme ( colorScheme : string ) {
34+ return colorScheme === 'dark' || colorScheme === 'light'
35+ ? `${ classNames . logo } --${ colorScheme } `
36+ : undefined
8637}
8738
8839class CssVar {
@@ -282,18 +233,18 @@ export function render(options: {
282233 id ="mppx-panel- ${ i } "
283234 ${ i !== 0 ? 'hidden' : '' }
284235 >
285- < div id ="${ rootId } - ${ i } " aria-label ="Payment form "> </ div >
236+ < div id ="${ ids . root } - ${ i } " aria-label ="Payment form "> </ div >
286237 </ div > ` ,
287238 )
288239 . join ( '' )
289- : html `< div id ="${ rootId } " aria-label ="Payment form "> </ div > `
240+ : html `< div id ="${ ids . root } " aria-label ="Payment form "> </ div > `
290241
291242 const contentScripts = hasTabs
292243 ? entries
293244 . map ( ( entry ) =>
294245 entry . content . replace (
295246 '<script>' ,
296- `<script ${ challengeIdAttr } ="${ sanitize ( entry . challenge . id ) } ">` ,
247+ `<script ${ attrs . challengeId } ="${ sanitize ( entry . challenge . id ) } ">` ,
297248 ) ,
298249 )
299250 . join ( '\n' )
@@ -327,9 +278,9 @@ export function render(options: {
327278 </ section >
328279 ${ tabListHtml } ${ panelsHtml }
329280 < script
330- id ="${ dataId } "
281+ id ="${ ids . data } "
331282 type ="application/json "
332- ${ entries . length > 1 ? ` ${ remainingAttr } ="${ entries . length } "` : '' }
283+ ${ entries . length > 1 ? ` ${ attrs . remaining } ="${ entries . length } "` : '' }
333284 >
334285 ${ Json . stringify ( dataMap satisfies Record < string , Data > ) . replace ( / < / g, '\\u003c' ) }
335286 </ script >
@@ -410,12 +361,12 @@ function style(theme: {
410361 .${ classNames . logo } {
411362 max-height : 1.75rem ;
412363 }
413- .${ classNames . logoColorScheme ( 'dark' ) } {
364+ .${ logoColorScheme ( 'dark' ) } {
414365 @media (prefers-color-scheme : light) {
415366 display : none;
416367 }
417368 }
418- .${ classNames . logoColorScheme ( 'light' ) } {
369+ .${ logoColorScheme ( 'light' ) } {
419370 @media (prefers-color-scheme : dark) {
420371 display : none;
421372 }
@@ -497,7 +448,7 @@ function logo(value: Theme) {
497448 ( entry ) =>
498449 html `< img
499450 alt =""
500- class ="${ classNames . logo } ${ classNames . logoColorScheme ( entry [ 0 ] ) } "
451+ class ="${ classNames . logo } ${ logoColorScheme ( entry [ 0 ] ) } "
501452 src ="${ sanitize ( entry [ 1 ] ) } "
502453 /> ` ,
503454 )
0 commit comments