Skip to content

Commit

Permalink
wip: optimize history
Browse files Browse the repository at this point in the history
  • Loading branch information
vipzhicheng committed Mar 19, 2023
1 parent 48160ad commit a603911
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.html",
"logseq": {
"icon": "./icon.png",
"id": "logseq-plugin-move-block",
"id": "logseq-move-block",
"title": "Move Block"
},
"scripts": {
Expand Down
6 changes: 4 additions & 2 deletions src/components/Target.vue
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ const selectFavorite = selectHistory;
</n-form>
</div>

<div class="border-l border-dashed pl-4 w-1/2">
<div
class="border-l border-dashed pl-4 w-1/2 h-[500px] overflow-x-auto overflow-y-scroll"
>
<div class="favorites-card border-b border-dashed pb-2">
<div class="font-bold mb-2 flex flex-row justify-start items-center">
FAVORITES
Expand Down Expand Up @@ -256,7 +258,7 @@ const selectFavorite = selectHistory;
</n-tooltip>
<b>{{ cc.sentenceCase(o.action) }}</b> to
<b
>{{ o.to }}
>{{ cc.noCase(o.to) }}
{{ o.to === "page" ? o.page : "" }}
{{ o.to === "journal" ? o.journal : "" }}</b
>
Expand Down
47 changes: 37 additions & 10 deletions src/stores/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,38 @@ export const useTargetStore = defineStore('target', {
this.favorites = favoritesStorage;
}
}

let historyStorageExists = await assetStorage.hasItem('history.json');

if (!historyStorageExists) {
this.history = [];
} else {
let historyStorage = await assetStorage.getItem('history.json');
historyStorage = historyStorage ? JSON.parse(historyStorage) : [];
if (historyStorage.length === 0) {
// default fav
this.history = [];
} else {
this.history = historyStorage;
}
}
},
async saveFavorites() {
await assetStorage.setItem(
'favorites.json',
JSON.stringify(this.favorites)
);
},
deleteFavorite(index) {
async deleteFavorite(index) {
this.favorites.splice(index, 1);
await this.saveFavorites();
},
async addCurrentToFavorites() {
await this.addFavorite(this.destination);
},
clearFavorites() {
async clearFavorites() {
this.favorites = [];
await this.saveFavorites();
},
async addFavorite(data) {
const findedIndex = this.favorites.findIndex(item => {
Expand Down Expand Up @@ -266,19 +289,21 @@ export const useTargetStore = defineStore('target', {
this.favorites.pop();
}

await assetStorage.setItem(
'favorites.json',
JSON.stringify(this.favorites)
);
await this.saveFavorites();
},

deleteHistory(index) {
async saveHistory() {
await assetStorage.setItem('history.json', JSON.stringify(this.history));
},
async deleteHistory(index) {
this.history.splice(index, 1);
await this.saveHistory();
},
clearHistory() {
async clearHistory() {
this.history = [];
await this.saveHistory();
},
addHistory(data) {
async addHistory(data) {
const findedIndex = this.history.findIndex(item => {
if (
item.to === data.to &&
Expand Down Expand Up @@ -308,6 +333,8 @@ export const useTargetStore = defineStore('target', {
if (this.history.length > 15) {
this.history.pop();
}

await this.saveHistory();
},

setFallbackUUID(uuid: string) {
Expand Down Expand Up @@ -336,7 +363,7 @@ export const useTargetStore = defineStore('target', {
},
async submit() {
const { to, action, journal, after, page, at } = this.destination;
this.addHistory(this.destination);
await this.addHistory(this.destination);
const selected = await logseq.Editor.getSelectedBlocks();
let processed;
if (selected && selected.length > 1) {
Expand Down

0 comments on commit a603911

Please sign in to comment.