mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-14 11:14:04 +02:00
Support custom scrollIntoView from user side. (#4037)
simplify scroll into view call
This commit is contained in:
@@ -91,6 +91,7 @@ export type EditableProps = {
|
|||||||
renderElement?: (props: RenderElementProps) => JSX.Element
|
renderElement?: (props: RenderElementProps) => JSX.Element
|
||||||
renderLeaf?: (props: RenderLeafProps) => JSX.Element
|
renderLeaf?: (props: RenderLeafProps) => JSX.Element
|
||||||
renderPlaceholder?: (props: RenderPlaceholderProps) => JSX.Element
|
renderPlaceholder?: (props: RenderPlaceholderProps) => JSX.Element
|
||||||
|
scrollSelectionIntoView?: (editor: ReactEditor, domRange: DOMRange) => void
|
||||||
as?: React.ElementType
|
as?: React.ElementType
|
||||||
} & React.TextareaHTMLAttributes<HTMLDivElement>
|
} & React.TextareaHTMLAttributes<HTMLDivElement>
|
||||||
|
|
||||||
@@ -108,6 +109,7 @@ export const Editable = (props: EditableProps) => {
|
|||||||
renderElement,
|
renderElement,
|
||||||
renderLeaf,
|
renderLeaf,
|
||||||
renderPlaceholder = props => <DefaultPlaceholder {...props} />,
|
renderPlaceholder = props => <DefaultPlaceholder {...props} />,
|
||||||
|
scrollSelectionIntoView = defaultScrollSelectionIntoView,
|
||||||
style = {},
|
style = {},
|
||||||
as: Component = 'div',
|
as: Component = 'div',
|
||||||
...attributes
|
...attributes
|
||||||
@@ -192,7 +194,6 @@ export const Editable = (props: EditableProps) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise the DOM selection is out of sync, so update it.
|
// Otherwise the DOM selection is out of sync, so update it.
|
||||||
const el = ReactEditor.toDOMNode(editor, editor)
|
|
||||||
state.isUpdatingSelection = true
|
state.isUpdatingSelection = true
|
||||||
|
|
||||||
const newDomRange = selection && ReactEditor.toDOMRange(editor, selection)
|
const newDomRange = selection && ReactEditor.toDOMRange(editor, selection)
|
||||||
@@ -213,15 +214,7 @@ export const Editable = (props: EditableProps) => {
|
|||||||
newDomRange.endOffset
|
newDomRange.endOffset
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
const leafEl = newDomRange.startContainer.parentElement!
|
scrollSelectionIntoView(editor, newDomRange)
|
||||||
leafEl.getBoundingClientRect = newDomRange.getBoundingClientRect.bind(
|
|
||||||
newDomRange
|
|
||||||
)
|
|
||||||
scrollIntoView(leafEl, {
|
|
||||||
scrollMode: 'if-needed',
|
|
||||||
})
|
|
||||||
// @ts-ignore
|
|
||||||
delete leafEl.getBoundingClientRect
|
|
||||||
} else {
|
} else {
|
||||||
domSelection.removeAllRanges()
|
domSelection.removeAllRanges()
|
||||||
}
|
}
|
||||||
@@ -230,6 +223,7 @@ export const Editable = (props: EditableProps) => {
|
|||||||
// COMPAT: In Firefox, it's not enough to create a range, you also need
|
// COMPAT: In Firefox, it's not enough to create a range, you also need
|
||||||
// to focus the contenteditable element too. (2016/11/16)
|
// to focus the contenteditable element too. (2016/11/16)
|
||||||
if (newDomRange && IS_FIREFOX) {
|
if (newDomRange && IS_FIREFOX) {
|
||||||
|
const el = ReactEditor.toDOMNode(editor, editor)
|
||||||
el.focus()
|
el.focus()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1169,6 +1163,22 @@ export const DefaultPlaceholder = ({
|
|||||||
|
|
||||||
export const defaultDecorate: (entry: NodeEntry) => Range[] = () => []
|
export const defaultDecorate: (entry: NodeEntry) => Range[] = () => []
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A default implement to scroll dom range into view.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const defaultScrollSelectionIntoView = (
|
||||||
|
editor: ReactEditor,
|
||||||
|
domRange: DOMRange
|
||||||
|
) => {
|
||||||
|
const leafEl = domRange.startContainer.parentElement!
|
||||||
|
leafEl.getBoundingClientRect = domRange.getBoundingClientRect.bind(domRange)
|
||||||
|
scrollIntoView(leafEl, {
|
||||||
|
scrollMode: 'if-needed',
|
||||||
|
})
|
||||||
|
delete leafEl.getBoundingClientRect
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if two DOM range objects are equal.
|
* Check if two DOM range objects are equal.
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user