-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(performance): allow
PortableTextEditable
to be compiled
- Loading branch information
Showing
2 changed files
with
104 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type {BaseEditor, Operation} from 'slate' | ||
import type {ReactEditor} from 'slate-react' | ||
import type {PortableTextSlateEditor} from '../types/editor' | ||
|
||
// React Compiler considers `slateEditor` as immutable, and opts-out if we do this inline in a useEffect, doing it in a function moves it out of the scope, and opts-in again for the rest of the component. | ||
export function withSyncRangeDecorations( | ||
slateEditor: BaseEditor & ReactEditor & PortableTextSlateEditor, | ||
syncRangeDecorations: (operation?: Operation) => void, | ||
) { | ||
const originalApply = slateEditor.apply | ||
slateEditor.apply = (op: Operation) => { | ||
originalApply(op) | ||
if (op.type !== 'set_selection') { | ||
syncRangeDecorations(op) | ||
} | ||
} | ||
return () => { | ||
slateEditor.apply = originalApply | ||
} | ||
} |