Pretend that insert_displayname is a completion on that user

This has the advantage of us not reimplementing things and fixing the same bugs twice.
This commit is contained in:
Luke Barnard 2017-07-20 16:46:53 +01:00
parent 4bee9bd8c7
commit 0600c5b049

View file

@ -286,29 +286,19 @@ export default class MessageComposerInput extends React.Component {
editor.focus(); editor.focus();
break; break;
case 'insert_displayname': { case 'insert_displayname': {
const entityKey = Entity.create( // Pretend that we've autocompleted this user because keeping two code
'LINK', 'IMMUTABLE', // paths for inserting a user pill is not fun
{ url: `https://matrix.to/#/${payload.user_id}`},
);
const selection = this.state.editorState.getSelection(); const selection = this.state.editorState.getSelection();
contentState = Modifier.replaceText( const member = this.props.room.getMember(payload.user_id);
contentState, const completion = member ? member.name : payload.user_id;
this.setDisplayedCompletion({
completion,
selection, selection,
`${payload.user_id}`, href: `https://matrix.to/#/${payload.user_id}`,
null, suffix: selection.getStartOffset() === 0 ? ': ' : ' ',
entityKey, });
);
const suffix = selection.getStartOffset() === 0 ? ': ' : ' ';
contentState = Modifier.replaceText(contentState, contentState.getSelectionAfter(), suffix);
let editorState = EditorState.push(this.state.editorState, contentState, 'insert-characters');
editorState = EditorState.forceSelection(editorState, contentState.getSelectionAfter());
this.onEditorContentChanged(editorState);
editor.focus();
} }
break; break;
case 'quote': { case 'quote': {
let {body, formatted_body} = payload.event.getContent(); let {body, formatted_body} = payload.event.getContent();
formatted_body = formatted_body || escape(body); formatted_body = formatted_body || escape(body);
@ -946,7 +936,8 @@ export default class MessageComposerInput extends React.Component {
return false; return false;
} }
const {range = {}, completion = '', href = null, suffix = ''} = displayedCompletion; const {range = null, completion = '', href = null, suffix = ''} = displayedCompletion;
let entityKey; let entityKey;
let mdCompletion; let mdCompletion;
if (href) { if (href) {
@ -956,11 +947,18 @@ export default class MessageComposerInput extends React.Component {
} }
} }
let selection;
if (range) {
selection = RichText.textOffsetsToSelectionState(
range, activeEditorState.getCurrentContent().getBlocksAsArray(),
);
} else {
selection = activeEditorState.getSelection();
}
let contentState = Modifier.replaceText( let contentState = Modifier.replaceText(
activeEditorState.getCurrentContent(), activeEditorState.getCurrentContent(),
RichText.textOffsetsToSelectionState( selection,
range, activeEditorState.getCurrentContent().getBlocksAsArray(),
),
mdCompletion || completion, mdCompletion || completion,
null, null,
entityKey, entityKey,