/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ import type { LexicalEditor } from 'lexical'; import { $createCodeNode, $isCodeNode } from '@lexical/code'; import { exportFile, importFile } from '@lexical/file'; import { $convertFromMarkdownString, $convertToMarkdownString, } from '@lexical/markdown'; import { useCollaborationContext } from '@lexical/react/LexicalCollaborationContext'; import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'; import { mergeRegister } from '@lexical/utils'; import { CONNECTED_COMMAND, TOGGLE_CONNECT_COMMAND } from '@lexical/yjs'; import { $createTextNode, $getRoot, $isParagraphNode, CLEAR_EDITOR_COMMAND, COMMAND_PRIORITY_EDITOR, } from 'lexical'; import * as React from 'react'; import { useCallback, useEffect, useState } from 'react'; import useModal from '../hooks/useModal'; import Button from '../ui/Button'; import { PLAYGROUND_TRANSFORMERS } from './MarkdownTransformers'; import { SPEECH_TO_TEXT_COMMAND, SUPPORT_SPEECH_RECOGNITION, } from './SpeechToTextPlugin'; import { useTranslation } from 'react-i18next'; import { TFunction } from 'i18next'; export default function ActionsPlugin({ isRichText, }: { isRichText: boolean; }): JSX.Element { const [editor] = useLexicalComposerContext(); const [isEditable, setIsEditable] = useState(() => editor.isEditable()); const [isSpeechToText, setIsSpeechToText] = useState(false); const [connected, setConnected] = useState(false); const [isEditorEmpty, setIsEditorEmpty] = useState(true); const [modal, showModal] = useModal(); const { yjsDocMap } = useCollaborationContext(); const isCollab = yjsDocMap.get('main') !== undefined; const { t } = useTranslation(['action']); useEffect(() => { return mergeRegister( editor.registerEditableListener((editable) => { setIsEditable(editable); }), editor.registerCommand( CONNECTED_COMMAND, (payload) => { const isConnected = payload; setConnected(isConnected); return false; }, COMMAND_PRIORITY_EDITOR ) ); }, [editor]); useEffect(() => { return editor.registerUpdateListener(() => { editor.getEditorState().read(() => { const root = $getRoot(); const children = root.getChildren(); if (children.length > 1) { setIsEditorEmpty(false); } else { if ($isParagraphNode(children[0])) { const paragraphChildren = children[0].getChildren(); setIsEditorEmpty(paragraphChildren.length === 0); } else { setIsEditorEmpty(false); } } }); }); }, [editor]); const handleMarkdownToggle = useCallback(() => { editor.update(() => { const root = $getRoot(); const firstChild = root.getFirstChild(); if ($isCodeNode(firstChild) && firstChild.getLanguage() === 'markdown') { $convertFromMarkdownString( firstChild.getTextContent(), PLAYGROUND_TRANSFORMERS ); } else { const markdown = $convertToMarkdownString(PLAYGROUND_TRANSFORMERS); root .clear() .append( $createCodeNode('markdown').append($createTextNode(markdown)) ); } root.selectEnd(); }); }, [editor]); return (
{SUPPORT_SPEECH_RECOGNITION && ( )} {isCollab && ( )} {modal}
); } function ShowClearDialog({ editor, onClose, t, }: { editor: LexicalEditor; onClose: () => void; t: TFunction; }): JSX.Element { return ( <> {t('action:Confirm_Clear')}
{' '}
); }