Rich Editor: why does a null state become a paragraph? Working on a PR for forced content structures

Hi! I am currently working on a PR to support forced content structures (as seen in https://tiptap.dev/docs/examples/advanced/forced-content-structure) in the Rich Editor which will allow you to do something like this:
RichEditor::make('content')->contentStructure('heading block*')
RichEditor::make('content')->contentStructure('heading block*')
I quickly ran into the issue that the Rich Editor always forces an empty paragraph when the state is null, as seen here: Snippet from RichEditorStateCast.php
$editor = $this->richEditor->getTipTapEditor()
->setContent($state ?? [
'type' => 'doc',
'content' => [
[
'type' => 'paragraph',
'content' => [],
],
],
])
$editor = $this->richEditor->getTipTapEditor()
->setContent($state ?? [
'type' => 'doc',
'content' => [
[
'type' => 'paragraph',
'content' => [],
],
],
])
This conflicts with the contentStructure feature, since you could configure that to start with a heading - or any other block - instead. I'm sure there is a good reason for the empty paragraph state which I would love to know about. I'm considering a few options to solve this in my PR: Option 1: leave the state at null in RichEditorStateCast.php whenever a contentStructure is specified Option 2: add something like RichEditor::make('content')->contentStructure('heading block*')->omitInitialParagraph() Option 3: leave it to the user to pass a default structure when using contentStructure, for example:
->default(["type" => "doc","content" => [["type" => "heading","content" => [],"attrs" => ["level" => 1]],["type" => "paragraph","content" => [],]]])
->default(["type" => "doc","content" => [["type" => "heading","content" => [],"attrs" => ["level" => 1]],["type" => "paragraph","content" => [],]]])
However: this seems to cause issues with the ->require() validation Would love your input for this matter!
2 Replies
Dennis Koch
Dennis Koch3d ago
I think it's just the default of TipTap. Nothing that Filament applies.
Thijs
ThijsOP3d ago
@Dennis Koch it is something that Filament applies though, see the RichEditorStateCast.php snippet in my post. I didn't encounter that behaviour in the filament-tiptap-editor package or Tiptap itself when I applied a content structure there. So that made me wonder why an empty paragraph is the default state, since there might have been a good reason it has been put there

Did you find this page helpful?