actions.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import * as types from './mutation-type'
  2. import api from '../../api/editor'
  3. import Page from '../../model/Page'
  4. import Theme from '../../model/Theme'
  5. import Element from '../../model/Element'
  6. import tools from '../../util/tools'
  7. import { Loading } from 'element-ui';
  8. /**
  9. * 保存页面数据
  10. */
  11. export const saveTheme = ({ commit }, theme) => {
  12. if (theme && theme._id) {
  13. return Promise.resolve(api.updateTheme(theme).then((res) => {
  14. commit(types.UPDATE_THEME_SUCCESS, res)
  15. window.hasSaveFlag = true;
  16. }))
  17. } else {
  18. return Promise.resolve(api.saveTheme(theme).then((res) => {
  19. commit(types.ADD_THEME_SUCCESS, res)
  20. window.hasSaveFlag = true;
  21. }))
  22. }
  23. }
  24. /**
  25. * 获取用户所有场景主题
  26. * @param commit
  27. */
  28. export const getUserThemeList = ({ commit }, type) => {
  29. api.getUserThemeList(type).then((res) => {
  30. commit(types.GET_USER_THEME_LIST, res)
  31. })
  32. }
  33. /**
  34. * 创建场景主题
  35. * @param commit
  36. */
  37. export const createTheme = ({ commit }, type) => {
  38. var theme = new Theme({ type: type })
  39. commit(types.CREATE_THEME, theme)
  40. commit(types.SET_CUR_EDITOR_THEME, theme)
  41. }
  42. /**
  43. * 设置当前编辑的主题
  44. */
  45. export const setEditorTheme = ({ commit }, theme) => {
  46. var newTheme = new Theme(theme)
  47. commit(types.SET_CUR_EDITOR_THEME, newTheme)
  48. }
  49. /**
  50. * 设置当前正在编辑的页面
  51. * @param commit
  52. * @param page
  53. */
  54. export const setEditorPage = ({ commit }, page) => {
  55. commit(types.SET_CUR_EDITOR_PAGE, page)
  56. }
  57. /**
  58. * 给主题添加页面
  59. * @param commit
  60. */
  61. export const addPage = ({ commit }) => {
  62. var page = new Page()
  63. commit(types.ADD_PAGE, page)
  64. commit(types.SET_CUR_EDITOR_PAGE, page)
  65. }
  66. export const addPagePosition = ({ commit }, position) => {
  67. var page = new Page()
  68. page.position = position
  69. commit(types.ADD_PAGE_POSITION, page)
  70. commit(types.SET_CUR_EDITOR_PAGE, page)
  71. }
  72. /**
  73. * 添加页面元素
  74. */
  75. export const addElement = ({ commit, state }, data) => {
  76. commit(types.ADD_PIC_ELEMENT, new Element(data))
  77. var list = state.editorPage.elements
  78. var lastIndex = list.length - 1
  79. list[lastIndex]['zindex'] = lastIndex ? list[lastIndex - 1]['zindex'] + 1 : 2
  80. commit(types.SET_CUR_EDITOR_ELEMENT, state.editorPage.elements[lastIndex])
  81. }
  82. /**
  83. * 添加背景图片
  84. */
  85. export const addBGElement = ({ commit }, data) => {
  86. var element = new Element(data)
  87. commit(types.SET_BG_ELEMENT, element)
  88. commit(types.SET_CUR_EDITOR_ELEMENT, null)
  89. }
  90. /**
  91. * 保存图片
  92. * @param commit
  93. * @param data
  94. */
  95. export const savePic = ({ commit }, data) => {
  96. commit(types.PUSH_PIC_LIST, data)
  97. }
  98. /**
  99. * 清除背景
  100. * @param commit
  101. */
  102. export const cleanBG = ({ commit }) => {
  103. commit(types.CLEAN_BG)
  104. }
  105. export const cleanEle = ({ commit }, ele) => {
  106. commit(types.CLEAN_ELE, ele)
  107. }
  108. /**
  109. * 复制页面
  110. * @param commit
  111. */
  112. export const copyPage = ({ commit }, data) => {
  113. var page = tools.vue2json(data)
  114. commit(types.ADD_PAGE, page)
  115. }
  116. /**
  117. * 删除页面
  118. * @param commit
  119. */
  120. export const delPage = ({ commit }, page) => {
  121. commit(types.DELETE_PAGE, page)
  122. }
  123. export const getPageByThemeId = ({ dispatch, commit }, id) => {
  124. let loading = Loading.service({
  125. lock: true,
  126. text: '模板加载中...',
  127. spinner: 'el-icon-loading',
  128. background: 'rgba(0, 0, 0, 0.7)'
  129. });
  130. api.getPageByThemeId(id).then((res) => {
  131. console.log("XXXXXXXXXXXXXXX", res);
  132. var single = JSON.parse(res.single.jsonString);
  133. console.log("XXXXXXXXXXXXXXX", single);
  134. single.pages = single.pages || [];
  135. single.backgroundAudio = single.backgroundAudio || '';
  136. single.title = res.single.title || '';
  137. single.resultPageCount = res.single.resultPageCount || 0;
  138. single.shareContent = res.single.shareContent || '';
  139. single.shareImg = res.single.shareImg || '';
  140. single.shareTitle = res.single.shareTitle || '';
  141. single.shareUrl = res.single.shareUrl || '';
  142. single.pages.forEach((page) => {
  143. page.elements.forEach((element) => {
  144. element.animatedFont = element.animatedFont || ''
  145. element.eleCanvas = element.eleCanvas || ''
  146. })
  147. })
  148. commit(types.SET_CUR_EDITOR_THEME, single)
  149. commit(types.SET_CUR_EDITOR_PAGE, single.pages[0])
  150. loading.close()
  151. setTimeout(function () {
  152. window.revocationFlag = true
  153. }, 800)
  154. }).then(() => {
  155. dispatch('sortElementsByZindex')
  156. loading.close()
  157. setTimeout(function () {
  158. window.revocationFlag = true
  159. }, 800)
  160. })
  161. }
  162. export const setEditorElement = ({ commit }, element) => {
  163. if (element && element.locked) {
  164. return false
  165. }
  166. commit(types.SET_CUR_EDITOR_ELEMENT, element)
  167. }
  168. // 删除元素
  169. export const deleteElement = ({ commit }, element) => {
  170. commit(types.DELETE_ELEMENT, element)
  171. }
  172. export const deleteSelectedElement = ({ commit, state }) => {
  173. commit(types.DELETE_ELEMENT, state.editorElement)
  174. }
  175. export const playAnimate = ({ state, commit, getters }) => {
  176. commit(types.PLAY_ANIMATE)
  177. let target = getters['editingElement'] || getters['editingPageElements'] || null
  178. let time = 0
  179. if (target instanceof Array) {
  180. target.forEach(v => {
  181. time = v['animatedName'] && (v['duration'] + v['delay']) > time ? (v['duration'] + v['delay']) : time
  182. })
  183. } else if (target instanceof Object) {
  184. time = (target['duration'] + target['delay'])
  185. }
  186. setTimeout(() => {
  187. commit(types.STOP_ANIMATE, target)
  188. }, time * 1000)
  189. }
  190. export const getPicListByThemeId = ({ commit }, _id) => {
  191. api.getPicListByThemeId(_id).then((res) => {
  192. commit(types.FETCH_PIC_LIST, res)
  193. })
  194. }
  195. export const cleanPicList = ({ commit }) => {
  196. commit(types.CLEAN_PIC_LIST)
  197. }
  198. export const sortElementsByZindex = ({ commit }, location) => {
  199. commit(types.SORTELEMENTS_BY_ZINDEX, location)
  200. }
  201. export const deleteTheme = ({ commit }, theme) => {
  202. return Promise.resolve(api.delTheme(theme).then((res) => {
  203. commit(types.DELETE_THEME, theme)
  204. }))
  205. }
  206. export const uploadPsd = ({ commit }, _file) => {
  207. api.uploadPsd(_file).then((res) => {
  208. })
  209. }