Use Dexie instead of Pinia store values in tileList inside mapEditor

This commit is contained in:
Dennis Postma 2025-01-25 13:49:03 +01:00
parent 9cdfcbcc56
commit 69f9944dc7
5 changed files with 37 additions and 21 deletions

View File

@ -55,12 +55,10 @@ import type { MapObject } from '@/application/types'
import ChipsInput from '@/components/forms/ChipsInput.vue' import ChipsInput from '@/components/forms/ChipsInput.vue'
import { useAssetManagerStore } from '@/stores/assetManagerStore' import { useAssetManagerStore } from '@/stores/assetManagerStore'
import { useGameStore } from '@/stores/gameStore' import { useGameStore } from '@/stores/gameStore'
import { useMapEditorStore } from '@/stores/mapEditorStore'
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue' import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
const gameStore = useGameStore() const gameStore = useGameStore()
const assetManagerStore = useAssetManagerStore() const assetManagerStore = useAssetManagerStore()
const mapEditorStore = useMapEditorStore()
const selectedMapObject = computed(() => assetManagerStore.selectedMapObject) const selectedMapObject = computed(() => assetManagerStore.selectedMapObject)
@ -105,10 +103,6 @@ function refreshObjectList(unsetSelectedMapObject = true) {
if (unsetSelectedMapObject) { if (unsetSelectedMapObject) {
assetManagerStore.setSelectedMapObject(null) assetManagerStore.setSelectedMapObject(null)
} }
if (mapEditorStore.active) {
mapEditorStore.setMapObjectList(response)
}
}) })
} }

View File

@ -30,9 +30,11 @@ import { useAssetManagerStore } from '@/stores/assetManagerStore'
import { useGameStore } from '@/stores/gameStore' import { useGameStore } from '@/stores/gameStore'
import { useMapEditorStore } from '@/stores/mapEditorStore' import { useMapEditorStore } from '@/stores/mapEditorStore'
import { computed, onBeforeUnmount, onMounted, ref, toRaw, watch } from 'vue' import { computed, onBeforeUnmount, onMounted, ref, toRaw, watch } from 'vue'
import { TileStorage } from '@/storage/storages'
const gameStore = useGameStore() const gameStore = useGameStore()
const assetManagerStore = useAssetManagerStore() const assetManagerStore = useAssetManagerStore()
const tileStorage = new TileStorage()
const selectedTile = computed(() => assetManagerStore.selectedTile) const selectedTile = computed(() => assetManagerStore.selectedTile)
@ -54,12 +56,13 @@ watch(selectedTile, (tile: Tile | null) => {
tileTags.value = tile.tags tileTags.value = tile.tags
}) })
function deleteTile() { async function deleteTile() {
gameStore.connection?.emit('gm:tile:delete', { id: selectedTile.value?.id }, (response: boolean) => { gameStore.connection?.emit('gm:tile:delete', { id: selectedTile.value?.id }, async (response: boolean) => {
if (!response) { if (!response) {
console.error('Failed to delete tile') console.error('Failed to delete tile')
return return
} }
await tileStorage.delete(selectedTile.value!.id)
refreshTileList() refreshTileList()
}) })
} }

View File

