sourceStore.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { defineStore } from 'pinia'
  2. import {
  3. emptyBookSource,
  4. emptyRssSource,
  5. getSourceUniqueKey,
  6. convertSourcesToMap,
  7. } from '@utils/souce'
  8. import type { BookSoure, RssSource, Source } from '@/source'
  9. const isBookSource = /bookSource/i.test(location.href)
  10. const emptySource = isBookSource ? emptyBookSource : emptyRssSource
  11. export const useSourceStore = defineStore('source', {
  12. state: () => {
  13. return {
  14. bookSources: shallowRef([] as BookSoure[]), // 临时存放所有书源,
  15. rssSources: shallowRef([] as RssSource[]), // 临时存放所有订阅源
  16. savedSources: [] as Source[], // 批量保存到阅读app成功的源
  17. currentSource: JSON.parse(JSON.stringify(emptySource)) as Source, // 当前编辑的源
  18. currentTab: localStorage.getItem('tabName') || 'editTab',
  19. editTabSource: {} as Source, // 生成序列化的json数据
  20. isDebuging: false,
  21. }
  22. },
  23. getters: {
  24. sources: (state): Source[] =>
  25. isBookSource ? state.bookSources : state.rssSources,
  26. sourcesMap: function (): Map<string, Source> {
  27. return convertSourcesToMap(this.sources)
  28. },
  29. savedSourcesMap: (state): Map<string, Source> =>
  30. convertSourcesToMap(state.savedSources),
  31. currentSourceUrl: state =>
  32. isBookSource
  33. ? (state.currentSource as BookSoure).bookSourceUrl
  34. : (state.currentSource as RssSource).sourceUrl,
  35. searchKey: (state): string =>
  36. isBookSource
  37. ? (state.currentSource as BookSoure)?.ruleSearch?.checkKeyWord || '我的'
  38. : '',
  39. },
  40. actions: {
  41. startDebug() {
  42. this.currentTab = 'editDebug'
  43. this.isDebuging = true
  44. },
  45. debugFinish() {
  46. this.isDebuging = false
  47. },
  48. //拉取源后保存
  49. saveSources(data: Source[]) {
  50. if (isBookSource) {
  51. this.bookSources = markRaw(data) as BookSoure[]
  52. } else {
  53. this.rssSources = markRaw(data) as RssSource[]
  54. }
  55. },
  56. //批量推送
  57. setPushReturnSources(returnSoures: Source[]) {
  58. this.savedSources = returnSoures
  59. },
  60. //删除源
  61. deleteSources(data: Source[]) {
  62. const sources: Source[] = isBookSource
  63. ? this.bookSources
  64. : this.rssSources
  65. data.forEach(source => {
  66. const index = sources.indexOf(source)
  67. if (index > -1) sources.splice(index, 1)
  68. })
  69. },
  70. //保存当前编辑源
  71. saveCurrentSource() {
  72. const source = this.currentSource,
  73. map = this.sourcesMap
  74. map.set(getSourceUniqueKey(source), JSON.parse(JSON.stringify(source)))
  75. this.saveSources(Array.from(map.values()))
  76. },
  77. // 更改当前编辑的源qq
  78. changeCurrentSource(source: Source) {
  79. this.currentSource = JSON.parse(JSON.stringify(source))
  80. },
  81. // update editTab tabName and editTab info
  82. changeTabName(tabName: string) {
  83. this.currentTab = tabName
  84. localStorage.setItem('tabName', tabName)
  85. },
  86. changeEditTabSource(source: Source) {
  87. this.editTabSource = JSON.parse(JSON.stringify(source))
  88. },
  89. editHistory(history: Source) {
  90. let historyObj
  91. if (localStorage.getItem('history')) {
  92. historyObj = JSON.parse(localStorage.getItem('history')!)
  93. historyObj.new.push(history)
  94. if (historyObj.new.length > 50) {
  95. historyObj.new.shift()
  96. }
  97. if (historyObj.old.length > 50) {
  98. historyObj.old.shift()
  99. }
  100. localStorage.setItem('history', JSON.stringify(historyObj))
  101. } else {
  102. const arr = { new: [history], old: [] }
  103. localStorage.setItem('history', JSON.stringify(arr))
  104. }
  105. },
  106. editHistoryUndo() {
  107. if (localStorage.getItem('history')) {
  108. const historyObj = JSON.parse(localStorage.getItem('history')!)
  109. historyObj.old.push(this.currentSource)
  110. if (historyObj.new.length) {
  111. this.currentSource = historyObj.new.pop()
  112. }
  113. localStorage.setItem('history', JSON.stringify(historyObj))
  114. }
  115. },
  116. clearAllHistory() {
  117. localStorage.setItem('history', JSON.stringify({ new: [], old: [] }))
  118. },
  119. clearEdit() {
  120. this.editTabSource = {} as Source
  121. this.currentSource = JSON.parse(JSON.stringify(emptySource)) //复制一份新对象
  122. },
  123. // clear all source
  124. clearAllSource() {
  125. this.bookSources = []
  126. this.rssSources = []
  127. this.savedSources = []
  128. },
  129. },
  130. })