@@ -18,12 +18,15 @@ import "../style.css"
1818import "../styles/highlight-theme-light.css"
1919import "../styles/highlight-theme-dark.css"
2020
21+ const utf8CompatibleEncodings = [ "utf-8" , "ASCII" , "ISO-8859-1" ]
22+
2123export function DisplayPaste ( ) {
2224 const [ pasteFile , setPasteFile ] = useState < File | undefined > ( undefined )
2325 const [ pasteContentBuffer , setPasteContentBuffer ] = useState < ArrayBuffer | undefined > ( undefined )
2426 const [ pasteLang , setPasteLang ] = useState < string | undefined > ( undefined )
2527
2628 const [ isFileBinary , setFileBinary ] = useState ( false )
29+ const [ guessedEncoding , setGuessedEncoding ] = useState < string | null > ( null )
2730 const [ isDecrypted , setDecrypted ] = useState < "not encrypted" | "encrypted" | "decrypted" > ( "not encrypted" )
2831 const [ forceShowBinary , setForceShowBinary ] = useState ( false )
2932 const showFileContent = pasteFile !== undefined && ( ! isFileBinary || forceShowBinary )
@@ -80,7 +83,8 @@ export function DisplayPaste() {
8083 setFileBinary ( true )
8184 } else {
8285 const encoding = chardet . detect ( respBytes )
83- setFileBinary ( encoding !== "utf8" && encoding !== "ASCII" )
86+ setFileBinary ( encoding === null || ! utf8CompatibleEncodings . includes ( encoding ) )
87+ setGuessedEncoding ( encoding )
8488 }
8589 } else {
8690 let key : CryptoKey | undefined
@@ -106,8 +110,9 @@ export function DisplayPaste() {
106110 setPasteLang ( lang || undefined )
107111
108112 const encoding = chardet . detect ( decrypted )
109- setFileBinary ( encoding !== "utf8" && encoding !== "ASCII" )
113+ setFileBinary ( encoding === null || ! utf8CompatibleEncodings . includes ( encoding ) )
110114 setDecrypted ( "decrypted" )
115+ setGuessedEncoding ( encoding )
111116 }
112117 } finally {
113118 setIsLoading ( false )
@@ -123,14 +128,15 @@ export function DisplayPaste() {
123128 < div className = "absolute top-[50%] left-[50%] translate-[-50%] flex flex-col items-center w-full" >
124129 < div className = "text-foreground-600 mb-2" > { `${ pasteFile ?. name } (${ formatSize ( pasteFile . size ) } )` } </ div >
125130 < div className = "w-fit text-center" >
126- This file seems to be binary or not in UTF-8. { " "}
131+ This file seems to be binary or not in UTF-8{ guessedEncoding ? ` ( ${ guessedEncoding } guessed). ` : ". "}
127132 < button className = "text-primary-500 inline" onClick = { ( ) => setForceShowBinary ( true ) } >
128133 (Click to show)
129134 </ button >
130135 </ div >
131136 </ div >
132137 )
133138
139+ const lineNumOffset = `${ Math . floor ( Math . log10 ( pasteLineCount ) ) + 3 } ch`
134140 const buttonClasses = `rounded-full bg-background hover:bg-default-100 ${ tst } `
135141 return (
136142 < main
@@ -193,8 +199,9 @@ export function DisplayPaste() {
193199 </ div >
194200 < div className = "font-mono relative" role = "article" >
195201 < pre
196- style = { { paddingLeft : ` ${ Math . floor ( Math . log10 ( pasteLineCount ) ) + 3 } ch ` } }
202+ style = { { marginLeft : lineNumOffset , width : `calc(100% - ${ lineNumOffset } ) ` } }
197203 dangerouslySetInnerHTML = { { __html : highlightedHTML } }
204+ className = { "overflow-x-auto" }
198205 />
199206 < span
200207 className = {
0 commit comments