/** * Copyright 2023-present DreamNum Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import type { Nullable } from '@univerjs/core'; import type { IThreadComment } from '@univerjs/thread-comment'; import type { Observable } from 'rxjs'; import { ICommandService, LocaleService, UniverInstanceType, UserManagerService } from '@univerjs/core'; import { ActionRow, Button, Select } from '@univerjs/design'; import { IncreaseIcon } from '@univerjs/icons'; import { ThreadCommentModel } from '@univerjs/thread-comment'; import { useDependency, useObservable } from '@univerjs/ui'; import { useEffect, useMemo, useRef, useState } from 'react'; import type { LocaleKey } from '../locale/types'; import type { ThreadCommentPanelSection } from './thread-comment-panel/util'; import type { IThreadCommentTreeProps } from './ThreadCommentTree'; import { SetActiveCommentOperation } from '../commands/operations/comment.operations'; import { ThreadCommentPanelService } from '../services/thread-comment-panel.service'; import { getThreadCommentPanelItemKey, isSameThreadCommentTarget, shouldClearThreadCommentTarget } from './thread-comment-panel/util'; import { ThreadCommentTree, ThreadCommentTreeLocation } from './ThreadCommentTree'; export interface IThreadCommentPanelProps { unitId: string; subUnitId$: Observable; type: UniverInstanceType; onAdd: () => void; getSubUnitName: (subUnitId: string) => string; onResolve?: (id: string, resolved: boolean) => void; sortComments?: (comments: IThreadComment[]) => IThreadComment[]; onItemLeave?: (comment: IThreadComment) => void; onItemEnter?: (comment: IThreadComment) => void; disableAdd?: boolean; tempComment?: Nullable; onAddComment?: IThreadCommentTreeProps['onAddComment']; onDeleteComment?: IThreadCommentTreeProps['onDeleteComment']; onAfterDeleteComment?: IThreadCommentTreeProps['onAfterDeleteComment']; showComments?: string[]; formatRef?: (comment: IThreadComment) => string; /** Hides threads the current user is not allowed to read, such as comments in a protected range. */ filterComment?: (comment: IThreadComment) => boolean; onTempCommentClose?: () => void; autoFocusActiveComment?: boolean; } interface IThreadCommentWithUsers extends IThreadComment { users: Set; } export const ThreadCommentPanel = (props: IThreadCommentPanelProps) => { const { unitId, subUnitId$, type, onAdd, getSubUnitName, onResolve, sortComments, onItemLeave, onItemEnter, disableAdd, tempComment, onAddComment, onDeleteComment, onAfterDeleteComment, showComments, formatRef, filterComment, onTempCommentClose, autoFocusActiveComment = false, } = props; const [unit, setUnit] = useState('all'); const [status, setStatus] = useState('all'); const localeService = useDependency(LocaleService); const userService = useDependency(UserManagerService); const threadCommentModel = useDependency(ThreadCommentModel); const panelService = useDependency(ThreadCommentPanelService); const activeCommentId = useObservable(panelService.activeCommentId$); useObservable(threadCommentModel.commentUpdate$); const unitComments = threadCommentModel.getUnit(unitId); const scopedTempComment = tempComment?.unitId === unitId ? tempComment : null; const activeExists = activeCommentId ? Boolean( threadCommentModel.getComment(activeCommentId.unitId, activeCommentId.subUnitId, activeCommentId.commentId) || (scopedTempComment && isSameThreadCommentTarget(activeCommentId, scopedTempComment)) ) : false; const shouldClearActiveTarget = shouldClearThreadCommentTarget(activeCommentId, unitId, activeExists); const commandService = useDependency(ICommandService); const subUnitId = useObservable(subUnitId$); const shouldScrollRef = useRef(true); const location = ThreadCommentTreeLocation.PANEL; const currentUser = useObservable(userService.currentUser$); const comments = useMemo(() => { const allComments = unit === 'all' && type !== UniverInstanceType.UNIVER_SLIDE ? unitComments : unitComments.filter((i) => i.subUnitId === subUnitId); const sort = sortComments ?? ((a) => a); const visibleComments = filterComment ? allComments.filter((i) => filterComment(i.root)) : allComments; const res: IThreadCommentWithUsers[] = visibleComments.map((i) => ({ ...i.root, children: i.children ?? [], users: i.relativeUsers })); if (showComments) { const map = new Map(); res.forEach((comment) => { map.set(comment.id, comment); }); return [...showComments, ''].map((id) => map.get(id)).filter(Boolean) as IThreadCommentWithUsers[]; } else { return sort(res) as IThreadCommentWithUsers[]; } }, [showComments, unit, unitComments, sortComments, subUnitId, type, filterComment]); const commentsSorted = useMemo(() => [ ...comments.filter((comment) => !comment.resolved), ...comments.filter((comment) => comment.resolved), ], [comments]); const statuedComments = useMemo(() => { if (status === 'resolved') { return commentsSorted.filter((comment) => comment.resolved); } if (status === 'unsolved') { return commentsSorted.filter((comment) => !comment.resolved); } if (status === 'concern_me') { if (!currentUser?.userID) { return commentsSorted; } return commentsSorted.filter((comment) => comment?.users.has(currentUser.userID)); } return commentsSorted; }, [commentsSorted, currentUser?.userID, status]); const renderComments = scopedTempComment ? [scopedTempComment, ...statuedComments] : statuedComments; const unSolvedComments = renderComments.filter((comment) => !comment.resolved); const solvedComments = renderComments.filter((comment) => comment.resolved); const isFiltering = status !== 'all' || unit !== 'all'; const onReset = () => { setStatus('all'); setUnit('all'); }; useEffect(() => { if (shouldClearActiveTarget) { panelService.setActiveComment(undefined); } }, [panelService, shouldClearActiveTarget]); useEffect(() => { if (tempComment && (tempComment.unitId !== unitId || (subUnitId && tempComment.subUnitId !== subUnitId))) { onTempCommentClose?.(); } }, [onTempCommentClose, subUnitId, tempComment, unitId]); useEffect(() => { if (!activeCommentId) { return; } const handlePointerDown = (event: PointerEvent) => { const activeComment = panelService.activeCommentId; const target = event.target; if (event.button !== 0 || !activeComment || !(target instanceof Element)) { return; } const isInsideActiveComment = Object.values(ThreadCommentTreeLocation).some((commentLocation) => { const activeElement = document.getElementById( `${commentLocation}-${activeComment.unitId}-${activeComment.subUnitId}-${activeComment.commentId}` ); return activeElement?.contains(target); }); if ( isInsideActiveComment || target.closest('button, input, textarea, select, a, [contenteditable="true"], [role="menu"], [role="menuitem"], [role="option"], [role="listbox"], [role="combobox"], [role="separator"]') ) { return; } commandService.executeCommand(SetActiveCommentOperation.id); if (scopedTempComment && isSameThreadCommentTarget(activeComment, scopedTempComment)) { onTempCommentClose?.(); } }; document.addEventListener('pointerdown', handlePointerDown, true); return () => document.removeEventListener('pointerdown', handlePointerDown, true); }, [activeCommentId, commandService, onTempCommentClose, panelService, scopedTempComment]); useEffect(() => { if (!activeCommentId) { return; } if (!shouldScrollRef.current) { shouldScrollRef.current = true; return; } const { unitId, subUnitId, commentId } = activeCommentId; const id = `${location}-${unitId}-${subUnitId}-${commentId}`; document.getElementById(id)?.scrollIntoView({ block: 'center' }); }, [activeCommentId, location]); const renderComment = (section: ThreadCommentPanelSection) => (comment: IThreadComment, index: number) => ( { shouldScrollRef.current = false; if (!comment.resolved) { commandService.executeCommand( SetActiveCommentOperation.id, { unitId: comment.unitId, subUnitId: comment.subUnitId, commentId: comment.id, temp: false, } ); } else { commandService.executeCommand(SetActiveCommentOperation.id); } }} onMouseEnter={() => { panelService.setHoveredComment({ unitId: comment.unitId, subUnitId: comment.subUnitId, commentId: comment.id, trigger: 'panel-hover', }); onItemEnter?.(comment); }} onMouseLeave={() => { if (isSameThreadCommentTarget(panelService.hoveredCommentId, comment)) { panelService.setHoveredComment(undefined); } onItemLeave?.(comment); }} onAddComment={onAddComment} onDeleteComment={onDeleteComment} onAfterDeleteComment={onAfterDeleteComment} onResolve={(resolved: boolean) => onResolve?.(comment.id, resolved)} onClose={!comment.id ? onTempCommentClose : undefined} /> ); return (
{type === UniverInstanceType.UNIVER_SHEET ? ( ('thread-comment-ui.filter.status.all'), }, { value: 'resolved', label: localeService.t('thread-comment-ui.filter.status.resolved'), }, { value: 'unsolved', label: localeService.t('thread-comment-ui.filter.status.unsolved'), }, { value: 'concern_me', label: localeService.t('thread-comment-ui.filter.status.concernMe'), }, ]} onChange={setStatus} />
{renderComments.length === 0 ? (
{localeService.t('thread-comment-ui.panel.empty')} {isFiltering ? ( ) : !disableAdd ? ( ) : null}
) : (
{unSolvedComments.map(renderComment('unsolved'))} {solvedComments.length > 0 && (
{localeService.t('thread-comment-ui.panel.solved')}
)} {solvedComments.map(renderComment('solved'))}
)}
); };