Skip to content

Commit 773d7ed

Browse files
committed
Plausible this could work.
1 parent 2f5d81d commit 773d7ed

File tree

4 files changed

+51
-56
lines changed

4 files changed

+51
-56
lines changed

browser-extension/src/lib/enhancer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { OverType } from '../overtype/mock-overtype'
1+
import type { OverTypeInstance } from '../overtype/overtype'
22

33
/**
44
* stores enough info about the location of a draft to:
@@ -18,7 +18,7 @@ export interface CommentEnhancer<Spot extends CommentSpot = CommentSpot> {
1818
* whenever a new `textarea` is added to any webpage, this method is called.
1919
* if we return non-null, then we become the handler for that text area.
2020
*/
21-
tryToEnhance(textarea: HTMLTextAreaElement): [OverType, Spot] | null
21+
tryToEnhance(textarea: HTMLTextAreaElement): [OverTypeInstance, Spot] | null
2222

2323
tableIcon(spot: Spot): string
2424
tableTitle(spot: Spot): string

browser-extension/src/lib/enhancers/github.ts

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { OverType } from '../../overtype/mock-overtype'
1+
import hljs from 'highlight.js'
2+
import OverType, { type OverTypeInstance } from '../../overtype/overtype'
23
import type { CommentEnhancer, CommentSpot } from '../enhancer'
34

45
const 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+
}

browser-extension/src/lib/registries.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { OverType } from '../overtype/mock-overtype'
1+
import type { OverTypeInstance } from '../overtype/overtype'
22
import type { CommentEnhancer, CommentSpot } from './enhancer'
33
import { GitHubAddCommentEnhancer } from './enhancers/github'
44

55
export interface EnhancedTextarea<T extends CommentSpot = CommentSpot> {
66
textarea: HTMLTextAreaElement
77
spot: T
8-
handler: CommentEnhancer<T>
9-
overtype: OverType
8+
enhancer: CommentEnhancer<T>
9+
overtype: OverTypeInstance
1010
}
1111

1212
export class EnhancerRegistry {
@@ -22,12 +22,12 @@ export class EnhancerRegistry {
2222
}
2323

2424
tryToEnhance(textarea: HTMLTextAreaElement): EnhancedTextarea<any> | null {
25-
for (const handler of this.enhancers) {
25+
for (const enhancer of this.enhancers) {
2626
try {
27-
const result = handler.tryToEnhance(textarea)
27+
const result = enhancer.tryToEnhance(textarea)
2828
if (result) {
2929
const [overtype, spot] = result
30-
return { handler, overtype, spot, textarea }
30+
return { enhancer, overtype, spot, textarea }
3131
}
3232
} catch (error) {
3333
console.warn('Handler failed to identify textarea:', error)

browser-extension/src/overtype/mock-overtype.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)