From 0882ce3042176fc2bc7c33439e727b115f514ce8 Mon Sep 17 00:00:00 2001 From: Yongjoo Yoo Date: Fri, 28 Oct 2022 13:29:11 -0700 Subject: [PATCH 1/2] PORTALS-2382: tag user or team in markdown editor --- .../containers/markdown/MarkdownEditor.tsx | 40 ++++++++++++++++-- .../containers/markdown/UserMentionModal.tsx | 42 +++++++++++++++++++ 2 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 src/lib/containers/markdown/UserMentionModal.tsx diff --git a/src/lib/containers/markdown/MarkdownEditor.tsx b/src/lib/containers/markdown/MarkdownEditor.tsx index 52824ec97f..76db11c1d4 100644 --- a/src/lib/containers/markdown/MarkdownEditor.tsx +++ b/src/lib/containers/markdown/MarkdownEditor.tsx @@ -6,6 +6,9 @@ import { } from '../../utils/synapseTypes/MarkdownCommands' import IconSvg from '../IconSvg' import MarkdownSynapse from './MarkdownSynapse' +import { UserMentionModal } from './UserMentionModal' +import { startCase } from 'lodash-es' +import Tooltip from '../../utils/tooltip/Tooltip' export enum MarkdownEditorTabs { WRITE = 'WRITE', @@ -24,6 +27,7 @@ export const MarkdownEditor: React.FunctionComponent = ({ ) const [text, setText] = useState('') const [selectionStart, setSelectionStart] = useState(0) + const [isShowingTagModal, setIsShowingTagModal] = useState(false) const textAreaRef = useRef(null) /** @@ -37,6 +41,23 @@ export const MarkdownEditor: React.FunctionComponent = ({ } }, [textAreaRef, selectionStart]) + useEffect(() => { + textAreaRef.current?.focus() + }, [isShowingTagModal]) + + const handleUserTag = (user: string) => { + const newText: string[] = [] + const textVal = textAreaRef.current + if (textVal) { + const start = textVal?.selectionStart + const textBeforeTag = text.substring(0, start) + const textAfterTag = text.substring(start, text.length) + setSelectionStart(start + user.length + 1) + newText.push(textBeforeTag, `@${user}`, textAfterTag) + } + setText(newText.join('')) + } + const handleCommands = (command: CommandListType) => { const textVal = textAreaRef.current if (textVal) { @@ -61,7 +82,7 @@ export const MarkdownEditor: React.FunctionComponent = ({ ) setText(newText.join('\r\n')) - textAreaRef.current.focus() + textVal.focus() // adds 2 due to new line setSelectionStart(start + openSyntax.length + 2) break @@ -76,7 +97,7 @@ export const MarkdownEditor: React.FunctionComponent = ({ case 'link': case 'image': { const newText = `${textBeforeSelection}${openSyntax}${selected}${closeSyntax}${textAfterSelection}` - textAreaRef.current.focus() + textVal.focus() setSelectionStart(start + openSyntax.length) setText(newText) } @@ -110,10 +131,18 @@ export const MarkdownEditor: React.FunctionComponent = ({ {commandList.map(type => { return ( ) })} + + + )} @@ -133,6 +162,11 @@ export const MarkdownEditor: React.FunctionComponent = ({ 'Nothing to preview' )} + setIsShowingTagModal(false)} + handleUserTag={handleUserTag} + /> ) } diff --git a/src/lib/containers/markdown/UserMentionModal.tsx b/src/lib/containers/markdown/UserMentionModal.tsx new file mode 100644 index 0000000000..3cd2444759 --- /dev/null +++ b/src/lib/containers/markdown/UserMentionModal.tsx @@ -0,0 +1,42 @@ +import React, { useCallback } from 'react' +import { Modal } from 'react-bootstrap' +import { UserGroupHeader } from '../../utils/synapseTypes' +import UserSearchBoxV2 from '../UserSearchBoxV2' + +export type UserMentionModalProps = { + show: boolean + onClose: () => void + handleUserTag: (user: string) => void +} + +export const UserMentionModal: React.FC = ({ + show, + onClose, + handleUserTag, +}: UserMentionModalProps) => { + const onUserChange = useCallback( + (selected: string | null, header: UserGroupHeader | null) => { + if (selected && header) { + handleUserTag(header.userName) + } + onClose() + }, + [onClose, handleUserTag], + ) + + return ( + <> + + + Find User or Team + + + + + + + ) +} From 83ca007bf0309e0b09ac351706e3d9780d2560bb Mon Sep 17 00:00:00 2001 From: Yongjoo Yoo Date: Fri, 28 Oct 2022 14:37:27 -0700 Subject: [PATCH 2/2] PORTALS-2382: set UserSearchBox only to search users --- src/lib/containers/markdown/UserMentionModal.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/containers/markdown/UserMentionModal.tsx b/src/lib/containers/markdown/UserMentionModal.tsx index 3cd2444759..cd7162a921 100644 --- a/src/lib/containers/markdown/UserMentionModal.tsx +++ b/src/lib/containers/markdown/UserMentionModal.tsx @@ -1,6 +1,6 @@ import React, { useCallback } from 'react' import { Modal } from 'react-bootstrap' -import { UserGroupHeader } from '../../utils/synapseTypes' +import { TYPE_FILTER, UserGroupHeader } from '../../utils/synapseTypes' import UserSearchBoxV2 from '../UserSearchBoxV2' export type UserMentionModalProps = { @@ -34,6 +34,7 @@ export const UserMentionModal: React.FC = ({