Skip to content

Commit

Permalink
Update mention links and their ticks
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan committed Apr 8, 2019
1 parent 4825452 commit 88a7487
Show file tree
Hide file tree
Showing 13 changed files with 265 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Meteor.startup(function() {
message: 'discussion-created',
data(message) {
return {
// channelLink: `<a class="mention-link" data-channel= ${ message.channels[0]._id } title="">${ TAPi18n.__('discussion') }</a>`,
message: `<svg class="rc-icon" aria-hidden="true"><use xlink:href="#icon-discussion"></use></svg> ${ message.msg }`,
};
},
Expand Down
21 changes: 7 additions & 14 deletions app/mentions/client/client.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { Meteor } from 'meteor/meteor';
import { callbacks } from '../../callbacks';
import { settings } from '../../settings';
import Mentions from '../lib/Mentions';
import { MentionsParser } from '../lib/MentionsParser';

const MentionsClient = new Mentions({
pattern() {
return settings.get('UTF8_Names_Validation');
},
useRealName() {
return settings.get('UI_Use_Real_Name');
},
me() {
const me = Meteor.user();
return me && me.username;
},
const instance = new MentionsParser({
pattern: () => settings.get('UTF8_Names_Validation'),
useRealName: () => settings.get('UI_Use_Real_Name'),
me: () => Meteor.userId() && Meteor.user().username,
});

callbacks.add('renderMessage', (message) => MentionsClient.parse(message), callbacks.priority.MEDIUM, 'mentions-message');
callbacks.add('renderMentions', (message) => MentionsClient.parse(message), callbacks.priority.MEDIUM, 'mentions-mentions');
callbacks.add('renderMessage', (message) => instance.parse(message), callbacks.priority.MEDIUM, 'mentions-message');
callbacks.add('renderMentions', (message) => instance.parse(message), callbacks.priority.MEDIUM, 'mentions-mentions');
1 change: 1 addition & 0 deletions app/mentions/client/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import './client';
import './mentionLink.css';
27 changes: 27 additions & 0 deletions app/mentions/client/mentionLink.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.mention-link {
padding: 0 6px 2px;

transition: border-radius 0.3s;

color: var(--mention-link-text-color) !important;

border-radius: var(--mention-link-radius);

background-color: var(--mention-link-background) !important;

font-weight: 700;

&:hover {
border-radius: var(--border-radius);
}

&--me {
color: var(--mention-link-me-text-color) !important;
background-color: var(--mention-link-me-background) !important;
}

&--group {
color: var(--mention-link-group-text-color) !important;
background-color: var(--mention-link-group-background) !important;
}
}
51 changes: 36 additions & 15 deletions app/mentions/lib/Mentions.js → app/mentions/lib/MentionsParser.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,73 @@
/*
* Mentions is a named function that will process Mentions
* @param {Object} message - The message object
*/
import s from 'underscore.string';
export default class {

export class MentionsParser {
constructor({ pattern, useRealName, me }) {
this.pattern = pattern;
this.useRealName = useRealName;
this.me = me;
}

set me(m) {
this._me = m;
}

get me() {
return typeof this._me === 'function' ? this._me() : this._me;
}

set pattern(p) {
this._pattern = p;
}

get pattern() {
return typeof this._pattern === 'function' ? this._pattern() : this._pattern;
}

set useRealName(s) {
this._useRealName = s;
}

get useRealName() {
return typeof this._useRealName === 'function' ? this._useRealName() : this._useRealName;
}

get userMentionRegex() {
return new RegExp(`(^|\\s|<p>|<br> ?)@(${ this.pattern }(@(${ this.pattern }))?)`, 'gm');
}

get channelMentionRegex() {
return new RegExp(`(^|\\s|<p>)#(${ this.pattern }(@(${ this.pattern }))?)`, 'gm');
}
replaceUsers(str, message, me) {
return str.replace(this.userMentionRegex, (match, prefix, username) => {
if (['all', 'here'].includes(username)) {
return `${ prefix }<a class="mention-link mention-link-me mention-link-all">@${ username }</a>`;

replaceUsers(msg, { mentions = [], temp }, me) {
return msg.replace(this.userMentionRegex, (match, prefix, mention) => {
const isGroupMention = ['all', 'here'].includes(mention);
const className = [
'mention-link',
'mention-link--user',
mention === 'all' && 'mention-link--all',
mention === 'here' && 'mention-link--here',
mention === me && 'mention-link--me',
isGroupMention && 'mention-link--group',
].filter(Boolean).join(' ');

if (isGroupMention) {
return `${ prefix }<a class="${ className }">${ mention }</a>`;
}

const mentionObj = message.mentions && message.mentions.find((m) => m.username === username);
const label = (mentions || [])
.filter(({ username }) => username === mention)
.map(({ name, username }) => (this.useRealName ? name : username))
.map((label) => s.escapeHTML(label))[0];

if (message.temp == null && mentionObj == null) {
if (!temp && !label) {
return match;
}

const name = this.useRealName && mentionObj && s.escapeHTML(mentionObj.name);

return `${ prefix }<a class="mention-link ${ username === me ? 'mention-link-me' : '' }" data-username="${ username }" title="${ name ? username : '' }">${ name || `@${ username }` }</a>`;
return `${ prefix }<a class="${ className }" data-username="${ mention }" title="${ this.useRealName ? mention : label }">${ label }</a>`;
});
}

replaceChannels(str, message) {
// since apostrophe escaped contains # we need to unescape it
return str.replace(/&#39;/g, '\'').replace(this.channelMentionRegex, (match, prefix, name) => {
Expand All @@ -59,15 +77,18 @@ export default class {

const channel = message.channels && message.channels.find((c) => c.name === name);
const roomNameorId = channel ? channel._id : name;
return `${ prefix }<a class="mention-link" data-channel="${ roomNameorId }">${ `#${ name }` }</a>`;
return `${ prefix }<a class="mention-link mention-link--room" data-channel="${ roomNameorId }">${ `#${ name }` }</a>`;
});
}

getUserMentions(str) {
return (str.match(this.userMentionRegex) || []).map((match) => match.trim());
}

getChannelMentions(str) {
return (str.match(this.channelMentionRegex) || []).map((match) => match.trim());
}

parse(message) {
let msg = (message && message.html) || '';
if (!msg.trim()) {
Expand Down
4 changes: 2 additions & 2 deletions app/mentions/server/Mentions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* Mentions is a named function that will process Mentions
* @param {Object} message - The message object
*/
import Mentions from '../lib/Mentions';
import { MentionsParser } from '../lib/MentionsParser';

export default class MentionsServer extends Mentions {
export default class MentionsServer extends MentionsParser {
constructor(args) {
super(args);
this.messageMaxAll = args.messageMaxAll;
Expand Down
Loading

0 comments on commit 88a7487

Please sign in to comment.