-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubIssueCreateEnhancer.tsx
More file actions
101 lines (90 loc) · 2.73 KB
/
GitHubIssueCreateEnhancer.tsx
File metadata and controls
101 lines (90 loc) · 2.73 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
import { IssueOpenedIcon } from "@primer/octicons-react"
import OverType, { type OverTypeInstance } from "overtype"
import { LinkOutOfPopup } from "@/components/LinkOutOfPopup"
import type {
CommentEnhancer,
CommentSpot,
StrippedLocation,
} from "../../enhancer"
import { logger } from "../../logger"
import { modifyDOM } from "../modifyDOM"
import { commonGitHubOptions, prepareGitHubHighlighter } from "./github-common"
const GH_ISSUE_CREATE = "GH_ISSUE_CREATE" as const
export interface GitHubIssueCreateSpot extends CommentSpot {
type: typeof GH_ISSUE_CREATE
domain: string
slug: string // owner/repo
title: string
}
export class GitHubIssueCreateEnhancer
implements CommentEnhancer<GitHubIssueCreateSpot>
{
forSpotTypes(): string[] {
return [GH_ISSUE_CREATE]
}
tryToEnhance(
textarea: HTMLTextAreaElement,
location: StrippedLocation
): GitHubIssueCreateSpot | null {
if (textarea.id === "feedback") {
return null
}
if (location.host !== "github.com") {
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\/new)/
)
logger.debug(`${this.constructor.name} found match`, location.pathname)
if (!match) return null
const [, owner, repo] = match
const slug = `${owner}/${repo}`
const unique_key = `github.com:${slug}:new`
const titleInput = document.querySelector(
'input[placeholder="Title"]'
) as HTMLInputElement
const title = titleInput?.value || ""
return {
domain: location.host,
slug,
title,
type: GH_ISSUE_CREATE,
unique_key,
}
}
enhance(
textArea: HTMLTextAreaElement,
_spot: GitHubIssueCreateSpot
): OverTypeInstance {
prepareGitHubHighlighter()
const overtypeContainer = modifyDOM(textArea)
return new OverType(overtypeContainer, {
...commonGitHubOptions,
minHeight: "400px",
placeholder: "Type your description here...",
})[0]!
}
tableUpperDecoration(spot: GitHubIssueCreateSpot): React.ReactNode {
return (
<>
<span className="flex h-4 w-4 flex-shrink-0 items-center justify-center">
<IssueOpenedIcon size={16} />
</span>
<span>
<draft>{" "}
<LinkOutOfPopup href={`https://${spot.domain}/${spot.slug}`}>
{spot.slug}
</LinkOutOfPopup>
</span>
</>
)
}
tableTitle(spot: GitHubIssueCreateSpot): string {
return spot.title || "<untitled issue>"
}
buildUrl(spot: GitHubIssueCreateSpot): string {
return `https://${spot.domain}/${spot.slug}/issue/new`
}
}