@@ -47,6 +47,8 @@ const completionsInput = document.getElementById("completions-input");
4747const completeButton = document . getElementById ( "complete-button" ) ;
4848const completionsSettingsButton = document . getElementById ( "completions-settings-button" ) ;
4949const completionsStopButton = document . getElementById ( "completions-stop-button" ) ;
50+ const uploadButton = document . getElementById ( "upload-button" ) ;
51+ const fileUpload = document . getElementById ( "file-upload" ) ;
5052
5153let abortController = null ;
5254let disableAutoScroll = false ;
@@ -308,19 +310,26 @@ async function fixImageDataUri(dataUri, maxLength = 1024 * 1024) {
308310}
309311
310312async function onFile ( file ) {
311- if ( ! file . type . toLowerCase ( ) . startsWith ( 'image/' ) ) {
312- console . warn ( 'Only image files are supported' ) ;
313+ const reader = new FileReader ( ) ;
314+ if ( file . type . toLowerCase ( ) . startsWith ( 'image/' ) ) {
315+ reader . onloadend = async function ( ) {
316+ const description = file . name ;
317+ const realDataUri = await fixImageDataUri ( reader . result ) ;
318+ const fakeDataUri = 'data:,placeholder/' + generateId ( ) ;
319+ uploadedFiles . push ( [ fakeDataUri , realDataUri ] ) ;
320+ insertText ( chatInput , `` ) ;
321+ } ;
322+ reader . readAsDataURL ( file ) ;
323+ } else if ( file . type . toLowerCase ( ) . startsWith ( 'text/' ) ) {
324+ reader . onloadend = function ( ) {
325+ const content = reader . result ;
326+ insertText ( chatInput , `\`\`\`\n${ content } \n\`\`\`` ) ;
327+ } ;
328+ reader . readAsText ( file ) ;
329+ } else {
330+ console . warn ( 'Only image and text files are supported' ) ;
313331 return ;
314332 }
315- const reader = new FileReader ( ) ;
316- reader . onloadend = async function ( ) {
317- const description = file . name ;
318- const realDataUri = await fixImageDataUri ( reader . result ) ;
319- const fakeDataUri = 'data:,placeholder/' + generateId ( ) ;
320- uploadedFiles . push ( [ fakeDataUri , realDataUri ] ) ;
321- insertText ( chatInput , `` ) ;
322- } ;
323- reader . readAsDataURL ( file ) ;
324333}
325334
326335function insertText ( elem , text ) {
@@ -676,6 +685,17 @@ async function chatbot() {
676685 document . addEventListener ( "drop" , onDragEnd ) ;
677686 document . addEventListener ( "drop" , onDrop ) ;
678687 document . addEventListener ( "paste" , onPaste ) ;
688+
689+ uploadButton . addEventListener ( "click" , ( ) => {
690+ fileUpload . click ( ) ;
691+ } ) ;
692+
693+ fileUpload . addEventListener ( "change" , ( e ) => {
694+ if ( e . target . files [ 0 ] ) {
695+ onFile ( e . target . files [ 0 ] ) ;
696+ e . target . value = '' ;
697+ }
698+ } ) ;
679699}
680700
681701chatbot ( ) ;
0 commit comments