Skip to content

Commit d0f705d

Browse files
committed
First cut.
1 parent ded9837 commit d0f705d

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import type { BackgroundMessage, CommentDraft, CommentSpot } from '../lib/enhancer'
2+
import { JsonMap } from '../lib/jsonmap'
3+
4+
interface Tab {
5+
tabId: number
6+
windowId: number
7+
}
8+
interface TabAndSpot {
9+
tab: Tab
10+
spot: CommentSpot
11+
}
12+
interface CommentState {
13+
tab: Tab
14+
spot: CommentSpot
15+
drafts: [number, CommentDraft][]
16+
}
17+
18+
const _states = new JsonMap<TabAndSpot, CommentState>()
19+
20+
browser.runtime.onMessage.addListener((message: BackgroundMessage, sender) => {
21+
if (message.action === 'COMMENT_EVENT' && sender.tab?.id && sender.tab?.windowId) {
22+
const tab: Tab = {
23+
tabId: sender.tab.id,
24+
windowId: sender.tab.windowId,
25+
}
26+
27+
const tabAndSpot: TabAndSpot = {
28+
spot: message.event.spot,
29+
tab,
30+
}
31+
32+
if (message.event.type === 'ENHANCED') {
33+
const commentState: CommentState = {
34+
drafts: [],
35+
spot: message.event.spot,
36+
tab,
37+
}
38+
_states.set(tabAndSpot, commentState)
39+
} else if (message.event.type === 'DESTROYED') {
40+
_states.delete(tabAndSpot)
41+
}
42+
}
43+
})

browser-extension/src/entrypoints/content.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
import { CONFIG, type ModeType } from '../lib/config'
2+
import type { BackgroundMessage, CommentSpot } from '../lib/enhancer'
23
import { logger } from '../lib/logger'
34
import { EnhancerRegistry, TextareaRegistry } from '../lib/registries'
45
import { githubPrNewCommentContentScript } from '../playgrounds/github-playground'
56

67
const enhancers = new EnhancerRegistry()
78
const enhancedTextareas = new TextareaRegistry()
89

10+
function sendEventToBackground(type: 'ENHANCED' | 'DESTROYED', spot: CommentSpot): void {
11+
const message: BackgroundMessage = {
12+
action: 'COMMENT_EVENT',
13+
event: { spot, type },
14+
}
15+
browser.runtime.sendMessage(message).catch((error) => {
16+
logger.debug('Failed to send event to background:', error)
17+
})
18+
}
19+
20+
enhancedTextareas.setEventHandlers(
21+
(spot) => sendEventToBackground('ENHANCED', spot),
22+
(spot) => sendEventToBackground('DESTROYED', spot),
23+
)
24+
925
export default defineContentScript({
1026
main() {
1127
if ((CONFIG.MODE as ModeType) === 'PLAYGROUNDS_PR') {

browser-extension/src/lib/enhancer.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ export interface CommentEvent {
2323
draft: CommentDraft | undefined
2424
}
2525

26+
export interface BackgroundMessage {
27+
action: 'COMMENT_EVENT'
28+
event: {
29+
type: Extract<CommentEventType, 'ENHANCED' | 'DESTROYED'>
30+
spot: CommentSpot
31+
}
32+
}
33+
2634
/** Wraps the textareas of a given platform with Gitcasso's enhancements. */
2735
export interface CommentEnhancer<Spot extends CommentSpot = CommentSpot> {
2836
/** Guarantees to only return a type within this list. */

browser-extension/src/lib/registries.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,26 @@ export class EnhancerRegistry {
5959

6060
export class TextareaRegistry {
6161
private textareas = new Map<HTMLTextAreaElement, EnhancedTextarea>()
62+
private onEnhanced?: (spot: CommentSpot) => void
63+
private onDestroyed?: (spot: CommentSpot) => void
64+
65+
setEventHandlers(
66+
onEnhanced: (spot: CommentSpot) => void,
67+
onDestroyed: (spot: CommentSpot) => void,
68+
): void {
69+
this.onEnhanced = onEnhanced
70+
this.onDestroyed = onDestroyed
71+
}
6272

6373
register<T extends CommentSpot>(textareaInfo: EnhancedTextarea<T>): void {
6474
this.textareas.set(textareaInfo.textarea, textareaInfo)
65-
// TODO: register as a draft in progress with the global list
75+
this.onEnhanced?.(textareaInfo.spot)
6676
}
6777

6878
unregisterDueToModification(textarea: HTMLTextAreaElement): void {
69-
if (this.textareas.has(textarea)) {
70-
// TODO: register as abandoned or maybe submitted with the global list
79+
const textareaInfo = this.textareas.get(textarea)
80+
if (textareaInfo) {
81+
this.onDestroyed?.(textareaInfo.spot)
7182
this.textareas.delete(textarea)
7283
}
7384
}

0 commit comments

Comments
 (0)