restoreOldSettings.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. export default async function restoreOldSettings(settings, bookManager, commit) {
  2. const oldSets = localStorage['colorSetting'];
  3. let isOld = false;
  4. for (let i = 0; i < localStorage.length; i++) {
  5. let key = unescape(localStorage.key(i));
  6. if (key.indexOf('bpr-book-') == 0)
  7. isOld = true;
  8. }
  9. if (isOld || oldSets) {
  10. let newSettings = null;
  11. if (oldSets) {
  12. const [textColor, backgroundColor, lineStep, , , statusBarHeight, scInt] = unescape(oldSets).split('|');
  13. const fontSize = Math.round(lineStep*0.8);
  14. const scrollingDelay = fontSize*scInt;
  15. newSettings = Object.assign({}, settings, {
  16. textColor,
  17. backgroundColor,
  18. fontSize,
  19. statusBarHeight: statusBarHeight*1,
  20. scrollingDelay,
  21. });
  22. }
  23. for (let i = 0; i < localStorage.length; i++) {
  24. let key = localStorage.key(i);
  25. if (key.indexOf('bpr-') == 0) {
  26. let v = unescape(localStorage[key]);
  27. key = unescape(key);
  28. if (key.lastIndexOf('=timestamp') == key.length - 10) {
  29. continue;
  30. }
  31. if (key.indexOf('bpr-book-') == 0) {
  32. const url = key.substr(9);
  33. const [scrollTop, scrollHeight, ] = v.split('|');
  34. const bookPosPercent = scrollTop*1/(scrollHeight*1 + 1);
  35. const title = unescape(localStorage[`bpr-title-${escape(url)}`]);
  36. const author = unescape(localStorage[`bpr-author-${escape(url)}`]);
  37. const time = unescape(localStorage[`bpr-book-${escape(url)}=timestamp`]).split(';')[0];
  38. const touchTime = Date.parse(time);
  39. const bookKey = bookManager.keyFromUrl(url);
  40. const recent = await bookManager.getRecentBook({key: bookKey});
  41. if (!recent) {
  42. await bookManager.setRecentBook({
  43. key: bookKey,
  44. touchTime,
  45. bookPosPercent,
  46. url,
  47. fb2: {
  48. bookTitle: title,
  49. lastName: author,
  50. }
  51. }, true);
  52. }
  53. }
  54. }
  55. }
  56. localStorage.clear();
  57. if (oldSets)
  58. commit('reader/setSettings', newSettings);
  59. }
  60. }