1
1
import type { fabric } from 'fabric'
2
2
import { defineStore , storeToRefs } from 'pinia'
3
- import { ref , computed } from 'vue'
3
+ import { ref , computed , type Ref } from 'vue'
4
+ import { nanoid } from 'nanoid'
4
5
5
6
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
10
10
}
11
11
export type ElementList = ElementItem [ ]
12
12
@@ -16,9 +16,11 @@ export const usePlayerStore = defineStore('playerStore', () => {
16
16
// 播放列表
17
17
const playList = ref < string [ ] > ( [ ] )
18
18
// 轨道数
19
- const trackCount = computed ( ( ) => playList . value . length )
19
+ const trackCount = computed < number > ( ( ) => playList . value . length )
20
20
// 元素列表
21
- const elementList = ref < ElementList > ( [ ] )
21
+ const elementList : Ref < ElementList > = ref ( [ ] )
22
+ // 当前选中的元素
23
+ const focusElements : Ref < ElementList > = ref ( [ ] )
22
24
// 当前时间
23
25
const currentTime = ref < number > ( 0 )
24
26
// 总时长
@@ -30,13 +32,28 @@ export const usePlayerStore = defineStore('playerStore', () => {
30
32
}
31
33
// 添加元素
32
34
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 )
34
37
}
35
38
// 删除元素
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 )
38
41
if ( index !== - 1 ) elementList . value . splice ( index , 1 )
39
42
}
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
+ }
40
57
41
58
return {
42
59
playStatus,
@@ -45,9 +62,11 @@ export const usePlayerStore = defineStore('playerStore', () => {
45
62
currentTime,
46
63
duration,
47
64
elementList,
65
+ focusElements,
48
66
togglePlay,
49
67
addElement,
50
68
removeElement,
69
+ setFocusElements,
51
70
}
52
71
} )
53
72
0 commit comments