Skip to content
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

feat: Custom block serialization #257

Merged

Conversation

matthewlipski
Copy link
Collaborator

@matthewlipski matthewlipski commented Jun 22, 2023

This PR fixes how custom blocks are converted into HTML by implementing a custom DOMSerializer. The custom DOMSerializer allows us to use the HTML rendered by a custom block to the editor using its render function also when copying or exporting HTML. Alternatively, consumers can implement a serialize function for their custom blocks, which lets them override how the HTML conversion is done when exporting and copying to clipboard.

The PR also adds a new method for converting the HTML rendered by BlockNote, which includes many div elements for structuring the document, to HTML that more closely complies with the spec (removes structuring divs, wraps list elements in ul/ol elements, and removes non-list-item nesting). This method is called cleanHTML, but its implementation is basically the same as the old blocksToHTML. There is also a new markdown method which converts "clean" HTML to Markdown, similarly to the old blocksToMarkdown.

In addition, copying and pasting content within BlockNote is improved. Now, BlockNote will attempt to read blocknote/html from the clipboard when pasting content, otherwise it will use text/html or text/plain. Copying from BlockNote will write the selected content to the clipboard in the following formats:

  • text/plain: Markdown
  • text/html: "clean" HTML
  • blocknote/html: "structured" HTML

Since other editors & paste areas will generally read from text/plain or text/html, this ensures interoperability when copying content from BlockNote.

TODOs:

  • Implement custom DOMSerializer which uses a block's render function to serialize it, if the block has one (i.e. if it's a custom block).
  • Allow consumers to override the render function when serializing to export or clipboard.
  • Implement functions to convert a "structured" HTML string to "clean" HTML, and "clean" HTML to Markdown.
  • Write unit tests to ensure the custom DOMSerializer works with both default and custom blocks (just image for now).
  • Write unit tests to ensure "structured" HTML is converted to "clean" HTML correctly.
  • Write unit tests to ensure that overriding a block's render function when exporting and copying works.
  • Handle copy/paste in the editor to read content from & write content to the correct formats on the clipboard.
  • Ensure copy/paste handling works in Firefox and Safari.
  • Ensure the "clean" HTML is sanitized from unnecessary classes & attributes.

@vercel
Copy link

vercel bot commented Jun 22, 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 Nov 29, 2023 10:39am

@matthewlipski matthewlipski changed the title deat: Custom block serialization feat: Custom block serialization Jun 22, 2023
Copy link
Collaborator

@YousefED YousefED left a comment

Choose a reason for hiding this comment

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

Quite nice!!

  • Can you look into unit tests (not e2e) for the different scenarios (e.g.: vanilla / react, with / without inlinecontent, with / without children). We worked on these earlier, right?
  • Can we use this for markdown and html serialization as well (besides just copy to clipboard)?
  • What about parsing?

tests/utils/customblocks/ReactImage.tsx Show resolved Hide resolved
packages/react/src/ReactBlockSpec.tsx Outdated Show resolved Hide resolved
packages/core/src/extensions/Blocks/api/block.ts Outdated Show resolved Hide resolved
packages/core/src/extensions/Blocks/api/serialization.ts Outdated Show resolved Hide resolved
# Conflicts:
#	packages/core/src/BlockNoteEditor.ts
#	packages/core/src/BlockNoteExtensions.ts
#	packages/core/src/extensions/Blocks/api/block.ts
#	packages/core/src/extensions/Blocks/api/blockTypes.ts
#	packages/core/src/extensions/Blocks/api/defaultBlocks.ts
#	packages/react/src/ReactBlockSpec.tsx
#	tests/utils/customblocks/Image.tsx
#	tests/utils/customblocks/ReactImage.tsx
packages/core/src/api/serialization/serialization.test.ts Outdated Show resolved Hide resolved
packages/core/src/api/serialization/serialization.ts Outdated Show resolved Hide resolved
packages/core/src/extensions/Blocks/api/block.ts Outdated Show resolved Hide resolved
packages/core/src/api/serialization/serialization.test.ts Outdated Show resolved Hide resolved
packages/core/src/extensions/Blocks/api/block.ts Outdated Show resolved Hide resolved
packages/react/src/ReactBlockSpec.tsx Outdated Show resolved Hide resolved
# Conflicts:
#	packages/core/src/BlockNoteEditor.ts
#	packages/core/src/extensions/Blocks/nodes/Block.css
…locks' `blockContent` nodes from color default props and changed `toExternalHTML` typing for React custom blocks
* refactor types for blocks

* remove unused comment

* fix test

* run tests on all branches

* fix build

* change BlockFromBlockConfig to Block

* simplify customblockconfig

* rename BlockImplementation to TiptapBlockImplementation

* fix build

* add comment

