Skip to content

Commit 76ddf61

Browse files
committed
fix: x scroll and more guessed encodings
1 parent dbf47cc commit 76ddf61

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

frontend/components/CodeEditor.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export function CodeEditor({
7777
className,
7878
...rest
7979
}: CodeInputProps) {
80-
const refHighlighting = useRef<HTMLDivElement | null>(null)
80+
const refHighlighting = useRef<HTMLPreElement | null>(null)
8181
const refTextarea = useRef<HTMLTextAreaElement | null>(null)
8282

8383
const [heightPx, setHeightPx] = useState<number>(0)
@@ -178,10 +178,11 @@ export function CodeEditor({
178178
className={`relative w-full`}
179179
style={{ tabSize: tabSetting.char === "tab" ? tabSetting.width : undefined }}
180180
>
181-
<div className={"w-full font-mono overflow-auto top-0 left-0 absolute"} ref={refHighlighting}>
181+
<div className={"w-full font-mono top-0 left-0 absolute"}>
182182
<pre
183-
className={`text-foreground ${tst}`}
184-
style={{ paddingLeft: lineNumOffset, height: `${heightPx}px` }}
183+
ref={refHighlighting}
184+
className={`text-foreground ${tst} w-full overflow-x-hidden`}
185+
style={{ marginLeft: lineNumOffset, width: `calc(100% - ${lineNumOffset})`, height: `${heightPx}px` }}
185186
dangerouslySetInnerHTML={{ __html: highlightHTML(hljs, lang, handleNewLines(content)) }}
186187
></pre>
187188
<span
@@ -196,9 +197,10 @@ export function CodeEditor({
196197
</span>
197198
</div>
198199
<textarea
199-
className={`w-full font-mono min-h-[20em] text-[transparent] placeholder-default-400
200-
caret-foreground bg-transparent outline-none relative`}
201-
style={{ paddingLeft: lineNumOffset }}
200+
className={`w-full font-mono min-h-[20em] text-transparent placeholder-default-400
201+
caret-foreground bg-transparent outline-none relative overflow-x-auto`}
202+
style={{ marginLeft: lineNumOffset, width: `calc(100% - ${lineNumOffset})` }}
203+
wrap={"off"}
202204
ref={refTextarea}
203205
readOnly={disabled}
204206
placeholder={placeholder}

frontend/pages/DisplayPaste.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ import "../style.css"
1818
import "../styles/highlight-theme-light.css"
1919
import "../styles/highlight-theme-dark.css"
2020

21+
const utf8CompatibleEncodings = ["utf-8", "ASCII", "ISO-8859-1"]
22+
2123
export 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

Comments
 (0)