Skip to content

Commit

Permalink
feat(dicts): add dict Hjdict
Browse files Browse the repository at this point in the history
Closes crimx#252
  • Loading branch information
crimx committed Dec 4, 2018
1 parent 5375f67 commit 76b8210
Show file tree
Hide file tree
Showing 11 changed files with 6,656 additions and 2 deletions.
12 changes: 12 additions & 0 deletions config/fake-env/fake-ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ const fakeXHRData = [
]

const fakeFetchData = [
{
test: { url: /www\.hjdict\.com.*love$/ },
response: [require('raw-loader!../../test/specs/components/dictionaries/hjdict/response/love.html')],
},
{
test: { url: /www\.hjdict\.com.*henr$/ },
response: [require('raw-loader!../../test/specs/components/dictionaries/hjdict/response/henr.html')],
},
{
test: { url: /www\.hjdict\.com.*$/ },
response: [require('raw-loader!../../test/specs/components/dictionaries/hjdict/response/爱.html')],
},
{
test: { url: /m\.wikipedia\.org\/wiki\// },
response: [require('raw-loader!../../test/specs/components/dictionaries/wikipedia/response/数字.html')],
Expand Down
50 changes: 50 additions & 0 deletions src/app-config/dicts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,56 @@ export function getALlDicts () {
minor: false,
}
},
hjdict: {
/**
* Supported language: en, zh-CN, zh-TW, ja, kor, fr, de, es
* `1` for supported
*/
lang: '10011111',
/**
* 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,
/** Word count to start searching */
selectionWC: {
min: 1,
max: 10,
},
/** Only start searching if the selection contains the language. */
selectionLang: {
eng: true,
chs: true,
minor: true,
},
/**
* Optional dict custom options. Can only be boolean, number or string.
* For string, add additional `options_sel` field to list out choices.
*/
options: {
related: true,
chsas: 'jp/jc' as 'jp/cj' | 'jp/jc' | 'kor' | 'w' | 'fr' | 'de' | 'es',
engas: 'w' as 'w' | 'fr' | 'de' | 'es',
uas: 'fr' as 'fr' | 'de' | 'es',
aas: 'fr' as 'fr' | 'de',
eas: 'fr' as 'fr' | 'es',
},
options_sel: {
chsas: ['jp/cj', 'jp/jc', 'kor', 'w', 'fr', 'de', 'es'],
engas: ['w', 'fr', 'de', 'es'],
uas: ['fr', 'de', 'es'],
aas: ['fr', 'de'],
eas: ['fr', 'es'],
},
},
liangan: {
/**
* Supported language: en, zh-CN, zh-TW, ja, kor, fr, de, es
Expand Down
4 changes: 2 additions & 2 deletions src/components/__fake__/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import setupEnv from './devDict'

setupEnv({
dict: 'longman',
text: 'love', // 当たる 吐く
dict: 'hjdict',
text: 'henr', // 当たる 吐く
})

/*-----------------------------------------------*\
Expand Down
71 changes: 71 additions & 0 deletions src/components/dictionaries/hjdict/View.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import React from 'react'
import { HjdictResult, HjdictResultLex, HjdictResultRelated } from './engine'
import withStaticSpeaker from '@/components/withStaticSpeaker'
import { ViewPorps } from '@/components/dictionaries/helpers'

export default withStaticSpeaker((props: ViewPorps<HjdictResult>) => {
switch (props.result.type) {
case 'lex':
return renderLex(props.result, props.recalcBodyHeight)
case 'related':
return renderRelated(props.result)
}
return null
}, 'dictHjdict-Speaker')

function renderLex (result: HjdictResultLex, recalcBodyHeight: () => void) {
const { header, entries } = result
return (
<div className='dictHjdict-Entry' onClick={e => handleClick(e, recalcBodyHeight)}>
{header && (
<header className='word-details-header' dangerouslySetInnerHTML={{ __html: header }} />
)}
{entries.map((entry, i) => (
<div dangerouslySetInnerHTML={{ __html: entry }} key={i} />
))}
</div>
)
}

function renderRelated (result: HjdictResultRelated) {
return (
<div className='dictHjdict-Entry' dangerouslySetInnerHTML={{ __html: result.content }} />
)
}

function handleClick (e: React.MouseEvent<HTMLElement>, recalcBodyHeight: () => void): void {
const $tab = getWordDetailsTab(e.target)
if ($tab) {
if ($tab.classList.contains('word-details-tab-active')) {
return
}
const doc = $tab.ownerDocument
if (doc) {
const index = +($tab.dataset.categories || '0')

const $panes = doc.querySelectorAll('.word-details-pane')

doc.querySelectorAll('.word-details-tab')
.forEach(($tab, i) => {
if (i === index) {
$tab.classList.add('word-details-tab-active')
$panes[i].classList.add('word-details-pane-active')
} else {
$tab.classList.remove('word-details-tab-active')
$panes[i].classList.remove('word-details-pane-active')
}
})

recalcBodyHeight()
}
}
}

function getWordDetailsTab (target: any): HTMLElement | null {
for (let el = target; el; el = el.parentElement) {
if (el.classList && el.classList.contains('word-details-tab')) {
return el
}
}
return null
}
129 changes: 129 additions & 0 deletions src/components/dictionaries/hjdict/_locales.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
{
"name": {
"en": "Hu Jiang",
"zh_CN": "沪江小D",
"zh_TW": "沪江小D"
},
"options": {
"engas": {
"en": "Treat English as",
"zh_CN": "对全英文使用词典",
"zh_TW": "對全英文使用字典"
},
"chsas": {
"en": "Treat Chinese as",
"zh_CN": "对全中文使用词典",
"zh_TW": "對全中文使用字典"
},
"uas": {
"en": "Treat 'ü' as",
"zh_CN": "将 'ü' 字符当作",
"zh_TW": "把 'ü' 字元當成"
},
"aas": {
"en": "Treat 'ä' as",
"zh_CN": "将 'ä' 字符当作",
"zh_TW": "把 'ä' 字元當成"
},
"eas": {
"en": "Treat 'é' as",
"zh_CN": "将 'é' 字符当作",
"zh_TW": "把 'é' 字元當成"
},
"related": {
"en": "Show related results",
"zh_CN": "失败时显示备选",
"zh_TW": "失敗時顯示備選"
},
"chsas-jp/cj": {
"en": "中 → 日",
"zh_CN": "中 → 日",
"zh_TW": "中 → 日"
},
"chsas-jp/jc": {
"en": "日 → 中",
"zh_CN": "日 → 中",
"zh_TW": "日 → 中"
},
"chsas-kor": {
"en": "Korean",
"zh_CN": "韩文",
"zh_TW": "韓文"
},
"chsas-w": {
"en": "English",
"zh_CN": "英文",
"zh_TW": "英文"
},
"chsas-fr": {
"en": "French",
"zh_CN": "法文",
"zh_TW": "法文"
},
"chsas-de": {
"en": "Deutsch",
"zh_CN": "德文",
"zh_TW": "德文"
},
"chsas-es": {
"en": "Spanish",
"zh_CN": "西班牙文",
"zh_TW": "西班牙文"
},
"engas-w": {
"en": "English",
"zh_CN": "英文",
"zh_TW": "英文"
},
"engas-fr": {
"en": "French",
"zh_CN": "法文",
"zh_TW": "法文"
},
"engas-de": {
"en": "Deutsch",
"zh_CN": "德文",
"zh_TW": "德文"
},
"engas-es": {
"en": "Spanish",
"zh_CN": "西班牙文",
"zh_TW": "西班牙文"
},
"uas-fr": {
"en": "French",
"zh_CN": "法文",
"zh_TW": "法文"
},
"uas-de": {
"en": "Deutsch",
"zh_CN": "德文",
"zh_TW": "德文"
},
"uas-es": {
"en": "Spanish",
"zh_CN": "西班牙文",
"zh_TW": "西班牙文"
},
"aas-fr": {
"en": "French",
"zh_CN": "法文",
"zh_TW": "法文"
},
"aas-de": {
"en": "Deutsch",
"zh_CN": "德文",
"zh_TW": "德文"
},
"eas-fr": {
"en": "French",
"zh_CN": "法文",
"zh_TW": "法文"
},
"eas-es": {
"en": "Spanish",
"zh_CN": "西班牙文",
"zh_TW": "西班牙文"
}
}
}
Loading

0 comments on commit 76b8210

Please sign in to comment.