Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit fe76eb9

Browse files
committed
minor improvements
- use <p> for unstyled blocks - fix return key bug - editor placeholder
1 parent 001011d commit fe76eb9

2 files changed

Lines changed: 31 additions & 40 deletions

File tree

src/RichText.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import {Editor, ContentState, convertFromHTML, DefaultDraftBlockRenderMap, DefaultDraftInlineStyle} from 'draft-js';
22
const ReactDOM = require('react-dom');
33

4+
const BLOCK_RENDER_MAP = DefaultDraftBlockRenderMap.set('unstyled', {
5+
element: 'p' // draft uses <div> by default which we don't really like
6+
});
7+
48
const styles = {
59
BOLD: 'strong',
610
CODE: 'code',
@@ -11,24 +15,18 @@ const styles = {
1115

1216
export function contentStateToHTML(contentState:ContentState): String {
1317
const elem = contentState.getBlockMap().map((block) => {
14-
const elem = DefaultDraftBlockRenderMap.get(block.getType()).element;
18+
const elem = BLOCK_RENDER_MAP.get(block.getType()).element;
1519
const content = [];
1620
block.findStyleRanges(() => true, (s, e) => {
17-
console.log(block.getInlineStyleAt(s));
1821
const tags = block.getInlineStyleAt(s).map(style => styles[style]);
1922
const open = tags.map(tag => `<${tag}>`).join('');
2023
const close = tags.map(tag => `</${tag}>`).reverse().join('');
2124
content.push(`${open}${block.getText().substring(s, e)}${close}`);
2225
});
2326

24-
return (`
25-
<${elem}>
26-
${content.join('')}
27-
</${elem}>
28-
`);
27+
return (`<${elem}>${content.join('')}</${elem}>`);
2928
}).join('');
3029

31-
3230
return elem;
3331
}
3432

src/components/views/rooms/MessageComposerInput.js

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ marked.setOptions({
2828
});
2929

3030
import {Editor, EditorState, RichUtils} from 'draft-js';
31-
import {stateToHTML} from 'draft-js-export-html';
3231

3332
var MatrixClientPeg = require("../../../MatrixClientPeg");
3433
var SlashCommands = require("../../../SlashCommands");
@@ -39,29 +38,16 @@ var sdk = require('../../../index');
3938
var dis = require("../../../dispatcher");
4039
var KeyCode = require("../../../KeyCode");
4140

42-
import {contentStateToHTML} from '../../../RichText';
41+
import {contentStateToHTML, HTMLtoContentState} from '../../../RichText';
4342

4443
var TYPING_USER_TIMEOUT = 10000;
4544
var TYPING_SERVER_TIMEOUT = 30000;
4645
var MARKDOWN_ENABLED = true;
4746

48-
function mdownToHtml(mdown) {
49-
var html = marked(mdown) || "";
50-
html = html.trim();
51-
// strip start and end <p> tags else you get 'orrible spacing
52-
if (html.indexOf("<p>") === 0) {
53-
html = html.substring("<p>".length);
54-
}
55-
if (html.lastIndexOf("</p>") === (html.length - "</p>".length)) {
56-
html = html.substring(0, html.length - "</p>".length);
57-
}
58-
return html;
59-
}
60-
6147
/*
6248
* The textInput part of the MessageComposer
6349
*/
64-
module.exports = class extends React.Component {
50+
export default class MessageComposerInput extends React.Component {
6551
constructor(props, context) {
6652
super(props, context);
6753
this.onAction = this.onAction.bind(this);
@@ -75,7 +61,6 @@ module.exports = class extends React.Component {
7561
componentWillMount() {
7662
this.oldScrollHeight = 0;
7763
this.markdownEnabled = MARKDOWN_ENABLED;
78-
var self = this;
7964
this.sentHistory = {
8065
// The list of typed messages. Index 0 is more recent
8166
data: [],
@@ -150,34 +135,36 @@ module.exports = class extends React.Component {
150135
return true;
151136
},
152137

153-
saveLastTextEntry: function() {
138+
saveLastTextEntry: () => {
154139
// save the currently entered text in order to restore it later.
155140
// NB: This isn't 'originalText' because we want to restore
156141
// sent history items too!
157142
console.error('fixme');
158-
// window.sessionStorage.setItem("input_" + this.roomId, text);
143+
const contentHTML = contentStateToHTML(this.state.editorState.getCurrentContent());
144+
window.sessionStorage.setItem("input_" + this.roomId, contentHTML);
159145
},
160146

161-
setLastTextEntry: function() {
147+
setLastTextEntry: () => {
162148
console.error('fixme');
163-
// var text = window.sessionStorage.getItem("input_" + this.roomId);
164-
// if (text) {
165-
// this.element.value = text;
166-
// self.resizeInput();
167-
// }
149+
const contentHTML = window.sessionStorage.getItem("input_" + this.roomId);
150+
if (contentHTML) {
151+
const content = HTMLtoContentState(contentHTML);
152+
this.state.editorState = EditorState.createWithContent(content);
153+
self.resizeInput();
154+
}
168155
}
169156
};
170157
}
171158

172159
componentDidMount() {
173160
this.dispatcherRef = dis.register(this.onAction);
174161
this.sentHistory.init(
175-
this.refs.textarea,
162+
this.refs.editor,
176163
this.props.room.roomId
177164
);
178165
this.resizeInput();
179166
if (this.props.tabComplete) {
180-
this.props.tabComplete.setTextArea(this.refs.textarea);
167+
this.props.tabComplete.setTextArea(this.refs.editor);
181168
}
182169
}
183170

@@ -456,7 +443,7 @@ module.exports = class extends React.Component {
456443

457444
const contentState = this.state.editorState.getCurrentContent();
458445
if(!contentState.hasText())
459-
return false;
446+
return true;
460447

461448
const contentText = contentState.getPlainText(),
462449
contentHTML = contentStateToHTML(contentState);
@@ -471,9 +458,16 @@ module.exports = class extends React.Component {
471458
}
472459

473460
render() {
461+
const containerStyle = {
462+
overflow: 'auto'
463+
};
464+
474465
return (
475-
<div className="mx_MessageComposer_input" onClick={ this.onInputClick } style={{overflow: 'auto'}}>
466+
<div className="mx_MessageComposer_input"
467+
onClick={ this.onInputClick }
468+
style={containerStyle}>
476469
<Editor ref="editor"
470+
placeholder="Type a message…"
477471
editorState={this.state.editorState}
478472
onChange={(state) => this.onChange(state)}
479473
handleKeyCommand={(command) => this.handleKeyCommand(command)}
@@ -483,7 +477,7 @@ module.exports = class extends React.Component {
483477
}
484478
};
485479

486-
module.exports.propTypes = {
480+
MessageComposerInput.propTypes = {
487481
tabComplete: React.PropTypes.any,
488482

489483
// a callback which is called when the height of the composer is
@@ -495,5 +489,4 @@ module.exports.propTypes = {
495489
};
496490

497491
// the height we limit the composer to
498-
module.exports.MAX_HEIGHT = 100;
499-
492+
MessageComposerInput.MAX_HEIGHT = 100;

0 commit comments

Comments
 (0)