feat: Added "Add tag" option to file context menu

This commit is contained in:
Aman Harwara
2023-01-05 01:16:35 +05:30
parent 30dda73e90
commit ba4b6a580b
11 changed files with 160 additions and 42 deletions

View File

@@ -2,22 +2,28 @@ import { observer } from 'mobx-react-lite'
import { FunctionComponent, useCallback, useRef, useState } from 'react'
import Icon from '@/Components/Icon/Icon'
import { NavigationController } from '@/Controllers/Navigation/NavigationController'
import { NotesController } from '@/Controllers/NotesController/NotesController'
import { KeyboardKey } from '@standardnotes/ui-services'
import Popover from '../Popover/Popover'
import { IconType } from '@standardnotes/snjs'
import { classNames, DecryptedItemInterface, IconType, SNTag } from '@standardnotes/snjs'
import { getTitleForLinkedTag } from '@/Utils/Items/Display/getTitleForLinkedTag'
import { useApplication } from '../ApplicationProvider'
import MenuItem from '../Menu/MenuItem'
import Menu from '../Menu/Menu'
import { LinkingController } from '@/Controllers/LinkingController'
type Props = {
navigationController: NavigationController
notesController: NotesController
linkingController: LinkingController
selectedItems: DecryptedItemInterface[]
iconClassName: string
}
const AddTagOption: FunctionComponent<Props> = ({ navigationController, notesController, iconClassName }) => {
const AddTagOption: FunctionComponent<Props> = ({
navigationController,
linkingController,
selectedItems,
iconClassName,
}) => {
const application = useApplication()
const menuContainerRef = useRef<HTMLDivElement>(null)
const buttonRef = useRef<HTMLButtonElement>(null)
@@ -28,6 +34,18 @@ const AddTagOption: FunctionComponent<Props> = ({ navigationController, notesCon
setIsOpen((isOpen) => !isOpen)
}, [])
const isTagLinkedToSelectedItems = (tag: SNTag) => {
return selectedItems.every((item) => application.getItemTags(item).find((itemTag) => itemTag.uuid === tag.uuid))
}
const linkTagToSelectedItems = (tag: SNTag) => {
selectedItems.forEach((item) => linkingController.linkItems(item, tag))
}
const unlinkTagFromSelectedItems = (tag: SNTag) => {
selectedItems.forEach((item) => linkingController.unlinkItems(item, tag))
}
return (
<div ref={menuContainerRef}>
<MenuItem
@@ -59,9 +77,7 @@ const AddTagOption: FunctionComponent<Props> = ({ navigationController, notesCon
<MenuItem
key={tag.uuid}
onClick={() => {
notesController.isTagInSelectedNotes(tag)
? notesController.removeTagFromSelectedNotes(tag).catch(console.error)
: notesController.addTagToSelectedNotes(tag).catch(console.error)
isTagLinkedToSelectedItems(tag) ? unlinkTagFromSelectedItems(tag) : linkTagToSelectedItems(tag)
}}
>
{tag.iconString && (
@@ -72,8 +88,10 @@ const AddTagOption: FunctionComponent<Props> = ({ navigationController, notesCon
/>
)}
<span
className={`overflow-hidden overflow-ellipsis whitespace-nowrap
${notesController.isTagInSelectedNotes(tag) ? 'font-bold' : ''}`}
className={classNames(
'overflow-hidden overflow-ellipsis whitespace-nowrap',
isTagLinkedToSelectedItems(tag) ? 'font-bold' : '',
)}
>
{getTitleForLinkedTag(tag, application)?.longTitle}
</span>

View File

@@ -42,6 +42,7 @@ const NotesOptions = ({
application,
navigationController,
notesController,
linkingController,
historyModalController,
closeMenu,
}: NotesOptionsProps) => {
@@ -214,7 +215,8 @@ const NotesOptions = ({
<AddTagOption
iconClassName={iconClass}
navigationController={navigationController}
notesController={notesController}
selectedItems={notes}
linkingController={linkingController}
/>
)}
<MenuItem