From 1b1ca1d3719aed156f4493a0de7cb16b2293a8da Mon Sep 17 00:00:00 2001 From: WayneXuan Date: Sat, 9 Jan 2016 09:32:12 +0800 Subject: [PATCH 1/4] =?UTF-8?q?Enter=E6=97=B6,=E5=A6=82=E6=9E=9C=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E9=80=89=E4=B8=AD=E5=86=85=E5=AE=B9,$input=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E7=84=A6=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/javascript/popup.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/javascript/popup.js b/src/javascript/popup.js index 27b6924..f6f5854 100644 --- a/src/javascript/popup.js +++ b/src/javascript/popup.js @@ -378,9 +378,14 @@ toggleKey.onchange = function (event) { // updateSetting("duration", event.target.value); // }) -//在popup页内 Enter键 查询选中部分 +//在popup页内 Enter键 查询选中部分 如果没有选中部分,$input请求焦点 document.addEventListener('keyup',function(e){ if(document.activeElement.tagName=="BODY" && e.which==13){ - queryInPopup(window.getSelection().toString()); + var selection=window.getSelection().toString(); + if(selection.length>0) { + queryInPopup(selection); + }else{ + $input.focus(); + } } }); \ No newline at end of file From 1e84919ee0db225311d58374f17d10501fedb700 Mon Sep 17 00:00:00 2001 From: WayneXuan Date: Sat, 9 Jan 2016 10:38:14 +0800 Subject: [PATCH 2/4] =?UTF-8?q?popup=E5=86=85=E7=AE=80=E5=8D=95=E7=9A=84?= =?UTF-8?q?=E5=8E=86=E5=8F=B2=E8=AE=B0=E5=BD=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- popup.html | 1 + src/javascript/popup.js | 65 +++++++++++++++++++++++++++++++++++++++++ src/style.css | 11 ++++++- 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/popup.html b/popup.html index 00ba320..14f7d1b 100644 --- a/popup.html +++ b/popup.html @@ -10,6 +10,7 @@ +
diff --git a/src/javascript/popup.js b/src/javascript/popup.js index f6f5854..101e528 100644 --- a/src/javascript/popup.js +++ b/src/javascript/popup.js @@ -1,6 +1,8 @@ var $button = document.querySelector("#search"); //var $tipsContainer = document.querySelector("#tips"); var $input = document.querySelector("#query-word"); +var $historyList = document.querySelector("#query-history"); +var queryHistory=[]; var $queryResultContainer = document.querySelector("#query-result"); if (-1 !== window.navigator.platform.toLowerCase().indexOf("mac")) { @@ -49,6 +51,21 @@ var buildResult = function(response) { for (i = 0, len = voiceCollection.length; i < len; i++) { buildVoice(voiceCollection[i]); } + + //若有结果,无重复地写入历史记录 + if(resultObj.haveTranslation && resultObj.haveWebTranslation) { + for (i = 0,len=queryHistory.length; i < len; i++) { + if ($input.value == queryHistory[i]) { + break; + } + } + if(i==len){ + //只保留最近的6条记录 + if (queryHistory.unshift($input.value) > 6) { + queryHistory.pop(); + } + } + } } else { if (resultObj.errorCode == 20) { $queryResultContainer.innerHTML = "

这段文字太长,词典君无能为力了(┬_┬)

试试短一点的吧~

"; @@ -383,9 +400,57 @@ document.addEventListener('keyup',function(e){ if(document.activeElement.tagName=="BODY" && e.which==13){ var selection=window.getSelection().toString(); if(selection.length>0) { + $historyList.innerHTML = ""; queryInPopup(selection); }else{ $input.focus(); } } +}); + +/** + * 简单的历史记录功能 + * 焦点在输入框时,按上下箭头显示历史列表供选择,按其他键移除列表 + * 目前只保存本次会话的历史 + */ +$input.addEventListener('keyup',function(e){ + if(queryHistory.length>0) { + if (e.which == 38 || e.which == 40) { + e.preventDefault(); + e.stopPropagation(); + if ($historyList.children.length==0) { + for (var i = 0; i < queryHistory.length; i++) { + $historyList.appendChild(document.createElement("LI").appendChild(document.createTextNode(queryHistory[i])).parentNode); + } + return; + } + var cur=$historyList.querySelector(".cur"); + if (e.which == 38) { + if (cur == null) { + cur = $historyList.firstElementChild; + cur.className = "cur"; + } + cur.className = ""; + cur = cur.previousElementSibling; + if (cur == null) { + cur = $historyList.lastElementChild; + } + cur.className = "cur"; + } else { + if (cur == null) { + cur = $historyList.lastElementChild; + cur.className = "cur"; + } + cur.className = ""; + cur = cur.nextElementSibling; + if (cur == null) { + cur = $historyList.firstElementChild; + } + cur.className = "cur"; + } + queryInPopup(cur.innerHTML); + } else { + $historyList.innerHTML = ""; + } + } }); \ No newline at end of file diff --git a/src/style.css b/src/style.css index a556b7e..8d763ad 100644 --- a/src/style.css +++ b/src/style.css @@ -169,7 +169,16 @@ ul { font-family: "YDdict-Icon"; } - +#query-history{ + margin-left:20px; +} +#query-history li{ + border: inset transparent; + color: #999; +} +#query-history li.cur{ + border-color:#faf8ef; +} /*Result Block*/ .result-container { From 9bf6def927d389eed33e01589b523447a2341575 Mon Sep 17 00:00:00 2001 From: WayneXuan Date: Sat, 9 Jan 2016 12:27:10 +0800 Subject: [PATCH 3/4] =?UTF-8?q?grunt=20watch=20=E8=AF=AD=E6=B3=95=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gruntfile.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 70ca8f5..8c5cc58 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -68,11 +68,11 @@ module.exports = function(grunt) { watch: { scripts: { files: "src/**/*.js", - tasks: "uglify", + tasks: "uglify" }, sass: { files: "src/**/*.scss", - tasks: "sass", + tasks: "sass" }, css: { files: "src/*.css", From 8c4982f6acb9192d15205ba3b15a25843b9dec93 Mon Sep 17 00:00:00 2001 From: WayneXuan Date: Mon, 11 Jan 2016 14:07:36 +0800 Subject: [PATCH 4/4] =?UTF-8?q?popup=20Alt+Enter=20=E6=B8=85=E7=A9=BA?= =?UTF-8?q?=E5=B9=B6=E8=81=9A=E7=84=A6$input?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/javascript/popup.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/javascript/popup.js b/src/javascript/popup.js index 101e528..94d7607 100644 --- a/src/javascript/popup.js +++ b/src/javascript/popup.js @@ -395,9 +395,11 @@ toggleKey.onchange = function (event) { // updateSetting("duration", event.target.value); // }) -//在popup页内 Enter键 查询选中部分 如果没有选中部分,$input请求焦点 +/** + * popup页 快捷键绑定 + */ document.addEventListener('keyup',function(e){ - if(document.activeElement.tagName=="BODY" && e.which==13){ + if(document.activeElement.tagName=="BODY" && e.which==13){ //Enter 查询选中部分 如果没有选中部分,$input请求焦点 var selection=window.getSelection().toString(); if(selection.length>0) { $historyList.innerHTML = ""; @@ -405,6 +407,9 @@ document.addEventListener('keyup',function(e){ }else{ $input.focus(); } + }else if(e.which==18){ //Alt+Enter $input清空内容并请求焦点 + $input.value = ''; + $input.focus(); } });