Skip to content

Commit cf990da

Browse files
committed
feat: 完成设置focus 元素相关逻辑
1 parent 3da9a33 commit cf990da

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

src/stores/player.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type { fabric } from 'fabric'
22
import { defineStore, storeToRefs } from 'pinia'
3-
import { ref, computed } from 'vue'
3+
import { ref, computed, type Ref } from 'vue'
4+
import { nanoid } from 'nanoid'
45

56
type ElementTypes = 'text' | 'image' | 'video' | 'svg' | 'audio'
6-
export interface ElementItem {
7-
id: string
8-
type: ElementTypes
9-
element: fabric.Object
7+
export interface ElementItem extends fabric.Object {
8+
elementId?: string
9+
elementType?: ElementTypes
1010
}
1111
export type ElementList = ElementItem[]
1212

@@ -16,9 +16,11 @@ export const usePlayerStore = defineStore('playerStore', () => {
1616
// 播放列表
1717
const playList = ref<string[]>([])
1818
// 轨道数
19-
const trackCount = computed(() => playList.value.length)
19+
const trackCount = computed<number>(() => playList.value.length)
2020
// 元素列表
21-
const elementList = ref<ElementList>([])
21+
const elementList: Ref<ElementList> = ref([])
22+
// 当前选中的元素
23+
const focusElements: Ref<ElementList> = ref([])
2224
// 当前时间
2325
const currentTime = ref<number>(0)
2426
// 总时长
@@ -30,13 +32,28 @@ export const usePlayerStore = defineStore('playerStore', () => {
3032
}
3133
// 添加元素
3234
function addElement(type: ElementTypes, element: fabric.Object) {
33-
elementList.value.push({ id: String(Date.now()), element, type })
35+
Object.assign(element, { elementId: nanoid(), elementType: type })
36+
elementList.value.push(element as ElementItem)
3437
}
3538
// 删除元素
36-
function removeElement(id: string) {
37-
const index = elementList.value.findIndex((item) => item.id === id)
39+
function removeElement(elementId: string) {
40+
const index = elementList.value.findIndex((item) => item.elementId === elementId)
3841
if (index !== -1) elementList.value.splice(index, 1)
3942
}
43+
// 设置当前元素
44+
function setFocusElements(
45+
selected: ElementList | undefined,
46+
deselected: ElementList | undefined,
47+
) {
48+
if (selected) {
49+
focusElements.value = [...focusElements.value, ...selected]
50+
} else if (deselected) {
51+
const elementId = deselected.map((item) => item.elementId || '')
52+
focusElements.value = focusElements.value.filter(
53+
(item) => !elementId.includes(item.elementId || ''),
54+
)
55+
}
56+
}
4057

4158
return {
4259
playStatus,
@@ -45,9 +62,11 @@ export const usePlayerStore = defineStore('playerStore', () => {
4562
currentTime,
4663
duration,
4764
elementList,
65+
focusElements,
4866
togglePlay,
4967
addElement,
5068
removeElement,
69+
setFocusElements,
5170
}
5271
})
5372

0 commit comments

Comments
 (0)