Skip to content

Commit

Permalink
fix(portable-text-editor): ensure correct selection being emitted
Browse files Browse the repository at this point in the history
When setting value directly from props to an empty editor, make sure
we create a new selection and emit that afterwards.
  • Loading branch information
skogsmaskin authored and rexxars committed Sep 23, 2022
1 parent 2d2d48b commit 8acf960
Showing 1 changed file with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {ArraySchemaType, Path} from '@sanity/types'
import {Subscription, Subject, defer, of, EMPTY, Observable, OperatorFunction} from 'rxjs'
import {concatMap, share, switchMap, tap} from 'rxjs/operators'
import {randomKey} from '@sanity/util/content'
import {createEditor, Descendant} from 'slate'
import {createEditor, Descendant, Transforms} from 'slate'
import {debounce, isEqual, throttle} from 'lodash'
import {Slate} from '@sanity/slate-react'
import {compileType} from '../utils/schema'
Expand Down Expand Up @@ -308,13 +308,19 @@ export class PortableTextEditor extends React.Component<
isEqualToEmptyEditor(this.slateInstance.children, this.portableTextFeatures) &&
this.props.value
) {
const oldSel = this.slateInstance.selection
Transforms.deselect(this.slateInstance)
this.slateInstance.children = toSlateValue(
val,
{
portableTextFeatures: this.portableTextFeatures,
},
KEY_TO_SLATE_ELEMENT.get(this.slateInstance)
)
if (oldSel) {
Transforms.select(this.slateInstance, oldSel)
}
debug('Setting props.value directly to empty editor')
callbackFn()
return
}
Expand Down

0 comments on commit 8acf960

Please sign in to comment.