From cd23011d100348723a024c5f8bee24a1884ab985 Mon Sep 17 00:00:00 2001 From: Jimmy Liow Date: Wed, 23 Oct 2024 16:36:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=20EditorRefPlugin?= =?UTF-8?q?=EF=BC=9B=E4=BD=BF=E7=94=A8=20Lexical=20Editor=20=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E5=9B=BE=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/LexicalEditor/Index.jsx | 4 ++- .../LexicalEditor/plugins/EditorRefPlugin.jsx | 24 ++++++++++++++++++ src/views/DesktopApp.jsx | 8 ++++-- src/views/accounts/SnippetList.jsx | 25 ++++++++++++++++--- 4 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 src/components/LexicalEditor/plugins/EditorRefPlugin.jsx diff --git a/src/components/LexicalEditor/Index.jsx b/src/components/LexicalEditor/Index.jsx index 51f65f3..da46ffc 100644 --- a/src/components/LexicalEditor/Index.jsx +++ b/src/components/LexicalEditor/Index.jsx @@ -27,6 +27,7 @@ import ListMaxIndentLevelPlugin from "./plugins/ListMaxIndentLevelPlugin"; import CodeHighlightPlugin from "./plugins/CodeHighlightPlugin"; import AutoLinkPlugin from "./plugins/AutoLinkPlugin"; import TabFocusPlugin from './plugins/TabFocusPlugin'; +import EditorRefPlugin from './plugins/EditorRefPlugin' // import ImagesPlugin from './plugins/ImagesPlugin'; import { ImageNode } from './nodes/ImageNode'; import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'; @@ -126,7 +127,7 @@ function MyOnChangePlugin({ onChange }) { }, [editor, onChange]); return null; } -export default function Editor({ isRichText, onChange, initialValue, ...props }) { +export default function Editor({ isRichText, editorRef, onChange, initialValue, ...props }) { // const isEditable = useLexicalEditable(); return ( @@ -152,6 +153,7 @@ export default function Editor({ isRichText, onChange, initialValue, ...props }) + {/* */} {/* */} diff --git a/src/components/LexicalEditor/plugins/EditorRefPlugin.jsx b/src/components/LexicalEditor/plugins/EditorRefPlugin.jsx new file mode 100644 index 0000000..f53d588 --- /dev/null +++ b/src/components/LexicalEditor/plugins/EditorRefPlugin.jsx @@ -0,0 +1,24 @@ +import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext' +import { useEffect } from 'react' + +/** + * + * Use this plugin to access the editor instance outside of the + * LexicalComposer. This can help with things like buttons or other + * UI components that need to update or read EditorState but need to + * be positioned outside the LexicalComposer in the React tree. + */ +export default function EditorRefPlugin({ editorRef }) { + const [editor] = useLexicalComposerContext() + + useEffect(() => { + if (typeof editorRef === 'function') { + editorRef(editor) + } else if (typeof editorRef === 'object') { + editorRef.current = editor + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [editor]) + + return null +} diff --git a/src/views/DesktopApp.jsx b/src/views/DesktopApp.jsx index 0a68282..dfb8203 100644 --- a/src/views/DesktopApp.jsx +++ b/src/views/DesktopApp.jsx @@ -143,7 +143,11 @@ function DesktopApp() { items: [ { label: 个人资料, - key: '1', + key: 'profile', + }, + { + label: 图文集管理, + key: 'snippet-list', }, { type: 'divider' }, { label: , key: 'reload' }, @@ -152,7 +156,7 @@ function DesktopApp() { { type: 'divider' }, { label: 退出, - key: '3', + key: 'logout', }, ], }} diff --git a/src/views/accounts/SnippetList.jsx b/src/views/accounts/SnippetList.jsx index 3c69f19..f7d3232 100644 --- a/src/views/accounts/SnippetList.jsx +++ b/src/views/accounts/SnippetList.jsx @@ -6,11 +6,15 @@ import { copy, isNotEmpty, isEmpty } from '@/utils/commons' import { WhatsAppOutlined } from '@ant-design/icons' import { Row, Col, Tag, List, Form, Input, Button, Space } from 'antd' import dayjs from 'dayjs' -import { useCallback, useEffect, useState } from 'react' +import { useCallback, useEffect, useState, useRef } from 'react' import { Link } from 'react-router-dom' +import LexicalEditor from '@/components/LexicalEditor' +import {$generateNodesFromDOM} from '@lexical/html' function SnippetList() { const [form] = Form.useForm() + const editorRef = useRef(null) + const [editorContent, setEditorContent] = useState('initialContent') return ( <> @@ -34,10 +38,18 @@ function SnippetList() { - + - + @@ -52,7 +64,10 @@ function SnippetList() { 'Highlights Travel Family Loyalty Club', ]} renderItem={(item) => ( - + { + console.info(item) + setEditorContent('' + item + '') + }}> 类型 @@ -70,8 +85,10 @@ function SnippetList() {
           

Discover China with the award-winning and best-rated tour company for personalized travel in China. Honored as China's Leading Tour Operator by the World Travel Awards, we boast 10,000+ reviews and a remarkable 98.8% 5-star rating. Our expertise in customizing personalized China explorations is backed by our company-managed local services across China. Explore and kickstart your personalized travel experience with just a click!

+ {console.info('onChange')}} initialValue={editorContent} />
+ )