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
18 changes: 0 additions & 18 deletions src/entrypoints/content.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { CONFIG } from '../lib/config'
import type { CommentEvent, CommentSpot, StrippedLocation } from '../lib/enhancer'
import { logger } from '../lib/logger'
import { EnhancerRegistry, TextareaRegistry } from '../lib/registries'
Expand Down Expand Up @@ -98,8 +97,6 @@ function enhanceMaybe(textarea: HTMLTextAreaElement) {
logger.debug('textarea already registered {}', textarea)
return
}

injectStyles()
try {
const location = detectLocation()
logger.debug('[gitcasso] Calling tryToEnhance with location:', location)
Expand All @@ -118,18 +115,3 @@ function enhanceMaybe(textarea: HTMLTextAreaElement) {
logger.error(e)
}
}

const STYLES = `
.${CONFIG.ADDED_OVERTYPE_CLASS} {
background: cyan !important;
}
`

function injectStyles(): void {
if (!document.getElementById('gitcasso-styles')) {
const style = document.createElement('style')
style.textContent = STYLES
style.id = 'gitcasso-styles'
document.head.appendChild(style)
}
}
1 change: 0 additions & 1 deletion src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const LOG_LEVELS = ['DEBUG', 'INFO', 'WARN', 'ERROR'] as const
export type LogLevel = (typeof LOG_LEVELS)[number]

