1- import { OverType } from '../../overtype/mock-overtype'
1+ import hljs from 'highlight.js'
2+ import OverType , { type OverTypeInstance } from '../../overtype/overtype'
23import type { CommentEnhancer , CommentSpot } from '../enhancer'
34
45const GITHUB_SPOT_TYPES = [
@@ -27,7 +28,7 @@ export class GitHubAddCommentEnhancer implements CommentEnhancer<GitHubAddCommen
2728 return [ ...GITHUB_SPOT_TYPES ]
2829 }
2930
30- tryToEnhance ( textarea : HTMLTextAreaElement ) : [ OverType , GitHubAddCommentSpot ] | null {
31+ tryToEnhance ( textarea : HTMLTextAreaElement ) : [ OverTypeInstance , GitHubAddCommentSpot ] | null {
3132 // Only handle github.com domains TODO: identify GitHub Enterprise somehow
3233 if ( window . location . hostname !== 'github.com' ) {
3334 return null
@@ -49,8 +50,31 @@ export class GitHubAddCommentEnhancer implements CommentEnhancer<GitHubAddCommen
4950 type : 'GH_PR_ADD_COMMENT' ,
5051 unique_key,
5152 }
52- const overtype = new OverType ( textarea )
53- return [ overtype , spot ]
53+ return [ this . createOvertypeFor ( textarea ) , spot ]
54+ }
55+
56+ private createOvertypeFor ( ghCommentBox : HTMLTextAreaElement ) : OverTypeInstance {
57+ OverType . setCodeHighlighter ( hljsHighlighter )
58+ const overtypeContainer = this . modifyDOM ( ghCommentBox )
59+ return new OverType ( overtypeContainer , {
60+ autoResize : true ,
61+ minHeight : '102px' ,
62+ padding : 'var(--base-size-8)' ,
63+ placeholder : 'Add your comment here...' ,
64+ } ) [ 0 ] !
65+ }
66+
67+ private modifyDOM ( overtypeInput : HTMLTextAreaElement ) : HTMLElement {
68+ overtypeInput . classList . add ( 'overtype-input' )
69+ const overtypePreview = document . createElement ( 'div' )
70+ overtypePreview . classList . add ( 'overtype-preview' )
71+ overtypeInput . insertAdjacentElement ( 'afterend' , overtypePreview )
72+ const overtypeWrapper = overtypeInput . parentElement ! . closest ( 'div' ) !
73+ overtypeWrapper . classList . add ( 'overtype-wrapper' )
74+ overtypeInput . placeholder = 'Add your comment here...'
75+ const overtypeContainer = overtypeWrapper . parentElement ! . closest ( 'div' ) !
76+ overtypeContainer . classList . add ( 'overtype-container' )
77+ return overtypeContainer . parentElement ! . closest ( 'div' ) !
5478 }
5579
5680 tableTitle ( spot : GitHubAddCommentSpot ) : string {
@@ -66,3 +90,18 @@ export class GitHubAddCommentEnhancer implements CommentEnhancer<GitHubAddCommen
6690 return `https://${ spot . domain } /${ spot . slug } /pull/${ spot . number } `
6791 }
6892}
93+
94+ function hljsHighlighter ( code : string , language : string ) {
95+ try {
96+ if ( language && hljs . getLanguage ( language ) ) {
97+ const result = hljs . highlight ( code , { language } )
98+ return result . value
99+ } else {
100+ const result = hljs . highlightAuto ( code )
101+ return result . value
102+ }
103+ } catch ( error ) {
104+ console . warn ( 'highlight.js highlighting failed:' , error )
105+ return code
106+ }
107+ }
0 commit comments