forked from facebookarchive/draft-js
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove unnecessary DOM selection updates (#31)
* Update build * updates jest snapshots for latest jest format * More conservative selection updating Don't update the selection on a leaf if it only intersects the selection on the leaf's trailing edge and there are additional leaves in the block. Before this change, in this case, we were clearing the DOM selection and adding a point to it only to immediately clear it again when the following leaf processed. In this case we do not need to update the DOM selection at all (it isn't changing) so this was 100% unnecessary overhead. It has a significant performance impact in long editor states. * Update package.json * Symmetric check for more conservative selection updates This handles the opposite case: where the selection would be set on a node that is only on the trailing edge of the selection when there are already other leaves in the block within the selection. There's no reason that this should be added to the selection. * v0.11.6-descript.19 * v0.11.6-descript.20 * v0.11.6-descript.21 * v0.11.6-descript.22 * Option to update or set DOM selection in a single place * more cleanup * re-use new function to get dom selection * v0.11.6-descript.23 * fix doc * better docs
- Loading branch information
Showing
12 changed files
with
485 additions
and
50 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {SelectionObject} from '../utils/DraftDOMTypes'; | ||
import getCorrectDocumentFromNode from '../utils/getCorrectDocumentFromNode'; | ||
import containsNode from 'fbjs/lib/containsNode'; | ||
|
||
export function getDOMSelection(node: Node): SelectionObject | undefined { | ||
// 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. | ||
const documentObject = getCorrectDocumentFromNode(node); | ||
if (!containsNode(documentObject.documentElement, node)) { | ||
return undefined; | ||
} | ||
|
||
return documentObject.defaultView!.getSelection() as SelectionObject; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { | ||
addFocusToSelection, | ||
addPointToSelection, | ||
} from './setDraftEditorSelection'; | ||
import {SelectionObject} from '../utils/DraftDOMTypes'; | ||
import {SelectionState} from '../../model/immutable/SelectionState'; | ||
|
||
export type DOMLocation = { | ||
node: Node; | ||
offset: number; | ||
}; | ||
|
||
export type DOMSelectionUpdateFn = ( | ||
type: 'anchor' | 'focus', | ||
loc: DOMLocation, | ||
) => void; | ||
|
||
/** | ||
* Modifies the DOM selection according to a new anchor and focus. | ||
* This function attempts to perform a minimal update for performance | ||
* reasons (i.e., it the selection hasn't changed, it will not update | ||
* the selection; if only the focus has changed, it will not modify the | ||
* anchor). | ||
*/ | ||
export function updateDOMSelection( | ||
domSelection: SelectionObject, | ||
newAnchor: DOMLocation | undefined, | ||
newFocus: DOMLocation | undefined, | ||
draftSelection: SelectionState, | ||
): void { | ||
// if there's a missing focus or anchor, assume a point selection | ||
newAnchor = newAnchor || newFocus; | ||
newFocus = newFocus || newAnchor; | ||
if (!newAnchor || !newFocus) { | ||
// if neither, assume that a selection update was not needed | ||
return; | ||
} | ||
|
||
const anchorChanged = | ||
domSelection.anchorNode !== newAnchor.node || | ||
domSelection.anchorOffset !== newAnchor.offset; | ||
const focusChanged = | ||
domSelection.focusNode !== newFocus.node || | ||
domSelection.focusOffset !== newFocus.offset; | ||
|
||
// only update the selection if it is not already correct | ||
if (anchorChanged || focusChanged) { | ||
if (anchorChanged) { | ||
// start a selection from scratch | ||
domSelection.removeAllRanges(); | ||
addPointToSelection( | ||
domSelection, | ||
newAnchor.node, | ||
newAnchor.offset, | ||
draftSelection, | ||
); | ||
} | ||
// add the focus | ||
addFocusToSelection( | ||
domSelection, | ||
newFocus.node, | ||
newFocus.offset, | ||
draftSelection, | ||
); | ||
} | ||
} |
Oops, something went wrong.