web: show notebook & tag shortcuts when assigning notes

This commit is contained in:
Abdullah Atta
2023-11-11 14:30:15 +05:00
parent c848718fef
commit 80871ede3b
2 changed files with 121 additions and 50 deletions

View File

@@ -587,7 +587,6 @@ function colorsToMenuItems(
}
function notebooksMenuItems(ids: string[]): MenuItem[] {
console.log("NOTE IDS", ids);
return [
{
type: "button",
@@ -601,26 +600,53 @@ function notebooksMenuItems(ids: string[]): MenuItem[] {
key: "notebooks-lazy-loader",
async items() {
const notebooks: Map<string, NotebookItem> = new Map();
for (const id of ids) {
const linkedNotebooks = await db.relations
.to({ id, type: "note" }, "notebook")
.resolve();
linkedNotebooks.forEach((nb) => notebooks.set(nb.id, nb));
const notebookShortcuts: Map<string, NotebookItem> = new Map();
const linkedNotebooks = await db.relations
.to({ ids, type: "note" }, "notebook")
.resolve();
linkedNotebooks.forEach((nb) => notebooks.set(nb.id, nb));
for (const notebook of await db.shortcuts.resolved("notebooks")) {
if (notebooks.has(notebook.id)) continue;
notebookShortcuts.set(notebook.id, notebook);
}
const menuItems: MenuItem[] = [];
if (notebooks.size > 0)
menuItems.push(
{
type: "button",
key: "remove-from-all-notebooks",
title: "Unlink from all",
icon: RemoveShortcutLink.path,
onClick: async () => {
await db.notes.removeFromAllNotebooks(...ids);
store.refresh();
}
},
{ key: "sep", type: "separator" }
);
if (notebookShortcuts.size > 0) {
menuItems.push({ key: "sep3", type: "separator" });
notebookShortcuts.forEach((notebook) => {
menuItems.push({
type: "button",
key: notebook.id,
title: notebook.title,
icon: Notebook.path,
isChecked: false,
onClick: async () => {
await db.notes.addToNotebook(notebook.id, ...ids);
store.refresh();
}
});
});
if (notebooks.size > 0)
menuItems.push({ key: "sep2", type: "separator" });
}
if (notebooks.size <= 0) return [];
const menuItems: MenuItem[] = [
{
type: "button",
key: "remove-from-all-notebooks",
title: "Unlink from all",
icon: RemoveShortcutLink.path,
onClick: async () => {
await db.notes.removeFromAllNotebooks(...ids);
store.refresh();
}
},
{ key: "sep", type: "separator" }
];
notebooks.forEach((notebook) => {
menuItems.push({
@@ -629,7 +655,6 @@ function notebooksMenuItems(ids: string[]): MenuItem[] {
title: notebook.title,
icon: Notebook.path,
isChecked: true,
tooltip: "Click to remove from this notebook",
onClick: async () => {
await db.notes.removeFromNotebook(notebook.id, ...ids);
store.refresh();
@@ -656,30 +681,60 @@ function tagsMenuItems(ids: string[]): MenuItem[] {
key: "tags-lazy-loader",
async items() {
const tags: Map<string, Tag> = new Map();
for (const id of ids) {
const linkedTags = await db.relations
.to({ id, type: "note" }, "tag")
.resolve();
linkedTags.forEach((tag) => tags.set(tag.id, tag));
const tagShortcuts: Map<string, Tag> = new Map();
const linkedTags = await db.relations
.to({ ids, type: "note" }, "tag")
.resolve();
linkedTags.forEach((tag) => tags.set(tag.id, tag));
for (const tag of await db.shortcuts.resolved("tags")) {
if (tags.has(tag.id)) continue;
tagShortcuts.set(tag.id, tag);
}
if (tags.size <= 0) return [];
const menuItems: MenuItem[] = [
{
type: "button",
key: "remove-from-all-tags",
title: "Remove from all",
icon: RemoveShortcutLink.path,
onClick: async () => {
for (const id of ids) {
await db.relations.to({ id, type: "note" }, "tag").unlink();
const menuItems: MenuItem[] = [];
if (tags.size > 0)
menuItems.push(
{
type: "button",
key: "remove-from-all-tags",
title: "Remove from all",
icon: RemoveShortcutLink.path,
onClick: async () => {
for (const id of ids) {
await db.relations.to({ id, type: "note" }, "tag").unlink();
}
tagStore.get().refresh();
await editorStore.get().refreshTags();
await store.get().refresh();
}
tagStore.get().refresh();
await editorStore.get().refreshTags();
await store.get().refresh();
}
},
{ key: "sep", type: "separator" }
];
},
{ key: "sep", type: "separator" }
);
if (tagShortcuts.size > 0) {
menuItems.push({ key: "sep3", type: "separator" });
tagShortcuts.forEach((tag) => {
menuItems.push({
type: "button",
key: tag.id,
title: tag.title,
icon: TagIcon.path,
isChecked: false,
onClick: async () => {
for (const id of ids) {
await db.relations.add(tag, { id, type: "note" });
}
await tagStore.get().refresh();
await editorStore.get().refreshTags();
await store.get().refresh();
}
});
});
if (tags.size > 0) menuItems.push({ key: "sep2", type: "separator" });
}
tags.forEach((tag) => {
menuItems.push({
@@ -688,12 +743,11 @@ function tagsMenuItems(ids: string[]): MenuItem[] {
title: tag.title,
icon: TagIcon.path,
isChecked: true,
tooltip: "Click to remove from this tag",
onClick: async () => {
for (const id of ids) {
await db.relations.unlink(tag, { id, type: "note" });
}
tagStore.get().refresh();
await tagStore.get().refresh();
await editorStore.get().refreshTags();
await store.get().refresh();
}

View File

@@ -19,9 +19,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import Database from "../api";
import { SQLCachedCollection } from "../database/sql-cached-collection";
import { Shortcut } from "../types";
import { Notebook, Shortcut, Tag } from "../types";
import { ICollection } from "./collection";
type ResolveTypeToItem<T extends "notebooks" | "tags" | "all"> =
T extends "tags"
? Tag[]
: T extends "notebooks"
? Notebook[]
: (Tag | Notebook)[];
const ALLOWED_SHORTCUT_TYPES = ["notebook", "topic", "tag"];
export class Shortcuts implements ICollection {
name = "shortcuts";
@@ -88,19 +95,29 @@ export class Shortcuts implements ICollection {
return this.collection.items();
}
async resolved() {
async resolved<T extends "notebooks" | "tags" | "all">(
type: T = "all" as T
): Promise<ResolveTypeToItem<T>> {
const tagIds: string[] = [];
const notebookIds: string[] = [];
for (const shortcut of this.all) {
if (shortcut.itemType === "notebook") notebookIds.push(shortcut.itemId);
else if (shortcut.itemType === "tag") tagIds.push(shortcut.itemId);
if (
(type === "all" || type === "notebooks") &&
shortcut.itemType === "notebook"
)
notebookIds.push(shortcut.itemId);
else if (
(type === "all" || type === "tags") &&
shortcut.itemType === "tag"
)
tagIds.push(shortcut.itemId);
}
return [
...(notebookIds.length > 0
? await this.db.notebooks.all.items(notebookIds)
: []),
...(tagIds.length > 0 ? await this.db.tags.all.items(tagIds) : [])
];
] as ResolveTypeToItem<T>;
}
exists(id: string) {