diff --git a/packages/core/src/blocks/defaultBlockTypeGuards.ts b/packages/core/src/blocks/defaultBlockTypeGuards.ts index 5db988da9..b66fa5a81 100644 --- a/packages/core/src/blocks/defaultBlockTypeGuards.ts +++ b/packages/core/src/blocks/defaultBlockTypeGuards.ts @@ -1,9 +1,11 @@ import { CellSelection } from "prosemirror-tables"; import type { BlockNoteEditor } from "../editor/BlockNoteEditor.js"; import { + BlockConfig, BlockFromConfig, BlockSchema, FileBlockConfig, + InlineContentConfig, InlineContentSchema, StyleSchema, } from "../schema/index.js"; @@ -31,6 +33,20 @@ export function checkDefaultBlockTypeInSchema< ); } +export function checkBlockTypeInSchema< + BlockType extends string, + Config extends BlockConfig, +>( + blockType: BlockType, + blockConfig: Config, + editor: BlockNoteEditor, +): editor is BlockNoteEditor<{ [T in BlockType]: Config }, any, any> { + return ( + blockType in editor.schema.blockSchema && + editor.schema.blockSchema[blockType] === blockConfig + ); +} + export function checkDefaultInlineContentTypeInSchema< InlineContentType extends keyof DefaultInlineContentSchema, B extends BlockSchema, @@ -50,6 +66,20 @@ export function checkDefaultInlineContentTypeInSchema< ); } +export function checkInlineContentTypeInSchema< + InlineContentType extends string, + Config extends InlineContentConfig, +>( + inlineContentType: InlineContentType, + inlineContentConfig: Config, + editor: BlockNoteEditor, +): editor is BlockNoteEditor { + return ( + inlineContentType in editor.schema.inlineContentSchema && + editor.schema.inlineContentSchema[inlineContentType] === inlineContentConfig + ); +} + export function checkBlockIsDefaultType< BlockType extends keyof DefaultBlockSchema, I extends InlineContentSchema, diff --git a/packages/core/src/schema/inlineContent/types.ts b/packages/core/src/schema/inlineContent/types.ts index 6ec87055d..6415a86f6 100644 --- a/packages/core/src/schema/inlineContent/types.ts +++ b/packages/core/src/schema/inlineContent/types.ts @@ -22,6 +22,13 @@ export type InlineContentImplementation = node: Node; }; +export type InlineContentSchemaWithInlineContent< + IType extends string, + C extends InlineContentConfig, +> = { + [k in IType]: C; +}; + // Container for both the config and implementation of InlineContent, // and the type of `implementation` is based on that of the config export type InlineContentSpec = { diff --git a/packages/react/src/schema/ReactInlineContentSpec.tsx b/packages/react/src/schema/ReactInlineContentSpec.tsx index 63d81c014..67033ac2d 100644 --- a/packages/react/src/schema/ReactInlineContentSpec.tsx +++ b/packages/react/src/schema/ReactInlineContentSpec.tsx @@ -15,6 +15,7 @@ import { propsToAttributes, StyleSchema, BlockNoteEditor, + InlineContentSchemaWithInlineContent, } from "@blocknote/core"; import { NodeViewProps, @@ -27,19 +28,29 @@ import { FC } from "react"; import { renderToDOMSpec } from "./@util/ReactRenderUtil.js"; // this file is mostly analogoues to `customBlocks.ts`, but for React blocks +export type ReactCustomInlineContentRenderProps< + T extends CustomInlineContentConfig, + S extends StyleSchema, +> = { + inlineContent: InlineContentFromConfig; + updateInlineContent: ( + update: PartialCustomInlineContentFromConfig, + ) => void; + editor: BlockNoteEditor< + any, + InlineContentSchemaWithInlineContent, + S + >; + contentRef: (node: HTMLElement | null) => void; +}; + // extend BlockConfig but use a React render function export type ReactInlineContentImplementation< T extends CustomInlineContentConfig, // I extends InlineContentSchema, S extends StyleSchema, > = { - render: FC<{ - inlineContent: InlineContentFromConfig; - updateInlineContent: ( - update: PartialCustomInlineContentFromConfig, - ) => void; - contentRef: (node: HTMLElement | null) => void; - }>; + render: FC>; // TODO? // toExternalHTML?: FC<{ // block: BlockFromConfig; @@ -133,6 +144,7 @@ export function createReactInlineContentSpec< updateInlineContent={() => { // No-op }} + editor={editor} contentRef={refCB} /> ), @@ -168,6 +180,7 @@ export function createReactInlineContentSpec< >