-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubIssueAppendEnhancer.tsx
More file actions
141 lines (129 loc) · 3.97 KB
/
GitHubIssueAppendEnhancer.tsx
File metadata and controls
141 lines (129 loc) · 3.97 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { IssueOpenedIcon } from "@primer/octicons-react"
import OverType, { type OverTypeInstance } from "overtype"
import type React from "react"
import { LinkOutOfPopup } from "@/components/LinkOutOfPopup"
import type {
CommentEnhancer,
CommentSpot,
StrippedLocation,
} from "@/lib/enhancer"
import { logger } from "@/lib/logger"
import { fixupOvertype, modifyDOM } from "../overtype-misc"
import {
commonGitHubOptions,
isInProjectCommentBox,
isProjectUrl,
parseProjectIssueParam,
prepareGitHubHighlighter,
} from "./github-common"
const GH_ISSUE_APPEND = "GH_ISSUE_APPEND" as const
export interface GitHubIssueAppendSpot extends CommentSpot {
type: typeof GH_ISSUE_APPEND
title: string
domain: string
slug: string // owner/repo
number: number // issue number, undefined for new issues
}
export class GitHubIssueAppendEnhancer
implements CommentEnhancer<GitHubIssueAppendSpot>
{
forSpotTypes(): string[] {
return [GH_ISSUE_APPEND]
}
tryToEnhance(
textarea: HTMLTextAreaElement,
location: StrippedLocation
): GitHubIssueAppendSpot | null {
if (textarea.id === "feedback") {
return null
}
if (location.host !== "github.com") {
return null
}
// Don't enhance textareas that are within the issue/PR body editing container
const bodyContainer = textarea.closest(".react-issue-body")
if (bodyContainer) {
return null
}
// Check for project URLs with issue parameter first
if (isProjectUrl(location.pathname)) {
const params = new URLSearchParams(location.search)
// Only match textareas within Shared-module__CommentBox (those are for adding new comments)
if (isInProjectCommentBox(textarea)) {
const issueInfo = parseProjectIssueParam(params)
if (issueInfo) {
const unique_key = `github.com:${issueInfo.slug}:${issueInfo.number}`
// For project views, the title is in the side panel dialog
const title =
document
.querySelector('[data-testid="issue-title"]')
?.textContent?.trim() || ""
return {
domain: location.host,
number: issueInfo.number,
slug: issueInfo.slug,
title,
type: GH_ISSUE_APPEND,
unique_key,
}
}
}
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(
/^\/([^/]+)\/([^/]+)(?:\/issues\/(\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_ISSUE_APPEND,
unique_key,
}
}
enhance(
textArea: HTMLTextAreaElement,
_spot: GitHubIssueAppendSpot
): OverTypeInstance {
prepareGitHubHighlighter()
const overtypeContainer = modifyDOM(textArea)
return fixupOvertype(
new OverType(overtypeContainer, {
...commonGitHubOptions,
minHeight: "100px",
placeholder: "Use Markdown to format your comment",
})
)
}
tableUpperDecoration(spot: GitHubIssueAppendSpot): React.ReactNode {
return (
<>
<span className="flex h-4 w-4 flex-shrink-0 items-center justify-center">
<IssueOpenedIcon size={16} />
</span>
<span>
#{spot.number}{" "}
<LinkOutOfPopup href={`https://${spot.domain}/${spot.slug}`}>
{spot.slug}
</LinkOutOfPopup>
</span>
</>
)
}
tableTitle(spot: GitHubIssueAppendSpot): string {
return spot.title
}
}