Skip to content

Custom blocks example typing improvements #202

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

Merged

Conversation

YousefED
Copy link
Collaborator

I managed to at least get the correct type of block in the render functions. Unfortunately, getting the correct type of "editor" is impossible afaik, unless we decide to separate the "render" functions from the other spec definitions. Theoretically, it's not necessary to define them at the same time as they serve different purposes

Upgraded TS as part of this process to make sure I wasn't running into issues that have meanwhile been fixed

@YousefED YousefED requested a review from matthewlipski May 23, 2023 14:06
@vercel
Copy link

vercel bot commented May 23, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
blocknote-website ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 24, 2023 1:03pm

Copy link
Collaborator

@matthewlipski matthewlipski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Left a few comments, mostly just to clarify since complex types can be a little hard to read & understand.

* This is typed generically. If you want an editor with your custom schema, you need to
* cast it manually, e.g.: `const e = editor as BlockNoteEditor<typeof mySchema>;`
*/
editor: BlockNoteEditor<BSchema & { [k in Type]: BlockSpec<Type, PSchema> }>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I understand what this is for but just to clarify:

If I make an alert custom block like this:

const alertPropSchema = {...} satisfies PropSchema;

const Alert: BlockConfig<"alert", typeof alertPropSchema, ..., ...> = {...};

Then inside render, editor will be of type:

BlockNoteEditor<BlockSchema & BlockSpec<"alert",  typeof alertPropSchema>>

Whereas before it was just

BlockNoteEditor<BlockSchema>

Is that correct?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and BlockSchema was super generic. Now, at least we know it countains the type of the block being added

/**
* The custom block to render
*/
block: SpecificBlock<
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be simplified to

block: Block<{ [k in Type]: BlockSpec<Type, PSchema> }>

?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also as a side note - right now you could, if you wanted to, update block to a different type. Not relevant for now, but will be when we need to sort out cleaning up listeners, since I think that a listener for a custom block should be removed if the block changes type.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it can, because the children of the block should be able to be of other types

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point, missed that

@@ -141,6 +149,13 @@ export type Block<BSchema extends BlockSchema> =
children: Block<BSchema>[];
};

export type SpecificBlock<
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth adding a comment for a bit of clarification (the other types are now also commented on the custom-blocks-example branch)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, will do on main branch later

@matthewlipski matthewlipski self-requested a review May 24, 2023 12:54
…g-improvements

# Conflicts:
#	packages/core/src/extensions/Blocks/api/block.ts
#	packages/core/src/extensions/Blocks/api/blockTypes.ts
#	packages/core/src/extensions/Blocks/api/defaultBlocks.ts
@matthewlipski matthewlipski merged commit 018e676 into custom-blocks-example May 24, 2023
YousefED added a commit that referenced this pull request May 31, 2023
* simplify formattingtoolbar

* Fixed React component types and added customizable formatting toolbar factory

* Finished formatting toolbar customization with old props

* Changed formatting toolbar props to use BlockNoteEditor

* Fixed text alignment with basic selection object

* Fixed block nesting tests

* Removed multiple block shorthand for updateBlock

* Added comments

* Added basic mouse cursor position

* Added drag handle menu customization API

* Copied changes from PR and minor improvements

* Table block test

* wip: custom blocks typing and API

* add comments

* fix nodeConversions, add schema

* comments

* Implemented renderHTML and parseHTML for createCustomBlock

* Temp changes

* Mostly implemented stricter typing for custom blocks

* Cleanup & fixed build issues

* Fixed useBlockNote and improved some typing

* Implemented PR feedback

* Minor changes

* Made custom blocks use node views

* Created custom example and setup component testing

* Changed custom block `renderHTML` implementation

* Changed custom block `renderHTML` implementation

* Changed custom block testing setup and added custom block examples

* Slightly changed `blockConfig.render` type

* Fixed merge issues

* custom blocks react poc

* Added internal custom block copy/paste tests

* Small fix

* Added external custom block copy/paste tests

* Added expected serialized HTML for example custom blocks

* Updated playwright scripts

* Updated component tests and added serializer extension

* Updated React custom block API

* Reverted `App.tsx`

* Fixed `test:updateSnaps` script

* Added external copypaste snapshots

* Fixed failing tests

* Copy/paste tests are now skipped for WebKit

* Removed old `console.log`s

* Small change to `test-ct` script

* Removed image block from `defaultBlocks.ts`

* Updated component tests & snapshots

* General cleanup

* Updated firefox colors snapshot

* remove unnecessary generics for slash menu items

* fix types

* hack around TextAlignButton types

* Custom blocks example typing improvements (#202)

* improve typings

* add tests for types

---------

Co-authored-by: Matthew Lipski <[email protected]>

* Implemented most PR feedback

* Implemented most PR feedback

* Minor fixes to custom blocks

* Custom blocks docs (#217)

* Fixed custom block placeholders, formatting toolbar, and sizing

* Small styling changes

* Made text align button not show up when selected block/s doesn't/don't support the prop

* Updated docs

* Finished 1st draft custom block docs and demo

* Added type arguments in custom block docs & minor changes

* Implemented PR feedback

* Updated unit tests

* simplify docs

* package lock update

* add comment

* retreat, fallback slash command to "any"

* update packages incl prosemirror-view (#223)

* update packages incl prosemirror-view

* fix build

* fix ReactAlert + add logs

---------

Co-authored-by: yousefed <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants