How do I apply custom antd popover card to show comments? #7648
Unanswered
sazedur-strativ
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I can see all the comments from the side panel of comments. But I want to have a feature to show a popover when I will select a commented text. How can I achieve this?
First ss what i want to achieve and second ss is what I have now.
Code:
`
const CommentedText = memo(function CommentedText({ children, commentThread }) {
return (
<Popover
content={
<Card title="Comments" size="small" style={{ width: 300 }}>
{commentThread.comments.map(comment => (
<div key={comment.id} style={{ marginBottom: 8 }}>
{comment.author}: {comment.content}
))}
}
trigger="click"
placement="top"
className="comment-popover"
>
<span className="comment-highlight" style={{ background: '#fffbe6', cursor: 'pointer', padding: '0 2px' }}>
{children}
);
});
export default function CommentPlugin({ providerFactory }) {
const [editor] = useLexicalComposerContext();
const commentStore = useMemo(() => new CommentStore(editor), [editor]);
const comments = useCommentStore(commentStore);
const markNodeMap = useMemo(() => {
return new Map();
}, []);
const [activeAnchorKey, setActiveAnchorKey] = useState(null);
const [activeIDs, setActiveIDs] = useState([]);
const [showCommentInput, setShowCommentInput] = useState(false);
const [showComments, setShowComments] = useState(false);
useEffect(() => {
return editor.registerDecoratorListener(decorators => {
editor.getEditorState().read(() => {
const rootTextNodes = $getRoot().getAllTextNodes();
for (const node of rootTextNodes) {
if ($isMarkNode(node)) {
const ids = node.getIDs();
if (ids.length > 0) {
const thread = comments.find(t => t.id === ids[0]);
if (thread) {
const key = node.getKey();
const content = node.getTextContent();
}, [editor, comments]);
const cancelAddComment = useCallback(() => {
editor.update(() => {
const selection = $getSelection();
// Restore selection
if (selection !== null) {
selection.dirty = true;
}
});
setShowCommentInput(false);
}, [editor]);
const deleteCommentOrThread = useCallback(
(comment, thread) => {
if (comment.type === 'comment') {
const deletionInfo = commentStore.deleteCommentOrThread(comment, thread);
if (!deletionInfo) {
return;
}
const { markedComment, index } = deletionInfo;
commentStore.addComment(markedComment, thread, index);
} else {
commentStore.deleteCommentOrThread(comment);
// Remove ids from associated marks
const id = thread !== undefined ? thread.id : comment.id;
const markNodeKeys = markNodeMap.get(id);
if (markNodeKeys !== undefined) {
// Do async to avoid causing a React infinite loop
setTimeout(() => {
editor.update(() => {
for (const key of markNodeKeys) {
const node = $getNodeByKey(key);
if ($isMarkNode(node)) {
node.deleteID(id);
if (node.getIDs().length === 0) {
$unwrapMarkNode(node);
}
}
}
});
});
}
}
},
[commentStore, editor, markNodeMap],
);
const submitAddComment = useCallback(
(commentOrThread, isInlineComment, thread, selection) => {
commentStore.addComment(commentOrThread, thread);
if (isInlineComment) {
editor.update(() => {
if ($isRangeSelection(selection)) {
const isBackward = selection.isBackward();
const id = commentOrThread.id;
);
useEffect(() => {
const changedElems = [];
for (let i = 0; i < activeIDs.length; i++) {
const id = activeIDs[i];
const keys = markNodeMap.get(id);
if (keys !== undefined) {
for (const key of keys) {
const elem = editor.getElementByKey(key);
if (elem !== null) {
elem.classList.add('selected');
changedElems.push(elem);
setShowComments(true);
}
}
}
}
return () => {
for (let i = 0; i < changedElems.length; i++) {
const changedElem = changedElems[i];
changedElem.classList.remove('selected');
}
};
}, [activeIDs, editor, markNodeMap]);
useEffect(() => {
const markNodeKeysToIDs = new Map();
}, [editor, markNodeMap]);
const onAddComment = () => {
editor.dispatchCommand(INSERT_INLINE_COMMAND, undefined);
};
return (
<>
{showCommentInput && (
)}
{activeAnchorKey !== null && activeAnchorKey !== undefined && !showCommentInput && (
)}
{
<Button
className={
CommentPlugin_ShowCommentsButton ${showComments ? 'active' : ''}
}onClick={() => setShowComments(!showComments)}
title={showComments ? 'Hide Comments' : 'Show Comments'}
>
}
{showComments && (
)}
</>
);
}
`
Beta Was this translation helpful? Give feedback.
All reactions