File tree Expand file tree Collapse file tree 4 files changed +81
-3
lines changed Expand file tree Collapse file tree 4 files changed +81
-3
lines changed Original file line number Diff line number Diff line change 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+ } ) 
Original file line number Diff line number Diff line change 11import  {  CONFIG ,  type  ModeType  }  from  '../lib/config' 
2+ import  type  {  BackgroundMessage ,  CommentSpot  }  from  '../lib/enhancer' 
23import  {  logger  }  from  '../lib/logger' 
34import  {  EnhancerRegistry ,  TextareaRegistry  }  from  '../lib/registries' 
45import  {  githubPrNewCommentContentScript  }  from  '../playgrounds/github-playground' 
56
67const  enhancers  =  new  EnhancerRegistry ( ) 
78const  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+ 
925export  default  defineContentScript ( { 
1026  main ( )  { 
1127    if  ( ( CONFIG . MODE  as  ModeType )  ===  'PLAYGROUNDS_PR' )  { 
Original file line number Diff line number Diff 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. */ 
2735export  interface  CommentEnhancer < Spot  extends  CommentSpot  =  CommentSpot >  { 
2836  /** Guarantees to only return a type within this list. */ 
Original file line number Diff line number Diff line change @@ -59,15 +59,26 @@ export class EnhancerRegistry {
5959
6060export  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  } 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments