SlideStylePanel.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <template>
  2. <div class="slide-style-panel">
  3. <div class="title">背景填充</div>
  4. <div class="row">
  5. <Select
  6. style="flex: 10;"
  7. :value="background.type"
  8. @change="value => updateBackgroundType(value)"
  9. >
  10. <SelectOption value="solid">纯色填充</SelectOption>
  11. <SelectOption value="image">图片填充</SelectOption>
  12. <SelectOption value="gradient">渐变填充</SelectOption>
  13. </Select>
  14. <div style="flex: 1;"></div>
  15. <Popover trigger="click" v-if="background.type === 'solid'">
  16. <template #content>
  17. <ColorPicker
  18. :modelValue="background.color"
  19. @update:modelValue="color => updateBackground({ color })"
  20. />
  21. </template>
  22. <ColorButton :color="background.color || '#fff'" style="flex: 10;" />
  23. </Popover>
  24. <Select
  25. style="flex: 10;"
  26. :value="background.size || 'cover'"
  27. @change="value => updateBackground({ imageSize: value })"
  28. v-else-if="background.type === 'image'"
  29. >
  30. <SelectOption value="initial">原始大小</SelectOption>
  31. <SelectOption value="contain">缩放</SelectOption>
  32. <SelectOption value="repeat">拼贴</SelectOption>
  33. <SelectOption value="cover">缩放铺满</SelectOption>
  34. </Select>
  35. <Select
  36. style="flex: 10;"
  37. :value="background.gradientType"
  38. @change="value => updateBackground({ gradientType: value })"
  39. v-else
  40. >
  41. <SelectOption value="linear">线性渐变</SelectOption>
  42. <SelectOption value="radial">径向渐变</SelectOption>
  43. </Select>
  44. </div>
  45. <div class="background-image-wrapper" v-if="background.type === 'image'">
  46. <FileInput @change="files => uploadBackgroundImage(files)">
  47. <div class="background-image">
  48. <div class="content" :style="{ backgroundImage: `url(${background.image})` }">
  49. <IconPlus />
  50. </div>
  51. </div>
  52. </FileInput>
  53. </div>
  54. <div class="background-gradient-wrapper" v-if="background.type === 'gradient'">
  55. <div class="row">
  56. <div style="flex: 2;">起点颜色:</div>
  57. <Popover trigger="click">
  58. <template #content>
  59. <ColorPicker
  60. :modelValue="background.gradientColor[0]"
  61. @update:modelValue="value => updateBackground({ gradientColor: [value, background.gradientColor[1]] })"
  62. />
  63. </template>
  64. <ColorButton :color="background.gradientColor[0]" style="flex: 3;" />
  65. </Popover>
  66. </div>
  67. <div class="row">
  68. <div style="flex: 2;">终点颜色:</div>
  69. <Popover trigger="click">
  70. <template #content>
  71. <ColorPicker
  72. :modelValue="background.gradientColor[1]"
  73. @update:modelValue="value => updateBackground({ gradientColor: [background.gradientColor[0], value] })"
  74. />
  75. </template>
  76. <ColorButton :color="background.gradientColor[1]" style="flex: 3;" />
  77. </Popover>
  78. </div>
  79. <div class="row" v-if="background.gradientType === 'linear'">
  80. <div style="flex: 2;">渐变角度:</div>
  81. <Slider
  82. :min="0"
  83. :max="360"
  84. :step="15"
  85. :value="background.gradientRotate"
  86. style="flex: 3;"
  87. @change="value => updateBackground({ gradientRotate: value })"
  88. />
  89. </div>
  90. </div>
  91. <div class="row"><Button style="flex: 1;" @click="applyBackgroundAllSlide()">应用背景到全部</Button></div>
  92. <Divider />
  93. <div class="title">全局主题</div>
  94. <div class="row">
  95. <div style="flex: 2;">字体:</div>
  96. <Select
  97. style="flex: 3;"
  98. :value="theme.fontName"
  99. @change="value => updateTheme({ fontName: value })"
  100. >
  101. <SelectOptGroup label="系统字体">
  102. <SelectOption v-for="font in availableFonts" :key="font.en" :value="font.en">
  103. <span :style="{ fontFamily: font.en }">{{font.zh}}</span>
  104. </SelectOption>
  105. </SelectOptGroup>
  106. <SelectOptGroup label="在线字体">
  107. <SelectOption v-for="font in webFonts" :key="font.name" :value="font.name">
  108. <span>{{font.name}}</span>
  109. </SelectOption>
  110. </SelectOptGroup>
  111. </Select>
  112. </div>
  113. <div class="row">
  114. <div style="flex: 2;">字体颜色:</div>
  115. <Popover trigger="click">
  116. <template #content>
  117. <ColorPicker
  118. :modelValue="theme.fontColor"
  119. @update:modelValue="value => updateTheme({ fontColor: value })"
  120. />
  121. </template>
  122. <ColorButton :color="theme.fontColor" style="flex: 3;" />
  123. </Popover>
  124. </div>
  125. <div class="row">
  126. <div style="flex: 2;">背景颜色:</div>
  127. <Popover trigger="click">
  128. <template #content>
  129. <ColorPicker
  130. :modelValue="theme.backgroundColor"
  131. @update:modelValue="value => updateTheme({ backgroundColor: value })"
  132. />
  133. </template>
  134. <ColorButton :color="theme.backgroundColor" style="flex: 3;" />
  135. </Popover>
  136. </div>
  137. <div class="row">
  138. <div style="flex: 2;">主题色:</div>
  139. <Popover trigger="click">
  140. <template #content>
  141. <ColorPicker
  142. :modelValue="theme.themeColor"
  143. @update:modelValue="value => updateTheme({ themeColor: value })"
  144. />
  145. </template>
  146. <ColorButton :color="theme.themeColor" style="flex: 3;" />
  147. </Popover>
  148. </div>
  149. <div class="title" style="margin-top: 20px;">应用预置主题:</div>
  150. <div class="theme-list">
  151. <div
  152. class="theme-item"
  153. v-for="(item, index) in themes"
  154. :key="index"
  155. :style="{ backgroundColor: item.background }"
  156. @click="updateTheme({
  157. fontColor: item.text,
  158. backgroundColor: item.background,
  159. themeColor: item.color,
  160. })"
  161. >
  162. <div class="theme-item-content">
  163. <div class="text" :style="{ color: item.text }">Aa</div>
  164. <div class="color-block" :style="{ backgroundColor: item.color }"></div>
  165. </div>
  166. </div>
  167. </div>
  168. <div class="row"><Button style="flex: 1;" @click="applyThemeAllSlide()">应用主题到全部</Button></div>
  169. </div>
  170. </template>
  171. <script lang="ts">
  172. import { computed, defineComponent } from 'vue'
  173. import { MutationTypes, useStore } from '@/store'
  174. import { Slide, SlideBackground, SlideTheme } from '@/types/slides'
  175. import { PRESET_THEMES } from '@/configs/theme'
  176. import { WEB_FONTS } from '@/configs/font'
  177. import useHistorySnapshot from '@/hooks/useHistorySnapshot'
  178. import ColorButton from './common/ColorButton.vue'
  179. import { getImageDataURL } from '@/utils/image'
  180. const themes = PRESET_THEMES
  181. const webFonts = WEB_FONTS
  182. export default defineComponent({
  183. name: 'slide-style-panel',
  184. components: {
  185. ColorButton,
  186. },
  187. setup() {
  188. const store = useStore()
  189. const slides = computed(() => store.state.slides)
  190. const theme = computed(() => store.state.theme)
  191. const currentSlide = computed<Slide>(() => store.getters.currentSlide)
  192. const availableFonts = computed(() => store.state.availableFonts)
  193. const background = computed(() => {
  194. if(!currentSlide.value.background) {
  195. return {
  196. type: 'solid',
  197. value: '#fff',
  198. } as SlideBackground
  199. }
  200. return currentSlide.value.background
  201. })
  202. const { addHistorySnapshot } = useHistorySnapshot()
  203. const updateBackgroundType = (type: 'solid' | 'image' | 'gradient') => {
  204. if(type === 'solid') {
  205. const newBackground: SlideBackground = {
  206. ...background.value,
  207. type: 'solid',
  208. color: background.value.color || '#fff',
  209. }
  210. store.commit(MutationTypes.UPDATE_SLIDE, { background: newBackground })
  211. }
  212. else if(type === 'image') {
  213. const newBackground: SlideBackground = {
  214. ...background.value,
  215. type: 'image',
  216. image: background.value.image || '',
  217. imageSize: background.value.imageSize || 'cover',
  218. }
  219. store.commit(MutationTypes.UPDATE_SLIDE, { background: newBackground })
  220. }
  221. else {
  222. const newBackground: SlideBackground = {
  223. ...background.value,
  224. type: 'gradient',
  225. gradientType: background.value.gradientType || 'linear',
  226. gradientColor: background.value.gradientColor || ['#fff', '#fff'],
  227. gradientRotate: background.value.gradientRotate || 0,
  228. }
  229. store.commit(MutationTypes.UPDATE_SLIDE, { background: newBackground })
  230. }
  231. addHistorySnapshot()
  232. }
  233. const updateBackground = (props: Partial<SlideBackground>) => {
  234. store.commit(MutationTypes.UPDATE_SLIDE, { background: { ...background.value, ...props } })
  235. addHistorySnapshot()
  236. }
  237. const uploadBackgroundImage = (files: File[]) => {
  238. const imageFile = files[0]
  239. if(!imageFile) return
  240. getImageDataURL(imageFile).then(dataURL => updateBackground({ image: dataURL }))
  241. }
  242. const applyBackgroundAllSlide = () => {
  243. const newSlides = slides.value.map(slide => {
  244. return {
  245. ...slide,
  246. background: currentSlide.value.background,
  247. }
  248. })
  249. store.commit(MutationTypes.SET_SLIDES, newSlides)
  250. addHistorySnapshot()
  251. }
  252. const updateTheme = (themeProps: Partial<SlideTheme>) => {
  253. store.commit(MutationTypes.SET_THEME, themeProps)
  254. }
  255. const applyThemeAllSlide = () => {
  256. const newSlides: Slide[] = JSON.parse(JSON.stringify(slides.value))
  257. const { themeColor, backgroundColor, fontColor } = theme.value
  258. for(const slide of newSlides) {
  259. if(!slide.background || slide.background.type !== 'image') {
  260. slide.background = {
  261. ...slide.background,
  262. type: 'solid',
  263. color: backgroundColor
  264. }
  265. }
  266. const elements = slide.elements
  267. for(const el of elements) {
  268. if(el.type === 'shape') el.fill = themeColor
  269. else if(el.type === 'line') el.color = themeColor
  270. else if(el.type === 'text') {
  271. if(el.fill) el.fill = themeColor
  272. }
  273. else if(el.type === 'table') {
  274. if(el.theme) el.theme.color = themeColor
  275. }
  276. else if(el.type === 'chart') {
  277. el.themeColor = themeColor
  278. el.gridColor = fontColor
  279. }
  280. }
  281. }
  282. store.commit(MutationTypes.SET_SLIDES, newSlides)
  283. addHistorySnapshot()
  284. }
  285. return {
  286. availableFonts,
  287. background,
  288. updateBackgroundType,
  289. updateBackground,
  290. uploadBackgroundImage,
  291. applyBackgroundAllSlide,
  292. themes,
  293. theme,
  294. webFonts,
  295. updateTheme,
  296. applyThemeAllSlide,
  297. }
  298. },
  299. })
  300. </script>
  301. <style lang="scss" scoped>
  302. .row {
  303. width: 100%;
  304. display: flex;
  305. align-items: center;
  306. margin-bottom: 10px;
  307. }
  308. .title {
  309. margin-bottom: 10px;
  310. }
  311. .background-image-wrapper {
  312. margin-bottom: 10px;
  313. }
  314. .background-image {
  315. height: 0;
  316. padding-bottom: 56.25%;
  317. border: 1px dashed $borderColor;
  318. border-radius: $borderRadius;
  319. position: relative;
  320. transition: all .2s;
  321. &:hover {
  322. border-color: $themeColor;
  323. color: $themeColor;
  324. }
  325. .content {
  326. position: absolute;
  327. top: 0;
  328. bottom: 0;
  329. left: 0;
  330. right: 0;
  331. display: flex;
  332. justify-content: center;
  333. align-items: center;
  334. background-position: center;
  335. background-size: contain;
  336. background-repeat: no-repeat;
  337. cursor: pointer;
  338. }
  339. }
  340. .theme-list {
  341. @include grid-layout-wrapper();
  342. }
  343. .theme-item {
  344. @include grid-layout-item(4, 22%);
  345. padding-bottom: 22%;
  346. border-radius: $borderRadius;
  347. position: relative;
  348. cursor: pointer;
  349. .theme-item-content {
  350. position: absolute;
  351. top: 0;
  352. bottom: 0;
  353. left: 0;
  354. right: 0;
  355. display: flex;
  356. flex-direction: column;
  357. justify-content: center;
  358. align-items: center;
  359. transition: box-shadow .2s;
  360. &:hover {
  361. box-shadow: 0 0 4px #888;
  362. }
  363. }
  364. .text {
  365. font-size: 16px;
  366. }
  367. .color-block {
  368. width: 28px;
  369. height: 10px;
  370. margin-top: 5px;
  371. }
  372. }
  373. </style>