-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubPrAppendEnhancer.tsx
More file actions
108 lines (99 loc) · 3.02 KB
/
GitHubPrAppendEnhancer.tsx
File metadata and controls
108 lines (99 loc) · 3.02 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { GitPullRequestIcon } from "@primer/octicons-react"
import OverType, { type OverTypeInstance } from "overtype"
import type React from "react"
import type {
CommentEnhancer,
CommentSpot,
StrippedLocation,
} from "@/lib/enhancer"
import { logger } from "@/lib/logger"
import { modifyDOM } from "../modifyDOM"
import { commonGitHubOptions, prepareGitHubHighlighter } from "./github-common"
const GH_PR_APPEND = "GH_PR_APPEND" as const
export interface GitHubPrAppendSpot extends CommentSpot {
type: typeof GH_PR_APPEND
title: string
domain: string
slug: string // owner/repo
number: number // issue/PR number, undefined for new issues and PRs
}
export class GitHubPrAppendEnhancer
implements CommentEnhancer<GitHubPrAppendSpot>
{
forSpotTypes(): string[] {
return [GH_PR_APPEND]
}
tryToEnhance(
textarea: HTMLTextAreaElement,
location: StrippedLocation
): GitHubPrAppendSpot | null {
// Only handle github.com domains TODO: identify GitHub Enterprise somehow
if (location.host !== "github.com" || textarea.id !== "new_comment_field") {
return null
}
// Parse GitHub URL structure: /owner/repo/issues/123 or /owner/repo/pull/456
logger.debug(`${this.constructor.name} examing url`, location.pathname)
const match = location.pathname.match(
/^\/([^/]+)\/([^/]+)(?:\/pull\/(\d+))/
)
logger.debug(`${this.constructor.name} found match`, 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 = document
.querySelector("main h1")!
.textContent.replace(/\s*#\d+$/, "")
.trim()
return {
domain: location.host,
number,
slug,
title,
type: GH_PR_APPEND,
unique_key,
}
}
enhance(
textArea: HTMLTextAreaElement,
_spot: GitHubPrAppendSpot
): OverTypeInstance {
prepareGitHubHighlighter()
const overtypeContainer = modifyDOM(textArea)
const overtype = new OverType(overtypeContainer, {
...commonGitHubOptions,
minHeight: "102px",
padding: "var(--base-size-8)",
placeholder: "Add your comment here...",
})[0]!
const listenForEmpty = new MutationObserver(() => {
if (textArea.value === "") {
overtype.updatePreview()
}
})
listenForEmpty.observe(textArea, { attributes: true, characterData: true })
return overtype
}
tableUpperDecoration(spot: GitHubPrAppendSpot): React.ReactNode {
return (
<>
<span className="flex h-4 w-4 flex-shrink-0 items-center justify-center">
<GitPullRequestIcon size={16} />
</span>
<span>
#{spot.number}{" "}
<a
href={`https://${spot.domain}/${spot.slug}`}
className="truncate hover:underline"
>
{spot.slug}
</a>
</span>
</>
)
}
tableTitle(spot: GitHubPrAppendSpot): string {
return spot.title
}
}