@portabletext/editor
Version:
Portable Text Editor made in React
452 lines (414 loc) • 8.73 kB
TypeScript
import type {
KeyedSegment,
Path,
PortableTextBlock,
PortableTextChild,
PortableTextTextBlock,
} from '@sanity/types'
import {PortableTextObject, PortableTextSpan} from '@sanity/types'
/**
* @public
*/
declare type BaseDefinition = {
name: string
title?: string
}
/**
* @beta
*/
declare type BlockOffset = {
path: [KeyedSegment]
offset: number
}
/**
* @public
*/
export declare function blockOffsetsToSelection({
context,
offsets,
backward,
}: {
context: Pick<EditorContext, 'schema' | 'value'>
offsets: {
anchor: BlockOffset
focus: BlockOffset
}
backward?: boolean
}): EditorSelection
/**
* @public
*/
export declare function blockOffsetToBlockSelectionPoint({
context,
blockOffset,
}: {
context: Pick<EditorContext, 'value'>
blockOffset: BlockOffset
}): EditorSelectionPoint | undefined
/**
* @public
*/
export declare function blockOffsetToSelectionPoint({
context,
blockOffset,
direction,
}: {
context: Pick<EditorContext, 'schema' | 'value'>
blockOffset: BlockOffset
direction: 'forward' | 'backward'
}): EditorSelectionPoint | undefined
/**
* @public
*/
export declare function blockOffsetToSpanSelectionPoint({
context,
blockOffset,
direction,
}: {
context: Pick<EditorContext, 'schema' | 'value'>
blockOffset: BlockOffset
direction: 'forward' | 'backward'
}):
| {
path: [KeyedSegment, 'children', KeyedSegment]
offset: number
}
| undefined
/**
* @public
*/
export declare function childSelectionPointToBlockOffset({
context,
selectionPoint,
}: {
context: Pick<EditorContext, 'schema' | 'value'>
selectionPoint: EditorSelectionPoint
}): BlockOffset | undefined
declare type Converter<TMIMEType extends MIMEType = MIMEType> = {
mimeType: TMIMEType
serialize: Serializer<TMIMEType>
deserialize: Deserializer<TMIMEType>
}
declare type ConverterEvent<TMIMEType extends MIMEType = MIMEType> =
| {
type: 'serialize'
originEvent: 'clipboard.copy' | 'clipboard.cut' | 'drag.dragstart'
}
| {
type: 'serialization.failure'
mimeType: TMIMEType
originEvent: 'clipboard.copy' | 'clipboard.cut' | 'drag.dragstart'
reason: string
}
| {
type: 'serialization.success'
data: string
mimeType: TMIMEType
originEvent: 'clipboard.copy' | 'clipboard.cut' | 'drag.dragstart'
}
| {
type: 'deserialize'
data: string
}
| {
type: 'deserialization.failure'
mimeType: TMIMEType
reason: string
}
| {
type: 'deserialization.success'
data: Array<PortableTextBlock>
mimeType: TMIMEType
}
declare type Deserializer<TMIMEType extends MIMEType> = ({
snapshot,
event,
}: {
snapshot: EditorSnapshot
event: PickFromUnion<ConverterEvent<TMIMEType>, 'type', 'deserialize'>
}) => PickFromUnion<
ConverterEvent<TMIMEType>,
'type',
'deserialization.success' | 'deserialization.failure'
>
/**
* @public
*/
declare type EditorContext = {
converters: Array<Converter>
keyGenerator: () => string
readOnly: boolean
schema: EditorSchema
selection: EditorSelection
value: Array<PortableTextBlock>
}
/**
* @public
*/
declare type EditorSchema = {
annotations: ReadonlyArray<
BaseDefinition & {
fields: ReadonlyArray<{
name: string
type: string
}>
}
>
block: {
name: string
}
blockObjects: ReadonlyArray<
BaseDefinition & {
fields: ReadonlyArray<{
name: string
type: string
}>
}
>
decorators: ReadonlyArray<
BaseDefinition & {
/**
* @deprecated
* Use `name` instead
*/
value: string
}
>
inlineObjects: ReadonlyArray<
BaseDefinition & {
fields: ReadonlyArray<{
name: string
type: string
}>
}
>
span: {
name: string
}
styles: ReadonlyArray<
BaseDefinition & {
/**
* @deprecated
* Use `name` instead
*/
value: string
}
>
lists: ReadonlyArray<
BaseDefinition & {
/**
* @deprecated
* Use `name` instead
*/
value: string
}
>
}
/** @public */
declare type EditorSelection = {
anchor: EditorSelectionPoint
focus: EditorSelectionPoint
backward?: boolean
} | null
/** @public */
declare type EditorSelectionPoint = {
path: Path
offset: number
}
/**
* @public
*/
declare type EditorSnapshot = {
context: EditorContext
/**
* @beta
* Do not rely on this externally
*/
beta: {
activeAnnotations: Array<string>
activeDecorators: Array<string>
}
}
/**
* @public
*/
export declare function getBlockEndPoint({
context,
block,
}: {
context: Pick<EditorContext, 'schema'>
block: {
node: PortableTextBlock
path: [KeyedSegment]
}
}): EditorSelectionPoint
/**
* @public
*/
export declare function getBlockStartPoint({
context,
block,
}: {
context: Pick<EditorContext, 'schema'>
block: {
node: PortableTextBlock
path: [KeyedSegment]
}
}): EditorSelectionPoint
/**
* @public
*/
export declare function getSelectionEndPoint<
TEditorSelection extends NonNullable<EditorSelection> | null,
TEditorSelectionPoint extends
EditorSelectionPoint | null = TEditorSelection extends NonNullable<EditorSelection>
? EditorSelectionPoint
: null,
>(selection: TEditorSelection): TEditorSelectionPoint
/**
* @public
*/
export declare function getSelectionStartPoint<
TEditorSelection extends NonNullable<EditorSelection> | null,
TEditorSelectionPoint extends
EditorSelectionPoint | null = TEditorSelection extends NonNullable<EditorSelection>
? EditorSelectionPoint
: null,
>(selection: TEditorSelection): TEditorSelectionPoint
/**
* @public
*/
export declare function getTextBlockText(block: PortableTextTextBlock): string
/**
* @public
*/
export declare function isEmptyTextBlock(
context: Pick<EditorContext, 'schema'>,
block: PortableTextBlock,
): boolean
/**
* @public
*/
export declare function isEqualSelectionPoints(
a: EditorSelectionPoint,
b: EditorSelectionPoint,
): boolean
/**
* @public
*/
export declare function isEqualSelections(
a: EditorSelection,
b: EditorSelection,
): boolean
/**
* @public
*/
export declare function isKeyedSegment(
segment: unknown,
): segment is KeyedSegment
/**
* @public
*/
export declare function isSelectionCollapsed(
selection: EditorSelection,
): boolean
/**
* @public
*/
export declare function isSpan(
context: Pick<EditorContext, 'schema'>,
child: PortableTextChild,
): child is PortableTextSpan
/**
* @public
*/
export declare function isTextBlock(
context: Pick<EditorContext, 'schema'>,
block: unknown,
): block is PortableTextTextBlock
/**
* @beta
*/
export declare function mergeTextBlocks({
context,
targetBlock,
incomingBlock,
}: {
context: Pick<EditorContext, 'keyGenerator' | 'schema'>
targetBlock: PortableTextTextBlock
incomingBlock: PortableTextTextBlock
}): PortableTextTextBlock<PortableTextObject | PortableTextSpan>
declare type MIMEType = `${string}/${string}`
/**
* @internal
*/
declare type PickFromUnion<
TUnion,
TTagKey extends keyof TUnion,
TPickedTags extends TUnion[TTagKey],
> = TUnion extends Record<TTagKey, TPickedTags> ? TUnion : never
/**
* @public
*/
export declare function reverseSelection<
TEditorSelection extends NonNullable<EditorSelection> | null,
>(selection: TEditorSelection): TEditorSelection
/**
* @public
*/
export declare function selectionPointToBlockOffset({
context,
selectionPoint,
}: {
context: Pick<EditorContext, 'schema' | 'value'>
selectionPoint: EditorSelectionPoint
}): BlockOffset | undefined
declare type Serializer<TMIMEType extends MIMEType> = ({
snapshot,
event,
}: {
snapshot: EditorSnapshot
event: PickFromUnion<ConverterEvent<TMIMEType>, 'type', 'serialize'>
}) => PickFromUnion<
ConverterEvent<TMIMEType>,
'type',
'serialization.success' | 'serialization.failure'
>
/**
* @public
*/
export declare function sliceBlocks({
context,
blocks,
}: {
context: Pick<EditorContext, 'schema' | 'selection'>
blocks: Array<PortableTextBlock>
}): Array<PortableTextBlock>
/**
* @public
*/
export declare function spanSelectionPointToBlockOffset({
context,
selectionPoint,
}: {
context: Pick<EditorContext, 'schema' | 'value'>
selectionPoint: EditorSelectionPoint
}): BlockOffset | undefined
/**
* @beta
*/
export declare function splitTextBlock({
context,
block,
point,
}: {
context: Pick<EditorContext, 'schema'>
block: PortableTextTextBlock
point: EditorSelectionPoint
}):
| {
before: PortableTextTextBlock
after: PortableTextTextBlock
}
| undefined
export {}