Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(types): refactor portable text types used internally in the …
…studio
- Loading branch information
1 parent
dfd33ce
commit 48c03a9
Showing
2 changed files
with
80 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,70 @@ | ||
import type {Block, Span} from './types' | ||
import type {PortableTextTextBlock, PortableTextSpan, PortableTextObject} from './types' | ||
|
||
function isRecord(value: unknown): value is Record<string, unknown> { | ||
return !!value && (typeof value == 'object' || typeof value == 'function') | ||
} | ||
|
||
/** @internal */ | ||
export function isBlock<T = Span>(value: unknown): value is Block<T> { | ||
/** | ||
* Assert that a given object is a portable-text text-block type object | ||
* | ||
* @remarks | ||
* * The `markDefs` and `style` property of a block is optional. | ||
* * Block types can be named, so expect anything of the _type property. | ||
* | ||
* @alpha | ||
*/ | ||
export function isPortableTextTextBlock<T = PortableTextSpan | PortableTextObject>( | ||
value: unknown | ||
): value is PortableTextTextBlock<T> { | ||
return ( | ||
isRecord(value) && | ||
typeof value._type === 'string' && // block types can be named, so expect anything here. | ||
typeof value.style === 'string' && | ||
Array.isArray(value.children) && | ||
Array.isArray(value.markDefs) | ||
value.children.every((child) => isRecord(child)) && | ||
('markDefs' in value // optional property | ||
? Array.isArray(value.markDefs) && value.markDefs.every((def) => isRecord(def)) | ||
: false) && | ||
('style' in value ? typeof value.style === 'string' : true) // optional property | ||
) | ||
} | ||
|
||
/** @internal */ | ||
export function isSpan(value: unknown): value is Span { | ||
/** | ||
* Assert that a given object is a portable-text span-type object | ||
* | ||
* @remarks | ||
* The `marks` property of a block is optional. | ||
* | ||
* @alpha | ||
*/ | ||
export function isPortableTextSpan(value: unknown): value is PortableTextSpan { | ||
return ( | ||
isRecord(value) && | ||
value._type === 'span' && | ||
typeof value.text === 'string' && | ||
Array.isArray(value.marks) | ||
('marks' in value // optional property | ||
? Array.isArray(value.marks) && value.marks.every((mark) => typeof mark === 'string') | ||
: true) | ||
) | ||
} | ||
|
||
/** | ||
* Assert that a given object is a portable-text list-text-block-type object | ||
* | ||
* @remarks | ||
* Uses `isPortableTextTextBlock` and checks for `listItem` and `level` | ||
* | ||
* @see isPortableTextTextBlock | ||
* | ||
* @alpha | ||
*/ | ||
export function isPortableTextListBlock<T = PortableTextSpan | PortableTextObject>( | ||
value: unknown | ||
): value is PortableTextTextBlock<T> { | ||
return ( | ||
isPortableTextTextBlock(value) && | ||
'listItem' in value && | ||
typeof value.listItem === 'string' && | ||
'level' in value && | ||
Number.isInteger(value.level) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,38 @@ | ||
/** @public */ | ||
export interface Block<TChild = Span> { | ||
/** @alpha */ | ||
|
||
export type PortableTextBlock = PortableTextTextBlock | PortableTextObject | ||
|
||
/** @alpha */ | ||
export interface PortableTextTextBlock<TChild = PortableTextSpan | PortableTextObject> { | ||
_type: string | ||
_key: string | ||
style: string | ||
children: TChild[] | ||
markDefs: MarkDefinition[] | ||
markDefs?: PortableTextObject[] | ||
listItem?: string | ||
style?: string | ||
level?: number | ||
} | ||
|
||
/** @public */ | ||
export interface Span { | ||
_type: 'span' | ||
/** @alpha */ | ||
export interface PortableTextObject { | ||
_type: string | ||
_key: string | ||
marks: string[] | ||
text: string | ||
[other: string]: unknown | ||
} | ||
|
||
/** @public */ | ||
export interface MarkDefinition { | ||
[key: string]: unknown | ||
_type: string | ||
/** @alpha */ | ||
export interface PortableTextSpan { | ||
_key: string | ||
_type: 'span' | ||
text: string | ||
marks?: string[] | ||
} | ||
|
||
/** @alpha */ | ||
export type PortableTextChild = PortableTextObject | PortableTextSpan | ||
|
||
/** @alpha */ | ||
export interface PortableTextListBlock extends PortableTextTextBlock { | ||
listItem: string | ||
level: number | ||
} |