Skip to content

Commit 4d38467

Browse files
authored
GitHub issue add-comment (#14)
2 parents 08832f9 + 50daae0 commit 4d38467

File tree

6 files changed

+93
-20
lines changed

6 files changed

+93
-20
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import OverType, { type OverTypeInstance } from '../../../overtype/overtype'
2+
import type { CommentEnhancer, CommentSpot } from '../../enhancer'
3+
import { logger } from '../../logger'
4+
import { modifyDOM } from '../modifyDOM'
5+
import { githubHighlighter } from './githubHighlighter'
6+
7+
interface GitHubIssueAddCommentSpot extends CommentSpot {
8+
type: 'GH_ISSUE_ADD_COMMENT'
9+
domain: string
10+
slug: string // owner/repo
11+
number: number // issue number, undefined for new issues
12+
}
13+
14+
export class GitHubIssueAddCommentEnhancer implements CommentEnhancer<GitHubIssueAddCommentSpot> {
15+
forSpotTypes(): string[] {
16+
return ['GH_ISSUE_ADD_COMMENT']
17+
}
18+
19+
tryToEnhance(_textarea: HTMLTextAreaElement): GitHubIssueAddCommentSpot | null {
20+
if (window.location.hostname !== 'github.com' || _textarea.id !== ':r2v:') {
21+
return null
22+
}
23+
24+
// Parse GitHub URL structure: /owner/repo/issues/123 or /owner/repo/pull/456
25+
logger.debug(`${this.constructor.name} examing url`, window.location.pathname)
26+
27+
const match = window.location.pathname.match(/^\/([^/]+)\/([^/]+)(?:\/issues\/(\d+))/)
28+
logger.debug(`${this.constructor.name} found match`, window.location.pathname)
29+
if (!match) return null
30+
const [, owner, repo, numberStr] = match
31+
const slug = `${owner}/${repo}`
32+
const number = parseInt(numberStr!, 10)
33+
const unique_key = `github.com:${slug}:${number}`
34+
return {
35+
domain: 'github.com',
36+
number,
37+
slug,
38+
type: 'GH_ISSUE_ADD_COMMENT',
39+
unique_key,
40+
}
41+
}
42+
43+
prepareForFirstEnhancement(): void {
44+
OverType.setCodeHighlighter(githubHighlighter)
45+
}
46+
47+
enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueAddCommentSpot): OverTypeInstance {
48+
const overtypeContainer = modifyDOM(textArea)
49+
return new OverType(overtypeContainer, {
50+
autoResize: true,
51+
minHeight: '100px',
52+
padding: 'var(--base-size-16)',
53+
placeholder: 'Use Markdown to format your comment',
54+
})[0]!
55+
}
56+
57+
tableTitle(spot: GitHubIssueAddCommentSpot): string {
58+
const { slug, number } = spot
59+
return `${slug} Issue #${number}`
60+
}
61+
62+
tableIcon(_: GitHubIssueAddCommentSpot): string {
63+
return '🔄' // PR icon TODO: icon urls in /public
64+
}
65+
66+
buildUrl(spot: GitHubIssueAddCommentSpot): string {
67+
return `https://${spot.domain}/${spot.slug}/issue/${spot.number}`
68+
}
69+
}

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

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import OverType, { type OverTypeInstance } from '../../../overtype/overtype'
22
import type { CommentEnhancer, CommentSpot } from '../../enhancer'
33
import { logger } from '../../logger'
4+
import { modifyDOM } from '../modifyDOM'
45
import { githubHighlighter } from './githubHighlighter'
5-
import { GITHUB_SPOT_TYPES, type GitHubSpotType } from './githubSpotTypes'
66

77
interface GitHubPRAddCommentSpot extends CommentSpot {
8-
type: GitHubSpotType // Override to narrow from string to specific union
8+
type: 'GH_PR_ADD_COMMENT' // Override to narrow from string to specific union
99
domain: string
1010
slug: string // owner/repo
1111
number: number // issue/PR number, undefined for new issues and PRs
1212
}
1313

