-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubEditEnhancer.tsx
More file actions
112 lines (100 loc) · 3.2 KB
/
GitHubEditEnhancer.tsx
File metadata and controls
112 lines (100 loc) · 3.2 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
import OverType, { type OverTypeInstance } from "overtype"
import {
CommentEnhancerNoDraftHistory,
type CommentSpot,
DRAFT_STORAGE_UNSUPPORTED,
type StrippedLocation,
} from "@/lib/enhancer"
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 extends CommentEnhancerNoDraftHistory<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)) {
return {
isIssue: true,
type: GH_EDIT,
unique_key: DRAFT_STORAGE_UNSUPPORTED,
}
}
}
// 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() === "") {
return {
isIssue: true,
type: GH_EDIT,
unique_key: DRAFT_STORAGE_UNSUPPORTED,
}
}
}
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
}
// 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
}
return {
isIssue: !!(isIssueBodyRootEdit || isIssueBodyCommentEdit),
type: GH_EDIT,
unique_key: DRAFT_STORAGE_UNSUPPORTED,
}
}
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
}
}