diff --git a/packages/core/__tests__/tags.test.js b/packages/core/__tests__/tags.test.js index 815ed721c..f760b4747 100644 --- a/packages/core/__tests__/tags.test.js +++ b/packages/core/__tests__/tags.test.js @@ -76,7 +76,7 @@ describe.each([ let tag = db[collection].tag(value); await db[collection].rename(tag.id, value + "-new"); tag = db[collection].tag(tag.id); - expect(tag.alias).toBe(value + "-new"); + expect(db[collection].alias(tag.id)).toBe(value + "-new"); })); test(`remove a ${action}`, () => diff --git a/packages/core/api/settings.js b/packages/core/api/settings.js index d3381c5be..9e0e20348 100644 --- a/packages/core/api/settings.js +++ b/packages/core/api/settings.js @@ -37,6 +37,7 @@ class Settings { ...item.groupOptions, ...this._settings.groupOptions, }; + this._settings.aliases = {}; } else { this._initSettings(item); } @@ -68,6 +69,15 @@ class Settings { ); } + async setAlias(id, name) { + this._settings.aliases[id] = name; + await this._saveSettings(); + } + + getAlias(id) { + return this._settings.aliases[id]; + } + async pin(type, data) { if (type !== "notebook" && type !== "topic" && type !== "tag") throw new Error("This item cannot be pinned."); @@ -120,6 +130,7 @@ class Settings { id: id(), pins: [], groupOptions: {}, + aliases: {}, dateEdited: 0, dateCreated: 0, ...(settings || {}), diff --git a/packages/core/collections/tags.js b/packages/core/collections/tags.js index 33fa612de..ea3d6714f 100644 --- a/packages/core/collections/tags.js +++ b/packages/core/collections/tags.js @@ -19,18 +19,18 @@ export default class Tags extends Collection { title: tagId, }; - let id = tag.id || makeId(tag.title); + let id = tag.id || makeId(tag.title.toLowerCase()); let notes = tag.noteIds || []; tag = { type: "tag", - alias: tag.title, id, title: tag.title, noteIds: setManipulator.union(notes, noteIds), }; await this._collection.addItem(tag); + await this._db.settings.setAlias(tag.id, tag.title); return tag; } @@ -40,8 +40,17 @@ export default class Tags extends Collection { console.error(`No tag found. Tag id:`, tagId); return; } - tag.alias = newName; - await this._collection.updateItem(tag); + await this._db.settings.setAlias(tagId, newName); + } + + alias(tagId) { + let tag = this.tag(tagId); + if (!tag) { + console.error(`No tag found. Tag id:`, tagId); + return; + } + const alias = this._db.settings.getAlias(tagId); + return alias || tag.alias || tag.title; } get raw() {