From 58fe4b4fa9e67703ca952c2c8ded8d1e82866939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Mon, 17 Dec 2018 10:47:24 +0000 Subject: [PATCH 1/2] fix: normalise empty value in RichEditor --- frontend/components/RichEditor/RichEditor.jsx | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/frontend/components/RichEditor/RichEditor.jsx b/frontend/components/RichEditor/RichEditor.jsx index 4d4a29732..9882da49b 100644 --- a/frontend/components/RichEditor/RichEditor.jsx +++ b/frontend/components/RichEditor/RichEditor.jsx @@ -283,7 +283,7 @@ export default class RichEditor extends Component { this.linkInputElement.focus() } - if (value !== text) { + if (this.getNormalisedValue(value) !== text) { this.handleChange(value, { needsConversion: true, shouldPropagate: false @@ -291,6 +291,11 @@ export default class RichEditor extends Component { } } + componentWillUnmount() { + this.editorElement.removeEventListener('keypress', this.keyPressHandler) + document.removeEventListener('selectionchange', this.selectionHandler) + } + deserialiseSelection(serialisedSelection) { let nodes = serialisedSelection.split(',') let parsedNodes = [] @@ -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 @@ -409,15 +422,9 @@ 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 - this.setState({ html, - text: sanitisedText + text: this.getNormalisedValue(text) }) if (shouldPropagate && typeof onChange === 'function') { From 88b36e390b5591e47492a397c6f0b1a183fcea31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Mon, 17 Dec 2018 11:03:02 +0000 Subject: [PATCH 2/2] fix: use correct variable name --- frontend/components/RichEditor/RichEditor.jsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/frontend/components/RichEditor/RichEditor.jsx b/frontend/components/RichEditor/RichEditor.jsx index 9882da49b..57576f699 100644 --- a/frontend/components/RichEditor/RichEditor.jsx +++ b/frontend/components/RichEditor/RichEditor.jsx @@ -422,13 +422,15 @@ export default class RichEditor extends Component { text = this.getTextFromHTML(value) } + let normalisedValue = this.getNormalisedValue(text) + this.setState({ html, - text: this.getNormalisedValue(text) + text: normalisedValue }) if (shouldPropagate && typeof onChange === 'function') { - onChange(sanitisedText) + onChange(normalisedValue) } }