-
-
Notifications
You must be signed in to change notification settings - Fork 555
feat(type): optimize parameter type inference for the execute function of the slash menu #524
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
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@zcf0508 is attempting to deploy a commit to the TypeCell Team on Vercel. A member of the Team first needs to authorize it. |
Hi @zcf0508 ! Very interesting, thanks for providing this PR and especially for including new tests. I'm not sure the taken approach is necessary. I didn't know yet about However, I think the main issue you're trying to solve can also be fixed by removing the default value for
I think removing that fixes most of your tests and keeps the code simple. However, regardless of this, we're still running into an issue in |
I'm sorry that I forgot to run My idea is to let generics obtain as much information as possible. So I don't care about the |
@zcf0508 the build error is still there |
Yeah, I see the build error. I think we need that when users define menu using So we need to refactor the definition of type ReactBlockNoteEditorOptions<
BSpecs extends BlockSpecs,
ISpecs extends InlineContentSpecs,
SSpecs extends StyleSpecs
> = Omit<BlockNoteEditorOptions<BSpecs, ISpecs, SSpecs>, 'slashMenuItems'> & {
slashMenuItems: Array<
ReactSlashMenuItem<
BlockSchemaFromSpecs<NoInfer<BSpecs>>,
any,
any
>
>
}; And then we need change the const initEditor = <
BSpecs extends BlockSpecs,
ISpecs extends InlineContentSpecs,
SSpecs extends StyleSpecs
>(
options: Partial<ReactBlockNoteEditorOptions<BSpecs, ISpecs, SSpecs>>
) => ...
export const useBlockNote = <
BSpecs extends BlockSpecs = typeof defaultBlockSpecs,
ISpecs extends InlineContentSpecs = typeof defaultInlineContentSpecs,
SSpecs extends StyleSpecs = typeof defaultStyleSpecs
>(
options: Partial<ReactBlockNoteEditorOptions<BSpecs, ISpecs, SSpecs>> = {},
deps: DependencyList = []
) => ... Now, we can add const editor = useBlockNote({
domAttributes: {
editor: { class: styles.editor, "data-test": "editor" },
},
blockSpecs,
slashMenuItems: [
{
name: "Insert Alert",
execute: (editor) => { // the `BSchema` of `editor` will be `BlockSchemaFromSpecs<typeof blockSpecs>`
editor.insertBlocks(
[
{
type: "alert",
},
],
editor.getTextCursorPosition().block,
"after"
);
},
aliases: [
"alert",
"notification",
"emphasize",
"warning",
"error",
"info",
"success",
],
group: "Media",
icon: <RiAlertFill />,
hint: "Insert an alert block to emphasize text",
},
],
}); I think this is a good way for users to define a menu item. But this will prevent users from defining menu item out of |
This PR optimizes parameter type inference for the execute function of the slash menu.
getDefaultSlashMenuItems
function be able to infer types correctlyClose #191 (comment)