diff --git a/src/common.js b/src/common.js index d256c80b..ed66224e 100644 --- a/src/common.js +++ b/src/common.js @@ -42,4 +42,4 @@ Common.MENTION_MENU_HINT_TEXT = "选择回复的人:"; Common.MESSAGE_PREVENT_RECALL = "阻止了一次撤回"; Common.EMOJI_MAXIUM_SIZE = 120; -module.exports = Common; \ No newline at end of file +module.exports = Common; diff --git a/src/inject/preload.js b/src/inject/preload.js index 21ebe23d..47919fbd 100644 --- a/src/inject/preload.js +++ b/src/inject/preload.js @@ -44,6 +44,11 @@ class Injector { }); $rootScope.shareMenu = ShareMenu.inject; $rootScope.mentionMenu = Injector.mentionMenu.inject; + + setTimeout(() => { + console.log('watch currentUser'); + angular.element('#chatArea').scope().$watch('currentUser', self.restoreChatContent.bind(self)); + }) }]); return angularBootstrapReal.apply(angular, arguments); } : angularBootstrapReal, @@ -102,6 +107,8 @@ class Injector { Injector.lock(msg, 'MMDigest', Common.MESSAGE_PREVENT_RECALL); break; } + + this.saveHistory(msg); }); return value; } @@ -116,6 +123,42 @@ class Injector { } return value; } + + restoreChatContent(user) { + const scope = angular.element('#chatArea').scope(); + if (!scope.chatContent || scope.chatContent.length === 0) { + const his = this.getHistory(user); + for (let i in his) { + his[i].MMUnread = false; + scope.chatContent.push(his[i]); + } + } + } + + getHistory(user) { + let his = localStorage.getItem(user); + if (!his) { + return []; + } + return JSON.parse(his); + } + + saveHistory(msg) { + if (!msg) return; + + setTimeout(() => { + const user = msg.MMPeerUserName; + if (!user) return; + + const his = this.getHistory(user); + his.push(msg); + if (his.length > 20) { + his = his.splice(his.length - 20); + } + console.log(msg); + localStorage.setItem(user, JSON.stringify(his)); + }); + } } new Injector().init();