Skip to content

Commit 9faec3f

Browse files
committed
First version
0 parents  commit 9faec3f

File tree

12 files changed

+215
-0
lines changed

12 files changed

+215
-0
lines changed

Kernel.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Mahlenko\EditorJS;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class Kernel extends ServiceProvider
8+
{
9+
public function register(): void {
10+
$this->loadViewsFrom(__DIR__ .'/views', 'editor.js');
11+
12+
$this->publishes([
13+
__DIR__ .'/views' => resource_path('views/vendor/editor.js'),
14+
], 'editor.js');
15+
}
16+
}

Render.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Mahlenko\EditorJS;
4+
5+
use Illuminate\Support\Facades\View;
6+
7+
class Render {
8+
static public function make(string $json, string $prefix = 'editor__') {
9+
return View::make('editor.js::content', (array) json_decode($json))
10+
->with('prefix', $prefix);
11+
}
12+
}

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "mahlenko/editor-js-laravel",
3+
"autoload": {
4+
"psr-4": {
5+
"Mahlenko\\EditorJS\\": "src/"
6+
}
7+
},
8+
"authors": [
9+
{
10+
"name": "Makhlenko Sergey",
11+
"email": "mahlenko-weblive@yandex.ru"
12+
}
13+
],
14+
"require": {},
15+
"extra": {
16+
"laravel": {
17+
"providers": [
18+
"Mahlenko\\EditorJS\\Kernel"
19+
]
20+
}
21+
}
22+
}

readme.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Editor.js для Laravel
2+
3+
Преобразует JSON полученный с фронтенда от [Editor.js](https://editorjs.io) в HTML
4+
используя Laravel blade компоненты.
5+
6+
## Установка
7+
Для установки используйте `composer`:
8+
9+
```shell
10+
composer required mahlenko/editorjs-laravel
11+
```
12+
13+
Laravel автоматически подключит пакет `editorjs-laravel`.
14+
15+
## Инструкция
16+
Подключите библиотеку в вашей скрипте, например для модели `Page`:
17+
18+
```php
19+
use Mahlenko\EditorJS\Render;
20+
```
21+
22+
Добавьте в вашу модель метод `getHtmlAttribute()`,
23+
```php
24+
use Mahlenko\EditorJS\Render;
25+
use Illuminate\Database\Eloquent\Model;
26+
27+
class Page extends Model {
28+
// ...
29+
30+
public function getHtmlAttribute(): string {
31+
return Render::make($this->body);
32+
}
33+
}
34+
```
35+
36+
## Ручная установка
37+
38+
Для Laravel выше 11 версии, добавьте в `bootstrap/providers.php`
39+
провайдер:
40+
```php
41+
<?php
42+
43+
return [
44+
App\Providers\AppServiceProvider::class,
45+
// other packages ...
46+
47+
\Mahlenko\EditorJS\Kernel::class, // <- append row
48+
];
49+
```
50+
51+
Для Laravel ниже 11 версии, добавьте в файл `config/app.php`:
52+
```php
53+
/*
54+
* Application Service Providers...
55+
*/
56+
57+
// other packages ...
58+
59+
\Mahlenko\EditorJS\Kernel::class, // <- append row
60+
```
61+
62+
## Настройка
63+
Вы можете изменить отображение элементов, или добавить собственные типы,
64+
после публикации ресурсов:
65+
66+
```bash
67+
php artisan vendor:publish --tag=editor.js
68+
```
69+
70+
При использовании `Laravel Sail`
71+
72+
```bash
73+
./vendor/bin/sail artisan vendor:publish --tag=editor.js
74+
```

views/components/embed.blade.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@props(['block'])
2+
3+
<iframe
4+
id="{{ $block->id ?? null }}"
5+
{{ $attributes }}
6+
src="{{ $block->data->embed }}"
7+
width="{{ $block->data->width }}"
8+
height="{{ $block->data->height }}"
9+
scrolling="no"
10+
frameborder="0"
11+
allowtransparency="true"
12+
allowfullscreen="true"
13+
></iframe>

views/components/header.blade.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@props(['block'])
2+
{{ $attributes->setAttributes(['class' => "{$attributes->get('class')}-{$block->data->level}"]) }}
3+
4+
<h{{ $block->data->level }}
5+
id="{{ $block->id ?? null }}"
6+
{{ $attributes }}
7+
>{{ $block->data->text }}</h{{ $block->data->level }}>

views/components/list.blade.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@props(['block'])
2+
3+
{{--@dump($block)--}}
4+
@php($tag = $block->data->style === 'ordered' ? 'ol' : 'ul')
5+
6+
<{{ $tag }} id="{{ $block->id ?? null }}" {{ $attributes }}>
7+
@foreach($block->data->items as $item)
8+
<li>{{ $item }}</li>
9+
@endforeach
10+
</{{ $tag }}>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@props(['block'])
2+
<p id="{{ $block->id ?? null }}" {{ $attributes }}>{!! $block->data->text !!}</p>

views/components/quote.blade.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@props(['block'])
2+
<blockquote id="{{ $block->id ?? null }}"
3+
{{ $attributes }}
4+
data-alignment="{{ $block->data->alignment }}">
5+
<p>{{ $block->data->text }}</p>
6+
@if ($block->data->caption)
7+
<footer>{{ $block->data->caption }}</footer>
8+
@endif
9+
</blockquote>

views/components/table.blade.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
@props(['block'])
2+
<table
3+
id="{{ $block->id ?? null }}"
4+
{{ $attributes }}
5+
>
6+
@if ($block->data->withHeadings)
7+
<thead>
8+
<tr>
9+
@foreach($block->data->content[0] as $column)
10+
<th>{{ $column }}</th>
11+
@endforeach
12+
</tr>
13+
</thead>
14+
<tbody>
15+
@foreach(array_slice($block->data->content, 1) as $row)
16+
<tr>
17+
@foreach($row as $column)
18+
<td>{{ $column }}</td>
19+
@endforeach
20+
</tr>
21+
@endforeach
22+
</tbody>
23+
@else
24+
<tbody>
25+
@foreach($block->data->content as $row)
26+
<tr>
27+
@foreach($row as $column)
28+
<td>{{ $column }}</td>
29+
@endforeach
30+
</tr>
31+
@endforeach
32+
</tbody>
33+
@endif
34+
</table>

0 commit comments

Comments
 (0)