use <p/> as our root node everywhere and fix blank roundtrip bug

pull/21833/head
Matthew Hodgson 2018-05-20 01:07:25 +01:00
parent c3a6a41e5d
commit d76a2aba9b
1 changed files with 14 additions and 9 deletions

View File

@ -78,8 +78,7 @@ const ENTITY_TYPES = {
AT_ROOM_PILL: 'ATROOMPILL', AT_ROOM_PILL: 'ATROOMPILL',
}; };
// the Slate node type to default to for unstyled text when in RTE mode. // the Slate node type to default to for unstyled text
// (we use 'line' for oneliners however)
const DEFAULT_NODE = 'paragraph'; const DEFAULT_NODE = 'paragraph';
// map HTML elements through to our Slate schema node types // map HTML elements through to our Slate schema node types
@ -259,7 +258,7 @@ export default class MessageComposerInput extends React.Component {
} }
else { else {
// ...or create a new one. // ...or create a new one.
return Plain.deserialize('') return Plain.deserialize('', { defaultBlock: DEFAULT_NODE });
} }
} }
@ -544,7 +543,13 @@ export default class MessageComposerInput extends React.Component {
let editorState = null; let editorState = null;
if (enabled) { if (enabled) {
// for simplicity when roundtripping, we use slate-md-serializer rather than commonmark // for simplicity when roundtripping, we use slate-md-serializer rather than commonmark
editorState = this.md.deserialize(this.plainWithMdPills.serialize(this.state.editorState)); const markdown = this.plainWithMdPills.serialize(this.state.editorState);
if (markdown !== '') {
editorState = this.md.deserialize(markdown);
}
else {
editorState = Plain.deserialize('', { defaultBlock: DEFAULT_NODE });
}
// the alternative would be something like: // the alternative would be something like:
// //
@ -556,7 +561,10 @@ export default class MessageComposerInput extends React.Component {
// let markdown = RichText.stateToMarkdown(this.state.editorState.getCurrentContent()); // let markdown = RichText.stateToMarkdown(this.state.editorState.getCurrentContent());
// value = ContentState.createFromText(markdown); // value = ContentState.createFromText(markdown);
editorState = Plain.deserialize(this.md.serialize(this.state.editorState)); editorState = Plain.deserialize(
this.md.serialize(this.state.editorState),
{ defaultBlock: DEFAULT_NODE }
);
} }
Analytics.setRichtextMode(enabled); Analytics.setRichtextMode(enabled);
@ -937,6 +945,7 @@ export default class MessageComposerInput extends React.Component {
if (contentText === '') return true; if (contentText === '') return true;
if (shouldSendHTML) { if (shouldSendHTML) {
// FIXME: should we strip out the surrounding <p></p>?
contentHTML = this.html.serialize(editorState); // HtmlUtils.processHtmlForSending(); contentHTML = this.html.serialize(editorState); // HtmlUtils.processHtmlForSending();
} }
} else { } else {
@ -1230,10 +1239,6 @@ export default class MessageComposerInput extends React.Component {
const { attributes, children, node, isSelected } = props; const { attributes, children, node, isSelected } = props;
switch (node.type) { switch (node.type) {
case 'line':
// ideally we'd return { children }<br/>, but as this isn't
// a valid react component, we don't have much choice.
return <div {...attributes}>{children}</div>;
case 'paragraph': case 'paragraph':
return <p {...attributes}>{children}</p>; return <p {...attributes}>{children}</p>;
case 'block-quote': case 'block-quote':