@bookbox/core
Version:
Bookbox — e-book format
75 lines (74 loc) • 2.53 kB
TypeScript
export type BookScope = 'html' | 'custom';
export type Primitive = string | number | boolean | null;
export type Serialize<Leaf = Primitive> = Leaf | {
[key: string]: Serialize<Leaf>;
} | Serialize<Leaf>[];
export type BookElementProps = Record<string, Primitive | unknown>;
export type BookElementSchema = {
name: string;
props: BookElementProps;
children: BookSchema;
marker?: 'start' | 'end';
};
export type BookItem = BookElementSchema | string;
export type BookSchema = BookItem[];
export type BookLayoutView = 'block' | 'inline';
export type BookLinkedItem = {
bookElementSchema: BookElementSchema;
firstChild: BookLinkedItem | null;
lastChild: BookLinkedItem | null;
previous: BookLinkedItem | null;
next: BookLinkedItem | null;
previousLeaf: BookLinkedItem | null;
nextLeaf: BookLinkedItem | null;
parent: BookLinkedItem | null;
raw: string | null;
view: BookLayoutView;
};
export type BookLinkedSchema = {
start: BookLinkedItem | null;
end: BookLinkedItem | null;
tree: BookLinkedItem[];
};
export type CommonElementProps = {
key: string;
meta: Record<string, Primitive>;
hidden: boolean;
};
export type BookElement<Name extends string, Props extends BookElementProps = {}> = {
name: Name;
props: Props & CommonElementProps;
};
export type ElementName<Element extends BookElement<any, any>> = Element extends BookElement<infer Name, any> ? Name : never;
export type BookHeader<Token> = {
value: Token[];
text: string;
key: string;
level: number;
};
export declare const rootBookHeader: BookHeader<any>;
export type BookStore<Token> = {
/**
* хранилище элементов по ключам
*/
elementsByKeys: Record<string, BookElementSchema>;
/**
* хранилище токенов по ключам
*/
dataByKeys: Record<string, Token[]>;
};
export type BuildTokens<Token = unknown> = (schema: BookSchema) => Token[];
export type GetToken<Token> = (params: {
children: Token[];
store: BookStore<Token>;
parents: string[];
build: BuildTokens<Token>;
}) => Token;
export type TokenGetter<Props, Token> = (props: Partial<Props>) => GetToken<Token>;
export type BookBuilder<Token = unknown, ExternalProps = BookElementProps> = (params: {
schema: BookSchema;
store: BookStore<Token>;
externalBuilder?: Record<string, Record<string, TokenGetter<ExternalProps, Token>>>;
parents?: string[];
build: BuildTokens<Token>;
}) => Token[];