-
-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mention plugin improvements #383
Draft
snewcomer
wants to merge
1
commit into
yoopta-editor:master
Choose a base branch
from
snewcomer:sn/mention
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,29 +1,101 @@ | ||
import { PluginElementRenderProps } from '@yoopta/editor'; | ||
import { useSelected } from 'slate-react'; | ||
import React, { useState, useCallback, useEffect } from 'react'; | ||
import { PluginElementRenderProps, generateId } from '@yoopta/editor'; | ||
import { useSlate, useSelected } from 'slate-react'; | ||
import { Transforms, Editor } from 'slate'; | ||
|
||
const MentionRender = (props: PluginElementRenderProps) => { | ||
const { url, target, rel, character } = props.element.props || {}; | ||
const MENTION_TRIGGER = '@'; | ||
|
||
interface MentionRenderProps extends PluginElementRenderProps { | ||
initialMentions: string[]; | ||
fetchMentions: (query: string) => Promise<string[]>; | ||
} | ||
|
||
const MentionRender = (props: MentionRenderProps) => { | ||
const { character } = props.element.props || {}; | ||
const { initialMentions = [], fetchMentions } = props; | ||
const selected = useSelected(); | ||
const editor = useSlate(); | ||
|
||
const handleClick = (e) => { | ||
e.preventDefault(); | ||
}; | ||
const [showMenu, setShowMenu] = useState(false); | ||
const [search, setSearch] = useState(''); | ||
const [mentionList, setMentionList] = useState<string[]>(initialMentions); | ||
const [highlightedIndex, setHighlightedIndex] = useState(0); | ||
|
||
const bgColor = selected ? 'yoo-m-bg-[#e2e2e2]' : 'yoo-m-bg-[#f4f4f5]'; | ||
|
||
const handleKeyDown = useCallback( | ||
(event: KeyboardEvent) => { | ||
if (event.key === MENTION_TRIGGER) { | ||
setShowMenu(true); | ||
setSearch(''); | ||
setMentionList(initialMentions); // Populate list with initial mentions on open | ||
} else if (showMenu) { | ||
if (event.key === 'ArrowDown') { | ||
event.preventDefault(); | ||
setHighlightedIndex((prev) => (prev + 1) % mentionList.length); | ||
} else if (event.key === 'ArrowUp') { | ||
event.preventDefault(); | ||
setHighlightedIndex((prev) => (prev - 1 + mentionList.length) % mentionList.length); | ||
} else if (event.key === 'Enter') { | ||
event.preventDefault(); | ||
insertMention(editor, mentionList[highlightedIndex]); | ||
setShowMenu(false); | ||
} else if (event.key === 'Escape') { | ||
setShowMenu(false); | ||
} | ||
} | ||
}, | ||
[showMenu, mentionList, highlightedIndex, editor, initialMentions], | ||
); | ||
|
||
useEffect(() => { | ||
document.addEventListener('keydown', handleKeyDown); | ||
return () => document.removeEventListener('keydown', handleKeyDown); | ||
}, [handleKeyDown]); | ||
|
||
useEffect(() => { | ||
if (showMenu) { | ||
fetchMentions(search).then(setMentionList); // should we offer fuzzy search here? | ||
} | ||
}, [search, showMenu, fetchMentions]); | ||
|
||
const insertMention = (editor: Editor, character: string) => { | ||
const mention = { id: generateId(), type: 'mention', character, children: [{ text: '' }] }; | ||
Transforms.insertNodes(editor, mention); // @todo fix type: 'mention' types | ||
Transforms.move(editor); // move cursor after | ||
}; | ||
|
||
return ( | ||
<a | ||
draggable={false} | ||
href={url || ''} | ||
rel={rel} | ||
target={target} | ||
onClick={handleClick} | ||
className={`yoo-m-relative yoo-m-rounded yoo-m-cursor-pointer yoo-m-px-[0.3rem] yoo-m-py-[0.2rem] yoo-m-font-mono yoo-m-color-[#fff] yoo-m-text-sm yoo-m-font-semibold ${bgColor}`} | ||
{...props.attributes} | ||
> | ||
{character ? `@${character}` : null} | ||
{props.children} | ||
</a> | ||
<> | ||
<span | ||
className={`yoo-m-relative yoo-m-rounded yoo-m-cursor-pointer yoo-m-px-[0.3rem] yoo-m-py-[0.2rem] yoo-m-font-mono yoo-m-color-[#fff] yoo-m-text-sm yoo-m-font-semibold ${bgColor}`} | ||
{...props.attributes} | ||
> | ||
{character ? `@${character}` : null} | ||
{props.children} | ||
</span> | ||
|
||
{showMenu && ( | ||
<div role="listbox" aria-activedescendant={`mention-item-${highlightedIndex}`} className="mention-menu"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually we might need a new UI component for |
||
{mentionList.map((mention, index) => ( | ||
<div | ||
id={`mention-item-${index}`} | ||
role="option" | ||
aria-selected={highlightedIndex === index} | ||
key={index} | ||
className={`mention-item ${highlightedIndex === index ? 'highlighted' : ''}`} | ||
onClick={() => { | ||
insertMention(editor, mention); | ||
setShowMenu(false); | ||
}} | ||
onMouseEnter={() => setHighlightedIndex(index)} | ||
> | ||
@{mention} | ||
</div> | ||
))} | ||
</div> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we give the possibility to override this ? I might want to group mentions