mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-04-21 13:51:59 +02:00
Allow Operation type to be extended (#4708)
* Allow Operation type to be extended * fix lint * add changeset
This commit is contained in:
parent
fece5c1570
commit
2fc7ad924c
22
.changeset/tough-rockets-walk.md
Normal file
22
.changeset/tough-rockets-walk.md
Normal file
@ -0,0 +1,22 @@
|
||||
---
|
||||
'slate': patch
|
||||
---
|
||||
|
||||
Allow `Operation` type to be extended
|
||||
|
||||
For example:
|
||||
|
||||
```
|
||||
import type { BaseOperation } from 'slate'
|
||||
|
||||
type CustomOperation =
|
||||
| BaseOperation
|
||||
| YourCustomOperation
|
||||
| AnotherCustomOperation
|
||||
|
||||
declare module 'slate' {
|
||||
interface CustomTypes {
|
||||
Operation: CustomOperation;
|
||||
}
|
||||
}
|
||||
```
|
@ -9,6 +9,7 @@ type ExtendableTypes =
|
||||
| 'Selection'
|
||||
| 'Range'
|
||||
| 'Point'
|
||||
| 'Operation'
|
||||
| 'InsertNodeOperation'
|
||||
| 'InsertTextOperation'
|
||||
| 'MergeNodeOperation'
|
||||
|
@ -135,7 +135,8 @@ export type TextOperation = InsertTextOperation | RemoveTextOperation
|
||||
* collaboration, and other features.
|
||||
*/
|
||||
|
||||
export type Operation = NodeOperation | SelectionOperation | TextOperation
|
||||
export type BaseOperation = NodeOperation | SelectionOperation | TextOperation
|
||||
export type Operation = ExtendedType<'Operation', BaseOperation>
|
||||
|
||||
export interface OperationInterface {
|
||||
isNodeOperation: (value: any) => value is NodeOperation
|
||||
|
@ -4,6 +4,7 @@ import {
|
||||
BasePoint,
|
||||
BaseRange,
|
||||
Descendant,
|
||||
Operation,
|
||||
} from 'slate'
|
||||
|
||||
export type HeadingElement = {
|
||||
@ -25,6 +26,13 @@ export type CustomText = {
|
||||
text: string
|
||||
}
|
||||
|
||||
export type CustomOperation = {
|
||||
type: 'custom_op'
|
||||
value: string
|
||||
}
|
||||
|
||||
export type ExtendedOperation = Operation | CustomOperation
|
||||
|
||||
export type CustomElement = HeadingElement | ListItemElement
|
||||
|
||||
declare module 'slate' {
|
||||
@ -36,5 +44,6 @@ declare module 'slate' {
|
||||
Point: BasePoint
|
||||
Range: BaseRange
|
||||
Selection: BaseSelection
|
||||
Operation: ExtendedOperation
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,13 @@
|
||||
import { Operation } from 'slate'
|
||||
import { isCustomOperation } from './type-guards'
|
||||
|
||||
export const input: Operation = {
|
||||
type: 'insert_text',
|
||||
path: [0, 0],
|
||||
offset: 0,
|
||||
text: 'text',
|
||||
}
|
||||
|
||||
export const test = isCustomOperation
|
||||
|
||||
export const output = false
|
@ -0,0 +1,11 @@
|
||||
import { Operation } from 'slate'
|
||||
import { isCustomOperation } from './type-guards'
|
||||
|
||||
export const input: Operation = {
|
||||
type: 'custom_op',
|
||||
value: 'some value',
|
||||
}
|
||||
|
||||
export const test = isCustomOperation
|
||||
|
||||
export const output = true
|
@ -1,5 +1,5 @@
|
||||
import { Element, Text } from 'slate'
|
||||
import { CustomText, HeadingElement } from './custom-types'
|
||||
import { Element, Text, Operation } from 'slate'
|
||||
import { CustomText, CustomOperation, HeadingElement } from './custom-types'
|
||||
|
||||
export const isBoldText = (text: Text): text is CustomText =>
|
||||
!!(text as CustomText).bold
|
||||
@ -7,5 +7,9 @@ export const isBoldText = (text: Text): text is CustomText =>
|
||||
export const isCustomText = (text: Text): text is CustomText =>
|
||||
!!(text as CustomText).placeholder
|
||||
|
||||
export const isCustomOperation = (
|
||||
op: Operation
|
||||
): Operation is CustomOperation => (op as CustomOperation).type === 'custom_op'
|
||||
|
||||
export const isHeadingElement = (element: Element): element is HeadingElement =>
|
||||
element.type === 'heading'
|
||||
|
Loading…
x
Reference in New Issue
Block a user