1
- const mastodonPostTemplate = document . createElement ( "template" ) ;
2
-
3
- mastodonPostTemplate . innerHTML = `
1
+ const mastodonPostTemplate = document . createElement ( "template" ) ; mastodonPostTemplate . innerHTML = `
4
2
<figure>
5
3
<blockquote data-key="content"></blockquote>
6
4
<figcaption>
@@ -14,102 +12,4 @@ mastodonPostTemplate.innerHTML = `
14
12
</dl>
15
13
</figcaption>
16
14
</figure>
17
- ` ;
18
-
19
- mastodonPostTemplate . id = "mastodon-post-template" ;
20
-
21
- if ( ! document . getElementById ( mastodonPostTemplate . id ) ) {
22
- document . body . appendChild ( mastodonPostTemplate ) ;
23
- }
24
-
25
- class MastodonPost extends HTMLElement {
26
- static register ( tagName ) {
27
- if ( "customElements" in window ) {
28
- customElements . define ( tagName || "mastodon-post" , MastodonPost ) ;
29
- }
30
- }
31
-
32
- async connectedCallback ( ) {
33
- this . append ( this . template ) ;
34
-
35
- const data = { ...( await this . data ) , ...this . linkData } ;
36
-
37
- this . slots . forEach ( ( slot ) => {
38
- slot . dataset . key . split ( "," ) . forEach ( ( keyItem ) => {
39
- const value = this . getValue ( keyItem , data ) ;
40
- if ( keyItem === "content" ) {
41
- slot . innerHTML = value ;
42
- } else {
43
- this . populateSlot ( slot , value ) ;
44
- }
45
- } ) ;
46
- } ) ;
47
- }
48
-
49
- populateSlot ( slot , value ) {
50
- if ( typeof value == "string" && value . startsWith ( "http" ) ) {
51
- if ( slot . localName === "img" ) slot . src = value ;
52
- if ( slot . localName === "a" ) slot . href = value ;
53
- } else {
54
- slot . textContent = value ;
55
- }
56
- }
57
-
58
- handleKey ( object , key ) {
59
- const parsedKeyInt = parseFloat ( key ) ;
60
-
61
- if ( Number . isNaN ( parsedKeyInt ) ) {
62
- return object [ key ] ;
63
- }
64
-
65
- return object [ parsedKeyInt ] ;
66
- }
67
-
68
- getValue ( string , data ) {
69
- let keys = string . trim ( ) . split ( / \. | \[ | \] / g) ;
70
- keys = keys . filter ( ( string ) => string . length ) ;
71
-
72
- const value = keys . reduce (
73
- ( object , key ) => this . handleKey ( object , key ) ,
74
- data
75
- ) ;
76
- return value ;
77
- }
78
-
79
- get template ( ) {
80
- return document
81
- . getElementById (
82
- this . getAttribute ( "template" ) || `${ this . localName } -template`
83
- )
84
- . content . cloneNode ( true ) ;
85
- }
86
-
87
- get slots ( ) {
88
- return this . querySelectorAll ( "[data-key]" ) ;
89
- }
90
-
91
- get link ( ) {
92
- return this . querySelector ( "a" ) . href ;
93
- }
94
-
95
- get linkData ( ) {
96
- const url = new URL ( this . link ) ;
97
- const paths = url . pathname . split ( "/" ) . filter ( ( string ) => string . length ) ;
98
- return {
99
- url : this . link ,
100
- hostname : url . hostname ,
101
- username : paths . find ( ( path ) => path . startsWith ( "@" ) ) ,
102
- postId : paths . find ( ( path ) => ! path . startsWith ( "@" ) )
103
- } ;
104
- }
105
-
106
- get endpoint ( ) {
107
- return `https://${ this . linkData . hostname } /api/v1/statuses/${ this . linkData . postId } ` ;
108
- }
109
-
110
- get data ( ) {
111
- return fetch ( this . endpoint ) . then ( ( response ) => response . json ( ) ) ;
112
- }
113
- }
114
-
115
- MastodonPost . register ( ) ;
15
+ ` , mastodonPostTemplate . id = "mastodon-post-template" , document . getElementById ( mastodonPostTemplate . id ) || document . body . appendChild ( mastodonPostTemplate ) ; class MastodonPost extends HTMLElement { static register ( e ) { "customElements" in window && customElements . define ( e || "mastodon-post" , MastodonPost ) } async connectedCallback ( ) { this . append ( this . template ) ; const e = { ...await this . data , ...this . linkData } ; this . querySelectorAll ( "[data-key]" ) . forEach ( async t => { const { key :s } = t . dataset , n = e [ s ] ; s === "content" ?t . innerHTML = n :typeof n == "string" && n . startsWith ( "http" ) ?( t . localName === "a" && ( t . href = n ) , t . localName === "img" && ( t . src = n ) ) :t . textContent = n } ) } get template ( ) { return document . getElementById ( mastodonPostTemplate . id ) . content . cloneNode ( ! 0 ) } get link ( ) { return this . querySelector ( "a" ) . href } get linkData ( ) { const e = new URL ( this . link ) , t = e . pathname . split ( "/" ) . filter ( e => e . length ) ; return { url :this . link , hostname :e . hostname , username :t . find ( e => e . startsWith ( "@" ) ) , postId :t . find ( e => ! e . startsWith ( "@" ) ) } } get endpoint ( ) { return `https://${ this . linkData . hostname } /api/v1/statuses/${ this . linkData . postId } ` } get data ( ) { return fetch ( this . endpoint ) . then ( e => e . json ( ) ) } } MastodonPost . register ( )
0 commit comments