Skip to content

Commit 24c9319

Browse files
committed
Update tiptap-php, fix non-breaking-spaces, fix custom classes
1 parent fb5eccd commit 24c9319

File tree

28 files changed

+385
-136
lines changed

28 files changed

+385
-136
lines changed

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
],
1212
"require": {
1313
"getkirby/composer-installer": "^1.1",
14-
"ueberdosis/tiptap-php": "^1.4"
14+
"ueberdosis/tiptap-php": "^2.0"
15+
},
16+
"config": {
17+
"allow-plugins": {
18+
"getkirby/composer-installer": true
19+
}
1520
}
1621
}

composer.lock

Lines changed: 38 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
],
3939
'fieldMethods' => [
4040
'tiptapText' => function ($field, array $options = []) {
41+
// Add custom buttons from plugin configuration
42+
if (!isset($options['customButtons'])) {
43+
$options['customButtons'] = option('medienbaecker.tiptap.buttons', []);
44+
}
45+
4146
return convertTiptapToHtml(
4247
$field->value,
4348
$field->parent(),
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Medienbaecker\Tiptap\Extensions;
4+
5+
use Tiptap\Core\Extension;
6+
use Tiptap\Utils\HTML;
7+
8+
/**
9+
* Extension for adding custom attributes to any node type
10+
*/
11+
class CustomAttributes extends Extension
12+
{
13+
public static $name = 'customAttributes';
14+
15+
public function addOptions()
16+
{
17+
return [
18+
'customButtons' => [], // Custom button configurations from field options
19+
];
20+
}
21+
22+
public function addGlobalAttributes()
23+
{
24+
$globalAttributes = [];
25+
26+
// Process each custom button configuration
27+
foreach ($this->options['customButtons'] as $buttonName => $buttonConfig) {
28+
if (!isset($buttonConfig['attributes']) || !isset($buttonConfig['nodes'])) {
29+
continue;
30+
}
31+
32+
// Get the node types this button applies to
33+
$nodeTypes = $buttonConfig['nodes'];
34+
35+
// Create attribute definitions for each attribute in the button
36+
$attributeDefinitions = [];
37+
foreach ($buttonConfig['attributes'] as $attributeName => $defaultValue) {
38+
$attributeDefinitions[$attributeName] = [
39+
'default' => null,
40+
'parseHTML' => function ($DOMNode) use ($attributeName) {
41+
return $DOMNode->getAttribute($attributeName) ?: null;
42+
},
43+
'renderHTML' => function ($attributes) use ($attributeName) {
44+
if (!isset($attributes->{$attributeName}) || $attributes->{$attributeName} === null) {
45+
return null;
46+
}
47+
48+
// Handle potential object serialization issues
49+
$value = $attributes->{$attributeName};
50+
if (is_object($value) || is_array($value)) {
51+
// Skip invalid values to prevent [object Object] issues
52+
return null;
53+
}
54+
55+
return [$attributeName => $value];
56+
},
57+
];
58+
}
59+
60+
// Add global attributes for these node types
61+
$globalAttributes[] = [
62+
'types' => $nodeTypes,
63+
'attributes' => $attributeDefinitions,
64+
];
65+
}
66+
67+
return $globalAttributes;
68+
}
69+
}

lib/HtmlConverter.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Tiptap\Editor;
66
use Medienbaecker\Tiptap\Nodes\KirbyTagNode;
77
use Medienbaecker\Tiptap\Nodes\ConditionalTextNode;
8-
use Medienbaecker\Tiptap\Nodes\ParagraphWithClass;
8+
use Medienbaecker\Tiptap\Extensions\CustomAttributes;
99

1010
/**
1111
* Converts Tiptap JSON content to HTML
@@ -25,7 +25,8 @@ public static function convert($json, $parent, array $options = [])
2525
// Set default options
2626
$options = array_merge([
2727
'offsetHeadings' => 0,
28-
'allowHtml' => false
28+
'allowHtml' => false,
29+
'customButtons' => []
2930
], $options);
3031

3132
// Handle invalid input
@@ -75,16 +76,20 @@ public static function convert($json, $parent, array $options = [])
7576

7677
// Convert to HTML
7778
try {
79+
$extensions = [
80+
new \Tiptap\Extensions\StarterKit([
81+
'text' => false, // Disable default text node
82+
]),
83+
new ConditionalTextNode($options['allowHtml']), // Use our custom text handler
84+
new KirbyTagNode()
85+
];
86+
87+
$extensions[] = new CustomAttributes([
88+
'customButtons' => $options['customButtons']
89+
]);
90+
7891
$html = (new Editor([
79-
'extensions' => [
80-
new \Tiptap\Extensions\StarterKit([
81-
'text' => false, // Disable default text node
82-
'paragraph' => false, // Disable default paragraph node
83-
]),
84-
new ConditionalTextNode($options['allowHtml']), // Use our custom text handler
85-
new KirbyTagNode(),
86-
new ParagraphWithClass()
87-
]
92+
'extensions' => $extensions
8893
]))->setContent($json)->getHTML();
8994

9095
// Handle Smartypants

lib/helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
});
2222

2323
/**
24-
* Legacy function wrapper for Tiptap to HTML conversion
24+
* Convert Tiptap JSON to HTML
2525
* @param mixed $json Tiptap JSON content
2626
* @param object $parent Parent page/model for KirbyTag context
2727
* @param array $options Conversion options

src/components/TiptapInput.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ p.is-editor-empty:first-child::before {
256256
}
257257
258258
.Tiptap-invisible-character--non-breaking-space::before {
259-
content: ' ';
259+
content: ' ';
260260
position: absolute;
261261
height: 1px;
262262
background-color: currentColor;

src/components/Toolbar.vue

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ export default {
8282
return { type: '|' }
8383
}
8484
85-
// Handle object-style buttons (headings, paragraphClass, etc.)
8685
if (typeof button === 'object') {
87-
// Legacy headings format
8886
if (button.headings) {
8987
return {
9088
type: 'headings',

0 commit comments

Comments
 (0)