forked from advoor/nova-editor-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNovaEditorJsConverter.php
164 lines (137 loc) · 4.41 KB
/
NovaEditorJsConverter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
declare(strict_types=1);
namespace Advoor\NovaEditorJs;
use Closure;
use EditorJS\EditorJS;
use EditorJS\EditorJSException;
use Illuminate\Support\HtmlString;
use JsonException;
use stdClass;
class NovaEditorJsConverter
{
/**
* List of callbacks that can render blocks
*
* @var array<Closure>
*/
protected array $renderCallbacks = [];
public function __construct()
{
$this->registerDefaultCallbacks();
}
/**
* Add a custom render callback for the given block.
*
* @param string $block Name of the block, as defined in the JSON
* @param callable $callback Closure that returns a string (or a Stringable)
*/
public function addRender(string $block, callable $callback): void
{
$this->renderCallbacks[$block] = $callback;
}
/**
* Renders the given EditorJS data to safe HTML.
*
* @return \Illuminate\Support\HtmlString Safe, directly returnable string.
*/
public function generateHtmlOutput(mixed $data): HtmlString
{
if (empty($data) || $data == new stdClass) {
return new HtmlString('');
}
// Clean non-string data
if (! is_string($data)) {
try {
$data = json_encode($data, JSON_THROW_ON_ERROR);
} catch (JsonException $exception) {
// noop
}
}
$config = config('nova-editor-js.validationSettings');
try {
// Initialize Editor backend and validate structure
$editor = new EditorJS($data, json_encode($config));
// Get sanitized blocks (according to the rules from configuration)
$blocks = $editor->getBlocks();
$htmlOutput = '';
foreach ($blocks as $block) {
if (array_key_exists($block['type'], $this->renderCallbacks)) {
$htmlOutput .= $this->renderCallbacks[$block['type']]($block);
}
}
return new HtmlString(
view('nova-editor-js::content', ['content' => $htmlOutput])->render()
);
} catch (EditorJSException $exception) {
// process exception
return new HtmlString(
"Something went wrong: {$exception->getMessage()}"
);
}
}
/**
* Registers all default render helpers
*/
protected function registerDefaultCallbacks(): void
{
$this->addRender(
'header',
fn ($block) => view('nova-editor-js::heading', $block['data'])->render()
);
$this->addRender(
'paragraph',
fn ($block) => view('nova-editor-js::paragraph', $block['data'])->render()
);
$this->addRender(
'list',
fn ($block) => view('nova-editor-js::list', $block['data'])->render()
);
$this->addRender(
'image',
fn ($block) => view('nova-editor-js::image', array_merge($block['data'], [
'classes' => $this->calculateImageClasses($block['data']),
]))->render()
);
$this->addRender(
'code',
fn ($block) => view('nova-editor-js::code', $block['data'])->render()
);
$this->addRender(
'linkTool',
fn ($block) => view('nova-editor-js::link', $block['data'])->render()
);
$this->addRender(
'checklist',
fn ($block) => view('nova-editor-js::checklist', $block['data'])->render()
);
$this->addRender(
'delimiter',
fn ($block) => view('nova-editor-js::delimiter', $block['data'])->render()
);
$this->addRender(
'table',
fn ($block) => view('nova-editor-js::table', $block['data'])->render()
);
$this->addRender(
'raw',
fn ($block) => view('nova-editor-js::raw', $block['data'])->render()
);
$this->addRender(
'embed',
fn ($block) => view('nova-editor-js::embed', $block['data'])->render()
);
}
/**
* @return string
*/
protected function calculateImageClasses($blockData)
{
$classes = [];
foreach ($blockData as $key => $data) {
if (is_bool($data) && $data === true) {
$classes[] = $key;
}
}
return implode(' ', $classes);
}
}