From 6945fa54eac5c3f9d92a38a956d889bf30c3c15e Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Mon, 24 Jul 2017 14:41:13 +0100 Subject: [PATCH] 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. --- src/components/views/elements/Pill.js | 1 - .../views/rooms/MessageComposerInput.js | 34 ++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/components/views/elements/Pill.js b/src/components/views/elements/Pill.js index 39987228e5..89607f4eb8 100644 --- a/src/components/views/elements/Pill.js +++ b/src/components/views/elements/Pill.js @@ -92,7 +92,6 @@ export default React.createClass({ avatar = ; } } - const classes = classNames({ "mx_UserPill": isUserPill, "mx_RoomPill": isRoomPill, diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index 10414ecbeb..2863b13243 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -731,10 +731,33 @@ export default class MessageComposerInput extends React.Component { 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, - (markdownLink, text, resource, prefix) => { - return prefix === '@' ? text : markdownLink; + (markdownLink, text, resource, prefix, offset) => { + // 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; @@ -900,7 +923,10 @@ export default class MessageComposerInput extends React.Component { let entityKey; let mdCompletion; if (href) { - entityKey = Entity.create('LINK', 'IMMUTABLE', {url: href}); + entityKey = Entity.create('LINK', 'IMMUTABLE', { + url: href, + isCompletion: true, + }); if (!this.state.isRichtextEnabled) { mdCompletion = `[${completion}](${href})`; }