Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@ 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'
import { commonGitHubOptions, prepareGitHubHighlighter } from './github-common'

export interface GitHubEditCommentSpot extends CommentSpot {
type: 'GH_EDIT_COMMENT'
const GH_EDIT = 'GH_EDIT' as const

export interface GitHubEditSpot extends CommentSpot {
type: typeof GH_EDIT
}

export class GitHubEditCommentEnhancer implements CommentEnhancer<GitHubEditCommentSpot> {
export class GitHubEditEnhancer implements CommentEnhancer<GitHubEditSpot> {
forSpotTypes(): string[] {
return ['GH_EDIT_COMMENT']
return [GH_EDIT]
}

tryToEnhance(
textarea: HTMLTextAreaElement,
location: StrippedLocation,
): GitHubEditCommentSpot | null {
tryToEnhance(textarea: HTMLTextAreaElement, location: StrippedLocation): GitHubEditSpot | null {
if (location.host !== 'github.com') {
return null
}
Expand All @@ -44,27 +42,27 @@ export class GitHubEditCommentEnhancer implements CommentEnhancer<GitHubEditComm

logger.debug(`${this.constructor.name} enhanced issue/PR body textarea`, unique_key)
return {
type: 'GH_EDIT_COMMENT',
type: GH_EDIT,
unique_key,
}
}

enhance(textArea: HTMLTextAreaElement, _spot: GitHubEditCommentSpot): OverTypeInstance {
enhance(textArea: HTMLTextAreaElement, _spot: GitHubEditSpot): OverTypeInstance {
prepareGitHubHighlighter()
const overtypeContainer = modifyDOM(textArea)
return new OverType(overtypeContainer, {
...commonGithubOptions,
...commonGitHubOptions,
minHeight: '102px',
padding: 'var(--base-size-8)',
placeholder: 'Add your comment here...',
})[0]!
}

tableUpperDecoration(_spot: GitHubEditCommentSpot): React.ReactNode {
tableUpperDecoration(_spot: GitHubEditSpot): React.ReactNode {
return <span>N/A</span>
}

tableTitle(_spot: GitHubEditCommentSpot): string {
tableTitle(_spot: GitHubEditSpot): string {
return 'N/A'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@ 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'
import { commonGitHubOptions, prepareGitHubHighlighter } from './github-common'

export interface GitHubIssueAddCommentSpot extends CommentSpot {
type: 'GH_ISSUE_ADD_COMMENT'
const GH_ISSUE_APPEND = 'GH_ISSUE_APPEND' as const

export interface GitHubIssueAppendSpot extends CommentSpot {
type: typeof GH_ISSUE_APPEND
title: string
domain: string
slug: string // owner/repo
number: number // issue number, undefined for new issues
}

export class GitHubIssueAddCommentEnhancer implements CommentEnhancer<GitHubIssueAddCommentSpot> {
export class GitHubIssueAppendEnhancer implements CommentEnhancer<GitHubIssueAppendSpot> {
forSpotTypes(): string[] {
return ['GH_ISSUE_ADD_COMMENT']
return [GH_ISSUE_APPEND]
}

tryToEnhance(
textarea: HTMLTextAreaElement,
location: StrippedLocation,
): GitHubIssueAddCommentSpot | null {
): GitHubIssueAppendSpot | null {
if (textarea.id === 'feedback') {
return null
}
Expand Down Expand Up @@ -57,22 +58,22 @@ export class GitHubIssueAddCommentEnhancer implements CommentEnhancer<GitHubIssu
number,
slug,
title,
type: 'GH_ISSUE_ADD_COMMENT',
type: GH_ISSUE_APPEND,
unique_key,
}
}

enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueAddCommentSpot): OverTypeInstance {
enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueAppendSpot): OverTypeInstance {
prepareGitHubHighlighter()
const overtypeContainer = modifyDOM(textArea)
return new OverType(overtypeContainer, {
...commonGithubOptions,
...commonGitHubOptions,
minHeight: '100px',
placeholder: 'Use Markdown to format your comment',
})[0]!
}

tableUpperDecoration(spot: GitHubIssueAddCommentSpot): React.ReactNode {
tableUpperDecoration(spot: GitHubIssueAppendSpot): React.ReactNode {
return (
<>
<span className='flex h-4 w-4 flex-shrink-0 items-center justify-center'>
Expand All @@ -86,7 +87,7 @@ export class GitHubIssueAddCommentEnhancer implements CommentEnhancer<GitHubIssu
)
}

tableTitle(spot: GitHubIssueAddCommentSpot): string {
tableTitle(spot: GitHubIssueAppendSpot): string {
return spot.title
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@ import OverType, { type OverTypeInstance } from 'overtype'
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '../../enhancer'
import { logger } from '../../logger'
import { modifyDOM } from '../modifyDOM'
import { commonGithubOptions } from './ghOptions'
import { prepareGitHubHighlighter } from './githubHighlighter'
import { commonGitHubOptions, prepareGitHubHighlighter } from './github-common'

interface GitHubIssueNewCommentSpot extends CommentSpot {
type: 'GH_ISSUE_NEW_COMMENT'
const GH_ISSUE_CREATE = 'GH_ISSUE_CREATE' as const

interface GitHubIssueCreateSpot extends CommentSpot {
type: typeof GH_ISSUE_CREATE
domain: string
slug: string // owner/repo
title: string
}

export class GitHubIssueNewCommentEnhancer implements CommentEnhancer<GitHubIssueNewCommentSpot> {
export class GitHubIssueCreateEnhancer implements CommentEnhancer<GitHubIssueCreateSpot> {
forSpotTypes(): string[] {
return ['GH_ISSUE_NEW_COMMENT']
return [GH_ISSUE_CREATE]
}

tryToEnhance(
textarea: HTMLTextAreaElement,
location: StrippedLocation,
): GitHubIssueNewCommentSpot | null {
): GitHubIssueCreateSpot | null {
if (textarea.id === 'feedback') {
return null
}
Expand All @@ -44,22 +45,22 @@ export class GitHubIssueNewCommentEnhancer implements CommentEnhancer<GitHubIssu
domain: location.host,
slug,
title,
type: 'GH_ISSUE_NEW_COMMENT',
type: GH_ISSUE_CREATE,
unique_key,
}
}

enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueNewCommentSpot): OverTypeInstance {
enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueCreateSpot): OverTypeInstance {
prepareGitHubHighlighter()
const overtypeContainer = modifyDOM(textArea)
return new OverType(overtypeContainer, {
...commonGithubOptions,
...commonGitHubOptions,
minHeight: '400px',
placeholder: 'Type your description here...',
})[0]!
}

tableUpperDecoration(spot: GitHubIssueNewCommentSpot): React.ReactNode {
tableUpperDecoration(spot: GitHubIssueCreateSpot): React.ReactNode {
const { slug } = spot
return (
<>
Expand All @@ -69,11 +70,11 @@ export class GitHubIssueNewCommentEnhancer implements CommentEnhancer<GitHubIssu
)
}

tableTitle(spot: GitHubIssueNewCommentSpot): string {
tableTitle(spot: GitHubIssueCreateSpot): string {
return spot.title || 'New Issue'
}

buildUrl(spot: GitHubIssueNewCommentSpot): string {
buildUrl(spot: GitHubIssueCreateSpot): string {
return `https://${spot.domain}/${spot.slug}/issue/new`
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,29 @@ 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'
import { commonGitHubOptions, prepareGitHubHighlighter } from './github-common'

export interface GitHubPRAddCommentSpot extends CommentSpot {
type: 'GH_PR_ADD_COMMENT' // Override to narrow from string to specific union
const GH_PR_APPEND = 'GH_PR_APPEND' as const

export interface GitHubPrAppendSpot extends CommentSpot {
type: typeof GH_PR_APPEND
title: string
domain: string
slug: string // owner/repo
number: number // issue/PR number, undefined for new issues and PRs
}

export class GitHubPRAddCommentEnhancer implements CommentEnhancer<GitHubPRAddCommentSpot> {
export class GitHubPrAppendEnhancer implements CommentEnhancer<GitHubPrAppendSpot> {
forSpotTypes(): string[] {
return ['GH_PR_ADD_COMMENT']
return [GH_PR_APPEND]
}

tryToEnhance(
_textarea: HTMLTextAreaElement,
textarea: HTMLTextAreaElement,
location: StrippedLocation,
): GitHubPRAddCommentSpot | null {
): GitHubPrAppendSpot | null {
// Only handle github.com domains TODO: identify GitHub Enterprise somehow
if (location.host !== 'github.com' || _textarea.id !== 'new_comment_field') {
if (location.host !== 'github.com' || textarea.id !== 'new_comment_field') {
return null
}

Expand All @@ -47,23 +48,23 @@ export class GitHubPRAddCommentEnhancer implements CommentEnhancer<GitHubPRAddCo
number,
slug,
title,
type: 'GH_PR_ADD_COMMENT',
type: GH_PR_APPEND,
unique_key,
}
}

enhance(textArea: HTMLTextAreaElement, _spot: GitHubPRAddCommentSpot): OverTypeInstance {
enhance(textArea: HTMLTextAreaElement, _spot: GitHubPrAppendSpot): OverTypeInstance {
prepareGitHubHighlighter()
const overtypeContainer = modifyDOM(textArea)
return new OverType(overtypeContainer, {
...commonGithubOptions,
...commonGitHubOptions,
minHeight: '102px',
padding: 'var(--base-size-8)',
placeholder: 'Add your comment here...',
})[0]!
}

tableUpperDecoration(spot: GitHubPRAddCommentSpot): React.ReactNode {
tableUpperDecoration(spot: GitHubPrAppendSpot): React.ReactNode {
const { slug, number } = spot
return (
<>
Expand All @@ -73,7 +74,7 @@ export class GitHubPRAddCommentEnhancer implements CommentEnhancer<GitHubPRAddCo
)
}

tableTitle(spot: GitHubPRAddCommentSpot): string {
tableTitle(spot: GitHubPrAppendSpot): string {
return spot.title
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@ import OverType, { type OverTypeInstance } from 'overtype'
import type { CommentEnhancer, CommentSpot, StrippedLocation } from '../../enhancer'
import { logger } from '../../logger'
import { modifyDOM } from '../modifyDOM'
import { commonGithubOptions } from './ghOptions'
import { prepareGitHubHighlighter } from './githubHighlighter'
import { commonGitHubOptions, prepareGitHubHighlighter } from './github-common'

interface GitHubPRNewCommentSpot extends CommentSpot {
type: 'GH_PR_NEW_COMMENT'
const GH_PR_CREATE = 'GH_PR_CREATE' as const

interface GitHubPrCreateSpot extends CommentSpot {
type: typeof GH_PR_CREATE
domain: string
slug: string // owner/repo
title: string
head: string // `user:repo:branch` where changes are implemented
base: string // branch you want changes pulled into
}

export class GitHubPRNewCommentEnhancer implements CommentEnhancer<GitHubPRNewCommentSpot> {
export class GitHubPrCreateEnhancer implements CommentEnhancer<GitHubPrCreateSpot> {
forSpotTypes(): string[] {
return ['GH_PR_NEW_COMMENT']
return [GH_PR_CREATE]
}

tryToEnhance(
textarea: HTMLTextAreaElement,
location: StrippedLocation,
): GitHubPRNewCommentSpot | null {
): GitHubPrCreateSpot | null {
if (textarea.id === 'feedback') {
return null
}
Expand Down Expand Up @@ -54,22 +55,22 @@ export class GitHubPRNewCommentEnhancer implements CommentEnhancer<GitHubPRNewCo
head,
slug,
title,
type: 'GH_PR_NEW_COMMENT',
type: GH_PR_CREATE,
unique_key,
}
}

enhance(textArea: HTMLTextAreaElement, _spot: GitHubPRNewCommentSpot): OverTypeInstance {
enhance(textArea: HTMLTextAreaElement, _spot: GitHubPrCreateSpot): OverTypeInstance {
prepareGitHubHighlighter()
const overtypeContainer = modifyDOM(textArea)
return new OverType(overtypeContainer, {
...commonGithubOptions,
...commonGitHubOptions,
minHeight: '250px',
placeholder: 'Type your description here...',
})[0]!
}

tableUpperDecoration(spot: GitHubPRNewCommentSpot): React.ReactNode {
tableUpperDecoration(spot: GitHubPrCreateSpot): React.ReactNode {
const { slug } = spot
return (
<>
Expand All @@ -79,11 +80,11 @@ export class GitHubPRNewCommentEnhancer implements CommentEnhancer<GitHubPRNewCo
)
}

tableTitle(spot: GitHubPRNewCommentSpot): string {
tableTitle(spot: GitHubPrCreateSpot): string {
return spot.title || 'New Pull Request'
}

buildUrl(spot: GitHubPRNewCommentSpot): string {
buildUrl(spot: GitHubPrCreateSpot): string {
return `https://${spot.domain}/${spot.slug}/issue/new`
}
}
7 changes: 0 additions & 7 deletions src/lib/enhancers/github/ghOptions.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import hljs from 'highlight.js'
import type { Options } from 'overtype'
import OverType from 'overtype'
import { oncePerRefresh } from '@/lib/once-per-refresh'

export const commonGitHubOptions: Options = {
autoResize: true,
lineHeight: 'var(--text-body-lineHeight-medium, 1.4285)',
padding: 'var(--base-size-16)',
}

export function prepareGitHubHighlighter() {
oncePerRefresh('github-highlighter', () => {
OverType.setCodeHighlighter(githubHighlighter)
Expand Down
Loading