Skip to content

Commit 1c7a79f

Browse files
committed
feat: 支持简单检测文件编码进行自动加载
1 parent 6d50982 commit 1c7a79f

5 files changed

Lines changed: 17 additions & 56 deletions

File tree

frontend/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"axios": "^1.13.2",
1515
"buffer": "^6.0.3",
1616
"element-plus": "^2.11.9",
17-
"iconv-lite": "^0.7.0",
1817
"lodash": "^4.17.21",
1918
"marked": "^17.0.1",
2019
"monaco-editor": "^0.54.0",

frontend/pnpm-lock.yaml

Lines changed: 0 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/src/components/MonacoEditor.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
import { computed, onMounted, ref, watch } from 'vue'
5858
import MonacoEditor from 'monaco-editor-vue3'
5959
import { Camera } from '@element-plus/icons-vue'
60-
import * as iconv from 'iconv-lite'
6160
6261
import MdView from '@/components/MdView.vue'
6362
@@ -94,7 +93,7 @@ const { editorDidMount, changeLang, changeTheme, changeOption } = useEditor({ on
9493
9594
const changeEncode = async (v: string) => {
9695
const buffer = await code.blob.arrayBuffer()
97-
code.org = code.value = iconv.decode(new Uint8Array(buffer), v)
96+
code.org = code.value = new TextDecoder(v).decode(buffer)
9897
}
9998
10099
watch(

frontend/src/utils/file.ts

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as iconv from 'iconv-lite'
2-
31
import { HOST } from '@/utils/env'
42
import { ENCODING_OPTIONS } from '@/utils/option'
53

@@ -46,44 +44,25 @@ export const getKey = (path: string) =>
4644
.replace(/\./g, '_')
4745

4846
export const getEncodeValue = async (blob: Blob) => {
49-
const result = { encode: '', value: '' }
50-
51-
const buffer = new Uint8Array(await blob.arrayBuffer())
47+
const encode: string[] = []
48+
const buffer = await blob.arrayBuffer()
5249

53-
if (buffer.length >= 3 && buffer[0] === 0xef && buffer[1] === 0xbb && buffer[2] === 0xbf) {
54-
result.encode = 'utf8'
55-
} else if (buffer[0] === 0xfe && buffer[1] === 0xff) {
56-
result.encode = 'utf16be'
57-
} else if (buffer[0] === 0xff && buffer[1] === 0xfe) {
58-
result.encode = 'utf16le'
59-
}
50+
for (const item of ENCODING_OPTIONS) {
51+
try {
52+
const decoder = new TextDecoder(item.value)
53+
const text = decoder.decode(buffer)
6054

61-
if (!result.encode) {
62-
for (const item of ENCODING_OPTIONS) {
63-
try {
64-
const text = iconv.decode(buffer, item.value)
65-
const sum = text.length
66-
const err = text.split('').filter((i) => i === '�').length
67-
68-
if (err / sum > 0.3) {
69-
result.encode = item.value
70-
result.value = text
71-
}
72-
} catch {
55+
if (text.includes('�')) {
7356
continue
7457
}
75-
}
76-
}
77-
78-
if (!result.encode) {
79-
result.encode = 'utf8'
80-
}
8158

82-
if (!result.value) {
83-
result.value = iconv.decode(buffer, result.encode)
59+
encode.push(item.value)
60+
} catch {
61+
continue
62+
}
8463
}
8564

86-
return result
65+
return { encode: encode[0] || 'utf8', value: new TextDecoder(encode[0] || 'utf8').decode(buffer) }
8766
}
8867

8968
export const isBinaryContent = async (blob: Blob) => {

frontend/src/utils/option.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,16 @@ export const THEME_OPTIONS: { label: string; value: string; dark: boolean }[] =
8686

8787
export const ENCODING_OPTIONS = [
8888
// 推荐优先显示(常用编码)
89-
{ value: 'utf8', label: 'UTF-8' },
89+
{ value: 'utf-8', label: 'UTF-8' },
9090
{ value: 'gbk', label: 'GBK' },
9191
{ value: 'gb2312', label: 'GB2312' },
9292
{ value: 'gb18030', label: 'GB18030' },
9393
{ value: 'big5', label: 'Big5' },
9494

9595
// 国际通用编码
96-
{ value: 'utf16le', label: 'UTF-16 LE' },
97-
{ value: 'utf16be', label: 'UTF-16 BE' },
98-
{ value: 'utf16', label: 'UTF-16' },
96+
{ value: 'utf-16le', label: 'UTF-16 LE' },
97+
{ value: 'utf-16be', label: 'UTF-16 BE' },
98+
{ value: 'utf-16', label: 'UTF-16' },
9999
{ value: 'ascii', label: 'ASCII' },
100100
{ value: 'latin1', label: 'ISO-8859-1' },
101101
{ value: 'windows-1252', label: 'Windows-1252' },

0 commit comments

Comments
 (0)