mutations.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import * as types from './mutation-type'
  2. import app from '../../util/appConst'
  3. import Element from '../../model/Element'
  4. const mutations = {
  5. [types.SET_CUR_EDITOR_ELEMENT](state, data) {
  6. state.editorElement = data
  7. },
  8. [types.ADD_PIC_ELEMENT](state, data) {
  9. state.editorPage.elements.push(new Element(data))
  10. },
  11. [types.SET_BG_ELEMENT](state, data) {
  12. let haveBG = false
  13. state.editorPage.elements.findIndex((value, index, arr) => {
  14. if (value.type === 'bg') {
  15. haveBG = true
  16. value.imgSrc = data.imgSrc
  17. value.width = data.width
  18. value.height = data.height
  19. }
  20. })
  21. if (!haveBG) {
  22. state.editorPage.elements.push(new Element(data))
  23. }
  24. },
  25. // 播放动画
  26. [types.PLAY_ANIMATE](state) {
  27. let elements = state.editorPage.elements
  28. let editingElement = state.editorElement
  29. if (editingElement && editingElement.animatedName) {
  30. // 如存在有动画的选择元素
  31. editingElement.playing = true
  32. } else if (!editingElement) {
  33. // 不存在被选择的元素
  34. elements.forEach(v => {
  35. v.playing = true
  36. })
  37. }
  38. },
  39. // 停止播放动画
  40. [types.STOP_ANIMATE](state, data) {
  41. if (data instanceof Array) {
  42. // 该页元素
  43. data.forEach(v => {
  44. v['playing'] = false
  45. })
  46. } else if (data instanceof Object) {
  47. // 单个元素
  48. data['playing'] = false
  49. } else {
  50. // 不传参情况
  51. state['editorPage']['elements'].forEach(v => {
  52. v['playing'] = false
  53. })
  54. }
  55. },
  56. [types.ADD_PAGE](state, page) {
  57. state.editorTheme.pages.push(page);
  58. },
  59. [types.ADD_PAGE_POSITION](state, page, position) {
  60. console.log("XXXXXXXXX", position);
  61. state.editorTheme.pages.splice(2, 0, page);
  62. },
  63. [types.DELETE_PAGE](state, data) {
  64. state.editorTheme.pages.findIndex((value, index, arr) => {
  65. if (value === data) {
  66. state.editorTheme.pages.splice(index, 1)
  67. }
  68. })
  69. },
  70. [types.SET_CUR_EDITOR_PAGE](state, data) {
  71. state.editorPage = data
  72. },
  73. [types.GET_USER_THEME_LIST](state, data) {
  74. state.themeList = data
  75. },
  76. [types.SET_CUR_EDITOR_THEME](state, data) {
  77. state.editorTheme = data
  78. },
  79. [types.UPDATE_THEME_DES](state, { title, description, canvasHeight }) {
  80. state.editorTheme.title = title
  81. state.editorTheme.description = description
  82. state.editorTheme.canvasHeight = canvasHeight
  83. },
  84. [types.DELETE_ELEMENT](state, data) {
  85. state.editorPage.elements.findIndex((value, index, arr) => {
  86. if (value === data) {
  87. state.editorPage.elements.splice(index, 1)
  88. state.editorElement = null
  89. }
  90. })
  91. },
  92. [types.CREATE_THEME](state, data) {
  93. state.themeList.push(data)
  94. },
  95. [types.ADD_THEME_SUCCESS](state, data) {
  96. state.editorTheme._id = data._id
  97. },
  98. [types.UPDATE_THEME_SUCCESS](state, data) {
  99. },
  100. [types.SAVE_PIC](state, data) {
  101. state.editorElement.imgSrc = app.APP_MALL_API_URL + data.filePath
  102. },
  103. [types.GET_PAGE_THEMEID](state, data) {
  104. state.editorPage = data
  105. },
  106. [types.CLEAN_BG](state) {
  107. state.editorPage.elements.findIndex((value, index, arr) => {
  108. if (value && value.type === 'bg') {
  109. state.editorPage.elements.splice(index, 1)
  110. }
  111. })
  112. },
  113. [types.CLEAN_ELE](state, ele) {
  114. state.editorPage.elements.splice(state.editorPage.elements.indexOf(ele), 1)
  115. },
  116. [types.FETCH_PIC_LIST](state, picList) {
  117. state.picList = picList
  118. },
  119. [types.PUSH_PIC_LIST](state, ele) {
  120. state.picList.push(ele)
  121. },
  122. [types.CLEAN_PIC_LIST](state) {
  123. state.picList = []
  124. },
  125. [types.SORTELEMENTS](state, data) {
  126. let element = state.editorPage.elements[data.start]
  127. let end = parseInt(data.end)
  128. if (end !== -1) {
  129. state.editorPage.elements.splice(data.start, 1)
  130. if (end >= state.editorPage.elements.length) {
  131. state.editorPage.elements.push(element)
  132. } else {
  133. state.editorPage.elements.splice(end, 0, element)
  134. }
  135. state.editorPage.elements.map((value, index, arr) => {
  136. value.zindex = index + 1
  137. })
  138. }
  139. },
  140. [types.DELETE_THEME](state, data) {
  141. state.themeList.findIndex((value, index, arr) => {
  142. if (value === data) {
  143. state.themeList.splice(index, 1)
  144. }
  145. })
  146. },
  147. [types.SORTELEMENTS_BY_ZINDEX](state, data) {
  148. state.editorPage.elements.sort((a, b) => a['zindex'] - b['zindex'])
  149. state.editorPage.elements.forEach((v, i, arr) => {
  150. arr[i]['zindex'] = i + 1
  151. })
  152. }
  153. }
  154. export default mutations