@ -78,7 +78,7 @@ const toggleTag = (tag: string) => {
onMounted(async () => { onMounted(async () => {
isModalOpen.value = true isModalOpen.value = true
gameStore.connection?.emit('gm:mapObject:list', {}, (response: MapObject[]) => { gameStore.connection?.emit('gm:mapObject:list', {}, (response: MapObject[]) => {
mapEditorStore.setMapObjectList(response) // mapEditorStore.setMapObjectList(response)
}) })
}) })
</script> </script>

View File

@ -84,26 +84,27 @@
import config from '@/application/config' import config from '@/application/config'
import type { Tile } from '@/application/types' import type { Tile } from '@/application/types'
import Modal from '@/components/utilities/Modal.vue' import Modal from '@/components/utilities/Modal.vue'
import { useGameStore } from '@/stores/gameStore'
import { useMapEditorStore } from '@/stores/mapEditorStore' import { useMapEditorStore } from '@/stores/mapEditorStore'
import { computed, onMounted, ref } from 'vue' import { computed, onMounted, onUnmounted, ref } from 'vue'
import { TileStorage } from '@/storage/storages'
import { liveQuery } from 'dexie'
const gameStore = useGameStore() const tileStorage = new TileStorage()
const isModalOpen = ref(false)
const mapEditorStore = useMapEditorStore() const mapEditorStore = useMapEditorStore()
const searchQuery = ref('') const searchQuery = ref('')
const selectedTags = ref<string[]>([]) const selectedTags = ref<string[]>([])
const tileCategories = ref<Map<string, string>>(new Map()) const tileCategories = ref<Map<string, string>>(new Map())
const selectedGroup = ref<{ parent: Tile; children: Tile[] } | null>(null) const selectedGroup = ref<{ parent: Tile; children: Tile[] } | null>(null)
const tiles = ref<Tile[]>([])
const uniqueTags = computed(() => { const uniqueTags = computed(() => {
const allTags = mapEditorStore.tileList.flatMap((tile) => tile.tags || []) const allTags = tiles.value.flatMap((tile) => tile.tags || [])
return Array.from(new Set(allTags)) return Array.from(new Set(allTags))
}) })
const groupedTiles = computed(() => { const groupedTiles = computed(() => {
const groups: { parent: Tile; children: Tile[] }[] = [] const groups: { parent: Tile; children: Tile[] }[] = []
const filteredTiles = mapEditorStore.tileList.filter((tile) => { const filteredTiles = tiles.value.filter((tile) => {
const matchesSearch = !searchQuery.value || tile.name.toLowerCase().includes(searchQuery.value.toLowerCase()) const matchesSearch = !searchQuery.value || tile.name.toLowerCase().includes(searchQuery.value.toLowerCase())
const matchesTags = selectedTags.value.length === 0 || (tile.tags && selectedTags.value.some((tag) => tile.tags.includes(tag))) const matchesTags = selectedTags.value.length === 0 || (tile.tags && selectedTags.value.some((tag) => tile.tags.includes(tag)))
return matchesSearch && matchesTags return matchesSearch && matchesTags
@ -173,10 +174,8 @@ function processTile(tile: Tile) {
} }
function getDominantColor(imageData: ImageData) { function getDominantColor(imageData: ImageData) {
let r = 0, let r = 0, g = 0, b = 0, total = 0
g = 0,
b = 0,
total = 0
for (let i = 0; i < imageData.data.length; i += 4) { for (let i = 0; i < imageData.data.length; i += 4) {
if (imageData.data[i + 3] > 0) { if (imageData.data[i + 3] > 0) {
// Only consider non-transparent pixels // Only consider non-transparent pixels
@ -186,6 +185,7 @@ function getDominantColor(imageData: ImageData) {
total++ total++
} }
} }
return { return {
r: Math.round(r / total), r: Math.round(r / total),
g: Math.round(g / total), g: Math.round(g / total),
@ -226,7 +226,22 @@ function isActiveTile(tile: Tile): boolean {
return mapEditorStore.selectedTile === tile.id return mapEditorStore.selectedTile === tile.id
} }
onMounted(async () => { let subscription: any = null
isModalOpen.value = true
onMounted(() => {
subscription = liveQuery(() => tileStorage.liveQuery()).subscribe({
next: (result) => {
tiles.value = result
},
error: (error) => {
console.error('Failed to fetch tiles:', error)
}
})
})
onUnmounted(() => {
if (subscription) {
subscription.unsubscribe()
}
}) })
</script> </script>

View File

@ -68,6 +68,10 @@ export class BaseStorage<T extends { id: string }> {
} }
} }
liveQuery() {
return this.dexie.table(this.tableName).toArray()
}
async reset() { async reset() {
try { try {
await this.dexie.table(this.tableName).clear() await this.dexie.table(this.tableName).clear()