Skip to content

Commit

Permalink
Merge pull request #515 from sibizwolle/extension-placeholders
Browse files Browse the repository at this point in the history
Node specific placeholders and showOnlyCurrent property
  • Loading branch information
awcodes authored Jan 6, 2025
2 parents 783a989 + d612d23 commit 83abff3
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 48 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,33 @@ If you are using any of the tools that require a modal (e.g. Insert media, Inser
{{ $this->modal }}
```

### Placeholders

You can easily set a placeholder, the Filament way:

```php
TiptapEditor::make('content')
->placeholder('Write something...')
```

You can define specific placeholders for each node type using the `->nodePlaceholders()` method. This method accepts an associative array, where the keys are the node type names, and the values are the corresponding placeholder texts.

```php
TiptapEditor::make('content')
->nodePlaceholders([
'paragraph' => 'Start writing your paragraph...',
'heading' => 'Insert a heading...',
])
```

The `->showOnlyCurrentPlaceholder()` method allows you to control whether placeholders are shown for all nodes simultaneously or only for the currently active node.

```php
TiptapEditor::make('content')
// All nodes will immediately be displayed, instead of only the selected node
->showOnlyCurrentPlaceholder(false)
```

## Custom Extensions

You can add your own extensions to the editor by creating the necessary files and adding them to the config file extensions array.
Expand Down
3 changes: 2 additions & 1 deletion resources/css/plugin.css
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,8 @@
font-family: theme("fontFamily.mono") monospace;
}

.tiptap-editor p.is-editor-empty:first-child::before {
.tiptap-editor p.is-editor-empty:first-child::before,
.tiptap-wrapper .is-empty::before {
content: attr(data-placeholder);
float: left;
height: 0;
Expand Down
2 changes: 1 addition & 1 deletion resources/dist/filament-tiptap-editor.css

Large diffs are not rendered by default.

84 changes: 42 additions & 42 deletions resources/dist/filament-tiptap-editor.js

Large diffs are not rendered by default.

19 changes: 16 additions & 3 deletions resources/js/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ export default function tiptap({
floatingMenuTools = [],
placeholder = null,
mergeTags = [],
customDocument = null,
customDocument = null,
nodePlaceholders = [],
showOnlyCurrentPlaceholder = true,
}) {
let editor = null;

Expand Down Expand Up @@ -205,8 +207,16 @@ export default function tiptap({
TiptapBlock,
];

if (placeholder && (!disabled)) {
extensions.push(Placeholder.configure({placeholder}));
if ((placeholder || nodePlaceholders) && (!disabled)) {
extensions.push(
Placeholder.configure({
showOnlyCurrent: showOnlyCurrentPlaceholder,
placeholder: ({ node }) => {
const nodeSpecificPlaceholder = nodePlaceholders?.[node.type.name];
return nodeSpecificPlaceholder || placeholder || '';
},
})
);
}

if (tools.length) {
Expand Down Expand Up @@ -260,6 +270,9 @@ export default function tiptap({
appendTo: this.$refs.element,
zIndex: 10,
},
shouldShow: ({state, from, to}) => {
return isActive(state, 'paragraph');
}
}))

this.floatingMenuTools.forEach((tool) => {
Expand Down
6 changes: 5 additions & 1 deletion resources/views/tiptap-editor.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
$shouldSupportBlocks = $shouldSupportBlocks();
$shouldShowMergeTagsInBlocksPanel = $shouldShowMergeTagsInBlocksPanel();
$customDocument = $getCustomDocument();
$nodePlaceholders = $getNodePlaceholders();
$showOnlyCurrentPlaceholder = $getShowOnlyCurrentPlaceholder();
@endphp

<x-dynamic-component
Expand Down Expand Up @@ -44,7 +46,9 @@ class="relative z-0 tiptap-wrapper rounded-md bg-white dark:bg-gray-900 focus-wi
floatingMenuTools: @js($floatingMenuTools),
placeholder: @js($getPlaceholder()),
mergeTags: @js($mergeTags),
customDocument: @js($customDocument)
customDocument: @js($customDocument),
nodePlaceholders: @js($nodePlaceholders),
showOnlyCurrentPlaceholder: @js($showOnlyCurrentPlaceholder)
})"
x-init="$nextTick(() => { init() })"
x-on:click.away="blur()"
Expand Down
42 changes: 42 additions & 0 deletions src/TiptapEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class TiptapEditor extends Field

protected string | Closure | null $customDocument = null;

protected array | Closure | null $nodePlaceholders = null;

protected array | bool | null $showOnlyCurrentPlaceholder = false;

protected array $gridLayouts = [
'two-columns',
'three-columns',
Expand Down Expand Up @@ -407,6 +411,44 @@ public function getCustomDocument(): ?string
return $this->evaluate($this->customDocument);
}

/**
* Set placeholders for specific node types.
*
* You can provide an associative array where the keys are the node type names and the
* values are the corresponding placeholders. For instance:
*
* ['heading' => 'What’s the title?', 'paragraph' => 'Start writing here...']
* @return $this
*/
public function nodePlaceholders(array | Closure | null $nodePlaceholders): static
{
$this->nodePlaceholders = $nodePlaceholders;

return $this;
}

public function getNodePlaceholders(): ?array
{
return $this->evaluate($this->nodePlaceholders);
}

/**
* Show placeholder decorations only in currently selected node.
*
* @return $this
*/
public function showOnlyCurrentPlaceholder(bool | Closure | null $showOnlyCurrent): static
{
$this->showOnlyCurrentPlaceholder = $showOnlyCurrent;

return $this;
}

public function getShowOnlyCurrentPlaceholder(): ?bool
{
return $this->evaluate($this->showOnlyCurrentPlaceholder);
}

public function shouldDisableStylesheet(): bool
{
return $this->shouldDisableStylesheet ?? config('filament-tiptap-editor.disable_stylesheet');
Expand Down

0 comments on commit 83abff3

Please sign in to comment.