-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use native character insertion to fix browser/OS text features (#3888)
* use native character insertion to fix browser/OS text features. (flickering spellcheck, autocorrect, text shortcuts, etc.) move some checks into previous if statement, remove commented out code move native behavior into `slate-react`, and remove any external interface dont use native editing if marks are set, as a new node will be inserted match -> above remove nativeOperationsQueue from editor bail out of native queueing and immediately flush events if non insert_text operation is being applied. * Convert TextString to a functional component * Batch normalization of native op application * Add changeset * only proceed as native event if single character non-special character or space, to limit potential bad side effects. * Revert "fix ime double input with mark" * Comment wording tweak Co-authored-by: Dylan Schiemann <dylan@dojotoolkit.org> * Comment wording tweak Co-authored-by: Dylan Schiemann <dylan@dojotoolkit.org> * Comment wording tweak Co-authored-by: Dylan Schiemann <dylan@dojotoolkit.org> * Comment wording tweak Co-authored-by: Dylan Schiemann <dylan@dojotoolkit.org> * Comment wording tweak Co-authored-by: Dylan Schiemann <dylan@dojotoolkit.org> Co-authored-by: Ludwig Pettersson <luddep@gmail.com> Co-authored-by: Dylan Schiemann <dylan@dojotoolkit.org>
- Loading branch information
1 parent
55ff8f0
commit 25afbd4
Showing
6 changed files
with
157 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'slate-react': minor | ||
--- | ||
|
||
Use native character insertion to fix browser/OS text features |
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,37 @@ | ||
import { Editor, Operation } from 'slate' | ||
|
||
export const AS_NATIVE: WeakMap<Editor, boolean> = new WeakMap() | ||
export const NATIVE_OPERATIONS: WeakMap<Editor, Operation[]> = new WeakMap() | ||
|
||
/** | ||
* `asNative` queues operations as native, meaning native browser events will | ||
* not have been prevented, and we need to flush the operations | ||
* after the native events have propogated to the DOM. | ||
* @param {Editor} editor - Editor on which the operations are being applied | ||
* @param {callback} fn - Function containing .exec calls which will be queued as native | ||
*/ | ||
export const asNative = (editor: Editor, fn: () => void) => { | ||
AS_NATIVE.set(editor, true) | ||
fn() | ||
AS_NATIVE.set(editor, false) | ||
} | ||
|
||
/** | ||
* `flushNativeEvents` applies any queued native events. | ||
* @param {Editor} editor - Editor on which the operations are being applied | ||
*/ | ||
export const flushNativeEvents = (editor: Editor) => { | ||
const nativeOps = NATIVE_OPERATIONS.get(editor) | ||
|
||
// Clear list _before_ applying, as we might flush | ||
// events in each op, as well. | ||
NATIVE_OPERATIONS.set(editor, []) | ||
|
||
if (nativeOps) { | ||
Editor.withoutNormalizing(editor, () => { | ||
nativeOps.forEach(op => { | ||
editor.apply(op) | ||
}) | ||
}) | ||
} | ||
} |