File tree Expand file tree Collapse file tree 4 files changed +22
-9
lines changed
packages/floating-selection-ui-plugin/src Expand file tree Collapse file tree 4 files changed +22
-9
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ " @mdxeditor/floating-selection-ui-plugin " : patch
3+ ---
4+
5+ Support read-only mode
Original file line number Diff line number Diff line change 11import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext" ;
2- import { $getSelection } from "lexical" ;
2+ import {
3+ $getSelection ,
4+ COMMAND_PRIORITY_LOW ,
5+ SELECTION_CHANGE_COMMAND ,
6+ } from "lexical" ;
37import * as Popover from "@radix-ui/react-popover" ;
48import { useEffect , useState , useRef } from "react" ;
59import type {
@@ -63,19 +67,21 @@ export function FloatingSelectionUI({
6367 } , [ editor , shouldShow ] ) ;
6468
6569 useEffect ( ( ) => {
66- return editor . registerUpdateListener ( ( { editorState } ) => {
67- editorState . read ( ( ) => {
70+ return editor . registerCommand (
71+ SELECTION_CHANGE_COMMAND ,
72+ ( ) => {
6873 const selection = $getSelection ( ) ;
6974
75+ console . log ( "SELECTION_CHANGE_COMMAND received" , selection ) ;
7076 if ( ! selection || selection . isCollapsed ( ) ) {
7177 setSelectionRect ( null ) ;
7278 setIsVisible ( false ) ;
73- return ;
79+ return false ;
7480 }
7581
7682 const rect = getSelectionRectangle ( editor ) ;
7783 if ( ! rect ) {
78- return ;
84+ return false ;
7985 }
8086
8187 setSelectionRect ( rect ) ;
@@ -84,8 +90,10 @@ export function FloatingSelectionUI({
8490 const show = shouldShow ? shouldShow ( editor ) : true ;
8591 setIsVisible ( show ) ;
8692 }
87- } ) ;
88- } ) ;
93+ return false ;
94+ } ,
95+ COMMAND_PRIORITY_LOW ,
96+ ) ;
8997 } , [ editor , shouldShow ] ) ;
9098
9199 useEffect ( ( ) => {
Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ export const GetSelectionAsMarkdown: Story = () => {
2424 < div className = "App" style = { { padding : "2rem" } } >
2525 < div style = { { marginTop : "2rem" } } >
2626 < MDXEditor
27+ readOnly
2728 ref = { mdxEditorRef }
2829 markdown = { `# Welcome to MDXEditor
2930
Original file line number Diff line number Diff line change @@ -15,8 +15,7 @@ export function getSelectionRectangle(editor: LexicalEditor) {
1515 selection !== null &&
1616 nativeSelection !== null &&
1717 rootElement !== null &&
18- rootElement . contains ( nativeSelection . anchorNode ) &&
19- editor . isEditable ( )
18+ rootElement . contains ( nativeSelection . anchorNode )
2019 ) {
2120 const domRange = nativeSelection . getRangeAt ( 0 ) ;
2221 let rect ;
You can’t perform that action at this time.
0 commit comments