|
1 | | -import hljs from "highlight.js"; |
2 | | -import OverType from "../overtype/overtype"; |
| 1 | +import { CONFIG } from '../lib/config' |
| 2 | +import { logger } from '../lib/logger' |
| 3 | +import { EnhancerRegistry, TextareaRegistry } from '../lib/registries' |
| 4 | +import { githubPrNewCommentContentScript } from '../playgrounds/github-playground' |
| 5 | + |
| 6 | +const enhancers = new EnhancerRegistry() |
| 7 | +const enhancedTextareas = new TextareaRegistry() |
3 | 8 |
|
4 | 9 | export default defineContentScript({ |
5 | 10 | main() { |
6 | | - if (window.location.hostname !== "github.com") { |
7 | | - return; |
| 11 | + if (CONFIG.MODE === 'PLAYGROUNDS_PR') { |
| 12 | + githubPrNewCommentContentScript() |
| 13 | + return |
8 | 14 | } |
9 | | - OverType.setCodeHighlighter(hljsHighlighter); |
10 | | - const ghCommentBox = document.getElementById( |
11 | | - "new_comment_field" |
12 | | - ) as HTMLTextAreaElement | null; |
13 | | - if (ghCommentBox) { |
14 | | - const overtypeContainer = modifyDOM(ghCommentBox); |
15 | | - new OverType(overtypeContainer, { |
16 | | - placeholder: "Add your comment here...", |
17 | | - autoResize: true, |
18 | | - minHeight: "102px", |
19 | | - padding: "var(--base-size-8)", |
20 | | - }); |
| 15 | + const textAreasOnPageLoad = document.querySelectorAll<HTMLTextAreaElement>(`textarea`) |
| 16 | + for (const textarea of textAreasOnPageLoad) { |
| 17 | + enhanceMaybe(textarea) |
21 | 18 | } |
| 19 | + const observer = new MutationObserver(handleMutations) |
| 20 | + observer.observe(document.body, { |
| 21 | + childList: true, |
| 22 | + subtree: true, |
| 23 | + }) |
| 24 | + logger.debug('Extension loaded with', enhancers.getEnhancerCount, 'handlers') |
22 | 25 | }, |
23 | | - matches: ["<all_urls>"], |
24 | | - runAt: "document_end", |
25 | | -}); |
26 | | - |
27 | | -function modifyDOM(overtypeInput: HTMLTextAreaElement): HTMLElement { |
28 | | - overtypeInput.classList.add("overtype-input"); |
29 | | - const overtypePreview = document.createElement("div"); |
30 | | - overtypePreview.classList.add("overtype-preview"); |
31 | | - overtypeInput.insertAdjacentElement("afterend", overtypePreview); |
32 | | - const overtypeWrapper = overtypeInput.parentElement!.closest("div")!; |
33 | | - overtypeWrapper.classList.add("overtype-wrapper"); |
34 | | - overtypeInput.placeholder = "Add your comment here..."; |
35 | | - const overtypeContainer = overtypeWrapper.parentElement!.closest("div")!; |
36 | | - overtypeContainer.classList.add("overtype-container"); |
37 | | - return overtypeContainer.parentElement!.closest("div")!; |
38 | | -} |
| 26 | + matches: ['<all_urls>'], |
| 27 | + runAt: 'document_end', |
| 28 | +}) |
| 29 | + |
| 30 | +function handleMutations(mutations: MutationRecord[]): void { |
| 31 | + for (const mutation of mutations) { |
| 32 | + for (const node of mutation.addedNodes) { |
| 33 | + if (node.nodeType === Node.ELEMENT_NODE) { |
| 34 | + const element = node as Element |
| 35 | + if (element.tagName === 'TEXTAREA') { |
| 36 | + enhanceMaybe(element as HTMLTextAreaElement) |
| 37 | + } else { |
| 38 | + // Also check for textareas within added subtrees |
| 39 | + const textareas = element.querySelectorAll?.('textarea') |
| 40 | + if (textareas) { |
| 41 | + for (const textarea of textareas) { |
| 42 | + enhanceMaybe(textarea) |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
39 | 48 |
|
40 | | -function hljsHighlighter(code: string, language: string) { |
41 | | - try { |
42 | | - if (language && hljs.getLanguage(language)) { |
43 | | - const result = hljs.highlight(code, { language }); |
44 | | - return result.value; |
45 | | - } else { |
46 | | - const result = hljs.highlightAuto(code); |
47 | | - return result.value; |
| 49 | + for (const node of mutation.removedNodes) { |
| 50 | + if (node.nodeType === Node.ELEMENT_NODE) { |
| 51 | + const element = node as Element |
| 52 | + if (element.tagName === 'TEXTAREA') { |
| 53 | + enhancedTextareas.unregisterDueToModification(element as HTMLTextAreaElement) |
| 54 | + } else { |
| 55 | + // Also check for textareas within removed subtrees |
| 56 | + const textareas = element.querySelectorAll?.('textarea') |
| 57 | + if (textareas) { |
| 58 | + for (const textarea of textareas) { |
| 59 | + enhancedTextareas.unregisterDueToModification(textarea) |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
48 | 64 | } |
49 | | - } catch (error) { |
50 | | - console.warn("highlight.js highlighting failed:", error); |
51 | | - return code; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +function enhanceMaybe(textarea: HTMLTextAreaElement) { |
| 69 | + if (enhancedTextareas.get(textarea)) { |
| 70 | + logger.debug('textarea already registered {}', textarea) |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + logger.debug('activating textarea {}', textarea) |
| 75 | + injectStyles() |
| 76 | + |
| 77 | + const enhancedTextarea = enhancers.tryToEnhance(textarea) |
| 78 | + if (enhancedTextarea) { |
| 79 | + logger.debug( |
| 80 | + 'Identified textarea:', |
| 81 | + enhancedTextarea.spot.type, |
| 82 | + enhancedTextarea.spot.unique_key, |
| 83 | + ) |
| 84 | + enhancedTextareas.register(enhancedTextarea) |
| 85 | + } else { |
| 86 | + logger.debug('No handler found for textarea') |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +const STYLES = ` |
| 91 | +.${CONFIG.ADDED_OVERTYPE_CLASS} { |
| 92 | + background: cyan !important; |
| 93 | +} |
| 94 | +` |
| 95 | + |
| 96 | +function injectStyles(): void { |
| 97 | + if (!document.getElementById('gitcasso-styles')) { |
| 98 | + const style = document.createElement('style') |
| 99 | + style.textContent = STYLES |
| 100 | + style.id = 'gitcasso-styles' |
| 101 | + document.head.appendChild(style) |
52 | 102 | } |
53 | 103 | } |
0 commit comments