-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
133 lines (125 loc) · 3.85 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const { fromEvent } = require('rxjs')
const { debounceTime, distinctUntilChanged } = require('rxjs/operators')
const inputField = document.querySelector('.inputField')
const pinyinBtn = document.querySelector('.btn.pinyin')
const zhuyinBtn = document.querySelector('.btn.zhuyin')
const hanziBtn = document.querySelector('.btn.hanzi')
const output = document.querySelector('.output')
const addClickListeners = () => {
output.querySelectorAll('ul').forEach(ul => {
ul.addEventListener('click', () => {
ul.classList.toggle('active')
})
})
output.querySelectorAll('li').forEach(li => {
li.addEventListener('click', () => {
li.parentNode.querySelector('li.active').classList.remove('active')
li.classList.add('active')
})
})
}
const buildList = items => {
return '<ul>' + items.map((item, i) => {
if (i === 0) {
return '<li class="active">' + item + '</li>'
} else {
return '<li>' + item + '</li>'
}
}).join('') + '</ul>'
}
const convert = async () => {
const text = inputField.value
if (text.length > 0) {
if (pinyinBtn.classList.contains('active') || zhuyinBtn.classList.contains('active')) {
try {
let response = null
if (pinyinBtn.classList.contains('active')) {
response = await fetch('https://pinyin-rest.pepebecker.com/pinyin/' + encodeURIComponent(text))
}
if (zhuyinBtn.classList.contains('active')) {
response = await fetch('https://pinyin-rest.pepebecker.com/zhuyin/' + encodeURIComponent(text))
}
const result = await response.json()
if (Array.isArray(result.data)) {
output.innerHTML = result.data.map(item => {
if (typeof item === 'string') {
return ' ' + item + ' '
} else {
return ' ' + (item.length > 1 ? buildList(item) : item) + ' '
}
}).join('')
} else if (result.text) {
output.innerText = result.text
}
addClickListeners()
} catch (err) {
console.error(err)
}
}
if (hanziBtn.classList.contains('active')) {
try {
const response = await fetch('https://pinyin-rest.pepebecker.com/hanzi/' + encodeURIComponent(text) + '?getIndex=true')
const result = await response.json()
output.innerHTML = result.map(item => {
if (typeof item === 'string') {
return item
} else if (Array.isArray(item)) {
return (item.length > 1 ? buildList(item) : item)
} else {
return item.simplified
}
}).join('')
addClickListeners()
} catch (err) {
console.error(err)
}
}
} else {
if (pinyinBtn.classList.contains('active')) {
output.innerHTML = `<span class="placeholder">
wǒ de māo xǐhuan hē niúnǎi
</span>`
}
if (zhuyinBtn.classList.contains('active')) {
output.innerHTML = `<span class="placeholder">
ㄨㄛˇ ㄉㄜ ㄇㄠ ㄒㄧˇ ㄏㄨㄢ ㄏㄜ ㄋㄧㄡˊ ㄋㄞˇ
</span>`
}
if (hanziBtn.classList.contains('active')) {
output.innerHTML = `<span class="placeholder">
我的猫喜欢喝牛奶
</span>`
}
}
}
const setMode = (mode, shouldConvert = false) => {
if (mode === 'pinyin') {
pinyinBtn.classList.add('active')
zhuyinBtn.classList.remove('active')
hanziBtn.classList.remove('active')
}
if (mode === 'zhuyin') {
pinyinBtn.classList.remove('active')
zhuyinBtn.classList.add('active')
hanziBtn.classList.remove('active')
}
if (mode === 'hanzi') {
pinyinBtn.classList.remove('active')
zhuyinBtn.classList.remove('active')
hanziBtn.classList.add('active')
inputField.placeholder = 'wo3 de mao1 xi3huan he1 niu2nai3'
} else {
inputField.placeholder = '我的猫喜欢喝牛奶'
}
if (shouldConvert) {
convert()
}
}
pinyinBtn.addEventListener('click', () => setMode('pinyin', true))
zhuyinBtn.addEventListener('click', () => setMode('zhuyin', true))
hanziBtn.addEventListener('click', () => setMode('hanzi', true))
fromEvent(inputField, 'input')
.pipe(debounceTime(500))
.pipe(distinctUntilChanged())
.subscribe(convert)
convert()