forked from alaminfirdows/laravel-editorjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLaravelEditorJs.php
47 lines (37 loc) · 1.15 KB
/
LaravelEditorJs.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
<?php
namespace AlAminFirdows\LaravelEditorJs;
use EditorJS\EditorJS;
use EditorJS\EditorJSException;
use Exception;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Str;
class LaravelEditorJs
{
/**
* Render blocks
*
* @param string $data
* @return string
*/
public function render(string $data): string
{
try {
$configJson = json_encode(config('laravel_editorjs.config') ?: []);
$editor = new EditorJS($data, $configJson);
$renderedBlocks = [];
foreach ($editor->getBlocks() as $block) {
$viewName = "laravel_editorjs::blocks." . Str::snake($block['type'], '-');
if (!View::exists($viewName)) {
$viewName = 'laravel_editorjs::blocks.not-found';
}
$renderedBlocks[] = View::make($viewName, [
'type' => $block['type'],
'data' => $block['data']
])->render();
}
return implode($renderedBlocks);
} catch (EditorJSException $e) {
throw new Exception($e->getMessage());
}
}
}