Skip to content

Commit

Permalink
Merge pull request #651 from dadi/patch/rich-editor-normalised-values
Browse files Browse the repository at this point in the history
Normalise empty values in RichEditor
  • Loading branch information
eduardoboucas authored Dec 17, 2018
2 parents 11d6029 + 88b36e3 commit 61b5dc3
Showing 1 changed file with 17 additions and 8 deletions.
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

0 comments on commit 61b5dc3

Please sign in to comment.