@@ -15,6 +15,9 @@ export const InitStomp = (
1515 ? "localhost:8080"
1616 : window . location . hostname ;
1717 const serviceURL = `${ protocol } ${ hostname } /websocket` ;
18+ let snippets : Array < string > = [ ] ;
19+ let codesnipIdx = 0 ;
20+
1821 console . log ( "in the stomp init module" ) ;
1922 const client = new Client ( {
2023 brokerURL : serviceURL ,
@@ -44,6 +47,58 @@ export const InitStomp = (
4447 } ) ;
4548 client . activate ( ) ;
4649
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+
47102 const onMessage = ( msg : any ) => {
48103 let aiAnswer = JSON . parse ( msg . body ) . content ;
49104 if ( msg . data !== "connected" ) {
@@ -52,12 +107,14 @@ export const InitStomp = (
52107 setBusy ( false ) ;
53108 tempArray . pop ( ) ;
54109 messagesDP . current . data = [ ] ;
110+ const snipsExist = getClipboardOptions ( aiAnswer ) ;
55111 tempArray . push ( {
56112 id : tempArray . length as number ,
57113 answer : aiAnswer ,
58114 } ) ;
59115 chatData . current = tempArray ;
60116 setUpdate ( chatData . current ) ;
117+ snipsExist ? addButtons ( ) : null ;
61118 }
62119 } ;
63120 return client ;
0 commit comments