murph-doc/docs/utils/editors/editorjs.md

125 lines
2.7 KiB
Markdown
Raw Normal View History

2022-04-19 11:38:55 +02:00
# Editor.js
Workspace in classic editors is made of a single contenteditable element, used to create different HTML markups. Editor.js workspace consists of separate Blocks: paragraphs, headings, images, lists, quotes, etc. Each of them is an independent contenteditable element (or more complex structure) provided by Plugin and united by Editor's Core.
Editor.js is fully integrated in Murph as form types.
2022-04-19 11:47:32 +02:00
![](/_static/img/editors/editorjs.png)
2022-04-19 11:38:55 +02:00
## Classic form
2023-02-09 21:50:06 +01:00
```php-inline
2022-04-19 11:38:55 +02:00
// src/Form/ExampleType.php
namespace App\Form\ExampleType;
use App\Core\Form\Type\EditorJsTextareaType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class ExampleType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add(
'myField',
EditorJsTextareaType::class
);
// ...
}
// ...
}
```
Modified data should return stringified JSON array if empty:
2023-02-09 21:50:06 +01:00
```php-inline
2022-04-19 11:38:55 +02:00
public function getMyField(): string
{
if (empty($this->myField)) {
$this->myField = '[]';
}
return $this->myField;
}
```
## Page form
2023-02-09 21:50:06 +01:00
```php-inline
2022-04-19 11:38:55 +02:00
// src/Entity/Page/YourPage.php
namespace App\Entity\Page;
use App\Core\Entity\Site\Page\Block;
use App\Core\Form\Site\Page\EditorJsTextareaBlockType;
2023-02-09 21:50:06 +01:00
#[@ORM\Entity]
2022-04-19 11:38:55 +02:00
class YourPage extends Page
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add(
'myBlock',
EditorJsTextareaBlockType::class,
[
'label' => 'My block',
'row_attr' => [
],
'options' => [
// options given to the sub form
],
]
);
// ...
}
public function setMyBlock(Block $block)
{
return $this->setBlock($block);
}
public function getMyBlock(): Block
{
2022-04-25 15:03:10 +02:00
return $this->getBlock('myBlock');
2022-04-19 11:38:55 +02:00
}
// ...
}
```
## Rendering
Editor.js will generate a JSON which contains blocks.
Supported blocks:
* paragraph
* header
* quote
* delimiter
* warning
* list
* nestedList
* checkList
* table
* code
* raw
* image
* link
To render HTML, the basic way is: `{{ value|editorjs_to_html }}`
If you want to render specific blocks: `{{ value|editorjs_to_html(['paragraph', 'header', ...])) }}`
Block have default templates stored in `vendor/murph/murph-core/src/core/Resources/views/editorjs`.
They can be simply overridden in `config/packages/app.yaml`:
2023-02-09 21:50:06 +01:00
```yaml
2022-04-19 11:38:55 +02:00
core:
editor_js:
blocks:
paragraph: 'path/to/paragraph.html.twig'
header: 'path/to/header.html.twig'
```