feat: 최근 실행턴의 중복 관리를 개선
This commit is contained in:
@@ -70,7 +70,7 @@
|
|||||||
<div class="col-4 d-grid">
|
<div class="col-4 d-grid">
|
||||||
<BDropdown right text="최근 실행">
|
<BDropdown right text="최근 실행">
|
||||||
<BDropdownItem
|
<BDropdownItem
|
||||||
v-for="(action, idx) in recentActions"
|
v-for="(action, idx) of Array.from(recentActions.values()).reverse()"
|
||||||
:key="idx"
|
:key="idx"
|
||||||
@click="void reserveCommandDirect([[Array.from(selectedTurnList.values()), action]])"
|
@click="void reserveCommandDirect([[Array.from(selectedTurnList.values()), action]])"
|
||||||
>{{ action.brief }}</BDropdownItem>
|
>{{ action.brief }}</BDropdownItem>
|
||||||
|
|||||||
@@ -86,7 +86,7 @@
|
|||||||
<div class="col-md-4 d-grid">
|
<div class="col-md-4 d-grid">
|
||||||
<BDropdown right text="최근">
|
<BDropdown right text="최근">
|
||||||
<BDropdownItem
|
<BDropdownItem
|
||||||
v-for="(action, idx) in recentActions"
|
v-for="(action, idx) in Array.from(recentActions.values()).reverse()"
|
||||||
:key="idx"
|
:key="idx"
|
||||||
@click="void reserveCommandDirect([[Array.from(selectedTurnList.values()), action]])"
|
@click="void reserveCommandDirect([[Array.from(selectedTurnList.values()), action]])"
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import type { TurnObj } from '@/defs';
|
import type { TurnObj } from '@/defs';
|
||||||
import { ref, watch } from 'vue';
|
import { ref, watch, type Ref } from 'vue';
|
||||||
|
|
||||||
|
|
||||||
export class StoredActionsHelper {
|
export class StoredActionsHelper {
|
||||||
public readonly recentActions = ref<TurnObj[]>([]);
|
public readonly recentActions: Ref<Map<string, TurnObj>>;
|
||||||
public readonly storedActions = ref(new Map<string, [number[], TurnObj][]>());
|
public readonly storedActions = ref(new Map<string, [number[], TurnObj][]>());
|
||||||
public readonly clipboard = ref<[number[], TurnObj][] | undefined>(undefined);
|
public readonly clipboard = ref<[number[], TurnObj][] | undefined>(undefined);
|
||||||
public readonly activatedCategory = ref<string>("");
|
public readonly activatedCategory = ref<string>("");
|
||||||
@@ -16,6 +16,7 @@ export class StoredActionsHelper {
|
|||||||
public readonly editModeKey: string;
|
public readonly editModeKey: string;
|
||||||
|
|
||||||
constructor(protected serverNick: string, protected type: 'general' | 'nation', protected mapName: string, protected unitSet: string, protected maxRecent = 10) {
|
constructor(protected serverNick: string, protected type: 'general' | 'nation', protected mapName: string, protected unitSet: string, protected maxRecent = 10) {
|
||||||
|
this.recentActions = ref(new Map());
|
||||||
const typeKey = `${serverNick}_${mapName}_${unitSet}_${type}`;
|
const typeKey = `${serverNick}_${mapName}_${unitSet}_${type}`;
|
||||||
this.recentActionsKey = `${typeKey}RecentActions`;
|
this.recentActionsKey = `${typeKey}RecentActions`;
|
||||||
this.storedActionsKey = `${typeKey}StoredActions`;
|
this.storedActionsKey = `${typeKey}StoredActions`;
|
||||||
@@ -49,26 +50,47 @@ export class StoredActionsHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
loadRecentActions() {
|
loadRecentActions() {
|
||||||
this.recentActions.value = JSON.parse(localStorage.getItem(this.recentActionsKey) ?? '[]');
|
try {
|
||||||
|
const rawRecentActions = JSON.parse(localStorage.getItem(this.recentActionsKey) ?? '[]') as TurnObj[];
|
||||||
|
const recentActions = new Map<string, TurnObj>();
|
||||||
|
for (const action of rawRecentActions) {
|
||||||
|
const actionKey = JSON.stringify([action.action, action.arg]);
|
||||||
|
recentActions.set(actionKey, action);
|
||||||
|
}
|
||||||
|
this.recentActions.value = recentActions;
|
||||||
|
}
|
||||||
|
catch(e){
|
||||||
|
console.log(`loadRecentActions error ${e}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pushRecentActions(action: TurnObj) {
|
pushRecentActions(action: TurnObj) {
|
||||||
this.recentActions.value.unshift(action);
|
const actionKey = JSON.stringify([action.action, action.arg]);
|
||||||
if (this.recentActions.value.length > this.maxRecent) {
|
if (this.recentActions.value.has(actionKey)) {
|
||||||
this.recentActions.value.pop();
|
this.recentActions.value.delete(actionKey);
|
||||||
}
|
}
|
||||||
|
else if (this.recentActions.value.size > this.maxRecent) {
|
||||||
|
const firstKey = this.recentActions.value.keys().next().value;
|
||||||
|
this.recentActions.value.delete(firstKey);
|
||||||
|
}
|
||||||
|
this.recentActions.value.set(actionKey, action);
|
||||||
this.saveRecentActions();
|
this.saveRecentActions();
|
||||||
}
|
}
|
||||||
|
|
||||||
saveRecentActions() {
|
saveRecentActions() {
|
||||||
localStorage.setItem(this.recentActionsKey, JSON.stringify(this.recentActions.value));
|
localStorage.setItem(this.recentActionsKey, JSON.stringify(Array.from(this.recentActions.value.values())));
|
||||||
}
|
}
|
||||||
|
|
||||||
loadStoredActions() {
|
loadStoredActions() {
|
||||||
|
try {
|
||||||
const rawValue: [string, [number[], TurnObj][]][] = JSON.parse(
|
const rawValue: [string, [number[], TurnObj][]][] = JSON.parse(
|
||||||
localStorage.getItem(this.storedActionsKey) ?? '[]'
|
localStorage.getItem(this.storedActionsKey) ?? '[]'
|
||||||
);
|
);
|
||||||
this.storedActions.value = new Map(rawValue);
|
this.storedActions.value = new Map(rawValue);
|
||||||
|
}
|
||||||
|
catch(e){
|
||||||
|
console.log(`loadStoredActions error ${e}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setStoredActions(actionKey: string, actions: [number[], TurnObj][]) {
|
setStoredActions(actionKey: string, actions: [number[], TurnObj][]) {
|
||||||
|
|||||||
Reference in New Issue
Block a user