Skip to content

Commit cf10119

Browse files
authored
Generic return type for Node.fragment (#5926)
1 parent 8eeeecc commit cf10119

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

.changeset/pretty-cobras-sleep.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'slate': patch
3+
---
4+
5+
Use generics for the return type of `Node.fragment`

packages/slate/src/interfaces/node.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ export interface NodeInterface {
127127
/**
128128
* Get the sliced fragment represented by a range inside a root node.
129129
*/
130-
fragment: (root: Node, range: Range) => Descendant[]
130+
fragment: <T extends Ancestor = Editor>(
131+
root: T,
132+
range: Range
133+
) => T['children']
131134

132135
/**
133136
* Get the descendant node referred to by a specific path. If the path is an
@@ -353,15 +356,7 @@ export const Node: NodeInterface = {
353356
return [n, p]
354357
},
355358

356-
fragment(root: Node, range: Range): Descendant[] {
357-
if (Text.isText(root)) {
358-
throw new Error(
359-
`Cannot get a fragment starting from a root text node: ${Scrubber.stringify(
360-
root
361-
)}`
362-
)
363-
}
364-
359+
fragment<T extends Ancestor = Editor>(root: T, range: Range): T['children'] {
365360
const newRoot = produce({ children: root.children }, r => {
366361
const [start, end] = Range.edges(range)
367362
const nodeEntries = Node.nodes(r, {

0 commit comments

Comments
 (0)