Skip to content

Commit

Permalink
feat(panel): add wordEditor deleteCards
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed Aug 26, 2018
1 parent 750a745 commit 47f8f1b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/_locales/content/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@
"zh_TW": "關閉字典視窗",
"en": "Close the panel"
},

"fromSaladict": {
"zh_CN": "来自沙拉查词面板",
"zh_TW": "来自沙拉查詞介面",
"en": "From Saladict Panel"
},

"wordEditorDeleteConfirm": {
"zh_CN": "从单词本中移除?",
"zh_TW": "于單字本中移除?",
"en": "Delete from Notebook?"
},
"wordEditorTitle": {
"zh_CN": "保存到生词本",
"zh_TW": "保存到單字本",
Expand Down
13 changes: 13 additions & 0 deletions src/content/components/WordCards/_style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

.wordCards-Card {
list-style-type: none;
position: relative;
display: block;
overflow: hidden;
margin: 0 0 10px 0;
Expand All @@ -35,6 +36,18 @@
background: #fcf8e3;
}

.wordCards-CardClose {
position: absolute;
top: 5px;
right: 5px;
border: none;
font-size: 20px;
font-weight: bold;
color: #8a6d3b;
background: transparent;
cursor: pointer;
}

.wordCards-CardTitle {
margin: 0 0 0.5em;
text-align: center;
Expand Down
5 changes: 4 additions & 1 deletion src/content/components/WordCards/index.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import React from 'react'
import { translate } from 'react-i18next'
import { TranslationFunction } from 'i18next'
import { Word } from '@/background/database'
import { Word } from '@/_helpers/record-manager'

export interface WordCardsProps {
words: Word[]
deleteCard: (word: Word) => any
}

export class WordCards extends React.PureComponent<WordCardsProps & { t: TranslationFunction }> {
render () {
const {
t,
words,
deleteCard,
} = this.props

return (
Expand All @@ -24,6 +26,7 @@ export class WordCards extends React.PureComponent<WordCardsProps & { t: Transla
<li className='wordCards-Card'
key={word.date}
>
<button type='button' className='wordCards-CardClose' onClick={() => deleteCard(word)}>&times;</button>
<h2 className='wordCards-CardTitle'>{word.text}</h2>
{word.trans &&
<div className='wordCards-CardItem'>
Expand Down
15 changes: 10 additions & 5 deletions src/content/components/WordEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import { translate } from 'react-i18next'
import { TranslationFunction } from 'i18next'
import { SelectionInfo } from '@/_helpers/selection'
import { Word } from '@/background/database'
import { Word, deleteWords } from '@/_helpers/record-manager'
import WordCards from '../WordCards'

export interface WordEditorDispatchers {
Expand Down Expand Up @@ -75,12 +75,17 @@ export class WordEditor extends React.PureComponent<WordEditorProps & { t: Trans
if (word.date) {
words = words.filter(({ date }) => date !== word.date)
}
if (words.length > 0) {
this.setState({ relatedWords: words })
}
this.setState({ relatedWords: words })
})
}

deleteCard = (word: Word) => {
if (window.confirm(this.props.t('wordEditorDeleteConfirm'))) {
deleteWords('notebook', [word.date])
.then(this.getRelatedWords)
}
}

componentDidMount () {
this.getRelatedWords()
}
Expand Down Expand Up @@ -170,7 +175,7 @@ export class WordEditor extends React.PureComponent<WordEditorProps & { t: Trans
onChange={this.mapValueToState}
/>
</form>
{relatedWords.length > 0 && <WordCards words={relatedWords} /> }
{relatedWords.length > 0 && <WordCards words={relatedWords} deleteCard={this.deleteCard} /> }
</div>
<footer className='wordEditor-Footer'>
<button type='button'
Expand Down

0 comments on commit 47f8f1b

Please sign in to comment.