File size: 8,590 Bytes
89ce340 4db4b63 89ce340 4db4b63 89ce340 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
import { defineStore } from 'pinia'
import { omit } from 'lodash'
import type { Slide, SlideTheme, PPTElement, PPTAnimation, SlideTemplate } from '@/types/slides'
interface RemovePropData {
id: string
propName: string | string[]
}
interface UpdateElementData {
id: string | string[]
props: Partial<PPTElement>
slideId?: string
}
interface FormatedAnimation {
animations: PPTAnimation[]
autoNext: boolean
}
export interface SlidesState {
title: string
theme: SlideTheme
slides: Slide[]
slideIndex: number
viewportSize: number
viewportRatio: number
templates: SlideTemplate[]
}
export const useSlidesStore = defineStore('slides', {
state: (): SlidesState => ({
title: '未命名演示文稿', // 幻灯片标题
theme: {
themeColors: ['#5b9bd5', '#ed7d31', '#a5a5a5', '#ffc000', '#4472c4', '#70ad47'],
fontColor: '#333',
fontName: '',
backgroundColor: '#fff',
shadow: {
h: 3,
v: 3,
blur: 2,
color: '#808080',
},
outline: {
width: 2,
color: '#525252',
style: 'solid',
},
}, // 主题样式
slides: [], // 幻灯片页面数据
slideIndex: 0, // 当前页面索引
viewportSize: 1000, // 可视区域宽度基数
viewportRatio: 0.5625, // 可视区域比例,默认16:9
templates: [
{ name: '红色通用', id: 'template_1', cover: 'https://asset.pptist.cn/img/template_1.jpg' },
{ name: '蓝色通用', id: 'template_2', cover: 'https://asset.pptist.cn/img/template_2.jpg' },
{ name: '紫色通用', id: 'template_3', cover: 'https://asset.pptist.cn/img/template_3.jpg' },
{ name: '莫兰迪配色', id: 'template_4', cover: 'https://asset.pptist.cn/img/template_4.jpg' },
], // 模板
}),
getters: {
currentSlide(state) {
return state.slides[state.slideIndex]
},
currentSlideAnimations(state) {
const currentSlide = state.slides[state.slideIndex]
if (!currentSlide?.animations) return []
const els = currentSlide.elements
const elIds = els.map(el => el.id)
return currentSlide.animations.filter(animation => elIds.includes(animation.elId))
},
// 格式化的当前页动画
// 将触发条件为“与上一动画同时”的项目向上合并到序列中的同一位置
// 为触发条件为“上一动画之后”项目的上一项添加自动向下执行标记
formatedAnimations(state) {
const currentSlide = state.slides[state.slideIndex]
if (!currentSlide?.animations) return []
const els = currentSlide.elements
const elIds = els.map(el => el.id)
const animations = currentSlide.animations.filter(animation => elIds.includes(animation.elId))
const formatedAnimations: FormatedAnimation[] = []
for (const animation of animations) {
if (animation.trigger === 'click' || !formatedAnimations.length) {
formatedAnimations.push({ animations: [animation], autoNext: false })
}
else if (animation.trigger === 'meantime') {
const last = formatedAnimations[formatedAnimations.length - 1]
last.animations = last.animations.filter(item => item.elId !== animation.elId)
last.animations.push(animation)
formatedAnimations[formatedAnimations.length - 1] = last
}
else if (animation.trigger === 'auto') {
const last = formatedAnimations[formatedAnimations.length - 1]
last.autoNext = true
formatedAnimations[formatedAnimations.length - 1] = last
formatedAnimations.push({ animations: [animation], autoNext: false })
}
}
return formatedAnimations
},
},
actions: {
setTitle(title: string) {
if (!title) this.title = '未命名演示文稿'
else this.title = title
},
setTheme(themeProps: Partial<SlideTheme>) {
this.theme = { ...this.theme, ...themeProps }
},
setViewportSize(size: number) {
// 🔧 修复:确保视口尺寸的合理性和精度
if (typeof size !== 'number' || size <= 0 || size > 2000) {
console.warn(`⚠️ Invalid viewportSize: ${size}, using default 1000`)
this.viewportSize = 1000
} else {
// 保持整数精度,避免浮点误差
this.viewportSize = Math.round(size)
}
},
setViewportRatio(viewportRatio: number) {
// 🔧 修复:确保视口比例的合理性和高精度
if (typeof viewportRatio !== 'number' || viewportRatio <= 0 || viewportRatio > 2) {
console.warn(`⚠️ Invalid viewportRatio: ${viewportRatio}, using default 0.5625`)
this.viewportRatio = 0.5625
} else {
// 🔧 关键修复:保持极高精度,防止累积误差
// 使用8位小数精度确保视口比例的准确性
this.viewportRatio = Math.round(viewportRatio * 100000000) / 100000000
}
},
setSlides(slides: Slide[]) {
this.slides = slides
},
setTemplates(templates: SlideTemplate[]) {
this.templates = templates
},
addSlide(slide: Slide | Slide[]) {
const slides = Array.isArray(slide) ? slide : [slide]
for (const slide of slides) {
if (slide.sectionTag) delete slide.sectionTag
}
const addIndex = this.slideIndex + 1
this.slides.splice(addIndex, 0, ...slides)
this.slideIndex = addIndex
},
updateSlide(props: Partial<Slide>, slideId?: string) {
const slideIndex = slideId ? this.slides.findIndex(item => item.id === slideId) : this.slideIndex
this.slides[slideIndex] = { ...this.slides[slideIndex], ...props }
},
removeSlideProps(data: RemovePropData) {
const { id, propName } = data
const slides = this.slides.map(slide => {
return slide.id === id ? omit(slide, propName) : slide
}) as Slide[]
this.slides = slides
},
deleteSlide(slideId: string | string[]) {
const slidesId = Array.isArray(slideId) ? slideId : [slideId]
const slides: Slide[] = JSON.parse(JSON.stringify(this.slides))
const deleteSlidesIndex = []
for (const deletedId of slidesId) {
const index = slides.findIndex(item => item.id === deletedId)
deleteSlidesIndex.push(index)
const deletedSlideSection = slides[index].sectionTag
if (deletedSlideSection) {
const handleSlideNext = slides[index + 1]
if (handleSlideNext && !handleSlideNext.sectionTag) {
delete slides[index].sectionTag
slides[index + 1].sectionTag = deletedSlideSection
}
}
slides.splice(index, 1)
}
let newIndex = Math.min(...deleteSlidesIndex)
const maxIndex = slides.length - 1
if (newIndex > maxIndex) newIndex = maxIndex
this.slideIndex = newIndex
this.slides = slides
},
updateSlideIndex(index: number) {
this.slideIndex = index
},
addElement(element: PPTElement | PPTElement[]) {
const elements = Array.isArray(element) ? element : [element]
const currentSlideEls = this.slides[this.slideIndex].elements
const newEls = [...currentSlideEls, ...elements]
this.slides[this.slideIndex].elements = newEls
},
deleteElement(elementId: string | string[]) {
const elementIdList = Array.isArray(elementId) ? elementId : [elementId]
const currentSlideEls = this.slides[this.slideIndex].elements
const newEls = currentSlideEls.filter(item => !elementIdList.includes(item.id))
this.slides[this.slideIndex].elements = newEls
},
updateElement(data: UpdateElementData) {
const { id, props, slideId } = data
const elIdList = typeof id === 'string' ? [id] : id
const slideIndex = slideId ? this.slides.findIndex(item => item.id === slideId) : this.slideIndex
const slide = this.slides[slideIndex]
const elements = slide.elements.map(el => {
return elIdList.includes(el.id) ? { ...el, ...props } : el
})
this.slides[slideIndex].elements = (elements as PPTElement[])
},
removeElementProps(data: RemovePropData) {
const { id, propName } = data
const propsNames = typeof propName === 'string' ? [propName] : propName
const slideIndex = this.slideIndex
const slide = this.slides[slideIndex]
const elements = slide.elements.map(el => {
return el.id === id ? omit(el, propsNames) : el
})
this.slides[slideIndex].elements = (elements as PPTElement[])
},
},
}) |