mirror of
https://github.com/koodo-reader/koodo-reader.git
synced 2026-06-19 13:20:50 -04:00
58 lines
1.7 KiB
TypeScript
58 lines
1.7 KiB
TypeScript
// 记录书本打开记录
|
|
import BookModel from "../model/Book";
|
|
class RecordRecent {
|
|
static setRecent(bookKey: string) {
|
|
let bookArr =
|
|
localStorage.getItem("recentBooks") !== "{}" &&
|
|
localStorage.getItem("recentBooks")
|
|
? JSON.parse(localStorage.getItem("recentBooks") || "")
|
|
: [];
|
|
const index = bookArr.indexOf(bookKey);
|
|
if (index > -1) {
|
|
bookArr.splice(index, 1);
|
|
bookArr.unshift(bookKey);
|
|
} else {
|
|
bookArr.unshift(bookKey);
|
|
}
|
|
|
|
localStorage.setItem("recentBooks", JSON.stringify(bookArr));
|
|
}
|
|
static setAllRecent(books: BookModel[]) {
|
|
let bookArr: string[] = [];
|
|
books.forEach((item) => {
|
|
bookArr.push(item.key);
|
|
});
|
|
localStorage.setItem("recentBooks", JSON.stringify(bookArr));
|
|
}
|
|
static getRecent() {
|
|
let bookArr =
|
|
localStorage.getItem("recentBooks") !== "{}" &&
|
|
localStorage.getItem("recentBooks")
|
|
? JSON.parse(localStorage.getItem("recentBooks") || "")
|
|
: [];
|
|
return bookArr;
|
|
}
|
|
static clear(bookKey: string) {
|
|
let bookArr =
|
|
localStorage.getItem("recentBooks") !== "{}" &&
|
|
localStorage.getItem("recentBooks")
|
|
? JSON.parse(localStorage.getItem("recentBooks") || "")
|
|
: [];
|
|
const index = bookArr.indexOf(bookKey);
|
|
if (index > -1) {
|
|
bookArr.splice(index, 1);
|
|
}
|
|
localStorage.setItem("recentBooks", JSON.stringify(bookArr));
|
|
}
|
|
static getAllRecent() {
|
|
let bookArr =
|
|
localStorage.getItem("recentBooks") !== "{}" &&
|
|
localStorage.getItem("recentBooks")
|
|
? JSON.parse(localStorage.getItem("recentBooks") || "")
|
|
: [];
|
|
return bookArr || [];
|
|
}
|
|
}
|
|
|
|
export default RecordRecent;
|