@@ -15,6 +15,9 @@ export const InitStomp = (
15
15
? "localhost:8080"
16
16
: window . location . hostname ;
17
17
const serviceURL = `${ protocol } ${ hostname } /websocket` ;
18
+ let snippets : Array < string > = [ ] ;
19
+ let codesnipIdx = 0 ;
20
+
18
21
console . log ( "in the stomp init module" ) ;
19
22
const client = new Client ( {
20
23
brokerURL : serviceURL ,
@@ -44,6 +47,58 @@ export const InitStomp = (
44
47
} ) ;
45
48
client . activate ( ) ;
46
49
50
+ const getIdxs = ( searchTxt : string , data : string ) => {
51
+ let idx = 0 ;
52
+ let tempArray = [ ] ;
53
+ while ( idx !== - 1 ) {
54
+ idx = data . indexOf ( searchTxt , idx + 1 ) ;
55
+ tempArray . push ( idx ) ;
56
+ }
57
+ return tempArray ;
58
+ } ;
59
+
60
+ const getCodeSnippets = ( indexes : Array < number > , data : string ) => {
61
+ let i = 0 ;
62
+ snippets = [ ] ;
63
+ while ( i < indexes . length - 1 && indexes [ 0 ] !== - 1 ) {
64
+ let tempStr = data . substring ( indexes [ i ] , indexes [ i + 1 ] ) ;
65
+ let langLen = tempStr . indexOf ( "\n" ) ;
66
+ let trimmed = tempStr . substring ( langLen ) ;
67
+ snippets . push ( trimmed ) ;
68
+ i += 2 ;
69
+ }
70
+ console . log ( "snippets: " , snippets ) ;
71
+ console . log ( "codesnipIdx in getCodeSnippets: " , codesnipIdx ) ;
72
+ return snippets ;
73
+ } ;
74
+
75
+ const addButtons = ( ) => {
76
+ setTimeout ( ( ) => {
77
+ const codeSections : HTMLCollectionOf < HTMLPreElement > =
78
+ document . getElementsByTagName ( "pre" ) ;
79
+ let x = 0 ;
80
+ for ( let i = codesnipIdx ; i < Array . from ( codeSections ) . length ; i ++ ) {
81
+ let tempStr = JSON . stringify ( snippets [ x ] ) ;
82
+ let btn = document . createElement ( "button" ) ;
83
+ btn . setAttribute ( "id" , "btn-" + codesnipIdx ) ;
84
+ btn . setAttribute ( "onclick" , "copytoclip(" + tempStr + ")" ) ;
85
+ // btn.setAttribute("class", "copy-to-clip-btn");
86
+ btn . innerText = "Copy" ;
87
+ btn . style . cssText = `position:relative;float:right` ;
88
+ codeSections [ codesnipIdx ] ?. prepend ( btn ) ;
89
+ console . log ( "codesnipIdx before increment in addButton: " , codesnipIdx ) ;
90
+ codesnipIdx ++ ;
91
+ x ++ ;
92
+ }
93
+ } , 750 ) ;
94
+ } ;
95
+ const getClipboardOptions = ( data : string ) => {
96
+ const searchTxt = "```" ;
97
+ const indexes = getIdxs ( searchTxt , data ) ;
98
+ const snippets = getCodeSnippets ( indexes , data ) ;
99
+ return indexes [ 0 ] != - 1 ? true : false ;
100
+ } ;
101
+
47
102
const onMessage = ( msg : any ) => {
48
103
let aiAnswer = JSON . parse ( msg . body ) . content ;
49
104
if ( msg . data !== "connected" ) {
@@ -52,12 +107,14 @@ export const InitStomp = (
52
107
setBusy ( false ) ;
53
108
tempArray . pop ( ) ;
54
109
messagesDP . current . data = [ ] ;
110
+ const snipsExist = getClipboardOptions ( aiAnswer ) ;
55
111
tempArray . push ( {
56
112
id : tempArray . length as number ,
57
113
answer : aiAnswer ,
58
114
} ) ;
59
115
chatData . current = tempArray ;
60
116
setUpdate ( chatData . current ) ;
117
+ snipsExist ? addButtons ( ) : null ;
61
118
}
62
119
} ;
63
120
return client ;
0 commit comments