From 9abbe6e177d4f02aae6529136e4d48b3ef6a2c36 Mon Sep 17 00:00:00 2001 From: iamcc Date: Wed, 27 Apr 2016 12:36:36 +0800 Subject: [PATCH] =?UTF-8?q?WIP=EF=BC=9A=E6=8F=90=E4=BE=9B=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E4=BF=9D=E5=AD=98=E5=8E=86=E5=8F=B2=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E7=9A=84=E6=80=9D=E8=B7=AF=EF=BC=8C=E4=BD=86=E6=98=AF=E7=9B=AE?= =?UTF-8?q?=E5=89=8D=E5=AD=98=E5=9C=A8=E9=97=AE=E9=A2=98=EF=BC=8C=E6=AF=8F?= =?UTF-8?q?=E6=AC=A1=E7=99=BB=E5=BD=95=E7=9A=84=E6=97=B6=E5=80=99=E7=94=A8?= =?UTF-8?q?=E6=88=B7ID=E9=83=BD=E4=BC=9A=E5=8F=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common.js | 2 +- src/inject/preload.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) 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();