1414
export class GitHubPRAddCommentEnhancer implements CommentEnhancer<GitHubPRAddCommentSpot> {
1515
forSpotTypes(): string[] {
16-
return [...GITHUB_SPOT_TYPES]
16+
return ['GH_PR_ADD_COMMENT']
1717
}
1818

1919
tryToEnhance(_textarea: HTMLTextAreaElement): GitHubPRAddCommentSpot | null {
2020
// Only handle github.com domains TODO: identify GitHub Enterprise somehow
21-
if (window.location.hostname !== 'github.com') {
21+
if (window.location.hostname !== 'github.com' || _textarea.id !== 'new_comment_field') {
2222
return null
2323
}
2424

@@ -46,7 +46,7 @@ export class GitHubPRAddCommentEnhancer implements CommentEnhancer<GitHubPRAddCo
4646
}
4747

4848
enhance(textArea: HTMLTextAreaElement, _spot: GitHubPRAddCommentSpot): OverTypeInstance {
49-
const overtypeContainer = this.modifyDOM(textArea)
49+
const overtypeContainer = modifyDOM(textArea)
5050
return new OverType(overtypeContainer, {
5151
autoResize: true,
5252
minHeight: '102px',
@@ -55,19 +55,6 @@ export class GitHubPRAddCommentEnhancer implements CommentEnhancer<GitHubPRAddCo
5555
})[0]!
5656
}
5757

58-
private modifyDOM(overtypeInput: HTMLTextAreaElement): HTMLElement {
59-
overtypeInput.classList.add('overtype-input')
60-
const overtypePreview = document.createElement('div')
61-
overtypePreview.classList.add('overtype-preview')
62-
overtypeInput.insertAdjacentElement('afterend', overtypePreview)
63-
const overtypeWrapper = overtypeInput.parentElement!.closest('div')!
64-
overtypeWrapper.classList.add('overtype-wrapper')
65-
overtypeInput.placeholder = 'Add your comment here...'
66-
const overtypeContainer = overtypeWrapper.parentElement!.closest('div')!
67-
overtypeContainer.classList.add('overtype-container')
68-
return overtypeContainer.parentElement!.closest('div')!
69-
}
70-
7158
tableTitle(spot: GitHubPRAddCommentSpot): string {
7259
const { slug, number } = spot
7360
return `${slug} PR #${number}`

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export const GITHUB_SPOT_TYPES = [
22
'GH_PR_ADD_COMMENT',
3+
'GH_ISSUE_ADD_COMMENT',
34
/* TODO
45
'GH_ISSUE_NEW',
56
'GH_PR_NEW',
6-
'GH_ISSUE_ADD_COMMENT',
77
'GH_ISSUE_EDIT_COMMENT',
88
'GH_PR_EDIT_COMMENT',
99
'GH_PR_CODE_COMMENT',
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Modify the DOM to trick overtype into adopting it instead of recreating it
2+
export function modifyDOM(overtypeInput: HTMLTextAreaElement): HTMLElement {
3+
overtypeInput.classList.add('overtype-input')
4+
const overtypePreview = document.createElement('div')
5+
overtypePreview.classList.add('overtype-preview')
6+
overtypeInput.insertAdjacentElement('afterend', overtypePreview)
7+
const overtypeWrapper = overtypeInput.parentElement!.closest('div')!
8+
overtypeWrapper.classList.add('overtype-wrapper')
9+
overtypeInput.placeholder = 'Add your comment here...'
10+
const overtypeContainer = overtypeWrapper.parentElement!.closest('div')!
11+
overtypeContainer.classList.add('overtype-container')
12+
return overtypeContainer.parentElement!.closest('div')!
13+
}

browser-extension/src/lib/registries.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { OverTypeInstance } from '../overtype/overtype'
22
import type { CommentEnhancer, CommentSpot } from './enhancer'
3+
import { GitHubIssueAddCommentEnhancer } from './enhancers/github/githubIssueAddComment'
34
import { GitHubPRAddCommentEnhancer } from './enhancers/github/githubPRAddComment'
45

56
export interface EnhancedTextarea<T extends CommentSpot = CommentSpot> {
@@ -15,6 +16,7 @@ export class EnhancerRegistry {
1516

1617
constructor() {
1718
// Register all available handlers
19+
this.register(new GitHubIssueAddCommentEnhancer())
1820
this.register(new GitHubPRAddCommentEnhancer())
1921
}
2022

browser-extension/src/overtype/styles.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ export function generateStyles(options = {}) {
5454
}
5555
5656
/* Middle-ground CSS Reset - Prevent parent styles from leaking in */
57-
.overtype-container * {
57+
.overtype-container .overtype-wrapper,
58+
.overtype-container .overtype-input,
59+
.overtype-container .overtype-preview {
5860
/* Box model - these commonly leak */
5961
/* margin: 0 !important; */
6062
padding: 0 !important;

0 commit comments

Comments
 (0)