|
| 1 | +import OverType, { type OverTypeInstance } from 'overtype' |
| 2 | +import type React from 'react' |
| 3 | +import type { CommentEnhancer, CommentSpot } from '@/lib/enhancer' |
| 4 | +import { logger } from '@/lib/logger' |
| 5 | +import { modifyDOM } from '../modifyDOM' |
| 6 | +import { commonGithubOptions } from './ghOptions' |
| 7 | +import { githubHighlighter } from './githubHighlighter' |
| 8 | + |
| 9 | +export interface GitHubEditCommentSpot extends CommentSpot { |
| 10 | + type: 'GH_EDIT_COMMENT' |
| 11 | + title: string |
| 12 | + domain: string |
| 13 | + slug: string |
| 14 | + number: number |
| 15 | +} |
| 16 | + |
| 17 | +export class GitHubEditCommentEnhancer implements CommentEnhancer<GitHubEditCommentSpot> { |
| 18 | + forSpotTypes(): string[] { |
| 19 | + return ['GH_EDIT_COMMENT'] |
| 20 | + } |
| 21 | + |
| 22 | + tryToEnhance(_textarea: HTMLTextAreaElement): GitHubEditCommentSpot | null { |
| 23 | + if ( |
| 24 | + document.querySelector('meta[name="hostname"]')?.getAttribute('content') !== 'github.com' || |
| 25 | + !_textarea.matches('.TimelineItem textarea') |
| 26 | + ) { |
| 27 | + return null |
| 28 | + } |
| 29 | + |
| 30 | + // Parse GitHub URL structure: /owner/repo/issues/123 or /owner/repo/pull/456 |
| 31 | + logger.info(`${this.constructor.name} examing url`, window.location.pathname) |
| 32 | + |
| 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 |
| 36 | + const [, owner, repo, numberStr] = match |
| 37 | + const slug = `${owner}/${repo}` |
| 38 | + const number = parseInt(numberStr!, 10) |
| 39 | + const unique_key = `github.com:${slug}:${number}` |
| 40 | + const title = 'TODO_TITLE' |
| 41 | + return { |
| 42 | + domain: 'github.com', |
| 43 | + number, |
| 44 | + slug, |
| 45 | + title, |
| 46 | + type: 'GH_EDIT_COMMENT', |
| 47 | + unique_key, |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + prepareForFirstEnhancement(): void { |
| 52 | + OverType.setCodeHighlighter(githubHighlighter) |
| 53 | + } |
| 54 | + |
| 55 | + enhance(textArea: HTMLTextAreaElement, _spot: GitHubEditCommentSpot): OverTypeInstance { |
| 56 | + const overtypeContainer = modifyDOM(textArea) |
| 57 | + return new OverType(overtypeContainer, { |
| 58 | + ...commonGithubOptions, |
| 59 | + minHeight: '102px', |
| 60 | + padding: 'var(--base-size-8)', |
| 61 | + placeholder: 'Add your comment here...', |
| 62 | + })[0]! |
| 63 | + } |
| 64 | + |
| 65 | + tableUpperDecoration(spot: GitHubEditCommentSpot): React.ReactNode { |
| 66 | + const { slug, number } = spot |
| 67 | + return ( |
| 68 | + <> |
| 69 | + <span className='font-mono text-muted-foreground text-sm'>{slug}</span> |
| 70 | + <span className='ml-2 font-medium'>PR #{number}</span> |
| 71 | + </> |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + tableTitle(_spot: GitHubEditCommentSpot): string { |
| 76 | + return 'TITLE_TODO' |
| 77 | + } |
| 78 | +} |
0 commit comments