Skip to content

Commit 97600b9

Browse files
[lexical][lexical-clipboard][lexical-playground][lexical-react][lexical-selection][lexical-table][lexical-utils] Fix using getComposedRanges for all browsers
1 parent 83eb39a commit 97600b9

File tree

20 files changed

+251
-417
lines changed

20 files changed

+251
-417
lines changed

packages/lexical-clipboard/src/clipboard.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
BaseSelection,
2727
COMMAND_PRIORITY_CRITICAL,
2828
COPY_COMMAND,
29-
getDOMSelection,
29+
getDOMSelectionForEditor,
3030
isSelectionWithinEditor,
3131
LexicalEditor,
3232
LexicalNode,
@@ -467,9 +467,8 @@ export async function copyToClipboard(
467467
}
468468

469469
const rootElement = editor.getRootElement();
470-
const editorWindow = editor._window || window;
471470
const windowDocument = window.document;
472-
const domSelection = getDOMSelection(editorWindow, rootElement);
471+
const domSelection = getDOMSelectionForEditor(editor);
473472
if (rootElement === null || domSelection === null) {
474473
return false;
475474
}
@@ -518,10 +517,7 @@ function $copyToClipboardEvent(
518517
data?: LexicalClipboardData,
519518
): boolean {
520519
if (data === undefined) {
521-
const domSelection = getDOMSelection(
522-
editor._window,
523-
editor.getRootElement(),
524-
);
520+
const domSelection = getDOMSelectionForEditor(editor);
525521
if (!domSelection) {
526522
return false;
527523
}

packages/lexical-playground/src/Settings.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,7 @@ export default function Settings(): JSX.Element {
141141
text="Autocomplete"
142142
/>
143143
<Switch
144-
onClick={() => {
145-
setOption('isShadowDOM', !isShadowDOM);
146-
setTimeout(() => window.location.reload(), 500);
147-
}}
144+
onClick={() => setOption('isShadowDOM', !isShadowDOM)}
148145
checked={isShadowDOM}
149146
text="Shadow DOM"
150147
/>

packages/lexical-playground/src/plugins/CommentPlugin/index.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import {
5050
COMMAND_PRIORITY_EDITOR,
5151
COMMAND_PRIORITY_NORMAL,
5252
createCommand,
53-
getDOMSelection,
53+
getDOMSelectionForEditor,
5454
KEY_ESCAPE_COMMAND,
5555
} from 'lexical';
5656
import {
@@ -933,10 +933,7 @@ export default function CommentPlugin({
933933
editor.registerCommand(
934934
INSERT_INLINE_COMMAND,
935935
() => {
936-
const domSelection = getDOMSelection(
937-
editor._window,
938-
editor.getRootElement(),
939-
);
936+
const domSelection = getDOMSelectionForEditor(editor);
940937
if (domSelection !== null) {
941938
domSelection.removeAllRanges();
942939
}

packages/lexical-playground/src/plugins/FloatingLinkEditorPlugin/index.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
COMMAND_PRIORITY_CRITICAL,
2828
COMMAND_PRIORITY_HIGH,
2929
COMMAND_PRIORITY_LOW,
30-
getDOMSelection,
30+
getDOMSelectionForEditor,
3131
KEY_ESCAPE_COMMAND,
3232
LexicalEditor,
3333
SELECTION_CHANGE_COMMAND,
@@ -104,10 +104,7 @@ function FloatingLinkEditor({
104104
}
105105

106106
const editorElem = editorRef.current;
107-
const nativeSelection = getDOMSelection(
108-
editor._window,
109-
editor.getRootElement(),
110-
);
107+
const nativeSelection = getDOMSelectionForEditor(editor);
111108
const activeElement = document.activeElement;
112109

113110
if (editorElem === null) {

packages/lexical-playground/src/plugins/FloatingTextFormatToolbarPlugin/index.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
$isTextNode,
2222
COMMAND_PRIORITY_LOW,
2323
FORMAT_TEXT_COMMAND,
24-
getDOMSelection,
24+
getDOMSelectionForEditor,
2525
LexicalEditor,
2626
SELECTION_CHANGE_COMMAND,
2727
} from 'lexical';
@@ -122,10 +122,7 @@ function TextFormatFloatingToolbar({
122122
const selection = $getSelection();
123123

124124
const popupCharStylesEditorElem = popupCharStylesEditorRef.current;
125-
const nativeSelection = getDOMSelection(
126-
editor._window,
127-
editor.getRootElement(),
128-
);
125+
const nativeSelection = getDOMSelectionForEditor(editor);
129126

130127
if (popupCharStylesEditorElem === null) {
131128
return;
@@ -345,10 +342,7 @@ function useFloatingTextFormatToolbar(
345342
return;
346343
}
347344
const selection = $getSelection();
348-
const nativeSelection = getDOMSelection(
349-
editor._window,
350-
editor.getRootElement(),
351-
);
345+
const nativeSelection = getDOMSelectionForEditor(editor);
352346
const rootElement = editor.getRootElement();
353347

354348
if (

packages/lexical-playground/src/plugins/TableActionMenuPlugin/index.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import {
4141
$isTextNode,
4242
$setSelection,
4343
COMMAND_PRIORITY_CRITICAL,
44-
getDOMSelection,
44+
getDOMSelectionForEditor,
4545
isDOMNode,
4646
SELECTION_CHANGE_COMMAND,
4747
} from 'lexical';
@@ -729,10 +729,7 @@ function TableCellActionMenuContainer({
729729
const $moveMenu = useCallback(() => {
730730
const menu = menuButtonRef.current;
731731
const selection = $getSelection();
732-
const nativeSelection = getDOMSelection(
733-
editor._window,
734-
editor.getRootElement(),
735-
);
732+
const nativeSelection = getDOMSelectionForEditor(editor);
736733
const activeElement = document.activeElement;
737734
function disable() {
738735
if (menu) {

packages/lexical-playground/src/plugins/TestRecorderPlugin/index.tsx

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
$createParagraphNode,
1616
$createTextNode,
1717
$getRoot,
18-
getDOMSelection,
18+
getDOMSelectionForEditor,
1919
} from 'lexical';
2020
import * as React from 'react';
2121
import {useCallback, useEffect, useLayoutEffect, useRef, useState} from 'react';
@@ -172,10 +172,7 @@ function useTestRecorder(
172172

173173
const generateTestContent = useCallback(() => {
174174
const rootElement = editor.getRootElement();
175-
const browserSelection = getDOMSelection(
176-
editor._window,
177-
editor.getRootElement(),
178-
);
175+
const browserSelection = getDOMSelectionForEditor(editor);
179176

180177
if (
181178
rootElement == null ||
@@ -330,10 +327,7 @@ ${steps.map(formatStep).join(`\n`)}
330327
dirtyElements.size === 0 &&
331328
!skipNextSelectionChange
332329
) {
333-
const browserSelection = getDOMSelection(
334-
editor._window,
335-
editor.getRootElement(),
336-
);
330+
const browserSelection = getDOMSelectionForEditor(editor);
337331
if (
338332
browserSelection &&
339333
(browserSelection.anchorNode == null ||
@@ -390,11 +384,7 @@ ${steps.map(formatStep).join(`\n`)}
390384
if (!isRecording) {
391385
return;
392386
}
393-
const currentEditor = getCurrentEditor();
394-
const browserSelection = getDOMSelection(
395-
currentEditor._window,
396-
currentEditor.getRootElement(),
397-
);
387+
const browserSelection = getDOMSelectionForEditor(getCurrentEditor());
398388
if (
399389
browserSelection === null ||
400390
browserSelection.anchorNode == null ||

packages/lexical-react/src/LexicalTypeaheadMenuPlugin.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
COMMAND_PRIORITY_LOW,
2323
CommandListenerPriority,
2424
createCommand,
25-
getDOMSelection,
25+
getDOMSelectionForEditor,
2626
LexicalCommand,
2727
LexicalEditor,
2828
RangeSelection,
@@ -52,10 +52,9 @@ function getTextUpToAnchor(selection: RangeSelection): string | null {
5252
function tryToPositionRange(
5353
leadOffset: number,
5454
range: Range,
55-
editorWindow: Window,
56-
rootElement?: HTMLElement | null,
55+
editor: LexicalEditor,
5756
): boolean {
58-
const domSelection = getDOMSelection(editorWindow, rootElement);
57+
const domSelection = getDOMSelectionForEditor(editor);
5958
if (domSelection === null || !domSelection.isCollapsed) {
6059
return false;
6160
}
@@ -289,8 +288,7 @@ export function LexicalTypeaheadMenuPlugin<TOption extends MenuOption>({
289288
const isRangePositioned = tryToPositionRange(
290289
match.leadOffset,
291290
range,
292-
editorWindow,
293-
editor.getRootElement(),
291+
editor,
294292
);
295293
if (isRangePositioned !== null) {
296294
startTransition(() =>

packages/lexical-selection/src/utils.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
*/
88
import type {ElementNode, LexicalEditor, LexicalNode} from 'lexical';
99

10-
import {$getEditor, $isRootNode, $isTextNode} from 'lexical';
10+
import {
11+
$getEditor,
12+
$isRootNode,
13+
$isTextNode,
14+
getDocumentFromElement,
15+
} from 'lexical';
1116

1217
import {CSS_TO_STYLES} from './constants';
1318

@@ -54,7 +59,8 @@ export function createDOMRange(
5459
const anchorKey = anchorNode.getKey();
5560
const focusKey = focusNode.getKey();
5661
const rootElement = editor.getRootElement();
57-
const doc = rootElement ? rootElement.ownerDocument || document : document;
62+
const doc = getDocumentFromElement(rootElement);
63+
5864
const range = doc.createRange();
5965
let anchorDOM: Node | Text | null = editor.getElementByKey(anchorKey);
6066
let focusDOM: Node | Text | null = editor.getElementByKey(focusKey);

packages/lexical-table/src/LexicalTableObserver.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
$isParagraphNode,
2424
$isRootNode,
2525
$setSelection,
26-
getDOMSelection,
26+
getDOMSelectionForEditor,
2727
INSERT_PARAGRAPH_COMMAND,
2828
SELECTION_CHANGE_COMMAND,
2929
} from 'lexical';
@@ -320,10 +320,7 @@ export class TableObserver {
320320
/** @internal */
321321
updateDOMSelection() {
322322
if (this.anchorCell !== null && this.focusCell !== null) {
323-
const domSelection = getDOMSelection(
324-
this.editor._window,
325-
this.editor.getRootElement(),
326-
);
323+
const domSelection = getDOMSelectionForEditor(this.editor);
327324
// We are not using a native selection for tables, and if we
328325
// set one then the reconciler will undo it.
329326
// TODO - it would make sense to have one so that native

0 commit comments

Comments
 (0)