diff --git a/.changeset/rich-wasps-leave.md b/.changeset/rich-wasps-leave.md new file mode 100644 index 0000000000..10028cee22 --- /dev/null +++ b/.changeset/rich-wasps-leave.md @@ -0,0 +1,5 @@ +--- +'slate': patch +--- + +Add isElementType utility to Element interface diff --git a/packages/slate/src/interfaces/element.ts b/packages/slate/src/interfaces/element.ts index e3b1097c8e..d467fc7b6c 100644 --- a/packages/slate/src/interfaces/element.ts +++ b/packages/slate/src/interfaces/element.ts @@ -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 + isElementType: ( + value: any, + elementVal: string, + elementKey?: string + ) => value is T matches: (element: Element, props: Partial) => 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. @@ -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. */ @@ -58,6 +67,19 @@ export const Element: ElementInterface = { return (props as Partial).children !== undefined }, + /** + * Check if a value implements the `Element` interface and has elementKey with selected value. + * Default it check to `type` key value + */ + + isElementType: ( + value: any, + elementVal: string, + elementKey: string = 'type' + ): value is T => { + return isElement(value) && value[elementKey] === elementVal + }, + /** * Check if an element matches set of properties. * diff --git a/packages/slate/test/interfaces/Element/isElement/isElementDiscriminant.tsx b/packages/slate/test/interfaces/Element/isElement/isElementDiscriminant.tsx new file mode 100644 index 0000000000..64b8632549 --- /dev/null +++ b/packages/slate/test/interfaces/Element/isElement/isElementDiscriminant.tsx @@ -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 diff --git a/packages/slate/test/interfaces/Element/isElement/isElementDiscriminantFalse.tsx b/packages/slate/test/interfaces/Element/isElement/isElementDiscriminantFalse.tsx new file mode 100644 index 0000000000..375e615b8e --- /dev/null +++ b/packages/slate/test/interfaces/Element/isElement/isElementDiscriminantFalse.tsx @@ -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 diff --git a/packages/slate/test/interfaces/Element/isElement/isElementType.tsx b/packages/slate/test/interfaces/Element/isElement/isElementType.tsx new file mode 100644 index 0000000000..902e8a1365 --- /dev/null +++ b/packages/slate/test/interfaces/Element/isElement/isElementType.tsx @@ -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 diff --git a/packages/slate/test/interfaces/Element/isElement/isElementTypeFalse.tsx b/packages/slate/test/interfaces/Element/isElement/isElementTypeFalse.tsx new file mode 100644 index 0000000000..b3d88325f8 --- /dev/null +++ b/packages/slate/test/interfaces/Element/isElement/isElementTypeFalse.tsx @@ -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