Skip to content

Commit

Permalink
feat(dicts): add youdao
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed May 24, 2018
1 parent 73aaffa commit 57ec2b3
Show file tree
Hide file tree
Showing 11 changed files with 2,943 additions and 33 deletions.
27 changes: 27 additions & 0 deletions config/fake-env/fake-ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,33 @@ const fakeFetchData = [
text: () => require('raw-loader!../../test/specs/components/dictionaries/longman/response/jumblish.html')
},
},
{
test: {
method: /.*/,
url: /youdao\.com.*jumblish$/,
},
response: {
text: () => require('raw-loader!../../test/specs/components/dictionaries/youdao/response/jumblish.html')
},
},
{
test: {
method: /.*/,
url: /youdao\.com.*love$/,
},
response: {
text: () => require('raw-loader!../../test/specs/components/dictionaries/youdao/response/love.html')
},
},
{
test: {
method: /.*/,
url: /youdao\.com.*translation$/,
},
response: {
text: () => require('raw-loader!../../test/specs/components/dictionaries/youdao/response/translation.html')
},
},
]

/*-----------------------------------------------*\
Expand Down
103 changes: 70 additions & 33 deletions src/app-config/dicts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ export function getALlDicts () {
sentence: 4
}
},
eudic: {
cobuild: {
/**
* Full content page to jump to when user clicks the title.
* %s will be replaced with the current word.
* %z will be replaced with the traditional Chinese version of the current word.
* %z will be replaced with the traditional Chinese version of the current word
*/
page: 'https://dict.eudic.net/dicts/en/%s',
page: 'https://www.collinsdictionary.com/dictionary/english/%s',
/**
* If set to true, the dict start searching automatically.
* Otherwise it'll only start seaching when user clicks the unfold button.
Expand All @@ -58,7 +58,7 @@ export function getALlDicts () {
* the preferred height is used and a mask with a view-more button is shown.
* Otherwise the content height is used.
*/
preferredHeight: 240,
preferredHeight: 300,
/**
* Only start searching if the selection contains the language.
* Better set default to true and let user decide.
Expand All @@ -69,16 +69,16 @@ export function getALlDicts () {
},
/** Optional dict custom options. Can only be boolean or number. */
options: {
resultnum: 10
sentence: 4
}
},
longman: {
etymonline: {
/**
* Full content page to jump to when user clicks the title.
* %s will be replaced with the current word.
* %z will be replaced with the traditional Chinese version of the current word
*/
page: 'http://www.ldoceonline.com/search/?q=%s',
page: 'http://www.etymonline.com/search?q=%s',
/**
* If set to true, the dict start searching automatically.
* Otherwise it'll only start seaching when user clicks the unfold button.
Expand All @@ -100,23 +100,18 @@ export function getALlDicts () {
eng: true,
chs: true
},
/** Optional dict custom options. Can only be boolean or number. */
options: {
wordfams: false,
collocations: true,
grammar: true,
thesaurus: true,
examples: true,
bussinessFirst: true,
related: true,
resultnum: 2
}
},
cobuild: {
eudic: {
/**
* Full content page to jump to when user clicks the title.
* %s will be replaced with the current word.
* %z will be replaced with the traditional Chinese version of the current word
* %z will be replaced with the traditional Chinese version of the current word.
*/
page: 'https://www.collinsdictionary.com/dictionary/english/%s',
page: 'https://dict.eudic.net/dicts/en/%s',
/**
* If set to true, the dict start searching automatically.
* Otherwise it'll only start seaching when user clicks the unfold button.
Expand All @@ -129,7 +124,7 @@ export function getALlDicts () {
* the preferred height is used and a mask with a view-more button is shown.
* Otherwise the content height is used.
*/
preferredHeight: 300,
preferredHeight: 240,
/**
* Only start searching if the selection contains the language.
* Better set default to true and let user decide.
Expand All @@ -140,16 +135,16 @@ export function getALlDicts () {
},
/** Optional dict custom options. Can only be boolean or number. */
options: {
sentence: 4
resultnum: 10
}
},
etymonline: {
google: {
/**
* Full content page to jump to when user clicks the title.
* %s will be replaced with the current word.
* %z will be replaced with the traditional Chinese version of the current word
*/
page: 'http://www.etymonline.com/search?q=%s',
page: 'https://translate.google.com/#auto/zh-CN/%s',
/**
* If set to true, the dict start searching automatically.
* Otherwise it'll only start seaching when user clicks the unfold button.
Expand All @@ -162,27 +157,23 @@ export function getALlDicts () {
* the preferred height is used and a mask with a view-more button is shown.
* Otherwise the content height is used.
*/
preferredHeight: 265,
preferredHeight: 110,
/**
* Only start searching if the selection contains the language.
* Better set default to true and let user decide.
*/
selectionLang: {
eng: true,
chs: true
},
/** Optional dict custom options. Can only be boolean or number. */
options: {
resultnum: 2
}
},
google: {
guoyu: {
/**
* Full content page to jump to when user clicks the title.
* %s will be replaced with the current word.
* %z will be replaced with the traditional Chinese version of the current word
*/
page: 'https://translate.google.com/#auto/zh-CN/%s',
page: 'https://www.moedict.tw/%z',
/**
* If set to true, the dict start searching automatically.
* Otherwise it'll only start seaching when user clicks the unfold button.
Expand All @@ -195,7 +186,7 @@ export function getALlDicts () {
* the preferred height is used and a mask with a view-more button is shown.
* Otherwise the content height is used.
*/
preferredHeight: 110,
preferredHeight: 265,
/**
* Only start searching if the selection contains the language.
* Better set default to true and let user decide.
Expand All @@ -205,13 +196,13 @@ export function getALlDicts () {
chs: true
}
},
guoyu: {
liangan: {
/**
* Full content page to jump to when user clicks the title.
* %s will be replaced with the current word.
* %z will be replaced with the traditional Chinese version of the current word
*/
page: 'https://www.moedict.tw/%z',
page: 'https://www.moedict.tw/~%z',
/**
* If set to true, the dict start searching automatically.
* Otherwise it'll only start seaching when user clicks the unfold button.
Expand All @@ -234,13 +225,13 @@ export function getALlDicts () {
chs: true
}
},
liangan: {
longman: {
/**
* Full content page to jump to when user clicks the title.
* %s will be replaced with the current word.
* %z will be replaced with the traditional Chinese version of the current word
*/
page: 'https://www.moedict.tw/~%z',
page: 'http://www.ldoceonline.com/search/?q=%s',
/**
* If set to true, the dict start searching automatically.
* Otherwise it'll only start seaching when user clicks the unfold button.
Expand All @@ -261,6 +252,15 @@ export function getALlDicts () {
selectionLang: {
eng: true,
chs: true
},
options: {
wordfams: false,
collocations: true,
grammar: true,
thesaurus: true,
examples: true,
bussinessFirst: true,
related: true,
}
},
macmillan: {
Expand Down Expand Up @@ -357,6 +357,43 @@ export function getALlDicts () {
chs: true
}
},
youdao: {
/**
* Full content page to jump to when user clicks the title.
* %s will be replaced with the current word.
* %z will be replaced with the traditional Chinese version of the current word
*/
page: 'http://www.youdao.com/w/eng/%s',
/**
* If set to true, the dict start searching automatically.
* Otherwise it'll only start seaching when user clicks the unfold button.
* Default MUST be true and let user decide.
*/
defaultUnfold: true,
/**
* This is the default height when the dict first renders the result.
* If the content height is greater than the preferred height,
* the preferred height is used and a mask with a view-more button is shown.
* Otherwise the content height is used.
*/
preferredHeight: 265,
/**
* Only start searching if the selection contains the language.
* Better set default to true and let user decide.
*/
selectionLang: {
eng: true,
chs: true
},
options: {
basic: true,
collins: true,
discrimination: true,
sentence: true,
translation: true,
related: true,
}
},
zdic: {
/**
* Full content page to jump to when user clicks the title.
Expand Down
75 changes: 75 additions & 0 deletions src/components/dictionaries/youdao/View.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import React from 'react'
import Speaker from '@/components/Speaker'
import StarRates from '@/components/StarRates'
import { YoudaoResult, YoudaoResultLex, YoudaoResultRelated } from './engine'

export default class DictYoudao extends React.PureComponent<{ result: YoudaoResult }> {
renderLex (result: YoudaoResultLex) {
return (
<>
{result.title &&
<div className='dictYoudao-HeaderContainer'>
<h1 className='dictYoudao-Title'>{result.title}</h1>
<span className='dictYoudao-Pattern'>{result.pattern}</span>
</div>
}
{(result.stars > 0 || result.prons.length > 0) &&
<div className='dictYoudao-HeaderContainer'>
{result.stars > 0 && <StarRates className='dictYoudao-Stars' rate={result.stars} />}
{result.prons.map(({ phsym, url }) => (
<React.Fragment key={url}>
{phsym} <Speaker src={url} />
</React.Fragment>
))}
<span className='dictYoudao-Rank'>{result.rank}</span>
</div>
}
{result.basic &&
<div className='dictYoudao-Basic' dangerouslySetInnerHTML={{ __html: result.basic }} />
}
{result.collins &&
<div>
<h1 className='dictYoudao-SecTitle'>《柯林斯英汉双解》</h1>
<ul className='dictYoudao-Collins' dangerouslySetInnerHTML={{ __html: result.collins }} />
</div>
}
{result.discrimination &&
<div className='dictYoudao-Discrimination'>
<h1 className='dictYoudao-Discrimination_Title'>词义辨析</h1>
<div dangerouslySetInnerHTML={{ __html: result.discrimination }} />
</div>
}
{result.sentence &&
<div>
<h1 className='dictYoudao-SecTitle'>- 权威例句 -</h1>
<ol className='dictYoudao-Sentence' dangerouslySetInnerHTML={{ __html: result.sentence }} />
</div>
}
{result.translation &&
<div>
<h1 className='dictYoudao-SecTitle'>- 机器翻译 -</h1>
<div className='dictYoudao-Translation' dangerouslySetInnerHTML={{ __html: result.translation }} />
</div>
}
</>
)
}

renderRelated (result: YoudaoResultRelated) {
return (
<div className='dictYoudao-Related' dangerouslySetInnerHTML={{ __html: result.list }} />
)
}

render () {
const { result } = this.props
switch (result.type) {
case 'lex':
return this.renderLex(result)
case 'related':
return this.renderRelated(result)
default:
return null
}
}
}
39 changes: 39 additions & 0 deletions src/components/dictionaries/youdao/_locales.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": {
"en": "Youdao Dictionary & Translation",
"zh_CN": "有道词典(翻译)",
"zh_TW": "有道詞典(翻譯)"
},
"options": {
"basic": {
"en": "Show basic meaning",
"zh_CN": "显示简单释义",
"zh_TW": "顯示簡單解釋"
},
"collins": {
"en": "Show Collins result",
"zh_CN": "显示柯林斯双解",
"zh_TW": "顯示柯林斯雙解"
},
"discrimination": {
"en": "Show discrimination",
"zh_CN": "显示词语辨析",
"zh_TW": "顯示詞語辨析"
},
"sentence": {
"en": "Show sentences",
"zh_CN": "显示权威例句",
"zh_TW": "顯示權威例句"
},
"translation": {
"en": "Show translation",
"zh_CN": "显示有道翻译",
"zh_TW": "顯示有道翻譯"
},
"related": {
"en": "Show related results",
"zh_CN": "失败时显示备选",
"zh_TW": "失敗時顯示備選"
}
}
}
Loading

0 comments on commit 57ec2b3

Please sign in to comment.