Skip to content

Commit

Permalink
fix: check empty word fields
Browse files Browse the repository at this point in the history
Closes #363
  • Loading branch information
crimx committed May 11, 2019
1 parent 4ad4dcf commit 60f8066
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
24 changes: 24 additions & 0 deletions src/_helpers/record-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,30 @@ export interface Word {

export type Area = 'notebook' | 'history'

export function newWord (word?: Partial<Word>): Word {
return word
? {
date: word.date || Date.now(),
text: word.text || '',
context: word.context || '',
title: word.title || '',
url: word.url || '',
favicon: word.favicon || '',
trans: word.trans || '',
note: word.note || '',
}
: {
date: Date.now(),
text: '',
context: '',
title: '',
url: '',
favicon: '',
trans: '',
note: '',
}
}

export function isInNotebook (info: SelectionInfo): Promise<boolean> {
return message.send<MsgIsInNotebook>({ type: MsgType.IsInNotebook, info })
.catch(logError(false))
Expand Down
3 changes: 2 additions & 1 deletion src/components/WordPage/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,8 @@ export class WordPageMain extends React.Component<WordPageMainInnerProps, WordPa
</Button>
}

renderText = (text: string): React.ReactNode => {
renderText = (text?: string): React.ReactNode => {
if (!text) { return '' }
return text.split('\n').map((line, i) => (
<div key={i}>{line}</div>
))
Expand Down
12 changes: 8 additions & 4 deletions src/components/WordPage/ExportModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import { translate, TranslationFunction } from 'react-i18next'
import { Layout, Modal, Table } from 'antd'
import { ColumnProps } from 'antd/lib/table/interface'
import { Word } from '@/_helpers/record-manager'
import { Word, newWord } from '@/_helpers/record-manager'
import { storage } from '@/_helpers/browser-api'

const { Content, Sider } = Layout
Expand Down Expand Up @@ -120,13 +120,17 @@ export class ExportModalBody extends React.Component<ExportModalInnerProps, Expo
}

processWords = (template: string): string => {
const keys = Object.keys(newWord())
return this.props.rawWords
.map(word =>
template.replace(/%(\S+)%/g, (match, k) => {
if (k === 'date') {
return new Date(word.date).toLocaleDateString(this.props.locale)
if (keys.includes(k)) {
if (k === 'date') {
return new Date(word.date).toLocaleDateString(this.props.locale)
}
return word[k] || ''
}
return typeof word[k] === 'string' ? word[k] : match
return match
})
)
.join('\n')
Expand Down

0 comments on commit 60f8066

Please sign in to comment.