Skip to content

Commit

Permalink
feature(slate): Add isElementType utility function to Element Interfa…
Browse files Browse the repository at this point in the history
…ce (#4349)
  • Loading branch information
imdbsd authored Aug 9, 2021
1 parent 706f2fc commit 236754c
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/rich-wasps-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate': patch
---

Add isElementType utility to Element interface
38 changes: 30 additions & 8 deletions packages/slate/src/interfaces/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,25 @@ export interface ElementInterface {
isElement: (value: any) => value is Element
isElementList: (value: any) => value is Element[]
isElementProps: (props: any) => props is Partial<Element>
isElementType: <T extends Element>(
value: any,
elementVal: string,
elementKey?: string
) => value is T
matches: (element: Element, props: Partial<Element>) => boolean
}

/**
* Shared the function with isElementType utility
*/
const isElement = (value: any): value is Element => {
return (
isPlainObject(value) &&
Node.isNodeList(value.children) &&
!Editor.isEditor(value)
)
}

export const Element: ElementInterface = {
/**
* Check if a value implements the 'Ancestor' interface.
Expand All @@ -34,14 +50,7 @@ export const Element: ElementInterface = {
* Check if a value implements the `Element` interface.
*/

isElement(value: any): value is Element {
return (
isPlainObject(value) &&
Node.isNodeList(value.children) &&
!Editor.isEditor(value)
)
},

isElement,
/**
* Check if a value is an array of `Element` objects.
*/
Expand All @@ -58,6 +67,19 @@ export const Element: ElementInterface = {
return (props as Partial<Element>).children !== undefined
},

/**
* Check if a value implements the `Element` interface and has elementKey with selected value.
* Default it check to `type` key value
*/

isElementType: <T extends Element>(
value: any,
elementVal: string,
elementKey: string = 'type'
): value is T => {
return isElement(value) && value[elementKey] === elementVal
},

/**
* Check if an element matches set of properties.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Element } from 'slate'

export const input = {
source: 'heading-large',
children: [{ text: '' }],
}
export const test = value =>
Element.isElementType(value, 'heading-large', 'source')

export const output = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Element } from 'slate'

export const input = {
type: 'heading-large',
children: [{ text: '' }],
}
export const test = value => Element.isElementType(value, 'paragraph', 'source')

export const output = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Element } from 'slate'

export const input = {
type: 'paragraph',
children: [{ text: '' }],
}
export const test = value => Element.isElementType(value, 'paragraph')

export const output = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Element } from 'slate'

export const input = {
type: 'heading-large',
children: [{ text: '' }],
}
export const test = value => Element.isElementType(value, 'paragraph')

export const output = false

0 comments on commit 236754c

Please sign in to comment.