mirror of
https://github.com/streetwriters/notesnook.git
synced 2026-09-01 19:49:54 +02:00
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).
This commit is contained in:
committed by
Abdullah Atta
parent
0e6150a0f5
commit
0a94ee7c60
@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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>} 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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user