分享中心本地缓存

This commit is contained in:
fofolee 2022-05-01 09:57:52 +08:00
parent f215f3dc55
commit 03d9bed996

View File

@ -123,10 +123,10 @@ import commandTypes from "../js/options/commandTypes.js";
export default { export default {
data() { data() {
return { return {
currentPage: 1, initCommands: [],
commands: [],
allCommands: [],
matchedCommands: [], matchedCommands: [],
commands: [],
currentPage: 1,
perPage: 8, perPage: 8,
commandSearchKeyword: "", commandSearchKeyword: "",
releaseRepo: "fofolee/qcreleases", releaseRepo: "fofolee/qcreleases",
@ -149,8 +149,8 @@ export default {
}, },
mounted() { mounted() {
window.yuQueClient(`repos/${this.releaseRepo}/docs`).then((res) => { window.yuQueClient(`repos/${this.releaseRepo}/docs`).then((res) => {
this.allCommands = res.data.data; this.initCommands = res.data.data;
this.matchedCommands = _.cloneDeep(this.allCommands); this.matchedCommands = _.cloneDeep(this.initCommands);
this.fetchCommandDetails(1); this.fetchCommandDetails(1);
}); });
}, },
@ -165,17 +165,13 @@ export default {
this.matchedCommands this.matchedCommands
.slice((page - 1) * this.perPage, page * this.perPage) .slice((page - 1) * this.perPage, page * this.perPage)
.forEach((item) => { .forEach((item) => {
window this.getCommand(
.yuQueClient(`repos/${this.releaseRepo}/docs/${item.slug}?raw=1`) item.slug,
.then((res) => { item.content_updated_at.slice(0, 10),
let command = JSON.parse( item.last_editor.name
res.data?.data.body.match(/```json([\s\S]*)```/)?.[1] ).then((command) => {
); this.commands.push(command);
if (!command) return; });
command.authorName = item.last_editor.name;
command.updateTime = item.content_updated_at.slice(0, 10);
this.commands.push(command);
});
}); });
}, },
importCommand(command) { importCommand(command) {
@ -191,15 +187,31 @@ export default {
quickcommand.showMessageBox("导入成功!可到「来自分享」标签查看"); quickcommand.showMessageBox("导入成功!可到「来自分享」标签查看");
}, },
updateSearch() { updateSearch() {
if (!this.commandSearchKeyword) this.matchedCommands = this.allCommands; if (!this.commandSearchKeyword) this.matchedCommands = this.initCommands;
else else
this.matchedCommands = this.allCommands.filter((x) => this.matchedCommands = this.initCommands.filter((x) =>
x.title x.title
.toLowerCase() .toLowerCase()
.includes(this.commandSearchKeyword.toLowerCase()) .includes(this.commandSearchKeyword.toLowerCase())
); );
this.fetchCommandDetails(1); this.fetchCommandDetails(1);
}, },
//
async getCommand(id, updateTime, authorName) {
let localCache = JSON.parse(localStorage.getItem(id));
if (localCache?.updateTime === updateTime) return localCache;
let res = await window.yuQueClient(
`repos/${this.releaseRepo}/docs/${id}?raw=1`
);
let command = JSON.parse(
res.data?.data.body.match(/```json([\s\S]*)```/)?.[1]
);
if (!command) return;
command.authorName = authorName;
command.updateTime = updateTime;
localStorage.setItem(id, JSON.stringify(command));
return command;
},
}, },
}; };
</script> </script>