A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
web: add support for setting a tag as default (#8060)
01zulfi4028a29bdbd6
diff --git a/apps/web/__e2e__/models/item.model.ts b/apps/web/__e2e__/models/item.model.tsindex b9a29aa48..a9bfe2a36 100644--- a/apps/web/__e2e__/models/item.model.ts+++ b/apps/web/__e2e__/models/item.model.ts@@ -84,4 +84,9 @@ export class ItemModel extends BaseItemModel {await this.contextMenu.close();return state;}++ async setAsDefault() {+ await this.contextMenu.open(this.locator);+ await this.contextMenu.clickOnItem("set-as-default");+ }}diff --git a/apps/web/__e2e__/tags.test.ts b/apps/web/__e2e__/tags.test.tsindex bac1cf91a..60098d4d0 100644--- a/apps/web/__e2e__/tags.test.ts+++ b/apps/web/__e2e__/tags.test.ts@@ -20,7 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.import { test, expect } from "@playwright/test";import { AppModel } from "./models/app.model";import { Item } from "./models/types";-import { groupByOptions, NOTE, orderByOptions, sortByOptions } from "./utils";+import { NOTE, orderByOptions, sortByOptions } from "./utils";const TAG: Item = { title: "hello-world" };const EDITED_TAG: Item = { title: "hello-world-2" };@@ -236,3 +236,84 @@ test("creating more than 5 tags shouldn't be possible on basic plan", async ({]);expect(result).toBe(true);});++test("when default tag is set, created note in notes context should have default tag", async ({+ page+}) => {+ const app = new AppModel(page);+ await app.goto();+ let tags = await app.goToTags();+ let tag = await tags.createItem(TAG);+ await tag?.setAsDefault();++ const notes = await app.goToNotes();+ await notes?.createNote(NOTE);+ tags = await app.goToTags();+ tag = await tags.findItem(TAG);+ const openedTag = await tag?.open();++ expect(await openedTag?.findNote(NOTE)).toBeDefined();+});++test("when default tag is set, created note in other tag's context should not have default tag", async ({+ page+}) => {+ const app = new AppModel(page);+ await app.goto();+ let tags = await app.goToTags();+ let tag = await tags.createItem(TAG);+ await tag?.setAsDefault();++ const otherTag = await tags.createItem({+ title: "Other Tag"+ });+ const openedOtherTag = await otherTag?.open();+ await openedOtherTag?.createNote(NOTE);+ tags = await app.goToTags();+ tag = await tags.findItem(TAG);+ const openedTag = await tag?.open();++ expect(await openedTag?.findNote(NOTE)).toBeUndefined();+});++test("when default tag is set, created note in notebooks context should have default tag", async ({+ page+}) => {+ const app = new AppModel(page);+ await app.goto();+ let tags = await app.goToTags();+ let tag = await tags.createItem(TAG);+ await tag?.setAsDefault();++ const notebooks = await app.goToNotebooks();+ const notebook = await notebooks.createNotebook({ title: "Test Notebook" });+ const openedNotebook = await notebook?.openNotebook();+ await openedNotebook?.createNote(NOTE);+ tags = await app.goToTags();+ tag = await tags.findItem(TAG);+ const openedTag = await tag?.open();++ expect(await openedTag?.findNote(NOTE)).toBeDefined();+});++test("when default tag is set, created note in colors context should have default tag", async ({+ page+}) => {+ const coloredNote = { title: "Red note", content: NOTE.content };+ const app = new AppModel(page);+ await app.goto();+ let tags = await app.goToTags();+ let tag = await tags.createItem(TAG);+ await tag?.setAsDefault();++ const notes = await app.goToNotes();+ const note = await notes.createNote(NOTE);+ await note?.contextMenu.newColor({ color: "#ff0000", title: "red" });+ const color = await app.goToColor("red");+ await color?.createNote(coloredNote);+ tags = await app.goToTags();+ tag = await tags.findItem(TAG);+ const openedTag = await tag?.open();++ expect(await openedTag?.findNote(coloredNote)).toBeDefined();+});diff --git a/apps/web/src/components/tag/index.tsx b/apps/web/src/components/tag/index.tsxindex b5446fef7..8bbdd6aae 100644--- a/apps/web/src/components/tag/index.tsx+++ b/apps/web/src/components/tag/index.tsx@@ -105,6 +105,7 @@ export const tagMenuItems: (tag: TagType, ids?: string[]) => MenuItem[] = (tag,ids = []) => {+ const defaultTag = db.settings.getDefaultTag();return [{type: "button",@@ -113,6 +114,18 @@ export const tagMenuItems: (tag: TagType, ids?: string[]) => MenuItem[] = (icon: Edit.path,onClick: () => EditTagDialog.show(tag)},+ {+ type: "button",+ key: "set-as-default",+ title: strings.setAsDefault(),+ isChecked: defaultTag === tag.id,+ icon: TagIcon.path,+ onClick: async () => {+ const defaultTag = db.settings.getDefaultTag();+ const isDefault = defaultTag === tag.id;+ await db.settings.setDefaultTag(isDefault ? undefined : tag.id);+ }+ },{type: "lazy-loader",key: "sidebar-items-loader",diff --git a/apps/web/src/stores/editor-store.ts b/apps/web/src/stores/editor-store.tsindex d7f563a8d..5117c1966 100644--- a/apps/web/src/stores/editor-store.ts+++ b/apps/web/src/stores/editor-store.ts@@ -52,6 +52,7 @@ import { AppEventManager, AppEvents } from "../common/app-events";import Vault from "../common/vault";import { Mutex } from "async-mutex";import { useEditorManager } from "../components/editor/manager";+import { Context } from "../components/list-container/types";export enum SaveState {NotSaved = -1,@@ -1007,23 +1008,9 @@ class EditorStore extends BaseStore<EditorStore> {if (currentSession.type === "new") {const context = useNoteStore.getState().context;- const defaultNotebook = db.settings.getDefaultNotebook();- if (context) {- const { type } = context;- if (type === "notebook")- await db.notes.addToNotebook(context.id, note.id);- else if (type === "color" || type === "tag") {- await db.relations.add(- { type, id: context.id },- { id: note.id, type: "note" }- );- if (defaultNotebook)- await db.notes.addToNotebook(defaultNotebook, note.id);- }- } else {- if (defaultNotebook)- await db.notes.addToNotebook(defaultNotebook, note.id);- }+ await addNotebook(note, context);+ await addTag(note, context);+ await addColor(note, context);}const attachmentsLength = await db.attachments@@ -1367,3 +1354,36 @@ async function waitForSync() {db.eventManager.subscribe(EVENTS.syncCompleted, resolve, true);});}++async function addNotebook(note: Note, context?: Context) {+ const notebookId =+ context && context.type === "notebook"+ ? context.id+ : db.settings.getDefaultNotebook();+ if (!notebookId) return;++ await db.notes.addToNotebook(notebookId, note.id);+}++async function addTag(note: Note, context?: Context) {+ const tagId =+ context && context.type === "tag"+ ? context.id+ : db.settings.getDefaultTag();+ if (!tagId) return;++ await db.relations.add(+ { type: "tag", id: tagId },+ { type: "note", id: note.id }+ );+}++async function addColor(note: Note, context?: Context) {+ const colorId = context && context.type === "color" ? context.id : undefined;+ if (!colorId) return;++ await db.relations.add(+ { type: "color", id: colorId },+ { type: "note", id: note.id }+ );+}diff --git a/packages/core/src/collections/settings.ts b/packages/core/src/collections/settings.tsindex dda54f8ff..e975c3d7f 100644--- a/packages/core/src/collections/settings.ts+++ b/packages/core/src/collections/settings.ts@@ -56,6 +56,7 @@ const defaultSettings: SettingItemMap = {dateFormat: "DD-MM-YYYY",titleFormat: "Note $date$ $time$",defaultNotebook: undefined,+ defaultTag: undefined,trashCleanupInterval: 7,profile: undefined,@@ -171,6 +172,14 @@ export class Settings implements ICollection {return this.get("defaultNotebook");}+ setDefaultTag(item: string | undefined) {+ return this.set("defaultTag", item);+ }++ getDefaultTag() {+ return this.get("defaultTag");+ }+setTitleFormat(format: string) {return this.set("titleFormat", format);}diff --git a/packages/core/src/types.ts b/packages/core/src/types.tsindex 2f4fdf3a0..ec38b1e7c 100644--- a/packages/core/src/types.ts+++ b/packages/core/src/types.ts@@ -471,6 +471,7 @@ export type SettingItemMap = {timeFormat: TimeFormat;dateFormat: string;defaultNotebook: string | undefined;+ defaultTag: string | undefined;profile: Profile | undefined;} & Record<`groupOptions:${GroupingKey}`, GroupOptions> &Record<`toolbarConfig:${ToolbarConfigPlatforms}`, ToolbarConfig | undefined> &