-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithubEditComment.tsx
More file actions
70 lines (59 loc) · 2.16 KB
/
githubEditComment.tsx
File metadata and controls
70 lines (59 loc) · 2.16 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
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 } from './ghOptions'
import { prepareGitHubHighlighter } from './githubHighlighter'
export interface GitHubEditCommentSpot extends CommentSpot {
type: 'GH_EDIT_COMMENT'
}
export class GitHubEditCommentEnhancer implements CommentEnhancer<GitHubEditCommentSpot> {
forSpotTypes(): string[] {
return ['GH_EDIT_COMMENT']
}
tryToEnhance(
textarea: HTMLTextAreaElement,
location: StrippedLocation,
): GitHubEditCommentSpot | null {
if (location.host !== 'github.com') {
return null
}
// Only enhance textareas that are for editing issue/PR body
const isIssueBodyEdit = textarea.closest('.react-issue-body')
const isPRBodyEdit =
textarea.id?.match(/^issue-\d+-body$/) || textarea.name === 'pull_request[body]'
if (!isIssueBodyEdit && !isPRBodyEdit) {
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`
logger.debug(`${this.constructor.name} enhanced issue/PR body textarea`, unique_key)
return {
type: 'GH_EDIT_COMMENT',
unique_key,
}
}
enhance(textArea: HTMLTextAreaElement, _spot: GitHubEditCommentSpot): OverTypeInstance {
prepareGitHubHighlighter()
const overtypeContainer = modifyDOM(textArea)
return new OverType(overtypeContainer, {
...commonGithubOptions,
minHeight: '102px',
padding: 'var(--base-size-8)',
placeholder: 'Add your comment here...',
})[0]!
}
tableUpperDecoration(_spot: GitHubEditCommentSpot): React.ReactNode {
return <span>N/A</span>
}
tableTitle(_spot: GitHubEditCommentSpot): string {
return 'N/A'
}
}