Skip to content

Commit

Permalink
feat: 发音功能设置
Browse files Browse the repository at this point in the history
  • Loading branch information
dianjie committed May 17, 2022
1 parent 1fdb49a commit 7d3c617
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 90 deletions.
16 changes: 14 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "qwerty-learner",
"displayName": "Qwerty Learner",
"description": "为 Coder 设计的单词记忆与英语肌肉记忆锻炼软件 摸🐟版",
"version": "0.1.2",
"version": "0.1.4",
"publisher": "kaiyi",
"icon": "docs/logo.png",
"engines": {
Expand Down Expand Up @@ -95,6 +95,16 @@
"type": "boolean",
"default": false,
"description": "是否开启罚抄模式"
},
"qwerty-learner.voiceType": {
"type": "string",
"enum": [
"us",
"uk",
"close"
],
"default": "us",
"description": "是否开启发音"
}
}
}
Expand All @@ -106,7 +116,8 @@
"vscode:prepublish": "webpack --mode production",
"compile": "webpack --mode development",
"watch": "webpack --mode development --watch",
"test-compile": "tsc -p ./"
"test-compile": "tsc -p ./",
"vsce:publish": "vsce package"
},
"devDependencies": {
"@types/glob": "^7.1.3",
Expand All @@ -125,6 +136,7 @@
},
"dependencies": {
"lodash": "^4.17.21",
"node-loader": "^2.0.0",
"node-wav-player": "^0.2.0",
"ts-loader": "^8.0.17",
"webpack": "^5.21.2",
Expand Down
14 changes: 12 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import cet4 from './assets/CET4_T.json'
import { range } from 'lodash'
import { compareWord, getConfig, dicts, DictPickItem, getDictFile } from './utils'
import { soundPlayer } from './sound'
import { voicePlayer, getVoiceType } from './voice'

export function activate(context: vscode.ExtensionContext) {
const globalState = context.globalState
Expand All @@ -15,7 +16,9 @@ export function activate(context: vscode.ExtensionContext) {
chapter = globalState.get('chapter', 0),
order = prevOrder,
dict = cet4,
dictKey = 'cet4'
dictKey = 'cet4',
voiceType = getVoiceType(),
isLock = Boolean(voiceType)
let wordList = dict.slice(chapter * chapterLength, (chapter + 1) * chapterLength)
let totalChapters = Math.ceil(dict.length / chapterLength)
const wordBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, -100)
Expand Down Expand Up @@ -52,6 +55,13 @@ export function activate(context: vscode.ExtensionContext) {
inputBar.text = ''
transBar.text = phonetic ? `/${phonetic}/ ` : ''
transBar.text += wordList[order].trans.join('; ')
// 加锁防止狂输并错误的情况
if (voiceType) { isLock = true }
setTimeout(() => {
// 会阻塞进程
voicePlayer(wordList[order].name, voiceType)
isLock = false
}, 100)
updateGlobalState()
}

Expand Down Expand Up @@ -89,7 +99,7 @@ export function activate(context: vscode.ExtensionContext) {
});

vscode.workspace.onDidChangeTextDocument((e) => {
if (isStart) {
if (isStart && !isLock) {
const { uri } = e.document
// 避免破坏配置文件
if (uri.scheme.indexOf("vscode") !== -1) { return }
Expand Down
Binary file added src/rodio/index.node
Binary file not shown.
30 changes: 30 additions & 0 deletions src/voice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { playerPlay } = require("node-loader!./rodio/index.node");
import { getConfig } from './utils'

type voiceType = 'us' | 'uk' | 'close'

export const getVoiceType = () => {
const voiceType: voiceType = getConfig('voiceType')
let type
switch (voiceType) {
case 'us':
type = 2
break
case 'uk':
type = 1
break
case 'close':
type = ''
break
default:
type = ''
break
}
return type
}

export const voicePlayer = (word: string, type: string | number) => {
if (type) {
playerPlay(`https://dict.youdao.com/dictvoice?audio=${word}&type=${type}`)
}
}
Loading

0 comments on commit 7d3c617

Please sign in to comment.