forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed autocorrect causing dupe text (ianstormtaylor#655)
* Refactored out `getPoint` from components/content to utils so as to make it more reusable. * Fixed autocorrect causing dupe text. (ianstormtaylor#540) During autocorrection (in iOS’s Safari), `onSelect` event is triggered after `onBeforeInput` event. The plugins/core updates the state during `onBeforeInput` event, thus causing selection triggered by autocorrect to be lost/overridden. This behaviour caused dupe text bug during autocorrection. To overcome this issue, we try to query the selection and conditionally fix out of sync cases with an additional transform before inserting the text. * Removes Content#getPoint and use the new utility function instead. * Renames local variable nextTransform to transform. * Describe the solution to the autocorrect issue in a more descriptive manner.
- Loading branch information
Showing
3 changed files
with
80 additions
and
40 deletions.
There are no files selected for viewing
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,40 @@ | ||
import OffsetKey from './offset-key' | ||
|
||
/** | ||
* Get a point from a native selection's DOM `element` and `offset`. | ||
* | ||
* @param {Element} element | ||
* @param {Number} offset | ||
* @param {State} state | ||
* @param {Editor} editor | ||
* @return {Object} | ||
*/ | ||
|
||
function getPoint(element, offset, state, editor) { | ||
const { document } = state | ||
const schema = editor.getSchema() | ||
|
||
// If we can't find an offset key, we can't get a point. | ||
const offsetKey = OffsetKey.findKey(element, offset) | ||
if (!offsetKey) return null | ||
|
||
// COMPAT: If someone is clicking from one Slate editor into another, the | ||
// select event fires two, once for the old editor's `element` first, and | ||
// then afterwards for the correct `element`. (2017/03/03) | ||
const { key } = offsetKey | ||
const node = document.getDescendant(key) | ||
if (!node) return null | ||
|
||
const decorators = document.getDescendantDecorators(key, schema) | ||
const ranges = node.getRanges(decorators) | ||
const point = OffsetKey.findPoint(offsetKey, ranges) | ||
return point | ||
} | ||
|
||
/** | ||
* Export. | ||
* | ||
* @type {Function} | ||
*/ | ||
|
||
export default getPoint |