diff --git a/src/entrypoints/content.ts b/src/entrypoints/content.ts index 3136329..5dc5122 100644 --- a/src/entrypoints/content.ts +++ b/src/entrypoints/content.ts @@ -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' @@ -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) @@ -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) - } -} diff --git a/src/lib/config.ts b/src/lib/config.ts index 74b71b2..a98e375 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -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, diff --git a/src/lib/enhancer.ts b/src/lib/enhancer.ts index d835c4c..56dc6a4 100644 --- a/src/lib/enhancer.ts +++ b/src/lib/enhancer.ts @@ -37,12 +37,8 @@ export interface CommentEnhancer { * 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. */ diff --git a/src/lib/enhancers/CommentEnhancerMissing.tsx b/src/lib/enhancers/CommentEnhancerMissing.tsx index f59a6e6..b123e1c 100644 --- a/src/lib/enhancers/CommentEnhancerMissing.tsx +++ b/src/lib/enhancers/CommentEnhancerMissing.tsx @@ -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.') } diff --git a/src/lib/enhancers/github/githubEditComment.tsx b/src/lib/enhancers/github/githubEditComment.tsx index a457ae6..4f6ea46 100644 --- a/src/lib/enhancers/github/githubEditComment.tsx +++ b/src/lib/enhancers/github/githubEditComment.tsx @@ -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' @@ -48,11 +48,8 @@ export class GitHubEditCommentEnhancer implements CommentEnhancer { + OverType.setCodeHighlighter(githubHighlighter) + }) +} + +function githubHighlighter(code: string, language?: string) { try { if (language && hljs.getLanguage(language)) { const result = hljs.highlight(code, { language }) diff --git a/src/lib/enhancers/github/githubIssueAddComment.tsx b/src/lib/enhancers/github/githubIssueAddComment.tsx index 233508d..a36de96 100644 --- a/src/lib/enhancers/github/githubIssueAddComment.tsx +++ b/src/lib/enhancers/github/githubIssueAddComment.tsx @@ -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' @@ -53,11 +53,8 @@ export class GitHubIssueAddCommentEnhancer implements CommentEnhancer() + +/** + * 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) +} diff --git a/src/lib/registries.ts b/src/lib/registries.ts index 534d688..3a08e98 100644 --- a/src/lib/registries.ts +++ b/src/lib/registries.ts @@ -17,7 +17,6 @@ export interface EnhancedTextarea { export class EnhancerRegistry { private enhancers = new Set() - private preparedEnhancers = new Set() byType = new Map() constructor() { @@ -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 } diff --git a/tests/corpus-view.ts b/tests/corpus-view.ts index b7fdd00..464bf59 100644 --- a/tests/corpus-view.ts +++ b/tests/corpus-view.ts @@ -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'], })