import { css, cx } from '@emotion/css';
import { EditorContainerInnerPortal, TripdocsSdkContext } from '@src/Docs';
import { hashCode, intToRGB } from '@src/utils/hexColorUtils';
import { Avatar, Button, message, Tooltip } from 'antd';
import React, { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
import { CommentContent } from '../SideComment/renderSideComment';
import $ from 'jquery';
import { timeFormat } from '../SideComment/utils';
import { CommentOutlined } from '@ant-design/icons';
import GlobalCommentEditor from './globalCommentEditor';
import storage from '@src/utils/storage';
import { ELTYPE } from '../config';
import { getNodesByRangeId } from '../SideComment/commentOps';
import { deleteCommentByCommentId, deleteCommentByRangeId } from '../SideComment/deleteSideComment';
import { createRandomId } from '@src/utils/randomId';
import { insertSideComment } from '../SideComment/insertSideComment';
import { updateSideComment } from '../SideComment/updateSideComment';
import { ReactEditor } from '@src/components/slate-packages/slate-react';
import { Transforms } from '@src/components/slate-packages/slate';
import { bodySelectAll } from '@src/utils/selectionUtils';
import { f } from '@src/resource/string';
import { getCache } from '@src/utils/cacheUtils';
export const GlobalComment = (props: any) => {
const { commentData, setCommentData, isMobile, editor } = props;
const [globalCommentEditor, setGlobalCommentEditor] = useState(null);
const [commentValue, setCommentValue] = useState('');
return (
{}
{
e.preventDefault();
if (globalCommentEditor) {
const globalCommentEditorDom = ReactEditor.toDOMNode(globalCommentEditor, globalCommentEditor);
console.log('[scrollIntoView] globalCommentEditorDom :>> ', globalCommentEditor, globalCommentEditorDom);
const containerWrapDom = document.getElementById(`editor-content-wrap-${editor.docId}`);
const offsetTop =
(globalCommentEditorDom.closest('.comment-section-wrapper') as HTMLElement)?.offsetTop + globalCommentEditorDom.offsetTop;
const height = globalCommentEditorDom?.getBoundingClientRect().height;
if (typeof offsetTop === 'number' && typeof height === 'number' && containerWrapDom) {
containerWrapDom.scroll({ behavior: 'smooth', top: offsetTop - height });
}
}
}}
>
{f('reply')}
{getCommentContents(commentData)}
);
};
type commentGroupType = {
[rangeId: string]: {
anchorId: string;
refContent: string;
selection: Selection | null;
commentDataArr: any[];
};
};
const getCommentContents = (commentData: any) => {
const { docId, editor, WIPCommentRangeId } = useContext(TripdocsSdkContext);
const [isReplyingComment, setIsReplyingComment] = useState(false);
const [editingCommentId, setEditingCommentId] = useState(null);
const groupRangeId = (commentData: any[]) => {
const commentGroup: commentGroupType = {};
commentData.forEach(item => {
const { rangeId } = item;
if (!rangeId) return;
if (!commentGroup[rangeId]) {
try {
const { selection, refContent, anchorId } = rangeId.startsWith('global-comment_')
? { selection: null, refContent: null, anchorId: null }
: JSON.parse(rangeId);
commentGroup[rangeId] = {
anchorId,
refContent,
selection,
commentDataArr: [item],
};
} catch (error) {
console.error(error);
}
} else {
commentGroup[rangeId].commentDataArr = [...commentGroup[rangeId].commentDataArr, item];
}
});
console.log('[groupRangeId] commentGroup :>> ', commentGroup, WIPCommentRangeId, commentGroup[WIPCommentRangeId]);
if (commentGroup[WIPCommentRangeId]) {
delete commentGroup[WIPCommentRangeId];
}
return Object.values(commentGroup);
};
const commentGroup = groupRangeId(commentData);
console.log('commentGroup :>> ', commentGroup);
return useMemo(
() =>
commentGroup.map((content: any, index) => {
const { refContent, anchorId, selection, commentDataArr } = content;
const isGlobalComment =
JSON.stringify(selection) === '{"anchor":{"path":[0,0],"offset":0},"focus":{"path":[0,0],"offset":0}}' &&
refContent === f('globalComment') &&
anchorId === '0';
console.log(
'isGlobalComment :>> ',
isGlobalComment,
JSON.stringify(selection) === '{"anchor":{"path":[0,0],"offset":0},"focus":{"path":[0,0],"offset":0}}',
refContent,
f('globalComment'),
anchorId === '0'
);
return (
);
}),
[JSON.stringify(commentGroup), JSON.stringify(commentData), editingCommentId, isReplyingComment, WIPCommentRangeId]
);
};
function scrollToViewByAnchorId(docId = 'default') {
return (anchorId: string) => {
const { SlateEditor: Editor, editor, ReactEditor } = window.tripdocs.editorsMap[docId];
const curNodeEntry = Editor.nodes(editor, {
at: [],
match: (n: any) => n?.anchorId === anchorId,
}).next().value;
if (curNodeEntry) {
const anchorItemEl = ReactEditor.toDOMNode(editor, curNodeEntry[0]);
const anchorItemElRect = anchorItemEl.getBoundingClientRect();
const isInViewport = anchorItemElRect && anchorItemElRect.top >= 0 && anchorItemElRect.bottom <= window.innerHeight;
const scrollWrap: HTMLElement = getCache(docId, 'editorWrapDom');
if (scrollWrap && anchorItemEl && !isInViewport) {
scrollWrap.scrollTo({ top: anchorItemEl.offsetTop - 20, behavior: 'smooth' });
}
console.log('[scrollToViewByAnchorId] anchorItemEl :>> ', anchorItemEl);
$(anchorItemEl).on('webkitAnimationEnd animationEnd', function () {
$(this).removeClass('anchor-target');
});
$(anchorItemEl).addClass('anchor-target');
return true;
}
return false;
};
}