1- import { CommentContext , TextareaHandler } from '../datamodel/textarea-handler' ;
1+ import type { CommentContext , TextareaHandler } from '../datamodel/textarea-handler'
22
3- export type GitHubCommentType =
3+ export type GitHubCommentType =
44 | 'GH_ISSUE_NEW'
55 | 'GH_PR_NEW'
66 | 'GH_ISSUE_ADD_COMMENT'
77 | 'GH_ISSUE_EDIT_COMMENT'
88 | 'GH_PR_ADD_COMMENT'
99 | 'GH_PR_EDIT_COMMENT'
10- | 'GH_PR_CODE_COMMENT' ;
10+ | 'GH_PR_CODE_COMMENT'
1111
1212export interface GitHubContext extends CommentContext {
13- type : GitHubCommentType ; // Override to narrow from string to specific union
14- domain : string ;
15- slug : string ; // owner/repo
16- number ?: number | undefined ; // issue/PR number
17- commentId ?: string | undefined ; // for editing existing comments
13+ type : GitHubCommentType // Override to narrow from string to specific union
14+ domain : string
15+ slug : string // owner/repo
16+ number ?: number | undefined // issue/PR number
17+ commentId ?: string | undefined // for editing existing comments
1818}
1919
2020export class GitHubHandler implements TextareaHandler < GitHubContext > {
21-
2221 forCommentTypes ( ) : string [ ] {
2322 return [
2423 'GH_ISSUE_NEW' ,
@@ -27,144 +26,144 @@ export class GitHubHandler implements TextareaHandler<GitHubContext> {
2726 'GH_ISSUE_EDIT_COMMENT' ,
2827 'GH_PR_ADD_COMMENT' ,
2928 'GH_PR_EDIT_COMMENT' ,
30- 'GH_PR_CODE_COMMENT'
31- ] ;
29+ 'GH_PR_CODE_COMMENT' ,
30+ ]
3231 }
3332
3433 identifyContextOf ( textarea : HTMLTextAreaElement ) : GitHubContext | null {
3534 // Only handle GitHub domains
3635 if ( ! window . location . hostname . includes ( 'github' ) ) {
37- return null ;
36+ return null
3837 }
3938
40- const pathname = window . location . pathname ;
41-
39+ const pathname = window . location . pathname
40+
4241 // Parse GitHub URL structure: /owner/repo/issues/123 or /owner/repo/pull/456
43- const match = pathname . match ( / ^ \/ ( [ ^ \/ ] + ) \/ ( [ ^ \/ ] + ) (?: \/ ( i s s u e s | p u l l ) \/ ( \d + ) ) ? / ) ;
44- if ( ! match ) return null ;
42+ const match = pathname . match ( / ^ \/ ( [ ^ / ] + ) \/ ( [ ^ / ] + ) (?: \/ ( i s s u e s | p u l l ) \/ ( \d + ) ) ? / )
43+ if ( ! match ) return null
44+
45+ const [ , owner , repo , urlType , numberStr ] = match
46+ const slug = `${ owner } /${ repo } `
47+ const number = numberStr ? parseInt ( numberStr , 10 ) : undefined
4548
46- const [ , owner , repo , urlType , numberStr ] = match ;
47- const slug = `${ owner } /${ repo } ` ;
48- const number = numberStr ? parseInt ( numberStr , 10 ) : undefined ;
49-
5049 // Check if editing existing comment
51- const commentId = this . getCommentId ( textarea ) ;
52-
50+ const commentId = this . getCommentId ( textarea )
51+
5352 // Determine comment type
54- let type : GitHubCommentType ;
55-
53+ let type : GitHubCommentType
54+
5655 // New issue
5756 if ( pathname . includes ( '/issues/new' ) ) {
58- type = 'GH_ISSUE_NEW' ;
57+ type = 'GH_ISSUE_NEW'
5958 }
6059 // New PR
6160 else if ( pathname . includes ( '/compare/' ) || pathname . endsWith ( '/compare' ) ) {
62- type = 'GH_PR_NEW' ;
61+ type = 'GH_PR_NEW'
6362 }
6463 // Existing issue or PR page
6564 else if ( urlType && number ) {
66- const isEditingComment = commentId !== null ;
67-
65+ const isEditingComment = commentId !== null
66+
6867 if ( urlType === 'issues' ) {
69- type = isEditingComment ? 'GH_ISSUE_EDIT_COMMENT' : 'GH_ISSUE_ADD_COMMENT' ;
68+ type = isEditingComment ? 'GH_ISSUE_EDIT_COMMENT' : 'GH_ISSUE_ADD_COMMENT'
7069 } else {
7170 // Check if it's a code comment (in Files Changed tab)
72- const isCodeComment = textarea . closest ( '.js-inline-comment-form' ) !== null ||
73- textarea . closest ( '[data-path]' ) !== null ;
74-
71+ const isCodeComment =
72+ textarea . closest ( '.js-inline-comment-form' ) !== null ||
73+ textarea . closest ( '[data-path]' ) !== null
74+
7575 if ( isCodeComment ) {
76- type = 'GH_PR_CODE_COMMENT' ;
76+ type = 'GH_PR_CODE_COMMENT'
7777 } else {
78- type = isEditingComment ? 'GH_PR_EDIT_COMMENT' : 'GH_PR_ADD_COMMENT' ;
78+ type = isEditingComment ? 'GH_PR_EDIT_COMMENT' : 'GH_PR_ADD_COMMENT'
7979 }
8080 }
8181 } else {
82- return null ;
82+ return null
8383 }
84-
84+
8585 // Generate unique key based on context
86- let unique_key = `github:${ slug } ` ;
86+ let unique_key = `github:${ slug } `
8787 if ( number ) {
88- unique_key += `:${ urlType } :${ number } ` ;
88+ unique_key += `:${ urlType } :${ number } `
8989 } else {
90- unique_key += ':new' ;
90+ unique_key += ':new'
9191 }
92-
92+
9393 if ( commentId ) {
94- unique_key += `:edit:${ commentId } ` ;
94+ unique_key += `:edit:${ commentId } `
9595 }
9696
9797 const context : GitHubContext = {
98- unique_key,
99- type,
98+ commentId : commentId || undefined ,
10099 domain : window . location . hostname ,
101- slug,
102100 number,
103- commentId : commentId || undefined
104- } ;
101+ slug,
102+ type,
103+ unique_key,
104+ }
105105
106- return context ;
106+ return context
107107 }
108108
109109 generateDisplayTitle ( context : GitHubContext ) : string {
110- const { slug, number, commentId } = context ;
111-
110+ const { slug, number, commentId } = context
111+
112112 if ( commentId ) {
113- return `Edit comment in ${ slug } ${ number ? ` #${ number } ` : '' } ` ;
113+ return `Edit comment in ${ slug } ${ number ? ` #${ number } ` : '' } `
114114 }
115-
115+
116116 if ( number ) {
117- return `Comment on ${ slug } #${ number } ` ;
117+ return `Comment on ${ slug } #${ number } `
118118 }
119-
120- return `New ${ window . location . pathname . includes ( '/issues/' ) ? 'issue' : 'PR' } in ${ slug } ` ;
119+
120+ return `New ${ window . location . pathname . includes ( '/issues/' ) ? 'issue' : 'PR' } in ${ slug } `
121121 }
122122
123123 generateIcon ( context : GitHubContext ) : string {
124124 switch ( context . type ) {
125125 case 'GH_ISSUE_NEW' :
126126 case 'GH_ISSUE_ADD_COMMENT' :
127127 case 'GH_ISSUE_EDIT_COMMENT' :
128- return '🐛' ; // Issue icon
128+ return '🐛' // Issue icon
129129 case 'GH_PR_NEW' :
130130 case 'GH_PR_ADD_COMMENT' :
131131 case 'GH_PR_EDIT_COMMENT' :
132- return '🔄' ; // PR icon
132+ return '🔄' // PR icon
133133 case 'GH_PR_CODE_COMMENT' :
134- return '💬' ; // Code comment icon
134+ return '💬' // Code comment icon
135135 default :
136- return '📝' ; // Generic comment icon
136+ return '📝' // Generic comment icon
137137 }
138138 }
139139
140140 buildUrl ( context : GitHubContext ) : string {
141- const baseUrl = `https://${ context . domain } /${ context . slug } ` ;
142-
141+ const baseUrl = `https://${ context . domain } /${ context . slug } `
142+
143143 if ( context . number ) {
144- const type = window . location . pathname . includes ( '/issues/' ) ? 'issues' : 'pull' ;
145- return `${ baseUrl } /${ type } /${ context . number } ${ context . commentId ? `#issuecomment-${ context . commentId } ` : '' } ` ;
144+ const type = window . location . pathname . includes ( '/issues/' ) ? 'issues' : 'pull'
145+ return `${ baseUrl } /${ type } /${ context . number } ${ context . commentId ? `#issuecomment-${ context . commentId } ` : '' } `
146146 }
147-
148- return baseUrl ;
147+
148+ return baseUrl
149149 }
150150
151151 private getCommentId ( textarea : HTMLTextAreaElement ) : string | null {
152152 // Look for edit comment form indicators
153- const commentForm = textarea . closest ( '[data-comment-id]' ) ;
153+ const commentForm = textarea . closest ( '[data-comment-id]' )
154154 if ( commentForm ) {
155- return commentForm . getAttribute ( 'data-comment-id' ) ;
155+ return commentForm . getAttribute ( 'data-comment-id' )
156156 }
157-
158- const editForm = textarea . closest ( '.js-comment-edit-form' ) ;
157+
158+ const editForm = textarea . closest ( '.js-comment-edit-form' )
159159 if ( editForm ) {
160- const commentContainer = editForm . closest ( '.js-comment-container' ) ;
160+ const commentContainer = editForm . closest ( '.js-comment-container' )
161161 if ( commentContainer ) {
162- const id = commentContainer . getAttribute ( 'data-gid' ) ||
163- commentContainer . getAttribute ( 'id' ) ;
164- return id ? id . replace ( 'issuecomment-' , '' ) : null ;
162+ const id = commentContainer . getAttribute ( 'data-gid' ) || commentContainer . getAttribute ( 'id' )
163+ return id ? id . replace ( 'issuecomment-' , '' ) : null
165164 }
166165 }
167-
168- return null ;
166+
167+ return null
169168 }
170- }
169+ }
0 commit comments