sourceStore.js 4.2 KB

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