Skip to content

Commit e60d10c

Browse files
committed
Enhance editing comments in issues and PRs.
1 parent 9b299c3 commit e60d10c

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import OverType, { type OverTypeInstance } from 'overtype'
2+
import type React from 'react'
3+
import type { CommentEnhancer, CommentSpot } from '@/lib/enhancer'
4+
import { logger } from '@/lib/logger'
5+
import { modifyDOM } from '../modifyDOM'
6+
import { commonGithubOptions } from './ghOptions'
7+
import { githubHighlighter } from './githubHighlighter'
8+
9+
export interface GitHubEditCommentSpot extends CommentSpot {
10+
type: 'GH_EDIT_COMMENT'
11+
title: string
12+
domain: string
13+
slug: string
14+
number: number
15+
}
16+
17+
export class GitHubEditCommentEnhancer implements CommentEnhancer<GitHubEditCommentSpot> {
18+
forSpotTypes(): string[] {
19+
return ['GH_EDIT_COMMENT']
20+
}
21+
22+
tryToEnhance(_textarea: HTMLTextAreaElement): GitHubEditCommentSpot | null {
23+
if (
24+
document.querySelector('meta[name="hostname"]')?.getAttribute('content') !== 'github.com' ||
25+
!_textarea.matches('.TimelineItem textarea')
26+
) {
27+
return null
28+
}
29+
30+
// Parse GitHub URL structure: /owner/repo/issues/123 or /owner/repo/pull/456
31+
logger.info(`${this.constructor.name} examing url`, window.location.pathname)
32+
33+
const match = window.location.pathname.match(/^\/([^/]+)\/([^/]+)(?:\/(pull|issues)\/(\d+))/)
34+
logger.info(`${this.constructor.name} found match`, window.location.pathname)
35+
if (!match) return null
36+
const [, owner, repo, numberStr] = match
37+
const slug = `${owner}/${repo}`
38+
const number = parseInt(numberStr!, 10)
39+
const unique_key = `github.com:${slug}:${number}`
40+
const title = 'TODO_TITLE'
41+
return {
42+
domain: 'github.com',
43+
number,
44+
slug,
45+
title,
46+
type: 'GH_EDIT_COMMENT',
47+
unique_key,
48+
}
49+
}
50+
51+
prepareForFirstEnhancement(): void {
52+
OverType.setCodeHighlighter(githubHighlighter)
53+
}
54+
55+
enhance(textArea: HTMLTextAreaElement, _spot: GitHubEditCommentSpot): OverTypeInstance {
56+
const overtypeContainer = modifyDOM(textArea)
57+
return new OverType(overtypeContainer, {
58+
...commonGithubOptions,
59+
minHeight: '102px',
60+
padding: 'var(--base-size-8)',
61+
placeholder: 'Add your comment here...',
62+
})[0]!
63+
}
64+
65+
tableUpperDecoration(spot: GitHubEditCommentSpot): React.ReactNode {
66+
const { slug, number } = spot
67+
return (
68+
<>
69+
<span className='font-mono text-muted-foreground text-sm'>{slug}</span>
70+
<span className='ml-2 font-medium'>PR #{number}</span>
71+
</>
72+
)
73+
}
74+
75+
tableTitle(_spot: GitHubEditCommentSpot): string {
76+
return 'TITLE_TODO'
77+
}
78+
}

browser-extension/src/lib/enhancers/github/githubPRNewComment.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ export class GitHubPRNewCommentEnhancer implements CommentEnhancer<GitHubPRNewCo
2323

2424
// /owner/repo/compare/feature/more-enhancers?expand=1
2525
// or /owner/repo/compare/feat/issue-static-and-dynamic...feature/more-enhancers?expand=1
26-
logger.info(`${this.constructor.name} examing url`, window.location.pathname)
26+
logger.debug(`${this.constructor.name} examing url`, window.location.pathname)
2727

2828
const match = window.location.pathname.match(
2929
/^\/([^/]+)\/([^/]+)\/compare\/(?:([^.?]+)\.\.\.)?([^?]+)/,
3030
)
31-
logger.info(`${this.constructor.name} found match`, window.location.pathname, match)
31+
logger.debug(`${this.constructor.name} found match`, window.location.pathname, match)
3232

3333
if (!match) return null
3434
const [, owner, repo, baseBranch, compareBranch] = match

browser-extension/src/lib/registries.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { OverTypeInstance } from 'overtype'
22
import OverType from 'overtype'
33
import type { CommentEnhancer, CommentSpot } from './enhancer'
44
import { CommentEnhancerMissing } from './enhancers/CommentEnhancerMissing'
5+
import { GitHubEditCommentEnhancer } from './enhancers/github/githubEditComment'
56
import { GitHubIssueAddCommentEnhancer } from './enhancers/github/githubIssueAddComment'
67
import { GitHubIssueNewCommentEnhancer } from './enhancers/github/githubIssueNewComment'
78
import { GitHubPRAddCommentEnhancer } from './enhancers/github/githubPRAddComment'
@@ -21,6 +22,7 @@ export class EnhancerRegistry {
2122

2223
constructor() {
2324
// Register all available handlers
25+
this.register(new GitHubEditCommentEnhancer())
2426
this.register(new GitHubIssueAddCommentEnhancer())
2527
this.register(new GitHubIssueNewCommentEnhancer())
2628
this.register(new GitHubPRAddCommentEnhancer())

0 commit comments

Comments
 (0)