-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithubPRNewComment.ts
More file actions
72 lines (61 loc) · 2.34 KB
/
githubPRNewComment.ts
File metadata and controls
72 lines (61 loc) · 2.34 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
import OverType, { type OverTypeInstance } from 'overtype'
import type { CommentEnhancer, CommentSpot } from '../../enhancer'
import { logger } from '../../logger'
import { modifyDOM } from '../modifyDOM'
import { githubHighlighter } from './githubHighlighter'
interface GitHubPRNewCommentSpot extends CommentSpot {
type: 'GH_PR_NEW_COMMENT'
domain: string
slug: string // owner/repo/base-branch/compare-branch
}
export class GitHubPRNewCommentEnhancer implements CommentEnhancer<GitHubPRNewCommentSpot> {
forSpotTypes(): string[] {
return ['GH_PR_NEW_COMMENT']
}
tryToEnhance(_textarea: HTMLTextAreaElement): GitHubPRNewCommentSpot | null {
if (document.querySelector('meta[name="hostname"]')?.getAttribute('content') !== 'github.com') {
return null
}
// /owner/repo/compare/feature/more-enhancers?expand=1
// or /owner/repo/compare/feat/issue-static-and-dynamic...feature/more-enhancers?expand=1
logger.info(`${this.constructor.name} examing url`, window.location.pathname)
const match = window.location.pathname.match(
/^\/([^/]+)\/([^/]+)\/compare\/(?:([^.?]+)\.\.\.)?([^?]+)/,
)
logger.info(`${this.constructor.name} found match`, window.location.pathname, match)
if (!match) return null
const [, owner, repo, baseBranch, compareBranch] = match
const slug = baseBranch
? `${owner}/${repo}/${baseBranch}...${compareBranch}`
: `${owner}/${repo}/${compareBranch}`
const unique_key = `github.com:${slug}`
return {
domain: 'github.com',
slug,
type: 'GH_PR_NEW_COMMENT',
unique_key,
}
}
prepareForFirstEnhancement(): void {
OverType.setCodeHighlighter(githubHighlighter)
}
enhance(textArea: HTMLTextAreaElement, _spot: GitHubPRNewCommentSpot): OverTypeInstance {
const overtypeContainer = modifyDOM(textArea)
return new OverType(overtypeContainer, {
autoResize: true,
minHeight: '250px',
padding: 'var(--base-size-16)',
placeholder: 'Type your description here...',
})[0]!
}
tableTitle(spot: GitHubPRNewCommentSpot): string {
const { slug } = spot
return `${slug} New Issue`
}
tableIcon(_: GitHubPRNewCommentSpot): string {
return '🔄' // PR icon TODO: icon urls in /public
}
buildUrl(spot: GitHubPRNewCommentSpot): string {
return `https://${spot.domain}/${spot.slug}/issue/new`
}
}