Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[form-builder] Fix portable text editor scroll bug where the editor s…
…crolled to top after acquiring focus. (#1562)
- Loading branch information
1 parent
e21d0ce
commit 4108464
Showing
1 changed file
with
10 additions
and
1 deletion.
There are no files selected for viewing
11 changes: 10 additions & 1 deletion
11
packages/@sanity/form-builder/src/inputs/BlockEditor/plugins/OnFocusPlugin.ts
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 |
---|---|---|
@@ -1,13 +1,22 @@ | ||
// This plugin runs when the editor receives focus | ||
// Ensures that an empty block is inserted when the editor gets focus and it's empty. | ||
|
||
// It also prevents default onFocus actions in slate-react being triggered as we | ||
// abort the plugin chain. The version of slate-react we are currently using (0.21.16) | ||
// has a terrible scrolling bug which will be prevented by canceling the plugin chain here. | ||
|
||
// TODO: remove this and continue once we have upgraded slate-react | ||
// (where the bug is fixed - see https://github.com/ianstormtaylor/slate/commit/d28f78c0607ad147f85ff4a363fa65d7ddfa7c8c) | ||
|
||
import {SlateEditor} from '../typeDefs' | ||
|
||
export default function OnFocusPlugin() { | ||
return { | ||
onFocus(event: any, editor: SlateEditor, next: (arg0: void) => void) { | ||
editor.command('ensurePlaceHolderBlock') | ||
return next() | ||
editor.focus() | ||
// Cancel slate-react's 'after' plugin chain to prevent built in scrolling bug. | ||
return true | ||
} | ||
} | ||
} |