-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubEditEnhancer.tsx
More file actions
139 lines (124 loc) · 3.99 KB
/
GitHubEditEnhancer.tsx
File metadata and controls
139 lines (124 loc) · 3.99 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
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 { fixupOvertype, modifyDOM } from "../overtype-misc"
import {
commonGitHubOptions,
isInProjectCommentBox,
isProjectUrl,
parseProjectIssueParam,
prepareGitHubHighlighter,
} from "./github-common"
const GH_EDIT = "GH_EDIT" as const
export interface GitHubEditSpot extends CommentSpot {
isIssue: boolean
type: typeof GH_EDIT
}
export class GitHubEditEnhancer implements CommentEnhancer<GitHubEditSpot> {
forSpotTypes(): string[] {
return [GH_EDIT]
}
tryToEnhance(
textarea: HTMLTextAreaElement,
location: StrippedLocation
): GitHubEditSpot | null {
if (location.host !== "github.com") {
return null
}
// Check for project draft edit first
if (isProjectUrl(location.pathname)) {
const params = new URLSearchParams(location.search)
const itemId = params.get("itemId")
// Handle draft editing (itemId parameter)
if (itemId) {
// Exclude textareas within Shared-module__CommentBox (those are for adding new comments, not editing)
if (!isInProjectCommentBox(textarea)) {
const unique_key = `github.com:project-draft:${itemId}:edit-body`
logger.debug(
`${this.constructor.name} enhanced project draft body textarea`,
unique_key
)
return {
isIssue: true,
type: GH_EDIT,
unique_key,
}
}
}
// Handle existing issue comment editing (issue parameter)
const issueInfo = parseProjectIssueParam(params)
if (issueInfo) {
// Edit mode: empty placeholder
// Add new comment mode: has placeholder "Add your comment here..." or similar
if (!textarea.placeholder || textarea.placeholder.trim() === "") {
const unique_key = `github.com:${issueInfo.slug}:${issueInfo.number}:edit-comment`
logger.debug(
`${this.constructor.name} enhanced project issue comment edit textarea`,
unique_key
)
return {
isIssue: true,
type: GH_EDIT,
unique_key,
}
}
}
return null
}
// Parse GitHub URL structure: /owner/repo/issues/123 or /owner/repo/pull/456
const match = location.pathname.match(
/^\/([^/]+)\/([^/]+)\/(?:issues|pull)\/(\d+)/
)
if (!match) {
return null
}
const [, owner, repo, numberStr] = match
const number = parseInt(numberStr!, 10)
const unique_key = `github.com:${owner}/${repo}:${number}:edit-body`
// Only enhance textareas that are for editing issue/PR body
const isIssueBodyRootEdit = textarea.closest(".react-issue-body")
const isIssueBodyCommentEdit = textarea.closest(
"[data-wrapper-timeline-id]"
)
const isPRBodyEdit =
textarea.name === "pull_request[body]" || // this is the root pr comment
textarea.name === "issue_comment[body]" // this is the other pr comments (surprising!)
if (!isIssueBodyRootEdit && !isIssueBodyCommentEdit && !isPRBodyEdit) {
return null
}
logger.debug(
`${this.constructor.name} enhanced issue/PR body textarea`,
unique_key
)
return {
isIssue: !!(isIssueBodyRootEdit || isIssueBodyCommentEdit),
type: GH_EDIT,
unique_key,
}
}
enhance(
textArea: HTMLTextAreaElement,
spot: GitHubEditSpot
): OverTypeInstance {
prepareGitHubHighlighter()
const overtypeContainer = modifyDOM(textArea)
const overtype = fixupOvertype(
new OverType(overtypeContainer, {
...commonGitHubOptions,
padding: spot.isIssue ? "var(--base-size-16)" : "var(--base-size-8)",
})
)
return overtype
}
tableUpperDecoration(_spot: GitHubEditSpot): React.ReactNode {
return <span>N/A</span>
}
tableTitle(_spot: GitHubEditSpot): string {
return "N/A"
}
}