Skip to content

Commit af83441

Browse files
authored
Merge pull request #3 from nadar/copilot/fix-ac8599b6-d99d-410b-889c-97c7f9f0f5e5
Deprecate Types enum and introduce NodeType enum for consistency with MarkType
2 parents 94119c1 + 9926b50 commit af83441

File tree

5 files changed

+125
-42
lines changed

5 files changed

+125
-42
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ For example, to adjust the rendering of the image node, you can include your own
4545

4646
```php
4747
$html = (new \Nadar\ProseMirror\Parser())
48-
->replaceNode(\Nadar\ProseMirror\Types::image, fn(\Nadar\ProseMirror\Node $node) => '<img src="' . $node->getAttr('src') . '" class="this-is-my-class" />')
48+
->replaceNode(\Nadar\ProseMirror\NodeType::image, fn(\Nadar\ProseMirror\Node $node) => '<img src="' . $node->getAttr('src') . '" class="this-is-my-class" />')
4949
->toHtml($json);
5050
```
5151

52-
> To see all default nodes declared, refer to the `Types` class.
52+
> To see all default nodes declared, refer to the `NodeType` class.
5353
5454
If you have a custom node with a specific name, you can add it to the parser using the `addNode()` method:
5555

@@ -59,7 +59,7 @@ $html = (new \Nadar\ProseMirror\Parser())
5959
->toHtml($json);
6060
```
6161

62-
> The `addNode()` and `replaceNode()` methods are almost identical internally, except that `replaceNode` should only be used when altering the output of default nodes. You can view all by-default declared nodes in the `Types` class.
62+
> The `addNode()` and `replaceNode()` methods are almost identical internally, except that `replaceNode` should only be used when altering the output of default nodes. You can view all by-default declared nodes in the `NodeType` class.
6363
6464
### Customizing Marks
6565

src/NodeType.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Nadar\ProseMirror;
4+
5+
/**
6+
* Enumeration representing different node types for ProseMirror.
7+
*
8+
* @author Basil <git@nadar.io>
9+
* @since 1.0.0
10+
*/
11+
enum NodeType
12+
{
13+
case doc; // Represents the document type.
14+
case default; // Represents the default type.
15+
case paragraph; // Represents a paragraph.
16+
case blockquote; // Represents a blockquote.
17+
case image; // Represents an image type.
18+
case heading; // Represents a heading type.
19+
case youtube; // Represents a YouTube embed type.
20+
case bulletList; // Represents a bullet list type.
21+
case orderedList; // Represents an ordered list type.
22+
case listItem; // Represents a list item type.
23+
case text; // Represents a text type.
24+
case codeBlock;
25+
case horizontalRule;
26+
case tableRow;
27+
case tableCell;
28+
case table;
29+
case hardBreak;
30+
}

src/Parser.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,39 +29,39 @@ public function __construct()
2929
public function getDefaultNodeRenderers(): array
3030
{
3131
return [
32-
Types::doc->name => static fn (Node $node) => $node->renderContent(),
32+
NodeType::doc->name => static fn (Node $node) => $node->renderContent(),
3333

34-
Types::default->name => static fn (Node $node) => '<div>'.$node->getType() . ' does not exists. ' . $node->renderContent().'</div>',
34+
NodeType::default->name => static fn (Node $node) => '<div>'.$node->getType() . ' does not exists. ' . $node->renderContent().'</div>',
3535

36-
Types::paragraph->name => static fn (Node $node) => '<p>' . $node->renderContent() . '</p>',
36+
NodeType::paragraph->name => static fn (Node $node) => '<p>' . $node->renderContent() . '</p>',
3737

38-
Types::blockquote->name => static fn (Node $node) => '<blockquote>' . $node->renderContent() . '</blockquote>',
38+
NodeType::blockquote->name => static fn (Node $node) => '<blockquote>' . $node->renderContent() . '</blockquote>',
3939

40-
Types::image->name => static fn (Node $node) => '<img src="' . $node->getAttr('src') . '" alt="' . $node->getAttr('alt') . '" title="' . $node->getAttr('title') . '" />',
40+
NodeType::image->name => static fn (Node $node) => '<img src="' . $node->getAttr('src') . '" alt="' . $node->getAttr('alt') . '" title="' . $node->getAttr('title') . '" />',
4141

42-
Types::heading->name => static fn (Node $node) => '<h' . $node->getAttr('level') . '>' . $node->renderContent() . '</h' . $node->getAttr('level') . '>',
42+
NodeType::heading->name => static fn (Node $node) => '<h' . $node->getAttr('level') . '>' . $node->renderContent() . '</h' . $node->getAttr('level') . '>',
4343

44-
Types::youtube->name => static fn (Node $node) => '<iframe width="560" height="315" src="' . $node->getAttr('src') . '" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>',
44+
NodeType::youtube->name => static fn (Node $node) => '<iframe width="560" height="315" src="' . $node->getAttr('src') . '" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>',
4545

46-
Types::bulletList->name => static fn (Node $node) => '<ul>' . implode('', array_map(static fn ($child) => '<li>' . $node->renderChildNode($child) . '</li>', $node->getContent())) . '</ul>',
46+
NodeType::bulletList->name => static fn (Node $node) => '<ul>' . implode('', array_map(static fn ($child) => '<li>' . $node->renderChildNode($child) . '</li>', $node->getContent())) . '</ul>',
4747

48-
Types::orderedList->name => static fn (Node $node) => '<ol>' . implode('', array_map(static fn ($child) => '<li>' . $node->renderChildNode($child) . '</li>', $node->getContent())) . '</ol>',
48+
NodeType::orderedList->name => static fn (Node $node) => '<ol>' . implode('', array_map(static fn ($child) => '<li>' . $node->renderChildNode($child) . '</li>', $node->getContent())) . '</ol>',
4949

50-
Types::listItem->name => static fn (Node $node) => $node->renderContent(),
50+
NodeType::listItem->name => static fn (Node $node) => $node->renderContent(),
5151

52-
Types::codeBlock->name => static fn (Node $node) => '<pre><code>' . $node->renderContent() . '</code></pre>',
52+
NodeType::codeBlock->name => static fn (Node $node) => '<pre><code>' . $node->renderContent() . '</code></pre>',
5353

54-
Types::horizontalRule->name => static fn () => '<hr />',
54+
NodeType::horizontalRule->name => static fn () => '<hr />',
5555

56-
Types::hardBreak->name => static fn () => '<br />',
56+
NodeType::hardBreak->name => static fn () => '<br />',
5757

58-
Types::table->name => static fn (Node $node) => "<table>{$node->renderContent()}</table>",
58+
NodeType::table->name => static fn (Node $node) => "<table>{$node->renderContent()}</table>",
5959

60-
Types::tableRow->name => static fn (Node $node) => "<tr>{$node->renderContent()}</tr>",
60+
NodeType::tableRow->name => static fn (Node $node) => "<tr>{$node->renderContent()}</tr>",
6161

62-
Types::tableCell->name => static fn (Node $node) => "<td>{$node->renderContent()}</td>",
62+
NodeType::tableCell->name => static fn (Node $node) => "<td>{$node->renderContent()}</td>",
6363

64-
Types::text->name => function (Node $node) {
64+
NodeType::text->name => function (Node $node) {
6565
$text = $node->getText();
6666
foreach ($node->getMarks() as $mark) {
6767
/** @var Mark $mark */
@@ -92,11 +92,11 @@ public function getDefaultMarkRenderers(): array
9292
/**
9393
* Replaces a node renderer with a custom renderer.
9494
*
95-
* @param Types $type The type of node to replace the renderer for.
95+
* @param NodeType $type The type of node to replace the renderer for.
9696
* @param callable $renderer The custom renderer function.
9797
* @return $this
9898
*/
99-
public function replaceNode(Types $type, callable $renderer): self
99+
public function replaceNode(NodeType $type, callable $renderer): self
100100
{
101101
$this->nodeRenderers[$type->name] = $renderer;
102102
return $this;

src/Types.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@
77
*
88
* @author Basil <git@nadar.io>
99
* @since 1.0.0
10+
* @deprecated Use NodeType instead. This class will be removed in a future version.
1011
*/
11-
enum Types
12+
final class Types
1213
{
13-
case doc; // Represents the document type.
14-
case default; // Represents the default type.
15-
case paragraph; // Represents a paragraph.
16-
case blockquote; // Represents a blockquote.
17-
case image; // Represents an image type.
18-
case heading; // Represents a heading type.
19-
case youtube; // Represents a YouTube embed type.
20-
case bulletList; // Represents a bullet list type.
21-
case orderedList; // Represents an ordered list type.
22-
case listItem; // Represents a list item type.
23-
case text; // Represents a text type.
24-
case codeBlock;
25-
case horizontalRule;
26-
case tableRow;
27-
case tableCell;
28-
case table;
29-
case hardBreak;
14+
public const doc = NodeType::doc;
15+
public const default = NodeType::default;
16+
public const paragraph = NodeType::paragraph;
17+
public const blockquote = NodeType::blockquote;
18+
public const image = NodeType::image;
19+
public const heading = NodeType::heading;
20+
public const youtube = NodeType::youtube;
21+
public const bulletList = NodeType::bulletList;
22+
public const orderedList = NodeType::orderedList;
23+
public const listItem = NodeType::listItem;
24+
public const text = NodeType::text;
25+
public const codeBlock = NodeType::codeBlock;
26+
public const horizontalRule = NodeType::horizontalRule;
27+
public const tableRow = NodeType::tableRow;
28+
public const tableCell = NodeType::tableCell;
29+
public const table = NodeType::table;
30+
public const hardBreak = NodeType::hardBreak;
3031
}

tests/ParserTest.php

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
namespace Nadar\ProseMirror\Tests;
66

77
use Nadar\ProseMirror\Node;
8+
use Nadar\ProseMirror\NodeType;
89
use Nadar\ProseMirror\Parser;
9-
use Nadar\ProseMirror\Types;
1010
use PHPUnit\Framework\TestCase;
1111

1212
class ParserTest extends TestCase
@@ -104,7 +104,7 @@ public function testCustomNodeRenderer()
104104
EOT;
105105

106106
$wysiwyg = new Parser();
107-
$wysiwyg->replaceNode(Types::paragraph, function (Node $node) {
107+
$wysiwyg->replaceNode(NodeType::paragraph, function (Node $node) {
108108
return '<p>Custom Paragraph</p>';
109109
});
110110

@@ -114,4 +114,56 @@ public function testCustomNodeRenderer()
114114

115115
$this->assertSame('<div>BarFoo: Hello World</div>', $result);
116116
}
117+
118+
public function testBackwardCompatibilityWithTypes()
119+
{
120+
$json = <<<EOT
121+
{
122+
"type": "doc",
123+
"content": [
124+
{
125+
"type": "paragraph",
126+
"content": [
127+
{
128+
"type": "text",
129+
"text": "Test paragraph"
130+
}
131+
]
132+
},
133+
{
134+
"type": "image",
135+
"attrs": {
136+
"src": "test.jpg",
137+
"alt": "Test",
138+
"title": "Test Title"
139+
}
140+
}
141+
]
142+
}
143+
EOT;
144+
145+
// Test that Types constants are actually NodeType instances
146+
$this->assertSame(NodeType::paragraph, \Nadar\ProseMirror\Types::paragraph);
147+
$this->assertInstanceOf(NodeType::class, \Nadar\ProseMirror\Types::paragraph);
148+
149+
// Test with deprecated Types class constants
150+
$parserWithTypes = new Parser();
151+
$parserWithTypes->replaceNode(\Nadar\ProseMirror\Types::paragraph, function (Node $node) {
152+
return '<p class="types-class">' . $node->renderContent() . '</p>';
153+
});
154+
$resultTypes = $parserWithTypes->toHtml(json_decode($json, true));
155+
156+
// Test with new NodeType enum
157+
$parserWithNodeType = new Parser();
158+
$parserWithNodeType->replaceNode(NodeType::paragraph, function (Node $node) {
159+
return '<p class="nodetype-class">' . $node->renderContent() . '</p>';
160+
});
161+
$resultNodeType = $parserWithNodeType->toHtml(json_decode($json, true));
162+
163+
// Both should work and produce expected output
164+
$this->assertStringContainsString('<p class="types-class">Test paragraph</p>', $resultTypes);
165+
$this->assertStringContainsString('<p class="nodetype-class">Test paragraph</p>', $resultNodeType);
166+
$this->assertStringContainsString('<img src="test.jpg"', $resultTypes);
167+
$this->assertStringContainsString('<img src="test.jpg"', $resultNodeType);
168+
}
117169
}

0 commit comments

Comments
 (0)