Преглед изворни кода

Remember last chosen tab on the settings UI.

This commit adds a UI setting that was accidentally lost from an
earlier PR.

It introduces `$utils.setPref()|getPref()` to save arbitrary key/value
preferences in a JSON blob under the app's namespace in localStorage.
Kailash Nadh пре 3 година
родитељ
комит
93366f4e9e
2 измењених фајлова са 30 додато и 1 уклоњено
  1. 20 0
      frontend/src/utils.js
  2. 10 1
      frontend/src/views/Settings.vue

+ 20 - 0
frontend/src/utils.js

@@ -8,6 +8,7 @@ import relativeTime from 'dayjs/plugin/relativeTime';
 dayjs.extend(relativeTime);
 
 const reEmail = /(.+?)@(.+?)/ig;
+const prefKey = 'listmonk_pref';
 
 const htmlEntities = {
   '&': '&',
@@ -188,4 +189,23 @@ export default class Utils {
 
     return obj;
   };
+
+  getPref = (key) => {
+    if (localStorage.getItem(prefKey) === null) {
+      return null;
+    }
+
+    const p = JSON.parse(localStorage.getItem(prefKey));
+    return key in p ? p[key] : null;
+  };
+
+  setPref = (key, val) => {
+    let p = {};
+    if (localStorage.getItem(prefKey) !== null) {
+      p = JSON.parse(localStorage.getItem(prefKey));
+    }
+
+    p[key] = val;
+    localStorage.setItem(prefKey, JSON.stringify(p));
+  }
 }

+ 10 - 1
frontend/src/views/Settings.vue

@@ -21,7 +21,7 @@
       <hr />
 
       <section class="wrap">
-          <b-tabs type="is-boxed" :animated="false">
+          <b-tabs type="is-boxed" :animated="false" v-model="tab">
             <b-tab-item :label="$t('settings.general.name')" label-position="on-border">
               <general-settings :form="form" :key="key" />
             </b-tab-item><!-- general -->
@@ -98,6 +98,7 @@ export default Vue.extend({
       // form is compared to detect changes.
       formCopy: '',
       form: {},
+      tab: 0,
     };
   },
 
@@ -237,7 +238,15 @@ export default Vue.extend({
   },
 
   mounted() {
+    this.tab = this.$utils.getPref('settings.tab') || 0;
     this.getSettings();
   },
+
+  watch: {
+    // Capture contentType and body passed from the parent as props.
+    tab(t) {
+      this.$utils.setPref('settings.tab', t);
+    },
+  },
 });
 </script>