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