sourceStore.js 4.0 KB

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