diff --git a/package-lock.json b/package-lock.json index 9ce0625..3805449 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10558,6 +10558,11 @@ "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==" }, + "lodash-id": { + "version": "0.14.0", + "resolved": "https://registry.npmjs.org/lodash-id/-/lodash-id-0.14.0.tgz", + "integrity": "sha1-uvSJNOVDobXWNG+MhGmLGoyAOJY=" + }, "lodash._reinterpolate": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz", diff --git a/package.json b/package.json index c2e0594..4ce2fe9 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "dependencies": { "electron-window-state": "^5.0.3", "is-url-relative-without-domain": "^2.0.0", + "lodash-id": "^0.14.0", "lowdb": "^1.0.0", "prism-themes": "^1.0.1", "prismjs": "^1.15.0", diff --git a/src/components/TabMain.vue b/src/components/TabMain.vue index 03aa34d..91dd9e2 100644 --- a/src/components/TabMain.vue +++ b/src/components/TabMain.vue @@ -35,7 +35,7 @@ export default { }, computed: { isActive() { - return parseInt(this.$route.params.id, 0) === this.item.id; + return this.$route.params.id === this.item.id; }, webview() { return this.$refs.webview.$refs.webview; diff --git a/src/db.js b/src/db.js index 187c91b..f891646 100644 --- a/src/db.js +++ b/src/db.js @@ -2,6 +2,7 @@ import fs from 'fs'; import { homedir } from 'os'; import path from 'path'; import lowdb from 'lowdb'; +import lodashId from 'lodash-id'; import FileSync from 'lowdb/adapters/FileSync'; export default class { @@ -30,6 +31,7 @@ export default class { init() { const adapter = new FileSync(path.join(this.options.path, this.options.file)); this.db = lowdb(adapter); + this.db._.mixin(lodashId); this.db.defaults(this.options.defaults).write(); } diff --git a/src/store/modules/Tabs.js b/src/store/modules/Tabs.js index 83b5b57..d961b8d 100644 --- a/src/store/modules/Tabs.js +++ b/src/store/modules/Tabs.js @@ -16,10 +16,10 @@ export default { }, getters: { active({ activeId, dbUpdated }) { - return db(dbUpdated).get('tabs').find(item => item.id === activeId).value() || {}; + return db(dbUpdated).get('tabs').getById(activeId).value() || {}; }, byId({ dbUpdated }) { - return id => db(dbUpdated).get('tabs').find(item => item.id === id).value() || {}; + return id => db(dbUpdated).get('tabs').getById(id).value() || {}; }, list({ dbUpdated }) { return db(dbUpdated) @@ -38,15 +38,15 @@ export default { state.activeId = id; state.dbUpdated = Date.now(); }, - create(state, item) { - db() - .get('tabs') - .push(item) - .write(); - + create(state, tab) { + /* + the actual tab creation is processed in action + because mutations don't return values but we need the id + to forward the user to the newly created tab + */ db() .get('sorting') - .push(item.id) + .push(tab.id) .write(); state.dbUpdated = Date.now(); @@ -54,7 +54,7 @@ export default { delete(state, item) { db() .get('tabs') - .remove(i => i.id === item.id) + .removeById(item.id) .write(); db() @@ -72,21 +72,18 @@ export default { update(state, { id, data }) { db() .get('tabs') - .find(i => i.id === id).assign(data) + .updateById(id, data) .write(); state.dbUpdated = Date.now(); }, }, actions: { - create({ commit }, payload = {}) { - const id = Date.now(); - const item = { - id, - ...payload, - }; - commit('create', item); - return item; + create({ commit }, item) { + const tab = db().get('tabs').insert(item).write(); + + commit('create', tab); + return tab; }, }, };