Skip to content

Commit

Permalink
feat: auto detect encoding for text preview
Browse files Browse the repository at this point in the history
  • Loading branch information
Wybxc committed Dec 3, 2024
1 parent 6171bd6 commit 640912c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://www.nn.ci"
},
"description": "The front end of Alist V3",
"packageManager": "pnpm@9.9.0",
"packageManager": "pnpm@9.14.4+sha512.c8180b3fbe4e4bca02c94234717896b5529740a6cbadf19fa78254270403ea2f27d4e1d46a08a0f56c89b63dc8ebfd3ee53326da720273794e6200fcf0d184ab",
"repository": {
"type": "git",
"url": "git+https://github.com/alist-org/alist-web.git"
Expand Down Expand Up @@ -67,6 +67,7 @@
"artplayer-plugin-danmuku": "^5.0.1",
"asciinema-player": "^3.6.3",
"axios": "^1.0.0",
"chardet": "^2.0.0",
"clsx": "^2.0.0",
"copy-to-clipboard": "^3.3.2",
"crypto-js": "^4.2.0",
Expand Down
26 changes: 17 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions src/components/EncodingSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Box } from "@hope-ui/solid"
import { SelectWrapper } from "./Base"
import chardet from "chardet"
import { createEffect } from "solid-js"

export function EncodingSelect(props: {
encoding: string
setEncoding: (encoding: string) => void
referenceText?: string | ArrayBuffer
}) {
const encodingLabels = [
"utf-8",
Expand Down Expand Up @@ -48,6 +51,25 @@ export function EncodingSelect(props: {
"x-user-defined",
"iso-2022-cn",
]

createEffect(() => {
if (props.referenceText) {
let buffer: Uint8Array
if (typeof props.referenceText === "string") {
buffer = new TextEncoder().encode(props.referenceText)
} else {
buffer = new Uint8Array(props.referenceText)
}
for (let encoding of chardet.analyse(buffer)) {
const encodingLabel = encoding.name.toLowerCase()
if (encodingLabels.includes(encodingLabel)) {
props.setEncoding(encodingLabel)
return
}
}
}
})

return (
<Box
pos="absolute"
Expand Down
6 changes: 5 additions & 1 deletion src/components/Markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,11 @@ export function Markdown(props: {
/>
</Show>
<Show when={!isString}>
<EncodingSelect encoding={encoding()} setEncoding={setEncoding} />
<EncodingSelect
encoding={encoding()}
setEncoding={setEncoding}
referenceText={props.children}
/>
</Show>
<MarkdownToc disabled={!props.toc} markdownRef={markdownRef()!} />
</Box>
Expand Down
6 changes: 5 additions & 1 deletion src/pages/home/previews/html.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ function Html(props: { children?: string | ArrayBuffer }) {
srcdoc={text(encoding())}
/>
<Show when={!isString}>
<EncodingSelect encoding={encoding()} setEncoding={setEncoding} />
<EncodingSelect
encoding={encoding()}
setEncoding={setEncoding}
referenceText={props.children}
/>
</Show>
</BoxWithFullScreen>
)
Expand Down
6 changes: 5 additions & 1 deletion src/pages/home/previews/text-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ function Editor(props: { data?: string | ArrayBuffer; contentType?: string }) {
return (
<VStack w="$full" alignItems="start" spacing="$2" pos="relative">
<Show when={!isString}>
<EncodingSelect encoding={encoding()} setEncoding={setEncoding} />
<EncodingSelect
encoding={encoding()}
setEncoding={setEncoding}
referenceText={props.data}
/>
</Show>
<MonacoEditorLoader
value={text(encoding())}
Expand Down

0 comments on commit 640912c

Please sign in to comment.