Skip to content

fix: Typing improvements #1736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/core/src/blocks/defaultBlockTypeGuards.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -31,6 +33,20 @@ export function checkDefaultBlockTypeInSchema<
);
}

export function checkBlockTypeInSchema<
BlockType extends string,
Config extends BlockConfig,
>(
blockType: BlockType,
blockConfig: Config,
editor: BlockNoteEditor<any, any, any>,
): 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,
Expand All @@ -50,6 +66,20 @@ export function checkDefaultInlineContentTypeInSchema<
);
}

export function checkInlineContentTypeInSchema<
InlineContentType extends string,
Config extends InlineContentConfig,
>(
inlineContentType: InlineContentType,
inlineContentConfig: Config,
editor: BlockNoteEditor<any, any, any>,
): editor is BlockNoteEditor<any, { [T in InlineContentType]: Config }, any> {
return (
inlineContentType in editor.schema.inlineContentSchema &&
editor.schema.inlineContentSchema[inlineContentType] === inlineContentConfig
);
}

export function checkBlockIsDefaultType<
BlockType extends keyof DefaultBlockSchema,
I extends InlineContentSchema,
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/schema/inlineContent/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ export type InlineContentImplementation<T extends InlineContentConfig> =
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<T extends InlineContentConfig> = {
Expand Down
27 changes: 20 additions & 7 deletions packages/react/src/schema/ReactInlineContentSpec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
propsToAttributes,
StyleSchema,
BlockNoteEditor,
InlineContentSchemaWithInlineContent,
} from "@blocknote/core";
import {
NodeViewProps,
Expand All @@ -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<T, S>;
updateInlineContent: (
update: PartialCustomInlineContentFromConfig<T, S>,
) => void;
editor: BlockNoteEditor<
any,
InlineContentSchemaWithInlineContent<T["type"], T>,
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<T, S>;
updateInlineContent: (
update: PartialCustomInlineContentFromConfig<T, S>,
) => void;
contentRef: (node: HTMLElement | null) => void;
}>;
render: FC<ReactCustomInlineContentRenderProps<T, S>>;
// TODO?
// toExternalHTML?: FC<{
// block: BlockFromConfig<T, I, S>;
Expand Down Expand Up @@ -133,6 +144,7 @@ export function createReactInlineContentSpec<
updateInlineContent={() => {
// No-op
}}
editor={editor}
contentRef={refCB}
/>
),
Expand Down Expand Up @@ -168,6 +180,7 @@ export function createReactInlineContentSpec<
>
<Content
contentRef={ref}
editor={editor}
inlineContent={
nodeToCustomInlineContent(
props.node,
Expand Down
Loading