Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
change global.getSelection to currentWindow.getSelection
  • Loading branch information
haikyuu committed Sep 20, 2018
commit acd6ad0af794e7bd5a89ea7c69408f3d9a20922f
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const removeTextWithStrategy = require('removeTextWithStrategy');

function keyCommandBackspaceToStartOfLine(
editorState: EditorState,
e: SyntheticKeyboardEvent<>,
): EditorState {
const afterRemoval = removeTextWithStrategy(
editorState,
Expand All @@ -30,8 +31,8 @@ function keyCommandBackspaceToStartOfLine(
if (selection.isCollapsed() && selection.getAnchorOffset() === 0) {
return moveSelectionBackward(strategyState, 1);
}

const domSelection = global.getSelection();
const {ownerDocument} = e.currentTarget;
const domSelection = ownerDocument.defaultView.getSelection();
let range = domSelection.getRangeAt(0);
range = expandRangeToStartOfLine(range);

Expand Down
3 changes: 2 additions & 1 deletion src/component/handlers/edit/editOnBeforeInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ function editOnBeforeInput(
// Chrome will also split up a node into two pieces if it contains a Tab
// char, for no explicable reason. Seemingly caused by this commit:
// https://chromium.googlesource.com/chromium/src/+/013ac5eaf3%5E%21/
const nativeSelection = global.getSelection();
const {ownerDocument} = e.currentTarget;
const nativeSelection = ownerDocument.defaultView.getSelection();
// Selection is necessarily collapsed at this point due to earlier check.
if (
nativeSelection.anchorNode &&
Expand Down
5 changes: 3 additions & 2 deletions src/component/handlers/edit/editOnBlur.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ function editOnBlur(editor: DraftEditor, e: SyntheticEvent<>): void {
// We therefore force the issue to be certain, checking whether the active
// element is `body` to force it when blurring occurs within the window (as
// opposed to clicking to another tab or window).
if (getActiveElement() === document.body) {
const selection = global.getSelection();
const {ownerDocument} = e.currentTarget;
if (getActiveElement(ownerDocument) === ownerDocument.body) {
const selection = ownerDocument.defaultView.getSelection();
const editorNode = editor.editor;
if (
selection.rangeCount === 1 &&
Expand Down
2 changes: 1 addition & 1 deletion src/component/handlers/edit/editOnInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function editOnInput(editor: DraftEditor): void {
editor._pendingStateFromBeforeInput = undefined;
}

const domSelection = global.getSelection();
const domSelection = editor.editor.ownerDocument.defaultView.getSelection();

const {anchorNode, isCollapsed} = domSelection;
const isNotTextNode = anchorNode.nodeType !== Node.TEXT_NODE;
Expand Down
5 changes: 3 additions & 2 deletions src/component/handlers/edit/editOnKeyDown.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const isChrome = UserAgent.isBrowser('Chrome');
function onKeyCommand(
command: DraftEditorCommand | string,
editorState: EditorState,
e: SyntheticKeyboardEvent<>,
): EditorState {
switch (command) {
case 'redo':
Expand All @@ -57,7 +58,7 @@ function onKeyCommand(
case 'backspace-word':
return keyCommandBackspaceWord(editorState);
case 'backspace-to-start-of-line':
return keyCommandBackspaceToStartOfLine(editorState);
return keyCommandBackspaceToStartOfLine(editorState, e);
case 'split-block':
return keyCommandInsertNewline(editorState);
case 'transpose-characters':
Expand Down Expand Up @@ -192,7 +193,7 @@ function editOnKeyDown(editor: DraftEditor, e: SyntheticKeyboardEvent<>): void {
return;
}

const newState = onKeyCommand(command, editorState);
const newState = onKeyCommand(command, editorState, e);
if (newState !== editorState) {
editor.update(newState);
}
Expand Down
2 changes: 1 addition & 1 deletion src/component/selection/getDraftEditorSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function getDraftEditorSelection(
editorState: EditorState,
root: HTMLElement,
): DOMDerivedSelection {
const selection = global.getSelection();
const selection = root.ownerDocument.defaultView.getSelection();

// No active selection.
if (selection.rangeCount === 0) {
Expand Down
2 changes: 2 additions & 0 deletions src/component/selection/getVisibleSelectionRect.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const getRangeBoundingClientRect = require('getRangeBoundingClientRect');
* Return the bounding ClientRect for the visible DOM selection, if any.
* In cases where there are no selected ranges or the bounding rect is
* temporarily invalid, return null.
*
* When using from an iframe, you should pass the iframe window object
*/
function getVisibleSelectionRect(global: any): ?FakeClientRect {
const selection = global.getSelection();
Expand Down
13 changes: 9 additions & 4 deletions src/component/selection/setDraftEditorSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const DraftJsDebugLogging = require('DraftJsDebugLogging');

const containsNode = require('containsNode');
const getActiveElement = require('getActiveElement');
const getCorrectDocumentFromNode = require('getCorrectDocumentFromNode');
const isElement = require('isElement');
const invariant = require('invariant');

Expand Down Expand Up @@ -51,7 +52,9 @@ function anonymizeTextWithin(

if (node.nodeType === Node.TEXT_NODE) {
const length = node.textContent.length;
return document.createTextNode(
const documentObject = getCorrectDocumentFromNode(node);

return documentObject.createTextNode(
'[text ' +
length +
(labels.length ? ' | ' + labels.join(', ') : '') +
Expand Down Expand Up @@ -113,11 +116,12 @@ function setDraftEditorSelection(
// It's possible that the editor has been removed from the DOM but
// our selection code doesn't know it yet. Forcing selection in
// this case may lead to errors, so just bail now.
if (!containsNode(document.documentElement, node)) {
const documentObject = getCorrectDocumentFromNode(node);
if (!containsNode(documentObject.documentElement, node)) {
return;
}

const selection = global.getSelection();
const selection = documentObject.defaultView.getSelection();
let anchorKey = selectionState.getAnchorKey();
let anchorOffset = selectionState.getAnchorOffset();
let focusKey = selectionState.getFocusKey();
Expand Down Expand Up @@ -314,7 +318,8 @@ function addPointToSelection(
offset: number,
selectionState: SelectionState,
): void {
const range = document.createRange();
const documentObject = getCorrectDocumentFromNode(node);
const range = documentObject.createRange();
// logging to catch bug that is being reported in t16250795
if (offset > getNodeLength(node)) {
// in this case we know that the call to 'range.setStart' is about to throw
Expand Down
8 changes: 8 additions & 0 deletions src/component/utils/getCorrectDocumentFromNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function getCorrectDocumentFromNode(node) {
if (!node || !node.ownerDocument) {
return document;
}
return node.ownerDocument;
}

module.exports = getCorrectDocumentFromNode;