import { BlockNoteSchema } from "../blocks/BlockNoteSchema.js"; import { BlockFromConfigNoChildren, BlockSchema, InlineContentFromConfig, InlineContentSchema, StyleSchema, Styles, } from "../schema/index.js"; import type { Exporter } from "./Exporter.js"; /** * Defines a mapping from all block types with a schema to a result type `R`. */ export type BlockMapping< B extends BlockSchema, I extends InlineContentSchema, S extends StyleSchema, RB, RI, > = { [K in keyof B]: ( block: BlockFromConfigNoChildren, // we don't know the exact types that are supported by the exporter at this point, // because the mapping only knows about converting certain types (which might be a subset of the supported types) // this is why there are many `any` types here (same for types below) exporter: Exporter, nestingLevel: number, numberedListIndex?: number, children?: Array>, ) => RB | Promise; }; /** * Defines a mapping from all inline content types with a schema to a result type R. */ export type InlineContentMapping< I extends InlineContentSchema, S extends StyleSchema, RI, TS, > = { [K in keyof I]: ( inlineContent: InlineContentFromConfig, exporter: Exporter, ) => RI; }; /** * Defines a mapping from all style types with a schema to a result type R. */ export type StyleMapping = { [K in keyof S]: ( style: Styles[K], exporter: Exporter, ) => RS; }; /** * The mapping factory is a utility function to easily create mappings for * a BlockNoteSchema. Using the factory makes it easier to get typescript code completion etc. */ export function mappingFactory< B extends BlockSchema, I extends InlineContentSchema, S extends StyleSchema, >(_schema: BlockNoteSchema) { return { createBlockMapping: (mapping: BlockMapping) => mapping, createInlineContentMapping: ( mapping: InlineContentMapping, ) => mapping, createStyleMapping: (mapping: StyleMapping) => mapping, }; }