feat: 优化评论显示定位

This commit is contained in:
yanmao 2021-11-24 19:34:36 +08:00
parent 8eb24893f3
commit 35e6263fd0
1 changed files with 22 additions and 2 deletions

View File

@ -189,7 +189,25 @@ const Comment: React.FC<CommentProps> = forwardRef<CommentRef, CommentProps>(
*
*/
useEffect(() => {
if (loading) return;
const update = () => {
setList((list) => {
return list.concat().map((item) => {
const top = getRectTop(
`[data-comment-id="${item.id}"]`,
);
if (top < 0) return item;
return { ...item, top };
});
});
};
const resizeObserver = new ResizeObserver(update);
resizeObserver.observe(editor.container[0]);
return () => {
resizeObserver.unobserve(editor.container[0]);
};
}, []);
useEffect(() => {
updateHeight();
normalize();
}, [loading, list]);
@ -210,7 +228,9 @@ const Comment: React.FC<CommentProps> = forwardRef<CommentRef, CommentProps>(
* @param selectors css
* @returns
*/
const getRectTop = (selectors: string | NodeInterface): number => {
const getRectTop = (
selectors: string | NodeInterface | Node,
): number => {
//获取选择器节点
let element =
typeof selectors === 'string'