Skip to content
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
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions packages/plugins/mention/src/plugin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ const Mention = new YooptaPlugin<PluginElementKeys, MentionElementProps>({
mention: {
render: MentionRender,
props: {
url: null,
target: '_blank',
rel: 'noreferrer',
character: null,
nodeType: 'inlineVoid',
initialMentions: null,
fetchMentions: () => null,
},
},
},
Expand Down
110 changes: 91 additions & 19 deletions packages/plugins/mention/src/ui/MentionRender.tsx
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">
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this use <Portal? Currently no positional information in this

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we might need a new UI component for ListItem or the like.

{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>
)}
</>
);
};

Expand Down