From 0a94ee7c6018cd90dd2eb91a4df1114ee5433c9d Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Wed, 12 Oct 2022 10:50:38 +0500 Subject: [PATCH] core: item should only have a single shortcut this changes the shortcut ids & deduplicates the old ones so that there is always only 1 shortcut of a given item (notebook, topic or tag). --- packages/core/collections/shortcuts.js | 31 +++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/packages/core/collections/shortcuts.js b/packages/core/collections/shortcuts.js index 9ac284bee..5891093c0 100644 --- a/packages/core/collections/shortcuts.js +++ b/packages/core/collections/shortcuts.js @@ -18,7 +18,7 @@ along with this program. If not, see . */ import Collection from "./collection"; -import getId from "../utils/id"; +import { EVENTS } from "../common"; /** * @typedef {{ @@ -45,6 +45,11 @@ export default class Shortcuts extends Collection { await this._collection.addItem(shortcut); } + async init() { + await super.init(); + await this.dedupe(); + } + /** * * @param {Partial} shortcut @@ -53,7 +58,9 @@ export default class Shortcuts extends Collection { async add(shortcut) { if (!shortcut) return; if (shortcut.remote) - throw new Error("Please use db.shortcuts.merge to merge remote notes."); + throw new Error( + "Please use db.shortcuts.merge to merge remote shortcuts." + ); if (!ALLOWED_SHORTCUT_TYPES.includes(shortcut.item.type)) throw new Error("Cannot create a shortcut for this type of item."); @@ -63,13 +70,14 @@ export default class Shortcuts extends Collection { : shortcut.id ? this._collection.getItem(shortcut.id) : null; - const id = shortcut.id || (oldShortcut && oldShortcut.id) || getId(); shortcut = { ...oldShortcut, ...shortcut }; + const id = shortcut.id || shortcut.item.id; + shortcut = { id, type: "shortcut", @@ -148,4 +156,21 @@ export default class Shortcuts extends Collection { await this._collection.removeItem(id); } } + + async dedupe() { + const oldShortcuts = this.all.filter( + (shortcut) => shortcut.id !== shortcut.item.id + ); + + for (const shortcut of oldShortcuts) { + await this._collection.addItem({ ...shortcut, id: shortcut.item.id }); + } + + for (const shortcut of oldShortcuts) { + await this._collection.removeItem(shortcut.id); + } + + if (oldShortcuts.length > 0) + this._db.eventManager.publish(EVENTS.appRefreshRequested); + } }