1
0
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:
Bryan Haakman 2021-12-04 16:54:16 +01:00 committed by GitHub
parent fece5c1570
commit 2fc7ad924c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 64 additions and 3 deletions

View 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;
}
}
```

View File

@ -9,6 +9,7 @@ type ExtendableTypes =
| 'Selection'
| 'Range'
| 'Point'
| 'Operation'
| 'InsertNodeOperation'
| 'InsertTextOperation'
| 'MergeNodeOperation'

View File

@ -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

View File

@ -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
}
}

View File

@ -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

View File

@ -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

View File

@ -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'