42 |
43 | {' '}
52 |
60 |
61 |
62 | );
63 | }
64 |
--------------------------------------------------------------------------------
/src/features/actions/convertfrommarkdown/ConvertFromMarkdownFeature.tsx:
--------------------------------------------------------------------------------
1 | import * as React from 'react';
2 | import { useCallback } from 'react';
3 |
4 | import { $createCodeNode, $isCodeNode } from '@lexical/code';
5 | import { $convertFromMarkdownString, $convertToMarkdownString } from '@lexical/markdown';
6 | import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
7 | import { $createTextNode, $getRoot } from 'lexical';
8 |
9 | import { useEditorConfigContext } from '../../../fields/LexicalRichText/EditorConfigProvider';
10 | import { PLAYGROUND_TRANSFORMERS } from '../../../fields/LexicalRichText/plugins/MarkdownTransformers';
11 | import { type Feature } from '../../../types';
12 |
13 | function ConvertFromMarkdownAction(): JSX.Element {
14 | const [editor] = useLexicalComposerContext();
15 |
16 | const { editorConfig } = useEditorConfigContext();
17 |
18 | const handleMarkdownToggle = useCallback(() => {
19 | editor.update(() => {
20 | const root = $getRoot();
21 | const firstChild = root.getFirstChild();
22 | if ($isCodeNode(firstChild) && firstChild.getLanguage() === 'markdown') {
23 | $convertFromMarkdownString(
24 | firstChild.getTextContent(),
25 | PLAYGROUND_TRANSFORMERS(editorConfig)
26 | );
27 | } else {
28 | const markdown = $convertToMarkdownString(PLAYGROUND_TRANSFORMERS(editorConfig));
29 | root.clear().append($createCodeNode('markdown').append($createTextNode(markdown)));
30 | }
31 | root.selectEnd();
32 | });
33 | }, [editor]);
34 |
35 | return (
36 |