mutations.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. console.log(data)
  42. if (data instanceof Array) {
  43. // 该页元素
  44. data.forEach(v => {
  45. v['playing'] = false
  46. })
  47. } else if (data instanceof Object) {
  48. // 单个元素
  49. data['playing'] = false
  50. } else {
  51. // 不传参情况
  52. state['editorPage']['elements'].forEach(v => {
  53. v['playing'] = false
  54. })
  55. }
  56. },
  57. [types.ADD_PAGE] (state, page) {
  58. state.editorTheme.pages.push(page)
  59. },
  60. [types.DELETE_PAGE] (state, data) {
  61. state.editorTheme.pages.findIndex((value, index, arr) => {
  62. if (value === data) {
  63. state.editorTheme.pages.splice(index, 1)
  64. }
  65. })
  66. },
  67. [types.SET_CUR_EDITOR_PAGE] (state, data) {
  68. state.editorPage = data
  69. },
  70. [types.GET_USER_THEME_LIST] (state, data) {
  71. state.themeList = data
  72. },
  73. [types.SET_CUR_EDITOR_THEME] (state, data) {
  74. state.editorTheme = data
  75. },
  76. [types.UPDATE_THEME_DES] (state, {title, description, canvasHeight}) {
  77. state.editorTheme.title = title
  78. state.editorTheme.description = description
  79. state.editorTheme.canvasHeight = canvasHeight
  80. },
  81. [types.DELETE_ELEMENT] (state, data) {
  82. state.editorPage.elements.findIndex((value, index, arr) => {
  83. if (value === data) {
  84. state.editorPage.elements.splice(index, 1)
  85. state.editorElement = null
  86. }
  87. })
  88. },
  89. [types.CREATE_THEME] (state, data) {
  90. state.themeList.push(data)
  91. },
  92. [types.ADD_THEME_SUCCESS] (state, data) {
  93. state.editorTheme._id = data._id
  94. },
  95. [types.UPDATE_THEME_SUCCESS] (state, data) {
  96. console.log('成功!!!!!')
  97. },
  98. [types.SAVE_PIC] (state, data) {
  99. state.editorElement.imgSrc = app.APP_MALL_API_URL + data.filePath
  100. },
  101. [types.GET_PAGE_THEMEID] (state, data) {
  102. state.editorPage = data
  103. },
  104. [types.CLEAN_BG] (state) {
  105. state.editorPage.elements.findIndex((value, index, arr) => {
  106. if (value && value.type === 'bg') {
  107. state.editorPage.elements.splice(index, 1)
  108. }
  109. })
  110. },
  111. [types.CLEAN_ELE] (state, ele) {
  112. state.editorPage.elements.splice(state.editorPage.elements.indexOf(ele), 1)
  113. },
  114. [types.FETCH_PIC_LIST] (state, picList) {
  115. state.picList = picList
  116. },
  117. [types.PUSH_PIC_LIST] (state, ele) {
  118. state.picList.push(ele)
  119. },
  120. [types.CLEAN_PIC_LIST] (state) {
  121. state.picList = []
  122. },
  123. [types.SORTELEMENTS] (state, data) {
  124. let element = state.editorPage.elements[data.start]
  125. let end = parseInt(data.end)
  126. if (end !== -1) {
  127. state.editorPage.elements.splice(data.start, 1)
  128. if (end >= state.editorPage.elements.length) {
  129. state.editorPage.elements.push(element)
  130. } else {
  131. state.editorPage.elements.splice(end, 0, element)
  132. }
  133. state.editorPage.elements.map((value, index, arr) => {
  134. value.zindex = index + 1
  135. })
  136. }
  137. },
  138. [types.DELETE_THEME] (state, data) {
  139. state.themeList.findIndex((value, index, arr) => {
  140. if (value === data) {
  141. state.themeList.splice(index, 1)
  142. }
  143. })
  144. },
  145. [types.SORTELEMENTS_BY_ZINDEX] (state, data) {
  146. state.editorPage.elements.sort((a, b) => a['zindex'] - b['zindex'])
  147. state.editorPage.elements.forEach((v, i, arr) => {
  148. arr[i]['zindex'] = i + 1
  149. })
  150. }
  151. }
  152. export default mutations