* feat: tables (#413)

* fix table types

* add tablecontent

* clean BNUpdateBlock

* add partial inline content

* add contentNodeToTableContent

* first draft of tablehandles

* implement table functions

* fix styles

* fix imports

* create separate TableExtension

* improve types

* test some types

* Fixed setting selection for table blocks

* Fixed backspace deleting table if at start of cell

* small code fixes

* Implemented PR feedback

* Improved table row/column drag & drop UX

* Fixed table menus moving around, drag indicator flakiness, menu z-index issues, and drag preview

* Implemented PR feedback

* Implemented PR feedback

* Fixed drag handles sometimes not showing

* Fixed scrolling behaviour

* Small fixes

* Fixed table handles UI

* Fixed remaining UX/UI issues

* Removed redundant state from table handles plugin

* Implemented table drag & drop logic

* Added table enter handling

* Small fix

* feat: custom styles and custom inline content (#418)

* wip custom styles

* fix

* fix tests

* simplify PartialInlineContent

* custom inline content

* clean nodeconversions test

* streamline tests

* update tests

* move schema files

* add custom style test

* inline content + tests

* misc

* clean imports

* fix react tests

* add react nodeconversions tests

* move tests and add test for ReactStyles

* fix react tests

* basis of new examples

* add react examples

* fix bug

* misc fixes

* wip

* clean

* small cleanup

* add comments

* move funcs

* fix tests

* address PR feedback

* fix inline content types

* feat: HTML paste handling (#422)

* refactor parse

* fix parse-divsc

* add test case

* add extra test (that should be fixed)

* readd markdown functions

* fix tests

* remove unused file

* remove comments

* add comment

* nested list handling

* add todos

* added comment

* use refs for blocks (#424)

* use refs for blocks

* update react htmlConversion test

* Custom inline content and styles commands/copy & paste fixes (#425)

* Fixed commands and internal copy/paste for inline content

* Fixed internal copy/paste for styles

* Small cleanup

* fix some tests

---------

Co-authored-by: yousefed <yousefdardiry@gmail.com>

---------

Co-authored-by: Matthew Lipski <50169049+matthewlipski@users.noreply.github.com>

* use processSync

---------

Co-authored-by: Matthew Lipski <50169049+matthewlipski@users.noreply.github.com>

* fix build

---------

Co-authored-by: Matthew Lipski <50169049+matthewlipski@users.noreply.github.com>

---------

Co-authored-by: Matthew Lipski <matthewlipski@gmail.com>
Co-authored-by: Matthew Lipski <50169049+matthewlipski@users.noreply.github.com>

---------

Co-authored-by: Matthew Lipski <matthewlipski@gmail.com>
Co-authored-by: Matthew Lipski <50169049+matthewlipski@users.noreply.github.com>
@YousefED YousefED changed the base branch from main to refactor/tables-customelements-copypaste November 29, 2023 10:39
@YousefED YousefED merged commit 01c9ef6 into refactor/tables-customelements-copypaste Nov 29, 2023
1 of 3 checks passed
matthewlipski added a commit that referenced this pull request Dec 4, 2023
…copy / paste handling (#426)

* feat: Custom block serialization (#257)

* Added serialization for vanilla custom blocks

* Added serialization for React custom blocks

* Cleaned up serializer implementation - no longer uses function override

* Revert "Cleaned up serializer implementation - no longer uses function override"

This reverts commit b4f3fb6.

* Removed comment

* Added ability to set custom serialization and parse functions for custom blocks (parse still WIP)

* Fixed build and most runtime issues

* Added `react-dom` dependency

* Added PoC copy/paste handling

* Small changes & fixes

* Added serialization tests

* Changed copy/paste implementation

* Small fix

* Implemented PR feedback

* Converted styles from modules to regular CSS

* Implemented PR feedback

* Updated serialization test snapshots

* Updated serialization tests to use BlockNote API

* Commented out custom block parsing code (out of scope for this PR)

* Improved `nodeToBlock` typing

* Small fixes

* Fixed `destroy` function not getting passed to TipTap node view

* Updated comment regarding clipboard issues

* Major restructure of copy/paste code

* Reduced code duplication for HTML serializer & exporter

* Implemented PR feedback & cleaned up CSS class names

* Updated serialization unit test snapshots

* Changed `DOMOutputSpec` implementation for default blocks

* Fixed some CSS issues

* Implemented PR feedback

* Reverted `nodeToBlock` typing

* Made external HTML conversions no longer `async` and fixed Firefox clipboard reading

* Fixed image test issues and small changes

* Fixed remaining image test issues

* Updated serialization unit test snapshots

* Excluded `formatConversions` test

* Fixed HTML export for custom blocks with inline content

* Fixed duplicate `blockContainer` attributes getting added to custom blocks' `blockContent` nodes from color default props and changed `toExternalHTML` typing for React custom blocks

* Added React serialization unit tests and extra vanilla tests

* Updated image e2e snapshots

* Small e2e test fix

* Added comments

* Fixed error when copying only nested blocks

* refactor types for blocks (#412)

* refactor types for blocks

* remove unused comment

* fix test

* run tests on all branches

* fix build

* change BlockFromBlockConfig to Block

* simplify customblockconfig

* rename BlockImplementation to TiptapBlockImplementation

* fix build

* add comment

* feat: tables (#413)

* fix table types

* add tablecontent

* clean BNUpdateBlock

* add partial inline content

* add contentNodeToTableContent

* first draft of tablehandles

* implement table functions

* fix styles

* fix imports

* create separate TableExtension

* improve types

* test some types

* Fixed setting selection for table blocks

* Fixed backspace deleting table if at start of cell

* small code fixes

* Implemented PR feedback

* Improved table row/column drag & drop UX

* Fixed table menus moving around, drag indicator flakiness, menu z-index issues, and drag preview

* Implemented PR feedback

* Implemented PR feedback

* Fixed drag handles sometimes not showing

* Fixed scrolling behaviour

* Small fixes

* Fixed table handles UI

* Fixed remaining UX/UI issues

* Removed redundant state from table handles plugin

* Implemented table drag & drop logic

* Added table enter handling

* Small fix

* feat: custom styles and custom inline content (#418)

* wip custom styles

* fix

* fix tests

* simplify PartialInlineContent

* custom inline content

* clean nodeconversions test

* streamline tests

* update tests

* move schema files

* add custom style test

* inline content + tests

* misc

* clean imports

* fix react tests

* add react nodeconversions tests

* move tests and add test for ReactStyles

* fix react tests

* basis of new examples

* add react examples

* fix bug

* misc fixes

* wip

* clean

* small cleanup

* add comments

* move funcs

* fix tests

* address PR feedback

* fix inline content types

* feat: HTML paste handling (#422)

* refactor parse

* fix parse-divsc

* add test case

* add extra test (that should be fixed)

* readd markdown functions

* fix tests

* remove unused file

* remove comments

* add comment

* nested list handling

* add todos

* added comment

* use refs for blocks (#424)

* use refs for blocks

* update react htmlConversion test

* Custom inline content and styles commands/copy & paste fixes (#425)

* Fixed commands and internal copy/paste for inline content

* Fixed internal copy/paste for styles

* Small cleanup

* fix some tests

---------

Co-authored-by: yousefed <yousefdardiry@gmail.com>

---------

Co-authored-by: Matthew Lipski <50169049+matthewlipski@users.noreply.github.com>

* use processSync

---------

Co-authored-by: Matthew Lipski <50169049+matthewlipski@users.noreply.github.com>

* fix build

---------

Co-authored-by: Matthew Lipski <50169049+matthewlipski@users.noreply.github.com>

---------

Co-authored-by: Matthew Lipski <matthewlipski@gmail.com>
Co-authored-by: Matthew Lipski <50169049+matthewlipski@users.noreply.github.com>

---------

Co-authored-by: Matthew Lipski <matthewlipski@gmail.com>
Co-authored-by: Matthew Lipski <50169049+matthewlipski@users.noreply.github.com>

---------

Co-authored-by: Yousef <yousefdardiry@gmail.com>

* fix: change parse function to only return props

* fix lint

* add comment

* Made block, inline content, and style content get parsed from `contentDOM` instead of `dom`

* Small fixes

* Fixed table enter handling

* Updated unit tests

* Fixed parsing error when `dom` is same as `contentDOM` (#429)

* Fixed parsing error for styles/inline content where `dom` was the same as `contentDOM`

* Updated react snapshots

* Playground custom elements (#430)

* Added playgrounds for vanilla blocks, React blocks, and vanilla inline content

* fix renderHTML error

* clean examples

---------

Co-authored-by: yousefed <yousefdardiry@gmail.com>

* Add markdown tests (#428)

* update docs

* add markdown tests

* remove unused file

* add missing space to mention tests

* move and update playwright tests (#427)

* move and update playwright tests

* fix build

* fix test setup

* fix pw config

* fix test command

* Playwright refactor fixes (#436)

* Fixed issues with most tests

* Temporarily commented out failing unit test

* Temporarily commented out failing unit test

* Fixed remaining failing playwright tests

* Re-added test that was causing issues

* re-add tests

* add pw-report

* skip tests and edit css

* revert style

* add logs

* fix unit tests

---------

Co-authored-by: yousefed <yousefdardiry@gmail.com>

---------

Co-authored-by: Matthew Lipski <50169049+matthewlipski@users.noreply.github.com>

* Block backspace key event at start of custom editable inline content (#435)

* Fixed parsing error for styles/inline content where `dom` was the same as `contentDOM`

* Updated react snapshots

* Blocked backspace at start of custom editable inline content

* add comment

---------

Co-authored-by: yousefed <yousefdardiry@gmail.com>

* fix empty table content

* update comments

* extract transformPasted

* widen slashmenu typings

* Updated docs for custom blocks and added tables (#442)

---------

Co-authored-by: Matthew Lipski <50169049+matthewlipski@users.noreply.github.com>
Co-authored-by: Matthew Lipski <matthewlipski@gmail.com>
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.

None yet

2 participants