|
| 1 | +import OverType, { type OverTypeInstance } from '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 GitHubPRNewCommentSpot extends CommentSpot { |
| 8 | + type: 'GH_PR_NEW_COMMENT' |
| 9 | + domain: string |
| 10 | + slug: string // owner/repo/base-branch/compare-branch |
| 11 | +} |
| 12 | + |
| 13 | +export class GitHubPRNewCommentEnhancer implements CommentEnhancer<GitHubPRNewCommentSpot> { |
| 14 | + forSpotTypes(): string[] { |
| 15 | + return ['GH_PR_NEW_COMMENT'] |
| 16 | + } |
| 17 | + |
| 18 | + tryToEnhance(_textarea: HTMLTextAreaElement): GitHubPRNewCommentSpot | null { |
| 19 | + if (document.querySelector('meta[name="hostname"]')?.getAttribute('content') !== 'github.com') { |
| 20 | + return null |
| 21 | + } |
| 22 | + |
| 23 | + // /owner/repo/compare/feature/more-enhancers?expand=1 |
| 24 | + // or /owner/repo/compare/feat/issue-static-and-dynamic...feature/more-enhancers?expand=1 |
| 25 | + logger.info(`${this.constructor.name} examing url`, window.location.pathname) |
| 26 | + |
| 27 | + const match = window.location.pathname.match( |
| 28 | + /^\/([^/]+)\/([^/]+)\/compare\/(?:([^.?]+)\.\.\.)?([^?]+)/, |
| 29 | + ) |
| 30 | + logger.info(`${this.constructor.name} found match`, window.location.pathname, match) |
| 31 | + |
| 32 | + if (!match) return null |
| 33 | + const [, owner, repo, baseBranch, compareBranch] = match |
| 34 | + const slug = baseBranch |
| 35 | + ? `${owner}/${repo}/${baseBranch}...${compareBranch}` |
| 36 | + : `${owner}/${repo}/${compareBranch}` |
| 37 | + const unique_key = `github.com:${slug}` |
| 38 | + return { |
| 39 | + domain: 'github.com', |
| 40 | + slug, |
| 41 | + type: 'GH_PR_NEW_COMMENT', |
| 42 | + unique_key, |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + prepareForFirstEnhancement(): void { |
| 47 | + OverType.setCodeHighlighter(githubHighlighter) |
| 48 | + } |
| 49 | + |
| 50 | + enhance(textArea: HTMLTextAreaElement, _spot: GitHubPRNewCommentSpot): OverTypeInstance { |
| 51 | + const overtypeContainer = modifyDOM(textArea) |
| 52 | + return new OverType(overtypeContainer, { |
| 53 | + autoResize: true, |
| 54 | + minHeight: '250px', |
| 55 | + padding: 'var(--base-size-16)', |
| 56 | + placeholder: 'Type your description here...', |
| 57 | + })[0]! |
| 58 | + } |
| 59 | + |
| 60 | + tableTitle(spot: GitHubPRNewCommentSpot): string { |
| 61 | + const { slug } = spot |
| 62 | + return `${slug} New Issue` |
| 63 | + } |
| 64 | + |
| 65 | + tableIcon(_: GitHubPRNewCommentSpot): string { |
| 66 | + return '🔄' // PR icon TODO: icon urls in /public |
| 67 | + } |
| 68 | + |
| 69 | + buildUrl(spot: GitHubPRNewCommentSpot): string { |
| 70 | + return `https://${spot.domain}/${spot.slug}/issue/new` |
| 71 | + } |
| 72 | +} |
0 commit comments