Skip to content

Commit 7ef5fe0

Browse files
authored
Merge pull request #4246 from sanju-singh/emptyMsgBreak
Partial fix for #4209: Empty message breaks showing all the messages
2 parents 38102b4 + 8ecf4bb commit 7ef5fe0

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/components/RichTextArea/RichTextArea.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,10 @@ class RichTextArea extends React.Component {
396396
const {className, avatarUrl, authorName, titlePlaceholder, contentPlaceholder, editMode, isCreating,
397397
isGettingComment, disableTitle, disableContent, expandedTitlePlaceholder, editingTopic, hasPrivateSwitch, canUploadAttachment, attachments, textAreaOnly } = this.props
398398
const {editorExpanded, editorState, titleValue, oldMDContent, currentMDContent, uploading, isPrivate, isAddLinkOpen, rawFiles, files} = this.state
399+
const emptyStringRegex = /^[\s\n\u200B]*$/g // empty string with space, new line, zero width space character
400+
399401
let canSubmit = (disableTitle || titleValue.trim())
400-
&& (disableContent || editorState.getCurrentContent().hasText())
402+
&& (disableContent || !emptyStringRegex.test(currentMDContent))
401403
if (editMode && canSubmit) {
402404
canSubmit = (!disableTitle && titleValue !== this.props.oldTitle) || (!disableContent && oldMDContent !== currentMDContent)
403405
|| (rawFiles.length > 0 || (attachments && files.length < attachments.length))

src/helpers/markdownToState.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {convertFromRaw} from 'draft-js'
22
import sanitizeHtml from 'sanitize-html'
3+
import Alert from 'react-s-alert'
34
const Remarkable = require('remarkable')
45

56
// Block level items, key is Remarkable's key for them, value returned is
@@ -257,6 +258,11 @@ function markdownToState(markdown, options = {}) {
257258
const BlockEntities = Object.assign({}, DefaultBlockEntities, options.blockEntities || {})
258259
const BlockStyles = Object.assign({}, DefaultBlockStyles, options.blockStyles || {})
259260

261+
// when there is no content, add empty paragraph
262+
if (parsedData.length === 0) {
263+
blocks.push(getNewBlock(BlockTypes['paragraph_open']()));
264+
}
265+
260266
parsedData.forEach((item) => {
261267

262268
if (item.type === 'bullet_list_open') {
@@ -347,10 +353,26 @@ function markdownToState(markdown, options = {}) {
347353
}, DefaultBlockType)
348354
}
349355

350-
return convertFromRaw({
351-
entityMap,
352-
blocks,
353-
})
356+
let result;
357+
try {
358+
result = convertFromRaw({
359+
entityMap,
360+
blocks,
361+
});
362+
} catch(error) {
363+
// If any error occurs set value to plain text
364+
const plainTextBlock = getNewBlock(BlockTypes['paragraph_open']());
365+
plainTextBlock.text = markdown;
366+
367+
result = convertFromRaw({
368+
entityMap: [],
369+
blocks: [plainTextBlock],
370+
});
371+
372+
Alert.warning('Some message could not be rendered properly, please contact Topcoder Support')
373+
}
374+
375+
return result;
354376
}
355377

356378
export default markdownToState

0 commit comments

Comments
 (0)