Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalise empty values in RichEditor #651

Merged
merged 2 commits into from
Dec 17, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions frontend/components/RichEditor/RichEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,19 @@ export default class RichEditor extends Component {
this.linkInputElement.focus()
}

if (value !== text) {
if (this.getNormalisedValue(value) !== text) {
this.handleChange(value, {
needsConversion: true,
shouldPropagate: false
})
}
}

componentWillUnmount() {
this.editorElement.removeEventListener('keypress', this.keyPressHandler)
document.removeEventListener('selectionchange', this.selectionHandler)
}

deserialiseSelection(serialisedSelection) {
let nodes = serialisedSelection.split(',')
let parsedNodes = []
Expand Down Expand Up @@ -378,6 +383,14 @@ export default class RichEditor extends Component {
return this.turndownService.turndown(html)
}

getNormalisedValue(value) {
if (typeof value !== 'string' || value.length === 0) {
return null
}

return value
}

getTextFromHTML(html) {
const {format} = this.props

Expand Down Expand Up @@ -409,19 +422,15 @@ export default class RichEditor extends Component {
text = this.getTextFromHTML(value)
}

// An empty string is not the best representation of an empty field.
// We look for that case and broadcast a `null` instead.
let sanitisedText = text.length > 0 ?
text :
null
let normalisedValue = this.getNormalisedValue(text)

this.setState({
html,
text: sanitisedText
text: normalisedValue
})

if (shouldPropagate && typeof onChange === 'function') {
onChange(sanitisedText)
onChange(normalisedValue)
}
}

Expand Down