import React, { useContext, useEffect, useRef, useState } from 'react';
import ReactDOM from 'react-dom';
import { Editor, Element, Node, Text, Range, Path } from '@src/components/slate-packages/slate';
import { ReactEditor, useFocused, useSelected } from '../../../slate-packages/slate-react/index';
import { ELTYPE } from '../config';
import { Transforms } from '../../../slate-packages/slate';
import { actionKey, applyOpt } from '../../../../utils/apiListener';
import { bodySelectAll } from '@src/utils/selectionUtils';
import { TripdocsSdkContext } from '@src/Docs';
import { Avatar, Popover } from 'antd';
import { css, cx } from '@emotion/css';
import { SEL_CELLS } from '@src/utils/weak-maps';
import { createUUID } from '@src/utils/randomId';
import { f } from '@src/resource/string';
import { getCache } from '@src/utils/cacheUtils';
interface SlateElement {
attributes: any;
children: any;
element: any;
editor: any;
editorId: string;
}
function MentionInfo(props) {
const { user } = props;
return (
<>
{user?.displayname || user?.c_name}
{}
{f('email')}:
{user?.ad_email}
{f('empcode')}:
{user?.empcode}
BU:
{user?.businessname}
{f('department')}:
{user?.department}
{f('positionname')}:
{user?.positionname}
>
);
}
export const MentionElement = ({ attributes, children, element, editorId, editor }: SlateElement): any => {
const selected = useSelected();
const focused = useFocused();
const { docId } = useContext(TripdocsSdkContext);
const { targetUser, senderUser, id } = element;
const [isSelected, setIsSelected] = useState(false);
const selCells = SEL_CELLS.get(editor);
const [isMe, setIsMe] = useState(false);
useEffect(() => {
const options = getCache(docId, 'options');
const { employee } = options.userInfo;
if (targetUser.empcode === employee || targetUser.userId === employee) {
setIsMe(true);
}
console.log(options);
}, []);
return (
<>
{
e.preventDefault();
e.stopPropagation();
const data = {
id,
type: 'click',
targetUser: targetUser,
senderUser: senderUser,
title: (Node.get(editor, [0, 0]) as any).text,
href: document.location.href,
};
applyOpt(actionKey.mentionCallback, data, editorId.split('-')[1]);
const path = ReactEditor.findPath(editor, element);
Transforms.select(editor, path);
}}
>
}
overlayInnerStyle={{ borderRadius: '4px' }}
trigger={['click']}
placement={'rightTop'}
visible={isSelected}
onVisibleChange={visible => setIsSelected(visible)}
>
{`@${element?.targetUser?.sn}`}
{children}
>
);
};
export const insertMention = (editor: any, character: any, cUser: any, docId: any) => {
const focusPath = editor.selection.focus.path;
const postLeafPath = [...focusPath.slice(0, -1), focusPath[focusPath.length - 1] + 2];
const id = createUUID();
const mention = {
type: ELTYPE.MENTION,
targetUser: character,
senderUser: cUser,
id,
children: [{ text: '' }],
};
console.log('insertMention', mention);
Transforms.insertNodes(editor, mention);
console.log('{{{postLeafPath}}}', editor, mention, focusPath);
const content = Node.string(Node.get(editor, [Range.start(editor.selection).path[0]]));
if (Editor.hasPath(editor, postLeafPath)) {
Transforms.select(editor, {
path: postLeafPath,
offset: 0,
});
}
const senderUser = cUser || {};
const data = {
id,
targetUser: character,
senderUser: senderUser,
title: (Node.get(editor, [0, 0]) as any).text,
href: document.location.href,
content: content,
format: '',
type: 'insert',
};
applyOpt(actionKey.mentionCallback, data, docId);
};