Skip to content

Commit

Permalink
replace html to emoji, trazyn#29
Browse files Browse the repository at this point in the history
  • Loading branch information
trazyn committed Sep 12, 2017
1 parent 2b5d680 commit 9c1fbda
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/js/stores/contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import session from './session';
import chat from './chat';
import storage from 'utils/storage';
import helper from 'utils/helper';
import { normalize } from 'utils/emoji';

class Contacts {
@observable loading = false;
Expand Down Expand Up @@ -80,7 +81,9 @@ class Contacts {
// Remove all official account and brand account
self.memberList = response.data.MemberList.filter(e => helper.isContact(e) && !helper.isOfficial(e) && !helper.isBrand(e)).concat(me);
self.memberList.map(e => {
e.HeadImgUrl = `${axios.defaults.baseURL}${e.HeadImgUrl.substr(1)}`;
e.MemberList = [];

return self.resolveUser(auth, e);
});

self.loading = false;
Expand Down Expand Up @@ -119,8 +122,14 @@ class Contacts {
}
}

user.NickName = normalize(user.NickName);
user.RemarkName = normalize(user.RemarkName);
user.Signature = normalize(user.Signature);

user.HeadImgUrl = `${axios.defaults.baseURL}${user.HeadImgUrl.substr(1)}`;
user.MemberList.map(e => {
e.NickName = normalize(e.NickName);
e.RemarkName = normalize(e.RemarkName);
e.HeadImgUrl = `${axios.defaults.baseURL}cgi-bin/mmwebwx-bin/webwxgeticon?username=${e.UserName}&chatroomid=${user.EncryChatRoomId}&skey=${auth.skey}&seq=0`;
});

Expand Down
3 changes: 3 additions & 0 deletions src/js/stores/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { observable, action } from 'mobx';

import helper from 'utils/helper';
import storage from 'utils/storage';
import { normalize } from 'utils/emoji';
import chat from './chat';
import contacts from './contacts';

Expand Down Expand Up @@ -185,6 +186,8 @@ class Session {
return chat.markedRead(fromYourPhone ? from : to);
}

e.Content = normalize(e.Content);

// Sync message from your phone
if (fromYourPhone) {
// Message is sync from your phone
Expand Down
20 changes: 19 additions & 1 deletion src/js/utils/emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,25 @@ function parser(text) {
text = decodeText = text.split(`${e}`).join(`<a class="${className}"></a>`);
});

return normalize(decodeText);
}

function normalize(text = '') {
var matchs = text.match(/<span class="emoji emoji[0-9a-fA-F]+"><\/span>/g) || [];
var decodeText = text;

try {
matchs.map(e => {
// Decode utf16 to emoji
var emojiCode = e.match(/emoji([0-9a-fA-F]+)/)[1].substr(0, 5);
var emoji = String.fromCodePoint(parseInt(emojiCode, 16));
text = decodeText = text.split(e).join(emoji);
});
} catch (ex) {
console.error('Failed decode %s: %o', text, ex);
}

return decodeText;
}

export { emoji, parser };
export { emoji, parser, normalize };

0 comments on commit 9c1fbda

Please sign in to comment.