-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a287ae
commit 039c17c
Showing
11 changed files
with
281 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './suggestion'; | ||
export * from './mention'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { cn } from '@tegonhq/ui/lib/utils'; | ||
import React, { | ||
forwardRef, | ||
useEffect, | ||
useImperativeHandle, | ||
useState, | ||
} from 'react'; | ||
|
||
import type { User } from 'common/types'; | ||
|
||
interface MentionListProps { | ||
items: User[]; | ||
command: (args: { id: string }) => void; | ||
} | ||
|
||
export const MentionList = forwardRef( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(props: MentionListProps, ref: React.Ref<any>) => { | ||
const [selectedIndex, setSelectedIndex] = useState(0); | ||
|
||
const selectItem = (index: number) => { | ||
const item = props.items[index]; | ||
|
||
if (item) { | ||
props.command({ id: item.id }); | ||
} | ||
}; | ||
|
||
const upHandler = () => { | ||
setSelectedIndex( | ||
(prevIndex) => | ||
(prevIndex + props.items.length - 1) % props.items.length, | ||
); | ||
}; | ||
|
||
const downHandler = () => { | ||
setSelectedIndex((prevIndex) => (prevIndex + 1) % props.items.length); | ||
}; | ||
|
||
const enterHandler = () => { | ||
selectItem(selectedIndex); | ||
}; | ||
|
||
useEffect(() => { | ||
setSelectedIndex(0); | ||
}, [props.items]); | ||
|
||
useImperativeHandle(ref, () => ({ | ||
onKeyDown: ({ event }: { event: KeyboardEvent }) => { | ||
switch (event.key) { | ||
case 'ArrowUp': | ||
upHandler(); | ||
return true; | ||
case 'ArrowDown': | ||
downHandler(); | ||
return true; | ||
case 'Enter': | ||
enterHandler(); | ||
return true; | ||
default: | ||
return false; | ||
} | ||
}, | ||
})); | ||
|
||
return ( | ||
<div className="bg-popover border border-border rounded shadow flex flex-col gap-0.5 overflow-auto p-1 relative"> | ||
{props.items.length > 0 ? ( | ||
props.items.map((item, index) => ( | ||
<button | ||
className={cn( | ||
'flex items-center gap-1 w-full text-left bg-transparent hover:bg-grayAlpha-100 p-1', | ||
index === selectedIndex ? 'bg-grayAlpha-100' : '', | ||
)} | ||
key={index} | ||
onClick={() => selectItem(index)} | ||
> | ||
{item.fullname} | ||
</button> | ||
)) | ||
) : ( | ||
<div className="item">No result</div> | ||
)} | ||
</div> | ||
); | ||
}, | ||
); | ||
|
||
MentionList.displayName = 'MentionList'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { cn } from '@tegonhq/ui/lib/utils'; | ||
import Mention from '@tiptap/extension-mention'; | ||
import { | ||
mergeAttributes, | ||
type NodeViewProps, | ||
NodeViewWrapper, | ||
ReactNodeViewRenderer, | ||
} from '@tiptap/react'; | ||
import { observer } from 'mobx-react-lite'; | ||
|
||
import type { User } from 'common/types'; | ||
|
||
import { useUsersData } from 'hooks/users'; | ||
|
||
export const MentionComponent = observer((props: NodeViewProps) => { | ||
const { users } = useUsersData(false); | ||
|
||
const user = users.find((user: User) => user.id === props.node.attrs.id); | ||
|
||
return ( | ||
<NodeViewWrapper className="inline w-fit"> | ||
<span | ||
className={cn( | ||
'mention bg-grayAlpha-100 p-0.5 px-1 rounded-sm text-primary', | ||
)} | ||
> | ||
{user.fullname} | ||
</span> | ||
</NodeViewWrapper> | ||
); | ||
}); | ||
|
||
export const CustomMention = Mention.extend({ | ||
addNodeView() { | ||
return ReactNodeViewRenderer(MentionComponent); | ||
}, | ||
parseHTML() { | ||
return [ | ||
{ | ||
tag: 'mention-component', | ||
}, | ||
]; | ||
}, | ||
renderHTML({ HTMLAttributes }) { | ||
return ['mention-component', mergeAttributes(HTMLAttributes)]; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { Editor } from '@tiptap/core'; | ||
import { ReactRenderer } from '@tiptap/react'; | ||
import tippy, { type Instance as TippyInstance } from 'tippy.js'; | ||
|
||
import { useUsersData } from 'hooks/users'; | ||
|
||
import { MentionList } from './mention-list'; | ||
|
||
interface SuggestionProps { | ||
editor: Editor; | ||
range: { from: number; to: number }; | ||
query: string; | ||
items: string[]; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
clientRect?: any; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export const useMentionSuggestions = (): any => { | ||
const { users } = useUsersData(false); | ||
|
||
return { | ||
items: ({ query }: { query: string }) => { | ||
return users | ||
.filter((item) => | ||
item.fullname.toLowerCase().startsWith(query.toLowerCase()), | ||
) | ||
.slice(0, 5); | ||
}, | ||
|
||
render: () => { | ||
let reactRenderer: ReactRenderer | null = null; | ||
let popup: TippyInstance[] | null = null; | ||
|
||
return { | ||
onStart: (props: SuggestionProps) => { | ||
if (!props.clientRect) { | ||
return; | ||
} | ||
|
||
reactRenderer = new ReactRenderer(MentionList, { | ||
props, | ||
editor: props.editor, | ||
}); | ||
|
||
popup = tippy('body', { | ||
getReferenceClientRect: props.clientRect, | ||
appendTo: () => document.body, | ||
content: reactRenderer.element, | ||
showOnCreate: true, | ||
interactive: true, | ||
trigger: 'manual', | ||
placement: 'bottom-start', | ||
}); | ||
}, | ||
|
||
onUpdate: (props: SuggestionProps) => { | ||
if (reactRenderer) { | ||
reactRenderer.updateProps(props); | ||
} | ||
|
||
if (props.clientRect && popup) { | ||
popup[0].setProps({ | ||
getReferenceClientRect: props.clientRect, | ||
}); | ||
} | ||
}, | ||
|
||
onKeyDown: (props: { | ||
event: KeyboardEvent; | ||
range: { from: number; to: number }; | ||
query: string; | ||
}) => { | ||
if (props.event.key === 'Escape') { | ||
popup?.[0]?.hide(); | ||
return true; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return (reactRenderer?.ref as any).onKeyDown(props) ?? false; | ||
}, | ||
|
||
onExit: () => { | ||
popup?.[0]?.destroy(); | ||
popup = null; | ||
reactRenderer?.destroy(); | ||
reactRenderer = null; | ||
}, | ||
}; | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.