wb-table/js/index.js

223 lines
5.4 KiB
JavaScript
Raw Normal View History

2022-03-11 12:13:08 +08:00
/**
*
* @author yutent<yutent.io@gmail.com>
* @date 2022/03/11 12:03:59
*/
import '//unpkg.yutent.top/anot/dist/anot.js'
2022-03-11 15:47:27 +08:00
import '//unpkg.yutent.top/@bytedo/wcui/dist/layer/index.js'
2022-03-11 12:13:08 +08:00
import '//unpkg.yutent.top/@bytedo/wcui/dist/form/input.js'
import '//unpkg.yutent.top/@bytedo/wcui/dist/form/button.js'
2022-03-14 00:16:51 +08:00
import '//unpkg.yutent.top/@bytedo/wcui/dist/form/link.js'
2022-03-11 12:13:08 +08:00
import '//unpkg.yutent.top/@bytedo/wcui/dist/form/radio.js'
2022-03-14 00:16:51 +08:00
import '//unpkg.yutent.top/@bytedo/wcui/dist/form/checkbox.js'
2022-03-11 12:13:08 +08:00
import '//unpkg.yutent.top/@bytedo/wcui/dist/form/switch.js'
import fetch from '//unpkg.yutent.top/@bytedo/fetch/dist/index.js'
2022-03-11 15:47:27 +08:00
import { Enum } from './lib/core.js'
2022-03-11 18:58:43 +08:00
import FIXED_18030 from './lib/18030.js'
2022-03-11 15:47:27 +08:00
2022-03-11 18:58:43 +08:00
const WB_CODE_NAME = { 1: '一级简码', 2: '二级简码', 3: '三级简码', 4: '四级简码' }
2022-03-11 15:47:27 +08:00
const WB_TABLE = new Enum()
2022-03-11 18:58:43 +08:00
const WB_TABLE_18030 = new Enum(FIXED_18030)
2022-03-13 01:23:31 +08:00
const WB_WORDS = new Enum()
const WB_DY = new Enum()
2022-03-11 12:13:08 +08:00
Anot({
$id: 'app',
state: {
2022-03-11 15:47:27 +08:00
single: 0,
words: 0,
2022-03-13 01:23:31 +08:00
dy: 0,
2022-03-11 15:47:27 +08:00
result: '',
filter: {
2022-03-11 18:58:43 +08:00
text: '',
2022-03-11 15:56:24 +08:00
table: '86'
2022-03-14 00:16:51 +08:00
},
dlOpt: {
2022-03-14 19:03:17 +08:00
pos: 'front',
reverse: true,
pinyin: true,
2022-03-14 00:16:51 +08:00
tables: ['table', 'words', 'dy']
2022-03-14 19:03:17 +08:00
},
preview: ''
2022-03-11 12:13:08 +08:00
},
mounted() {
2022-03-14 00:16:51 +08:00
Promise.all([
fetch('./data/table.txt').then(r => r.text()),
fetch('./data/words.txt').then(r => r.text()),
fetch('./data/dy.txt').then(r => r.text())
]).then(([table, words, dy]) => {
//
table.split('\n').forEach(it => {
it = it.split(' ')
let k = it.shift()
2022-03-13 01:23:31 +08:00
2022-03-14 00:16:51 +08:00
if (k) {
WB_TABLE.add(k, it)
}
})
2022-03-13 01:23:31 +08:00
2022-03-14 00:16:51 +08:00
words.split('\n').forEach(it => {
it = it.split(' ')
2022-03-13 01:23:31 +08:00
2022-03-14 00:16:51 +08:00
let k = it.shift()
2022-03-13 01:23:31 +08:00
2022-03-14 00:16:51 +08:00
if (k) {
WB_WORDS.add(k, it)
}
2022-03-13 01:23:31 +08:00
})
2022-03-14 00:16:51 +08:00
dy.split('\n').forEach(it => {
it = it.split(' ')
let k = it.shift()
if (k) {
WB_DY.add(k, it)
}
2022-03-13 01:23:31 +08:00
})
2022-03-14 00:16:51 +08:00
this.single = WB_TABLE.length
this.words = WB_WORDS.length
this.dy = WB_DY.length
})
2022-03-11 15:47:27 +08:00
},
methods: {
search() {
2022-03-11 18:58:43 +08:00
var { text, table } = this.filter
2022-03-11 15:56:24 +08:00
var reverse = false
2022-03-11 18:58:43 +08:00
var res, res18030
2022-03-11 15:47:27 +08:00
2022-03-11 18:58:43 +08:00
text = text.trim().toLowerCase()
2022-03-11 15:47:27 +08:00
2022-03-11 18:58:43 +08:00
if (!text) {
2022-03-14 19:03:17 +08:00
this.result = ''
2022-03-11 18:58:43 +08:00
return
}
reverse = /^[a-z]{1,4}$/.test(text)
2022-03-11 15:47:27 +08:00
2022-03-11 16:23:29 +08:00
if (!reverse) {
2022-03-11 18:58:43 +08:00
text = text.replace(/[\sa-z]/g, '')
2022-03-11 16:23:29 +08:00
}
2022-03-11 18:58:43 +08:00
if (reverse || text.length === 1) {
res = [WB_TABLE.get(text)]
if (table === '18030') {
res18030 = [WB_TABLE_18030.get(text)]
}
2022-03-11 16:48:07 +08:00
} else {
2022-03-11 18:58:43 +08:00
res = text.split('').map(t => WB_TABLE.get(t))
if (table === '18030') {
res18030 = text.split('').map(t => WB_TABLE_18030.get(t))
}
2022-03-11 16:23:29 +08:00
}
2022-03-11 15:47:27 +08:00
2022-03-11 16:23:29 +08:00
if (reverse) {
2022-03-11 18:58:43 +08:00
text = text.toUpperCase()
// 反查时, 直接替换结果
2022-03-11 19:09:06 +08:00
if (res18030 && res18030[0]) {
2022-03-11 18:58:43 +08:00
res = res18030
}
2022-03-11 16:23:29 +08:00
if (res[0]) {
2022-03-11 18:58:43 +08:00
res = `${text} 】👉\t${res[0]
.map(
t =>
2022-03-11 19:09:06 +08:00
`${t}(${(res18030 && res18030[0] ? WB_TABLE_18030.get(t) : WB_TABLE.get(t))
2022-03-11 18:58:43 +08:00
.join('、')
.toUpperCase()})`
)
.join('\t\t')}`
2022-03-11 15:56:24 +08:00
} else {
2022-03-11 18:58:43 +08:00
res = `${text} 】👉\t无结果, 请检查你的输入是否正确, 如果确认无误, 可以反馈缺失字库。`
2022-03-11 15:56:24 +08:00
}
2022-03-11 15:47:27 +08:00
} else {
2022-03-11 19:09:06 +08:00
if (res18030) {
2022-03-11 18:58:43 +08:00
res18030.forEach((it, i) => {
if (it) {
res[i] = it
}
})
}
2022-03-11 16:23:29 +08:00
res = res
.map((it, i) => {
if (it) {
2022-03-11 18:58:43 +08:00
return `${text[i]} 】👉\t${it
.map(t => `${WB_CODE_NAME[t.length]}: ${t.toUpperCase()}`)
2022-03-11 16:23:29 +08:00
.join('\t\t')}`
} else {
2022-03-13 01:23:31 +08:00
return `${text[i]} 】👉\t无结果, 请检查你的输入是否正确, 如果确认无误, 可以反馈缺失字库。`
2022-03-11 16:23:29 +08:00
}
})
.join('\n')
2022-03-11 15:47:27 +08:00
}
2022-03-11 16:23:29 +08:00
2022-03-11 18:58:43 +08:00
this.result = `查询结果: \n${res}`
2022-03-14 19:03:17 +08:00
},
fileChange(ev) {
var reader = new FileReader()
var file = ev.target.files[0]
var all = new Set()
var unknow = new Set()
console.log(file)
ev.target.value = ''
reader.onload = () => {
let arr = reader.result
.trim()
.split('\n')
.map(_ => _.trim())
for (let it of arr) {
all.add(it)
if (!WB_TABLE.get(it)) {
unknow.add(it)
}
}
all = Array.from(all)
unknow = Array.from(unknow)
this.preview =
`本次上传, 含有 ${arr.length} 个词条(有效词条 ${all.length} 个)。\n` +
`其中字库中已经存在 ${all.length - unknow.length}个, 未存在词条 ${
unknow.length
} , 如下:\n\n${unknow.join('\t')}`
// Promise.all(
// unknow.map(it =>
// fetch('https://www.qqxiuzi.cn/bianma/wubiShow.php', {
// method: 'post',
// body: { text: it, type: 0, version: 0, token: 'ad362ce31bd5584cf7bbcb13b5b08511' }
// }).then(r => r.text())
// )
// ).then(r => {
// console.log(r)
// })
// navigator.clipboard.writeText(Array.from(all).join('\n'))
}
reader.readAsText(file)
},
openDownloadPanel() {
this.$refs.dl.show()
},
closeDownloadPanel() {
this.$refs.dl.close()
},
download() {
//
2022-03-11 15:47:27 +08:00
}
2022-03-11 12:13:08 +08:00
}
})