|
| 1 | +import OverType, { type OverTypeInstance } from '../../../overtype/overtype' |
| 2 | +import type { CommentEnhancer, CommentSpot } from '../../enhancer' |
| 3 | +import { logger } from '../../logger' |
| 4 | +import { modifyDOM } from '../modifyDOM' |
| 5 | +import { githubHighlighter } from './githubHighlighter' |
| 6 | + |
| 7 | +interface GitHubPRAddCommentSpot extends CommentSpot { |
| 8 | + type: 'GH_PR_ADD_COMMENT' // Override to narrow from string to specific union |
| 9 | + domain: string |
| 10 | + slug: string // owner/repo |
| 11 | + number: number // issue/PR number, undefined for new issues and PRs |
| 12 | +} |
| 13 | + |
| 14 | +export class GitHubPRAddCommentEnhancer implements CommentEnhancer<GitHubPRAddCommentSpot> { |
| 15 | + forSpotTypes(): string[] { |
| 16 | + return ['GH_PR_ADD_COMMENT'] |
| 17 | + } |
| 18 | + |
| 19 | + tryToEnhance(_textarea: HTMLTextAreaElement): GitHubPRAddCommentSpot | null { |
| 20 | + // Only handle github.com domains TODO: identify GitHub Enterprise somehow |
| 21 | + if ( |
| 22 | + document.querySelector('meta[name="hostname"]')?.getAttribute('content') !== 'github.com' || |
| 23 | + _textarea.id !== 'new_comment_field' |
| 24 | + ) { |
| 25 | + return null |
| 26 | + } |
| 27 | + |
| 28 | + // Parse GitHub URL structure: /owner/repo/issues/123 or /owner/repo/pull/456 |
| 29 | + logger.debug(`${this.constructor.name} examing url`, window.location.pathname) |
| 30 | + |
| 31 | + const match = window.location.pathname.match(/^\/([^/]+)\/([^/]+)(?:\/pull\/(\d+))/) |
| 32 | + logger.debug(`${this.constructor.name} found match`, window.location.pathname) |
| 33 | + if (!match) return null |
| 34 | + const [, owner, repo, numberStr] = match |
| 35 | + const slug = `${owner}/${repo}` |
| 36 | + const number = parseInt(numberStr!, 10) |
| 37 | + const unique_key = `github.com:${slug}:${number}` |
| 38 | + return { |
| 39 | + domain: 'github.com', |
| 40 | + number, |
| 41 | + slug, |
| 42 | + type: 'GH_PR_ADD_COMMENT', |
| 43 | + unique_key, |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + prepareForFirstEnhancement(): void { |
| 48 | + OverType.setCodeHighlighter(githubHighlighter) |
| 49 | + } |
| 50 | + |
| 51 | + enhance(textArea: HTMLTextAreaElement, _spot: GitHubPRAddCommentSpot): OverTypeInstance { |
| 52 | + const overtypeContainer = modifyDOM(textArea) |
| 53 | + return new OverType(overtypeContainer, { |
| 54 | + autoResize: true, |
| 55 | + minHeight: '102px', |
| 56 | + padding: 'var(--base-size-8)', |
| 57 | + placeholder: 'Add your comment here...', |
| 58 | + })[0]! |
| 59 | + } |
| 60 | + |
| 61 | + tableTitle(spot: GitHubPRAddCommentSpot): string { |
| 62 | + const { slug, number } = spot |
| 63 | + return `${slug} PR #${number}` |
| 64 | + } |
| 65 | + |
| 66 | + tableIcon(_: GitHubPRAddCommentSpot): string { |
| 67 | + return '🔄' // PR icon TODO: icon urls in /public |
| 68 | + } |
| 69 | + |
| 70 | + buildUrl(spot: GitHubPRAddCommentSpot): string { |
| 71 | + return `https://${spot.domain}/${spot.slug}/pull/${spot.number}` |
| 72 | + } |
| 73 | +} |
0 commit comments