Skip to content

Commit

Permalink
feat(scan): autocomplete url from history
Browse files Browse the repository at this point in the history
  • Loading branch information
popstas committed Dec 27, 2020
1 parent bca740e commit d2bb1e0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
40 changes: 39 additions & 1 deletion pages/scan.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
></el-input>
</el-form-item>
<el-form-item v-if="!isUrls">
<el-input v-model="url" @keydown.enter.native="sendTask" autofocus class="form__url"></el-input>
<el-autocomplete v-model="url" @keydown.enter.native.prevent="sendTask" autofocus class="form__url"
:fetch-suggestions="historySearch"
></el-autocomplete>
</el-form-item>

<el-form-item>
Expand Down Expand Up @@ -187,6 +189,7 @@
<script>
import Panel from "~/components/Panel";
import firebase from "firebase";
import _ from "lodash";
const defaultForm = {
preset: 'seo',
Expand Down Expand Up @@ -250,6 +253,11 @@ export default {
return this.$store.state.log;
},
scanUrlHistory(){
return this.$store.state.scanUrlHistory;
},
url: {
get() {
return this.$store.state.url;
Expand Down Expand Up @@ -473,6 +481,11 @@ export default {
this.updateUrlQuery(true); // set in url only scanned
// save url scan history
if (!this.isUrls) {
this.$store.commit('addUrlHistory', this.url);
}
console.log("scan:", opts);
this.socket.emit('scan', opts);
Expand Down Expand Up @@ -530,6 +543,31 @@ export default {
}
});
},
// url history autocomplete
historySearch(q, cb) {
let res = [];
let all = [];
for (let url in this.scanUrlHistory) {
all.push(url);
if (url.includes(q)) res.push(url);
}
if (res.length <= 1) {
res = [...res, ...all];
}
res = _.uniq(res);
// limit 20
if (res.length > 20) res = res.slice(0, 20);
res = res.map(value => {
return { value }
});
cb(res);
},
},
async mounted() {
Expand Down
1 change: 1 addition & 0 deletions plugins/localStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default ({store}) => {
paths: [
'itemsJsonUrl',
'jsonUrlHistory',
'scanUrlHistory',
'visitCount',
'openGroups',
'showHumanColumns',
Expand Down
16 changes: 15 additions & 1 deletion store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export const state = () => ({
// constants
itemsJsonUrl: process.env.itemsJsonUrl || '',
jsonUrlHistory: {},
scanUrlHistory: {},
name: pjson.name,
version: pjson.version,
description: pjson.description,
Expand Down Expand Up @@ -368,6 +369,19 @@ export const mutations = {
jsonUrlHistory(state, newValue) {
state.jsonUrlHistory = newValue;
},
scanUrlHistory(state, newValue) {
state.scanUrlHistory = newValue;
},
addUrlHistory(state, url) {
if (!state.scanUrlHistory[url]) {
state.scanUrlHistory[url] = {
added: Date.now(),
used: Date.now(),
};
} else {
state.scanUrlHistory[url].used = Date.now();
}
},
fields(state, newValue) {
state.fields = newValue;
},
Expand Down Expand Up @@ -437,7 +451,7 @@ export const mutations = {

url(state, newValue) {
state.url = newValue;
},
},
urls(state, newValue) {
state.urls = newValue;
},
Expand Down

0 comments on commit d2bb1e0

Please sign in to comment.