1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-17 20:51:20 +02:00

Generic return type for Node.fragment (#5926)

This commit is contained in:
Joe Anderson
2025-08-13 22:27:19 +01:00
committed by GitHub
parent 8eeeecc238
commit cf10119ad8
2 changed files with 10 additions and 10 deletions

View File

@@ -0,0 +1,5 @@
---
'slate': patch
---
Use generics for the return type of `Node.fragment`

View File

@@ -127,7 +127,10 @@ export interface NodeInterface {
/**
* Get the sliced fragment represented by a range inside a root node.
*/
fragment: (root: Node, range: Range) => Descendant[]
fragment: <T extends Ancestor = Editor>(
root: T,
range: Range
) => T['children']
/**
* Get the descendant node referred to by a specific path. If the path is an
@@ -353,15 +356,7 @@ export const Node: NodeInterface = {
return [n, p]
},
fragment(root: Node, range: Range): Descendant[] {
if (Text.isText(root)) {
throw new Error(
`Cannot get a fragment starting from a root text node: ${Scrubber.stringify(
root
)}`
)
}
fragment<T extends Ancestor = Editor>(root: T, range: Range): T['children'] {
const newRoot = produce({ children: root.children }, r => {
const [start, end] = Range.edges(range)
const nodeEntries = Node.nodes(r, {