🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import type { DocumentCommentJson } from '@lobechat/types';
import type { IEditor } from '@lobehub/editor';
import type { EditorProps } from '@lobehub/editor/react';
import { useEditor } from '@lobehub/editor/react';
import { cx } from '@lobehub/ui';
import type { Ref } from 'react';
import { memo, useCallback, useImperativeHandle } from 'react';
import { mentionPlainClassName } from '@/features/ChatInput/InputEditor/mentionStyle';
import { EditorCanvas } from '@/features/EditorCanvas';
import {
readTopicCommentEditorValue,
type TopicCommentEditorValue,
} from '@/features/Portal/TopicComments/editorUtils';
import { useWorkspaceCommentMentionOption } from '@/features/Portal/TopicComments/useWorkspaceCommentMentionOption';
import { styles } from './styles';
import { useFreezeHeightWhileResizingImage } from './useFreezeHeightWhileResizingImage';
export type DocumentCommentEditorValue = TopicCommentEditorValue;
export interface DocumentCommentEditorRef {
clean: () => void;
focus: () => void;
getValue: () => DocumentCommentEditorValue;
setValue: (value: DocumentCommentEditorValue) => void;
}
interface DocumentCommentEditorProps {
autoFocus?: boolean;
compact?: boolean;
disabled?: boolean;
editor?: IEditor;
entityId: string;
/** Drop the editor's extra inline padding so the caret lines up with the action bar's first icon. */
flush?: boolean;
getPopupContainer?: EditorProps['getPopupContainer'];
initialContent: string;
initialEditorData?: DocumentCommentJson | null;
onChange?: (value: DocumentCommentEditorValue) => void;
onPressEnter?: (event: KeyboardEvent) => boolean | void;
placeholder: string;
ref?: Ref<DocumentCommentEditorRef>;
}
const DocumentCommentEditor = memo<DocumentCommentEditorProps>(
({
ref,
autoFocus,
compact = false,
disabled,
flush = false,
editor: externalEditor,
entityId,
getPopupContainer,
initialContent,
initialEditorData,
onChange,
onPressEnter,
placeholder,
}) => {
const internalEditor = useEditor();
const editor = externalEditor ?? internalEditor;
const mentionOption = useWorkspaceCommentMentionOption();
const rootRef = useFreezeHeightWhileResizingImage();
const setValue = useCallback(
(value: DocumentCommentEditorValue) => {
if (value.editorData) editor.setDocument('json', value.editorData);
else editor.setDocument('markdown', value.content);
},
[editor],
);
useImperativeHandle(
ref,
() => ({
clean: () => editor.cleanDocument(),
focus: () => editor.focus(),
getValue: () => readTopicCommentEditorValue(editor),
setValue,
}),
[editor, setValue],
);
const handleInit = useCallback(
(currentEditor: IEditor) => {
if (autoFocus) currentEditor.focus();
},
[autoFocus],
);
return (
<div
ref={rootRef}
className={cx(
styles.commentEditor,
flush && styles.commentEditorFlush,
mentionPlainClassName,
)}
>
<EditorCanvas
blockImageCaretGuard
disabled={disabled}
editor={editor}
editorData={{ content: initialContent, editorData: initialEditorData }}
entityId={entityId}
floatingToolbar={false}
getPopupContainer={getPopupContainer}
mentionOption={mentionOption}
placeholder={placeholder}
contentStyle={{
minHeight: compact ? 24 : 44,
// On top of ChatInput's own 8px/12px body padding: keep content —
// images especially — clear of the box edges. Only the vertical
// padding lives here: the editor forwards this style to its
// absolutely positioned placeholder as well, so a horizontal
// padding would push the placeholder 8px past the caret. The
// inline padding sits on `styles.commentEditor` instead.
paddingBlock: '6px 10px',
}}
onContentChange={() => onChange?.(readTopicCommentEditorValue(editor))}
onInit={handleInit}
onPressEnter={({ event }) => onPressEnter?.(event)}
/>
</div>
);
},
);
DocumentCommentEditor.displayName = 'DocumentCommentEditor';
export default DocumentCommentEditor;