editor.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import {
  2. INIT_BY_STATE,
  3. INIT_BY_DEFAULT,
  4. ADD_ITEM_AFTER,
  5. MODIFY_ITEM,
  6. DELETE_ITEM,
  7. SET_FOCUS,
  8. SET_FOCUS_BEFORE,
  9. SET_FOCUS_AFTER,
  10. USE_FOCUS,
  11. GET_SLIDE,
  12. CREATE_SLIDE
  13. } from '../types/editor-types'
  14. import uuid from 'uuid'
  15. import {
  16. getSlide,
  17. createSlide
  18. } from '@/server/slide'
  19. export default {
  20. state: {
  21. slideInfo: {},
  22. items: [],
  23. focus: null
  24. },
  25. mutations: {
  26. [INIT_BY_STATE](state, payload) {},
  27. [INIT_BY_DEFAULT](state, payload) {
  28. const newItemId = uuid.v4()
  29. state.items = [{
  30. id: newItemId,
  31. placeholder: '未编辑文档标题',
  32. content: '# '
  33. }]
  34. state.focus = {
  35. focusId: newItemId,
  36. offset: 0
  37. }
  38. state.slideInfo = { ...payload }
  39. },
  40. [ADD_ITEM_AFTER](state, payload) {
  41. const index = state.items.indexOf(state.items.filter(item => item.id === payload.id)[0])
  42. state.items = [
  43. ...state.items.slice(0, index + 1), {
  44. id: uuid.v4(),
  45. content: '',
  46. placeholder: '正文'
  47. },
  48. ...state.items.slice(index + 1)
  49. ]
  50. },
  51. [MODIFY_ITEM](state, payload) {
  52. state.items = [...state.items.map(item => {
  53. if (item.id === payload.id) {
  54. return {
  55. ...item,
  56. ...payload.diff
  57. }
  58. } else {
  59. return item
  60. }
  61. })]
  62. },
  63. [DELETE_ITEM](state, payload) {
  64. state.items = [...state.items.filter((item, index) => index === 0 || item.id !== payload.id)]
  65. },
  66. [SET_FOCUS](state, payload) {
  67. state.focus = {
  68. ...state.focus,
  69. ...payload.diff
  70. }
  71. },
  72. [SET_FOCUS_AFTER](state, payload) {
  73. let focusId = ''
  74. let tagetedFlag = false
  75. const isHit = state.items.some(item => {
  76. if (tagetedFlag) {
  77. focusId = item.id
  78. return true
  79. }
  80. if (item.id === payload.focusId) {
  81. tagetedFlag = true
  82. }
  83. return false
  84. })
  85. if (isHit) {
  86. state.focus = {
  87. focusId: focusId,
  88. offset: payload.offset ? payload.offset : 0
  89. }
  90. } else {
  91. // todo warn
  92. }
  93. },
  94. [SET_FOCUS_BEFORE](state, payload) {
  95. let focusId = ''
  96. let tagetedFlag = false
  97. const isHit = [...state.items].reverse().some(item => {
  98. if (tagetedFlag) {
  99. focusId = item.id
  100. return true
  101. }
  102. if (item.id === payload.focusId) {
  103. tagetedFlag = true
  104. }
  105. return false
  106. })
  107. if (isHit) {
  108. state.focus = {
  109. focusId: focusId,
  110. offset: payload.offset ? payload.offset : 99999
  111. }
  112. } else {
  113. // todo warn
  114. }
  115. },
  116. [USE_FOCUS](state, payload) {
  117. state.focus = null
  118. }
  119. },
  120. actions: {
  121. [CREATE_SLIDE](state, payload) {
  122. createSlide({
  123. courseId: payload.courseId,
  124. name: payload.name
  125. }).then(res => {
  126. console.log(res)
  127. })
  128. },
  129. [GET_SLIDE](state, payload) {
  130. getSlide(payload.id).then(res => {
  131. console.log(res)
  132. }).catch(err => {
  133. console.log(err)
  134. })
  135. }
  136. }
  137. }