mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-22 14:39:34 +01:00
feat: add tag deletion
This commit is contained in:
@@ -78,4 +78,14 @@ describe.each([
|
|||||||
tag = db[collection].tag(tag.id);
|
tag = db[collection].tag(tag.id);
|
||||||
expect(tag.alias).toBe(value + "-new");
|
expect(tag.alias).toBe(value + "-new");
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
test(`remove a ${action}`, () =>
|
||||||
|
noteTest().then(async ({ db, id }) => {
|
||||||
|
let note = db.notes.note(id);
|
||||||
|
await note[action](value);
|
||||||
|
|
||||||
|
let tag = db[collection].tag(value);
|
||||||
|
await db[collection].remove(tag.id);
|
||||||
|
expect(db[collection].tag(value)).toBeUndefined();
|
||||||
|
}));
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ export default class Notes extends Collection {
|
|||||||
}
|
}
|
||||||
if (!!oldNote.tags) {
|
if (!!oldNote.tags) {
|
||||||
for (let tag of oldNote.tags) {
|
for (let tag of oldNote.tags) {
|
||||||
await this._db.tags.remove(tag, id);
|
await this._db.tags.untag(tag, id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -285,7 +285,7 @@ export default class Notes extends Collection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (let tag of item.tags) {
|
for (let tag of item.tags) {
|
||||||
await this._db.tags.remove(tag, id);
|
await this._db.tags.untag(tag, id);
|
||||||
}
|
}
|
||||||
if (item.data.color) {
|
if (item.data.color) {
|
||||||
await this._db.colors.remove(item.data.color, id);
|
await this._db.colors.remove(item.data.color, id);
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import Collection from "./collection";
|
import Collection from "./collection";
|
||||||
import { qclone } from "qclone";
|
import { qclone } from "qclone";
|
||||||
import { makeId } from "../utils/id";
|
import { makeId } from "../utils/id";
|
||||||
import { deleteItem } from "../utils/array";
|
import { deleteItem, deleteItems } from "../utils/array";
|
||||||
|
import setManipulator from "../utils/set";
|
||||||
|
|
||||||
export default class Tags extends Collection {
|
export default class Tags extends Collection {
|
||||||
tag(id) {
|
tag(id) {
|
||||||
@@ -9,31 +10,29 @@ export default class Tags extends Collection {
|
|||||||
return tagItem;
|
return tagItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
async add(tagId, noteId) {
|
async add(tagId, ...noteIds) {
|
||||||
if (tagId.id || tagId.title) {
|
if (!tagId) {
|
||||||
tagId = tagId.id;
|
console.error("tagId cannot be undefined.");
|
||||||
}
|
|
||||||
|
|
||||||
if (!tagId || !noteId) {
|
|
||||||
console.error("tagId and noteId cannot be falsy.");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let tag = this.all.find((t) => t.id === tagId || t.title === tagId) || {
|
if (typeof tagId === "object") {
|
||||||
|
tagId = tagId.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
let tag = this.tag(tagId) || {
|
||||||
title: tagId,
|
title: tagId,
|
||||||
};
|
};
|
||||||
|
|
||||||
let id = tag.id || makeId(tag.title);
|
let id = tag.id || makeId(tag.title);
|
||||||
let notes = tag.noteIds || [];
|
let notes = tag.noteIds || [];
|
||||||
|
|
||||||
if (notes.find((id) => id === noteId)) return id;
|
|
||||||
|
|
||||||
tag = {
|
tag = {
|
||||||
type: "tag",
|
type: "tag",
|
||||||
alias: tag.title,
|
alias: tag.title,
|
||||||
id,
|
id,
|
||||||
title: tag.title,
|
title: tag.title,
|
||||||
noteIds: [...notes, noteId],
|
noteIds: setManipulator.union(notes, noteIds),
|
||||||
};
|
};
|
||||||
|
|
||||||
await this._collection.addItem(tag);
|
await this._collection.addItem(tag);
|
||||||
@@ -41,9 +40,9 @@ export default class Tags extends Collection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async rename(tagId, newName) {
|
async rename(tagId, newName) {
|
||||||
let tag = this.all.find((t) => t.id === tagId);
|
let tag = this.tag(tagId);
|
||||||
if (!tag) {
|
if (!tag) {
|
||||||
console.error(`No such tag found. Tag id:`, tagId);
|
console.error(`No tag found. Tag id:`, tagId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
tag.alias = newName;
|
tag.alias = newName;
|
||||||
@@ -58,26 +57,29 @@ export default class Tags extends Collection {
|
|||||||
return this._collection.getItems();
|
return this._collection.getItems();
|
||||||
}
|
}
|
||||||
|
|
||||||
async remove(tagTitle, noteId) {
|
async remove(tagId) {
|
||||||
if (!tagTitle || !noteId) {
|
let tag = this.tag(tagId);
|
||||||
console.error(
|
|
||||||
"tag title and noteId cannot be undefined.",
|
|
||||||
tagTitle,
|
|
||||||
noteId
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let tag = this.all.find((t) => t.title === tagTitle || t.id === tagTitle);
|
|
||||||
if (!tag) {
|
if (!tag) {
|
||||||
console.error(`No such tag found. Tag title:`, tagTitle);
|
console.error(`No tag found. Tag id:`, tagId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (let noteId of tag.noteIds) {
|
||||||
|
const note = this._db.notes.note(noteId);
|
||||||
|
if (!note) continue;
|
||||||
|
await note.untag(tagId);
|
||||||
|
}
|
||||||
|
await this._db.settings.unpin(tagId);
|
||||||
|
await this._collection.deleteItem(tagId);
|
||||||
|
}
|
||||||
|
|
||||||
|
async untag(tagId, ...noteIds) {
|
||||||
|
let tag = this.tag(tagId);
|
||||||
|
if (!tag) {
|
||||||
|
console.error(`No such tag found. Tag title:`, tagId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
tag = qclone(tag);
|
deleteItems(tag.noteIds, ...noteIds);
|
||||||
|
|
||||||
if (!deleteItem(tag.noteIds, noteId))
|
|
||||||
console.error(`No such note exists in tag.`, tag.id, noteId);
|
|
||||||
|
|
||||||
if (tag.noteIds.length > 0) await this._collection.addItem(tag);
|
if (tag.noteIds.length > 0) await this._collection.addItem(tag);
|
||||||
else {
|
else {
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ import EventManager from "./utils/event-manager";
|
|||||||
export const EV = new EventManager();
|
export const EV = new EventManager();
|
||||||
|
|
||||||
export async function sendCheckUserStatusEvent(type) {
|
export async function sendCheckUserStatusEvent(type) {
|
||||||
|
if (process.env.NODE_ENV === "testing") return true;
|
||||||
|
|
||||||
const results = await EV.publishWithResult(EVENTS.userCheckStatus, type);
|
const results = await EV.publishWithResult(EVENTS.userCheckStatus, type);
|
||||||
if (typeof results === "boolean") return results;
|
if (typeof results === "boolean") return results;
|
||||||
return results.some((r) => r.type === type && r.result === true);
|
return results.some((r) => r.type === type && r.result === true);
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
var nodeCrypto = require("crypto");
|
var nodeCrypto = require("crypto");
|
||||||
var jsdom = require("jsdom");
|
var jsdom = require("jsdom");
|
||||||
|
|
||||||
|
process.env.NODE_ENV = "testing";
|
||||||
|
|
||||||
const dom = new jsdom.JSDOM("", {});
|
const dom = new jsdom.JSDOM("", {});
|
||||||
global.window = dom.window;
|
global.window = dom.window;
|
||||||
global.document = dom.window.document;
|
global.document = dom.window.document;
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ export default class Note {
|
|||||||
|
|
||||||
async uncolor() {
|
async uncolor() {
|
||||||
if (!this._note.color) return;
|
if (!this._note.color) return;
|
||||||
await this._db.colors.remove(this._note.color, this._note.id);
|
await this._db.colors.untag(this._note.color, this._note.id);
|
||||||
await this._db.notes._collection.addItem({
|
await this._db.notes._collection.addItem({
|
||||||
...this._note,
|
...this._note,
|
||||||
color: undefined,
|
color: undefined,
|
||||||
@@ -136,7 +136,8 @@ export default class Note {
|
|||||||
|
|
||||||
async function addTag(tag) {
|
async function addTag(tag) {
|
||||||
if (this._note.tags.indexOf(tag) > -1)
|
if (this._note.tags.indexOf(tag) > -1)
|
||||||
throw new Error("Cannot add a duplicate tag.");
|
return console.error("Cannot add a duplicate tag.");
|
||||||
|
|
||||||
let tagItem = await this._db.tags.add(tag, this._note.id);
|
let tagItem = await this._db.tags.add(tag, this._note.id);
|
||||||
let arr = [...this._note.tags, tagItem.title];
|
let arr = [...this._note.tags, tagItem.title];
|
||||||
const note = { ...this._note, tags: arr };
|
const note = { ...this._note, tags: arr };
|
||||||
@@ -145,10 +146,11 @@ async function addTag(tag) {
|
|||||||
|
|
||||||
async function removeTag(tag) {
|
async function removeTag(tag) {
|
||||||
if (this._note.tags.indexOf(tag) <= -1)
|
if (this._note.tags.indexOf(tag) <= -1)
|
||||||
throw new Error("This note is not tagged by the specified tag.");
|
return console.error("This note is not tagged by the specified tag.");
|
||||||
|
|
||||||
let arr = [...this._note.tags];
|
let arr = [...this._note.tags];
|
||||||
arr.splice(arr.indexOf(tag), 1);
|
arr.splice(arr.indexOf(tag), 1);
|
||||||
const note = { ...this._note, tags: arr };
|
const note = { ...this._note, tags: arr };
|
||||||
await this._db.tags.remove(tag, note.id);
|
await this._db.tags.untag(tag, note.id);
|
||||||
await this._db.notes._collection.addItem(note);
|
await this._db.notes._collection.addItem(note);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,12 @@ export function deleteItem(array, item) {
|
|||||||
return deleteAtIndex(array, array.indexOf(item));
|
return deleteAtIndex(array, array.indexOf(item));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function deleteItems(array, ...items) {
|
||||||
|
for (let item of items) {
|
||||||
|
deleteItem(array, item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function findById(array, id) {
|
export function findById(array, id) {
|
||||||
return array.find((item) => item.id === id);
|
return array.find((item) => item.id === id);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user