export const CONFIG = {
ADDED_OVERTYPE_CLASS: 'gitcasso-overtype',
EXTENSION_NAME: 'gitcasso', // decorates logs
LOG_LEVEL: 'DEBUG' satisfies LogLevel,
MODE: 'PROD' satisfies ModeType,
Expand Down
4 changes: 0 additions & 4 deletions src/lib/enhancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,8 @@ export interface CommentEnhancer<Spot extends CommentSpot = CommentSpot> {
* If we return non-null, then we become the handler for that text area.
*/
tryToEnhance(textarea: HTMLTextAreaElement, location: StrippedLocation): Spot | null
/** This gets called the first time that `tryToEnhance` returns non-null. */
prepareForFirstEnhancement(): void
/**
* If `tryToEnhance` returns non-null, then this gets called.
* It is guaranteed that `prepareForFirstEnhancement` has been called
* exactly once since pageload before this gets called.
*/
enhance(textarea: HTMLTextAreaElement, spot: Spot): OverTypeInstance
/** Returns a ReactNode which will be displayed in the table row. */
Expand Down
3 changes: 0 additions & 3 deletions src/lib/enhancers/CommentEnhancerMissing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ export class CommentEnhancerMissing implements CommentEnhancer {
tryToEnhance(_textarea: HTMLTextAreaElement, _location: StrippedLocation): CommentSpot | null {
throw new Error('Method not implemented.')
}
prepareForFirstEnhancement(): void {
throw new Error('Method not implemented.')
}
enhance(_textarea: HTMLTextAreaElement, _spot: CommentSpot): OverTypeInstance {
throw new Error('Method not implemented.')
}
Expand Down
7 changes: 2 additions & 5 deletions src/lib/enhancers/github/githubEditComment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { CommentEnhancer, CommentSpot } from '@/lib/enhancer'
import { logger } from '@/lib/logger'
import { modifyDOM } from '../modifyDOM'
import { commonGithubOptions } from './ghOptions'
import { githubHighlighter } from './githubHighlighter'
import { prepareGitHubHighlighter } from './githubHighlighter'

export interface GitHubEditCommentSpot extends CommentSpot {
type: 'GH_EDIT_COMMENT'
Expand Down Expand Up @@ -48,11 +48,8 @@ export class GitHubEditCommentEnhancer implements CommentEnhancer<GitHubEditComm
}
}

prepareForFirstEnhancement(): void {
OverType.setCodeHighlighter(githubHighlighter)
}

enhance(textArea: HTMLTextAreaElement, _spot: GitHubEditCommentSpot): OverTypeInstance {
prepareGitHubHighlighter()
const overtypeContainer = modifyDOM(textArea)
return new OverType(overtypeContainer, {
...commonGithubOptions,
Expand Down
10 changes: 9 additions & 1 deletion src/lib/enhancers/github/githubHighlighter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import hljs from 'highlight.js'
import OverType from 'overtype'
import { oncePerRefresh } from '@/lib/once-per-refresh'

export function githubHighlighter(code: string, language?: string) {
export function prepareGitHubHighlighter() {
oncePerRefresh('github-highlighter', () => {
OverType.setCodeHighlighter(githubHighlighter)
})
}

function githubHighlighter(code: string, language?: string) {
try {
if (language && hljs.getLanguage(language)) {
const result = hljs.highlight(code, { language })
Expand Down
7 changes: 2 additions & 5 deletions src/lib/enhancers/github/githubIssueAddComment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { CommentEnhancer, CommentSpot, StrippedLocation } from '@/lib/enhan
import { logger } from '@/lib/logger'
import { modifyDOM } from '../modifyDOM'
import { commonGithubOptions } from './ghOptions'
import { githubHighlighter } from './githubHighlighter'
import { prepareGitHubHighlighter } from './githubHighlighter'

export interface GitHubIssueAddCommentSpot extends CommentSpot {
type: 'GH_ISSUE_ADD_COMMENT'
Expand Down Expand Up @@ -53,11 +53,8 @@ export class GitHubIssueAddCommentEnhancer implements CommentEnhancer<GitHubIssu
}
}

prepareForFirstEnhancement(): void {
OverType.setCodeHighlighter(githubHighlighter)
}

enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueAddCommentSpot): OverTypeInstance {
prepareGitHubHighlighter()
const overtypeContainer = modifyDOM(textArea)
return new OverType(overtypeContainer, {
...commonGithubOptions,
Expand Down
7 changes: 2 additions & 5 deletions src/lib/enhancers/github/githubIssueNewComment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { CommentEnhancer, CommentSpot, StrippedLocation } from '../../enhan
import { logger } from '../../logger'
import { modifyDOM } from '../modifyDOM'
import { commonGithubOptions } from './ghOptions'
import { githubHighlighter } from './githubHighlighter'
import { prepareGitHubHighlighter } from './githubHighlighter'

interface GitHubIssueNewCommentSpot extends CommentSpot {
type: 'GH_ISSUE_NEW_COMMENT'
Expand Down Expand Up @@ -42,11 +42,8 @@ export class GitHubIssueNewCommentEnhancer implements CommentEnhancer<GitHubIssu
}
}

prepareForFirstEnhancement(): void {
OverType.setCodeHighlighter(githubHighlighter)
}

enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueNewCommentSpot): OverTypeInstance {
prepareGitHubHighlighter()
const overtypeContainer = modifyDOM(textArea)
return new OverType(overtypeContainer, {
...commonGithubOptions,
Expand Down
7 changes: 2 additions & 5 deletions src/lib/enhancers/github/githubPRAddComment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { CommentEnhancer, CommentSpot, StrippedLocation } from '@/lib/enhan
import { logger } from '@/lib/logger'
import { modifyDOM } from '../modifyDOM'
import { commonGithubOptions } from './ghOptions'
import { githubHighlighter } from './githubHighlighter'
import { prepareGitHubHighlighter } from './githubHighlighter'

export interface GitHubPRAddCommentSpot extends CommentSpot {
type: 'GH_PR_ADD_COMMENT' // Override to narrow from string to specific union
Expand Down Expand Up @@ -49,11 +49,8 @@ export class GitHubPRAddCommentEnhancer implements CommentEnhancer<GitHubPRAddCo
}
}

prepareForFirstEnhancement(): void {
OverType.setCodeHighlighter(githubHighlighter)
}

enhance(textArea: HTMLTextAreaElement, _spot: GitHubPRAddCommentSpot): OverTypeInstance {
prepareGitHubHighlighter()
const overtypeContainer = modifyDOM(textArea)
return new OverType(overtypeContainer, {
...commonGithubOptions,
Expand Down
7 changes: 2 additions & 5 deletions src/lib/enhancers/github/githubPRNewComment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { CommentEnhancer, CommentSpot, StrippedLocation } from '../../enhan
import { logger } from '../../logger'
import { modifyDOM } from '../modifyDOM'
import { commonGithubOptions } from './ghOptions'
import { githubHighlighter } from './githubHighlighter'
import { prepareGitHubHighlighter } from './githubHighlighter'

interface GitHubPRNewCommentSpot extends CommentSpot {
type: 'GH_PR_NEW_COMMENT'
Expand Down Expand Up @@ -50,11 +50,8 @@ export class GitHubPRNewCommentEnhancer implements CommentEnhancer<GitHubPRNewCo
}
}

prepareForFirstEnhancement(): void {
OverType.setCodeHighlighter(githubHighlighter)
}

enhance(textArea: HTMLTextAreaElement, _spot: GitHubPRNewCommentSpot): OverTypeInstance {
prepareGitHubHighlighter()
const overtypeContainer = modifyDOM(textArea)
return new OverType(overtypeContainer, {
...commonGithubOptions,
Expand Down
13 changes: 0 additions & 13 deletions src/lib/enhancers/github/githubSpotTypes.ts

This file was deleted.

13 changes: 13 additions & 0 deletions src/lib/once-per-refresh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const hasRunSinceTheLastFullPageload = new Set<string>()

/**
* This function helps you run a given function only once per full pageload.
* So this will not run again due to turbolink navigation.
*/
export function oncePerRefresh(key: string, fun: () => void) {
if (hasRunSinceTheLastFullPageload.has(key)) {
return
}
fun()
hasRunSinceTheLastFullPageload.add(key)
}
6 changes: 0 additions & 6 deletions src/lib/registries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export interface EnhancedTextarea<T extends CommentSpot = CommentSpot> {

export class EnhancerRegistry {
private enhancers = new Set<CommentEnhancer>()
private preparedEnhancers = new Set<CommentEnhancer>()
byType = new Map<string, CommentEnhancer>()

constructor() {
Expand Down Expand Up @@ -66,11 +65,6 @@ export class EnhancerRegistry {
try {
const spot = enhancer.tryToEnhance(textarea, location)
if (spot) {
// Prepare enhancer on first use
if (!this.preparedEnhancers.has(enhancer)) {
enhancer.prepareForFirstEnhancement()
this.preparedEnhancers.add(enhancer)
}
const overtype = enhancer.enhance(textarea, spot)
this.handleDelayedValueInjection(overtype)
return { enhancer, overtype, spot, textarea }
Expand Down
2 changes: 1 addition & 1 deletion tests/corpus-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ app.post('/rebuild', async (_req, res) => {

// Run pnpm run build:dev
const buildProcess = spawn('pnpm', ['run', 'build:dev'], {
cwd: path.join(__dirname, '..', '..'),
cwd: path.join(__dirname, '..'),
stdio: ['pipe', 'pipe', 'pipe'],
})

Expand Down