Skip to content

Commit aa3388b

Browse files
authored
Fixup conflicts between editing and adding on issues and PRs (#63 fixes 21)
2 parents f30d185 + 512d53d commit aa3388b

File tree

3 files changed

+42
-73
lines changed

3 files changed

+42
-73
lines changed
Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,49 @@
11
import OverType, { type OverTypeInstance } from 'overtype'
22
import type React from 'react'
3-
import type { CommentEnhancer, CommentSpot } from '@/lib/enhancer'
3+
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '@/lib/enhancer'
44
import { logger } from '@/lib/logger'
55
import { modifyDOM } from '../modifyDOM'
66
import { commonGithubOptions } from './ghOptions'
77
import { prepareGitHubHighlighter } from './githubHighlighter'
88

99
export interface GitHubEditCommentSpot extends CommentSpot {
1010
type: 'GH_EDIT_COMMENT'
11-
title: string
12-
domain: string
13-
slug: string
14-
number: number
1511
}
1612

1713
export class GitHubEditCommentEnhancer implements CommentEnhancer<GitHubEditCommentSpot> {
1814
forSpotTypes(): string[] {
1915
return ['GH_EDIT_COMMENT']
2016
}
2117

22-
tryToEnhance(_textarea: HTMLTextAreaElement): GitHubEditCommentSpot | null {
23-
if (
24-
document.querySelector('meta[name="hostname"]')?.getAttribute('content') !== 'github.com' ||
25-
!_textarea.matches('.TimelineItem textarea')
26-
) {
18+
tryToEnhance(
19+
textarea: HTMLTextAreaElement,
20+
location: StrippedLocation,
21+
): GitHubEditCommentSpot | null {
22+
if (location.host !== 'github.com') {
23+
return null
24+
}
25+
26+
// Only enhance textareas that are for editing issue/PR body
27+
const isIssueBodyEdit = textarea.closest('.react-issue-body')
28+
const isPRBodyEdit =
29+
textarea.id?.match(/^issue-\d+-body$/) || textarea.name === 'pull_request[body]'
30+
31+
if (!isIssueBodyEdit && !isPRBodyEdit) {
2732
return null
2833
}
2934

3035
// Parse GitHub URL structure: /owner/repo/issues/123 or /owner/repo/pull/456
31-
logger.info(`${this.constructor.name} examing url`, window.location.pathname)
36+
const match = location.pathname.match(/^\/([^/]+)\/([^/]+)\/(?:issues|pull)\/(\d+)/)
37+
if (!match) {
38+
return null
39+
}
3240

33-
const match = window.location.pathname.match(/^\/([^/]+)\/([^/]+)(?:\/(pull|issues)\/(\d+))/)
34-
logger.info(`${this.constructor.name} found match`, window.location.pathname)
35-
if (!match) return null
3641
const [, owner, repo, numberStr] = match
37-
const slug = `${owner}/${repo}`
3842
const number = parseInt(numberStr!, 10)
39-
const unique_key = `github.com:${slug}:${number}`
40-
const title = 'TODO_TITLE'
43+
const unique_key = `github.com:${owner}/${repo}:${number}:edit-body`
44+
45+
logger.debug(`${this.constructor.name} enhanced issue/PR body textarea`, unique_key)
4146
return {
42-
domain: 'github.com',
43-
number,
44-
slug,
45-
title,
4647
type: 'GH_EDIT_COMMENT',
4748
unique_key,
4849
}
@@ -59,17 +60,11 @@ export class GitHubEditCommentEnhancer implements CommentEnhancer<GitHubEditComm
5960
})[0]!
6061
}
6162

62-
tableUpperDecoration(spot: GitHubEditCommentSpot): React.ReactNode {
63-
const { slug, number } = spot
64-
return (
65-
<>
66-
<span className='font-mono text-muted-foreground text-sm'>{slug}</span>
67-
<span className='ml-2 font-medium'>PR #{number}</span>
68-
</>
69-
)
63+
tableUpperDecoration(_spot: GitHubEditCommentSpot): React.ReactNode {
64+
return <span>N/A</span>
7065
}
7166

7267
tableTitle(_spot: GitHubEditCommentSpot): string {
73-
return 'TITLE_TODO'
68+
return 'N/A'
7469
}
7570
}

src/lib/enhancers/github/githubIssueAddComment.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ export class GitHubIssueAddCommentEnhancer implements CommentEnhancer<GitHubIssu
3131
return null
3232
}
3333

34+
// Don't enhance textareas that are within the issue/PR body editing container
35+
const bodyContainer = textarea.closest('.react-issue-body')
36+
if (bodyContainer) {
37+
return null
38+
}
39+
3440
// Parse GitHub URL structure: /owner/repo/issues/123 or /owner/repo/pull/456
3541
logger.debug(`${this.constructor.name} examing url`, location.pathname)
3642

tests/lib/enhancers/github.test.ts

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -197,31 +197,13 @@ describe('github', () => {
197197
{
198198
"for": "id=:rc3: name=null className=prc-Textarea-TextArea-13q4j focus-visible overtype-input",
199199
"spot": {
200-
"domain": "github.com",
201-
"number": 56,
202-
"slug": "diffplug/gitcasso",
203-
"title": "what about the draft?",
204-
"type": "GH_ISSUE_ADD_COMMENT",
205-
"unique_key": "github.com:diffplug/gitcasso:56",
200+
"type": "GH_EDIT_COMMENT",
201+
"unique_key": "github.com:diffplug/gitcasso:56:edit-body",
206202
},
207-
"title": "what about the draft?",
208-
"upperDecoration": <React.Fragment>
209-
<span
210-
className="flex h-4 w-4 flex-shrink-0 items-center justify-center"
211-
>
212-
<IssueOpenedIcon
213-
size={16}
214-
/>
215-
</span>
216-
#
217-
56
218-
<a
219-
className="truncate hover:underline"
220-
href="https://github.com/diffplug/gitcasso"
221-
>
222-
diffplug/gitcasso
223-
</a>
224-
</React.Fragment>,
203+
"title": "N/A",
204+
"upperDecoration": <span>
205+
N/A
206+
</span>,
225207
},
226208
{
227209
"for": "id=:ra7: name=null className=prc-Textarea-TextArea-13q4j overtype-input",
@@ -261,27 +243,13 @@ describe('github', () => {
261243
{
262244
"for": "id=issue-3429313834-body name=pull_request[body] className=js-comment-field js-paste-markdown js-task-list-field js-quick-submit js-size-to-fit size-to-fit js-session-resumable CommentBox-input FormControl-textarea js-saved-reply-shortcut-comment-field focus-visible overtype-input",
263245
"spot": {
264-
"domain": "github.com",
265-
"number": NaN,
266-
"slug": "diffplug/gitcasso",
267-
"title": "TODO_TITLE",
268246
"type": "GH_EDIT_COMMENT",
269-
"unique_key": "github.com:diffplug/gitcasso:NaN",
247+
"unique_key": "github.com:diffplug/gitcasso:58:edit-body",
270248
},
271-
"title": "TITLE_TODO",
272-
"upperDecoration": <React.Fragment>
273-
<span
274-
className="font-mono text-muted-foreground text-sm"
275-
>
276-
diffplug/gitcasso
277-
</span>
278-
<span
279-
className="ml-2 font-medium"
280-
>
281-
PR #
282-
NaN
283-
</span>
284-
</React.Fragment>,
249+
"title": "N/A",
250+
"upperDecoration": <span>
251+
N/A
252+
</span>,
285253
},
286254
{
287255
"for": "id=new_comment_field name=comment[body] className=js-comment-field js-paste-markdown js-task-list-field js-quick-submit FormControl-textarea CommentBox-input js-size-to-fit size-to-fit js-session-resumable js-saved-reply-shortcut-comment-field overtype-input",

0 commit comments

Comments
 (0)