Skip to content

Commit 1e03587

Browse files
authored
Cleanup and centralize all our "once per pageload" stuff (#61)
2 parents bda9caa + fe425af commit 1e03587

14 files changed

+33
-72
lines changed

src/entrypoints/content.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { CONFIG } from '../lib/config'
21
import type { CommentEvent, CommentSpot, StrippedLocation } from '../lib/enhancer'
32
import { logger } from '../lib/logger'
43
import { EnhancerRegistry, TextareaRegistry } from '../lib/registries'
@@ -98,8 +97,6 @@ function enhanceMaybe(textarea: HTMLTextAreaElement) {
9897
logger.debug('textarea already registered {}', textarea)
9998
return
10099
}
101-
102-
injectStyles()
103100
try {
104101
const location = detectLocation()
105102
logger.debug('[gitcasso] Calling tryToEnhance with location:', location)
@@ -118,18 +115,3 @@ function enhanceMaybe(textarea: HTMLTextAreaElement) {
118115
logger.error(e)
119116
}
120117
}
121-
122-
const STYLES = `
123-
.${CONFIG.ADDED_OVERTYPE_CLASS} {
124-
background: cyan !important;
125-
}
126-
`
127-
128-
function injectStyles(): void {
129-
if (!document.getElementById('gitcasso-styles')) {
130-
const style = document.createElement('style')
131-
style.textContent = STYLES
132-
style.id = 'gitcasso-styles'
133-
document.head.appendChild(style)
134-
}
135-
}

src/lib/config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const LOG_LEVELS = ['DEBUG', 'INFO', 'WARN', 'ERROR'] as const
77
export type LogLevel = (typeof LOG_LEVELS)[number]
88

99
export const CONFIG = {
10-
ADDED_OVERTYPE_CLASS: 'gitcasso-overtype',
1110
EXTENSION_NAME: 'gitcasso', // decorates logs
1211
LOG_LEVEL: 'DEBUG' satisfies LogLevel,
1312
MODE: 'PROD' satisfies ModeType,

src/lib/enhancer.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,8 @@ export interface CommentEnhancer<Spot extends CommentSpot = CommentSpot> {
3737
* If we return non-null, then we become the handler for that text area.
3838
*/
3939
tryToEnhance(textarea: HTMLTextAreaElement, location: StrippedLocation): Spot | null
40-
/** This gets called the first time that `tryToEnhance` returns non-null. */
41-
prepareForFirstEnhancement(): void
4240
/**
4341
* If `tryToEnhance` returns non-null, then this gets called.
44-
* It is guaranteed that `prepareForFirstEnhancement` has been called
45-
* exactly once since pageload before this gets called.
4642
*/
4743
enhance(textarea: HTMLTextAreaElement, spot: Spot): OverTypeInstance
4844
/** Returns a ReactNode which will be displayed in the table row. */

src/lib/enhancers/CommentEnhancerMissing.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ export class CommentEnhancerMissing implements CommentEnhancer {
4343
tryToEnhance(_textarea: HTMLTextAreaElement, _location: StrippedLocation): CommentSpot | null {
4444
throw new Error('Method not implemented.')
4545
}
46-
prepareForFirstEnhancement(): void {
47-
throw new Error('Method not implemented.')
48-
}
4946
enhance(_textarea: HTMLTextAreaElement, _spot: CommentSpot): OverTypeInstance {
5047
throw new Error('Method not implemented.')
5148
}

src/lib/enhancers/github/githubEditComment.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { CommentEnhancer, CommentSpot } from '@/lib/enhancer'
44
import { logger } from '@/lib/logger'
55
import { modifyDOM } from '../modifyDOM'
66
import { commonGithubOptions } from './ghOptions'
7-
import { githubHighlighter } from './githubHighlighter'
7+
import { prepareGitHubHighlighter } from './githubHighlighter'
88

99
export interface GitHubEditCommentSpot extends CommentSpot {
1010
type: 'GH_EDIT_COMMENT'
@@ -48,11 +48,8 @@ export class GitHubEditCommentEnhancer implements CommentEnhancer<GitHubEditComm
4848
}
4949
}
5050

51-
prepareForFirstEnhancement(): void {
52-
OverType.setCodeHighlighter(githubHighlighter)
53-
}
54-
5551
enhance(textArea: HTMLTextAreaElement, _spot: GitHubEditCommentSpot): OverTypeInstance {
52+
prepareGitHubHighlighter()
5653
const overtypeContainer = modifyDOM(textArea)
5754
return new OverType(overtypeContainer, {
5855
...commonGithubOptions,

src/lib/enhancers/github/githubHighlighter.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import hljs from 'highlight.js'
2+
import OverType from 'overtype'
3+
import { oncePerRefresh } from '@/lib/once-per-refresh'
24

3-
export function githubHighlighter(code: string, language?: string) {
5+
export function prepareGitHubHighlighter() {
6+
oncePerRefresh('github-highlighter', () => {
7+
OverType.setCodeHighlighter(githubHighlighter)
8+
})
9+
}
10+
11+
function githubHighlighter(code: string, language?: string) {
412
try {
513
if (language && hljs.getLanguage(language)) {
614
const result = hljs.highlight(code, { language })

src/lib/enhancers/github/githubIssueAddComment.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { CommentEnhancer, CommentSpot, StrippedLocation } from '@/lib/enhan
55
import { logger } from '@/lib/logger'
66
import { modifyDOM } from '../modifyDOM'
77
import { commonGithubOptions } from './ghOptions'
8-
import { githubHighlighter } from './githubHighlighter'
8+
import { prepareGitHubHighlighter } from './githubHighlighter'
99

1010
export interface GitHubIssueAddCommentSpot extends CommentSpot {
1111
type: 'GH_ISSUE_ADD_COMMENT'
@@ -53,11 +53,8 @@ export class GitHubIssueAddCommentEnhancer implements CommentEnhancer<GitHubIssu
5353
}
5454
}
5555

56-
prepareForFirstEnhancement(): void {
57-
OverType.setCodeHighlighter(githubHighlighter)
58-
}
59-
6056
enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueAddCommentSpot): OverTypeInstance {
57+
prepareGitHubHighlighter()
6158
const overtypeContainer = modifyDOM(textArea)
6259
return new OverType(overtypeContainer, {
6360
...commonGithubOptions,

src/lib/enhancers/github/githubIssueNewComment.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { CommentEnhancer, CommentSpot, StrippedLocation } from '../../enhan
33
import { logger } from '../../logger'
44
import { modifyDOM } from '../modifyDOM'
55
import { commonGithubOptions } from './ghOptions'
6-
import { githubHighlighter } from './githubHighlighter'
6+
import { prepareGitHubHighlighter } from './githubHighlighter'
77

88
interface GitHubIssueNewCommentSpot extends CommentSpot {
99
type: 'GH_ISSUE_NEW_COMMENT'
@@ -42,11 +42,8 @@ export class GitHubIssueNewCommentEnhancer implements CommentEnhancer<GitHubIssu
4242
}
4343
}
4444

45-
prepareForFirstEnhancement(): void {
46-
OverType.setCodeHighlighter(githubHighlighter)
47-
}
48-
4945
enhance(textArea: HTMLTextAreaElement, _spot: GitHubIssueNewCommentSpot): OverTypeInstance {
46+
prepareGitHubHighlighter()
5047
const overtypeContainer = modifyDOM(textArea)
5148
return new OverType(overtypeContainer, {
5249
...commonGithubOptions,

src/lib/enhancers/github/githubPRAddComment.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { CommentEnhancer, CommentSpot, StrippedLocation } from '@/lib/enhan
44
import { logger } from '@/lib/logger'
55
import { modifyDOM } from '../modifyDOM'
66
import { commonGithubOptions } from './ghOptions'
7-
import { githubHighlighter } from './githubHighlighter'
7+
import { prepareGitHubHighlighter } from './githubHighlighter'
88

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

52-
prepareForFirstEnhancement(): void {
53-
OverType.setCodeHighlighter(githubHighlighter)
54-
}
55-
5652
enhance(textArea: HTMLTextAreaElement, _spot: GitHubPRAddCommentSpot): OverTypeInstance {
53+
prepareGitHubHighlighter()
5754
const overtypeContainer = modifyDOM(textArea)
5855
return new OverType(overtypeContainer, {
5956
...commonGithubOptions,

src/lib/enhancers/github/githubPRNewComment.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { CommentEnhancer, CommentSpot, StrippedLocation } from '../../enhan
33
import { logger } from '../../logger'
44
import { modifyDOM } from '../modifyDOM'
55
import { commonGithubOptions } from './ghOptions'
6-
import { githubHighlighter } from './githubHighlighter'
6+
import { prepareGitHubHighlighter } from './githubHighlighter'
77

88
interface GitHubPRNewCommentSpot extends CommentSpot {
99
type: 'GH_PR_NEW_COMMENT'
@@ -50,11 +50,8 @@ export class GitHubPRNewCommentEnhancer implements CommentEnhancer<GitHubPRNewCo
5050
}
5151
}
5252

53-
prepareForFirstEnhancement(): void {
54-
OverType.setCodeHighlighter(githubHighlighter)
55-
}
56-
5753
enhance(textArea: HTMLTextAreaElement, _spot: GitHubPRNewCommentSpot): OverTypeInstance {
54+
prepareGitHubHighlighter()
5855
const overtypeContainer = modifyDOM(textArea)
5956
return new OverType(overtypeContainer, {
6057
...commonGithubOptions,

0 commit comments

Comments
 (0)