Reimplement so that only tab-completed mentions are stripped

Instead of blindly stripping all MD mentions, only strip those that were tab-completed. We do this by adding the `isCompleted` flag to the Entity data.
This commit is contained in:
Luke Barnard 2017-07-24 14:41:13 +01:00
parent 397201a74d
commit 6945fa54ea
2 changed files with 30 additions and 5 deletions

View file

@ -92,7 +92,6 @@ export default React.createClass({
avatar = <RoomAvatar room={room} width={16} height={16}/>; avatar = <RoomAvatar room={room} width={16} height={16}/>;
} }
} }
const classes = classNames({ const classes = classNames({
"mx_UserPill": isUserPill, "mx_UserPill": isUserPill,
"mx_RoomPill": isRoomPill, "mx_RoomPill": isRoomPill,

View file

@ -731,10 +731,33 @@ export default class MessageComposerInput extends React.Component {
sendTextFn = this.client.sendEmoteMessage; sendTextFn = this.client.sendEmoteMessage;
} }
// Strip MD user mentions to preserve plaintext mention behaviour // Strip MD user (tab-completed) mentions to preserve plaintext mention behaviour
contentText = contentText.replace(REGEX_MATRIXTO_MARKDOWN_GLOBAL, contentText = contentText.replace(REGEX_MATRIXTO_MARKDOWN_GLOBAL,
(markdownLink, text, resource, prefix) => { (markdownLink, text, resource, prefix, offset) => {
return prefix === '@' ? text : markdownLink; // Calculate the offset relative to the current block that the offset is in
let sum = 0;
const blocks = contentState.getBlocksAsArray();
let block;
for (let i = 0; i < blocks.length; i++) {
block = blocks[i];
sum += block.getLength();
if (sum > offset) {
sum -= block.getLength();
break;
}
}
offset -= sum;
const entityKey = block.getEntityAt(offset);
const entity = entityKey ? Entity.get(entityKey) : null;
if (entity && entity.getData().isCompletion && prefix === '@') {
// This is a completed mention, so do not insert MD link, just text
return text;
} else {
// This is either a MD link that was typed into the composer or another
// type of pill (e.g. room pill)
return markdownLink;
}
}); });
let sendMessagePromise; let sendMessagePromise;
@ -900,7 +923,10 @@ export default class MessageComposerInput extends React.Component {
let entityKey; let entityKey;
let mdCompletion; let mdCompletion;
if (href) { if (href) {
entityKey = Entity.create('LINK', 'IMMUTABLE', {url: href}); entityKey = Entity.create('LINK', 'IMMUTABLE', {
url: href,
isCompletion: true,
});
if (!this.state.isRichtextEnabled) { if (!this.state.isRichtextEnabled) {
mdCompletion = `[${completion}](${href})`; mdCompletion = `[${completion}](${href})`;
} }