From e3eb09353346079cdd9d05e5e6a7903a7d9ffb76 Mon Sep 17 00:00:00 2001 From: Jiayi Zhuang Date: Mon, 9 Dec 2024 23:58:33 +0800 Subject: [PATCH] feat: auto detect encoding for text preview (#204) --- package.json | 1 + pnpm-lock.yaml | 26 ++++++++++++++++--------- src/components/EncodingSelect.tsx | 22 +++++++++++++++++++++ src/components/Markdown.tsx | 6 +++++- src/pages/home/previews/html.tsx | 6 +++++- src/pages/home/previews/text-editor.tsx | 6 +++++- 6 files changed, 55 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 989187dbeb..a6e684de50 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e11dd7a8cd..739b883095 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -16,7 +16,7 @@ importers: version: 2.1.1 '@hope-ui/solid': specifier: 0.6.7 - version: 0.6.7(@stitches/core@1.2.8)(solid-js@1.4.8)(solid-transition-group@0.0.12) + version: 0.6.7(@stitches/core@1.2.8)(solid-js@1.4.8)(solid-transition-group@0.0.12(solid-js@1.4.8)) '@monaco-editor/loader': specifier: ^1.4.0 version: 1.4.0(monaco-editor@0.50.0) @@ -53,6 +53,9 @@ importers: axios: specifier: ^1.0.0 version: 1.1.3 + chardet: + specifier: ^2.0.0 + version: 2.0.0 clsx: specifier: ^2.0.0 version: 2.0.0 @@ -103,7 +106,7 @@ importers: version: 0.2.0 solid-contextmenu: specifier: 0.0.2 - version: 0.0.2(solid-js@1.4.8)(solid-transition-group@0.0.12) + version: 0.0.2(solid-js@1.4.8)(solid-transition-group@0.0.12(solid-js@1.4.8)) solid-icons: specifier: ^1.0.1 version: 1.0.1(solid-js@1.4.8) @@ -143,7 +146,7 @@ importers: version: 2.0.1 '@vitejs/plugin-legacy': specifier: ^2.0.1 - version: 2.0.1(terser@5.14.2)(vite@3.0.8) + version: 2.0.1(terser@5.14.2)(vite@3.0.8(terser@5.14.2)) husky: specifier: ^8.0.2 version: 8.0.2 @@ -170,7 +173,7 @@ importers: version: 0.4.4 vite-plugin-solid: specifier: ^2.3.0 - version: 2.3.0(solid-js@1.4.8)(vite@3.0.8) + version: 2.3.0(solid-js@1.4.8)(vite@3.0.8(terser@5.14.2)) packages: @@ -656,6 +659,9 @@ packages: character-entities@2.0.2: resolution: {integrity: sha512-shx7oQ0Awen/BRIdkjkvz54PnEEI/EjwXDSIZp86/KKdbafHh1Df/RYGBhn4hbe2+uKC9FnT5UCEdyPz3ai9hQ==} + chardet@2.0.0: + resolution: {integrity: sha512-xVgPpulCooDjY6zH4m9YW3jbkaBe3FKIAvF5sj5t7aBNsVl2ljIE+xwJ4iNgiDZHFQvNIpjdKdVOQvvk5ZfxbQ==} + chownr@1.1.4: resolution: {integrity: sha512-jJ0bqzaylmJtVnNgzTeSOs8DPavpbYgEr/b0YL8/2GO3xJEhInFmhKMUnEJQjZumK7KXGFhUy89PrsJWlakBVg==} @@ -2189,7 +2195,7 @@ snapshots: '@github/webauthn-json@2.1.1': {} - '@hope-ui/solid@0.6.7(@stitches/core@1.2.8)(solid-js@1.4.8)(solid-transition-group@0.0.12)': + '@hope-ui/solid@0.6.7(@stitches/core@1.2.8)(solid-js@1.4.8)(solid-transition-group@0.0.12(solid-js@1.4.8))': dependencies: '@floating-ui/dom': 0.4.4 '@stitches/core': 1.2.8 @@ -2386,7 +2392,7 @@ snapshots: '@viselect/vanilla@3.5.0': {} - '@vitejs/plugin-legacy@2.0.1(terser@5.14.2)(vite@3.0.8)': + '@vitejs/plugin-legacy@2.0.1(terser@5.14.2)(vite@3.0.8(terser@5.14.2))': dependencies: '@babel/standalone': 7.18.12 core-js: 3.24.1 @@ -2505,6 +2511,8 @@ snapshots: character-entities@2.0.2: {} + chardet@2.0.0: {} + chownr@1.1.4: {} clean-stack@2.2.0: {} @@ -3658,7 +3666,7 @@ snapshots: smoothscroll@0.4.0: {} - solid-contextmenu@0.0.2(solid-js@1.4.8)(solid-transition-group@0.0.12): + solid-contextmenu@0.0.2(solid-js@1.4.8)(solid-transition-group@0.0.12(solid-js@1.4.8)): dependencies: clsx: 1.2.1 mitt: 3.0.0 @@ -3879,7 +3887,7 @@ snapshots: dependencies: node-html-parser: 5.4.1 - vite-plugin-solid@2.3.0(solid-js@1.4.8)(vite@3.0.8): + vite-plugin-solid@2.3.0(solid-js@1.4.8)(vite@3.0.8(terser@5.14.2)): dependencies: '@babel/core': 7.18.10 '@babel/preset-typescript': 7.18.6(@babel/core@7.18.10) @@ -3897,9 +3905,9 @@ snapshots: postcss: 8.4.16 resolve: 1.22.1 rollup: 2.77.3 - terser: 5.14.2 optionalDependencies: fsevents: 2.3.3 + terser: 5.14.2 web-namespaces@2.0.1: {} diff --git a/src/components/EncodingSelect.tsx b/src/components/EncodingSelect.tsx index dbb628d2cb..260b175dcf 100644 --- a/src/components/EncodingSelect.tsx +++ b/src/components/EncodingSelect.tsx @@ -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", @@ -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 ( - + diff --git a/src/pages/home/previews/html.tsx b/src/pages/home/previews/html.tsx index a94c4db978..82efcdfb79 100644 --- a/src/pages/home/previews/html.tsx +++ b/src/pages/home/previews/html.tsx @@ -16,7 +16,11 @@ function Html(props: { children?: string | ArrayBuffer }) { srcdoc={text(encoding())} /> - + ) diff --git a/src/pages/home/previews/text-editor.tsx b/src/pages/home/previews/text-editor.tsx index d6d4547209..d200fdda7d 100644 --- a/src/pages/home/previews/text-editor.tsx +++ b/src/pages/home/previews/text-editor.tsx @@ -45,7 +45,11 @@ function Editor(props: { data?: string | ArrayBuffer; contentType?: string }) { return ( - +