mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-02 11:42:53 +02:00
Improve props
argument type in Transforms.setNodes()
(#4638)
* Fix `setNodes()` props argument type Because Typescript can know which type of nodes we are modifying thanks to the `T` inferred from `match` function, it can also properly narrow down the `props` argument type. * Fix TS errors in examples * Add a changeset
This commit is contained in:
5
.changeset/late-spoons-cheer.md
Normal file
5
.changeset/late-spoons-cheer.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'slate': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Improve typescript type of `props` argument of `Transforms.setNodes()`
|
@@ -67,7 +67,7 @@ export interface NodeTransforms {
|
|||||||
) => void
|
) => void
|
||||||
setNodes: <T extends Node>(
|
setNodes: <T extends Node>(
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
props: Partial<Node>,
|
props: Partial<T>,
|
||||||
options?: {
|
options?: {
|
||||||
at?: Location
|
at?: Location
|
||||||
match?: NodeMatch<T>
|
match?: NodeMatch<T>
|
||||||
|
@@ -73,7 +73,9 @@ const VideoElement = ({ attributes, children, element }) => {
|
|||||||
const newProperties: Partial<SlateElement> = {
|
const newProperties: Partial<SlateElement> = {
|
||||||
url: val,
|
url: val,
|
||||||
}
|
}
|
||||||
Transforms.setNodes(editor, newProperties, { at: path })
|
Transforms.setNodes<SlateElement>(editor, newProperties, {
|
||||||
|
at: path,
|
||||||
|
})
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
@@ -37,7 +37,9 @@ const withLayout = editor => {
|
|||||||
const enforceType = type => {
|
const enforceType = type => {
|
||||||
if (SlateElement.isElement(child) && child.type !== type) {
|
if (SlateElement.isElement(child) && child.type !== type) {
|
||||||
const newProperties: Partial<SlateElement> = { type }
|
const newProperties: Partial<SlateElement> = { type }
|
||||||
Transforms.setNodes(editor, newProperties, { at: childPath })
|
Transforms.setNodes<SlateElement>(editor, newProperties, {
|
||||||
|
at: childPath,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -67,7 +67,7 @@ const withShortcuts = editor => {
|
|||||||
const newProperties: Partial<SlateElement> = {
|
const newProperties: Partial<SlateElement> = {
|
||||||
type,
|
type,
|
||||||
}
|
}
|
||||||
Transforms.setNodes(editor, newProperties, {
|
Transforms.setNodes<SlateElement>(editor, newProperties, {
|
||||||
match: n => Editor.isBlock(editor, n),
|
match: n => Editor.isBlock(editor, n),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@@ -74,7 +74,7 @@ const toggleBlock = (editor, format) => {
|
|||||||
const newProperties: Partial<SlateElement> = {
|
const newProperties: Partial<SlateElement> = {
|
||||||
type: isActive ? 'paragraph' : isList ? 'list-item' : format,
|
type: isActive ? 'paragraph' : isList ? 'list-item' : format,
|
||||||
}
|
}
|
||||||
Transforms.setNodes(editor, newProperties)
|
Transforms.setNodes<SlateElement>(editor, newProperties)
|
||||||
|
|
||||||
if (!isActive && isList) {
|
if (!isActive && isList) {
|
||||||
const block = { type: format, children: [] }
|
const block = { type: format, children: [] }
|
||||||
|
Reference in New Issue
Block a user