Skip to content

Commit fdc5984

Browse files
committed
Support read-only mode
1 parent 4d0b227 commit fdc5984

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

.changeset/hungry-tigers-speak.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@mdxeditor/floating-selection-ui-plugin": patch
3+
---
4+
5+
Support read-only mode

packages/floating-selection-ui-plugin/src/FloatingSelectionUI.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { 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";
37
import * as Popover from "@radix-ui/react-popover";
48
import { useEffect, useState, useRef } from "react";
59
import 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(() => {

packages/floating-selection-ui-plugin/src/examples/basic.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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

packages/floating-selection-ui-plugin/src/utils/lexicalHelpers.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff 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;

0 commit comments

Comments
 (0)