Skip to content

Commit 0799319

Browse files
Add trix as an option
- Add a strategy pattern to WysiwygType - add new bundle config `bit_bag_sylius_cms.wysiwyg_editor` (options: trix/ckeditor, default: ckeditor) - add trix js - add trix twig widget (bitbag_sylius_cms_plugin_trix_strategy_widget)
1 parent f03dc4b commit 0799319

File tree

17 files changed

+295
-45
lines changed

17 files changed

+295
-45
lines changed

src/BitBagSyliusCmsPlugin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use BitBag\SyliusCmsPlugin\DependencyInjection\Compiler\MediaProviderPass;
1616
use Sylius\Bundle\CoreBundle\Application\SyliusPluginTrait;
1717
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
1819
use Symfony\Component\HttpKernel\Bundle\Bundle;
1920

2021
final class BitBagSyliusCmsPlugin extends Bundle

src/DependencyInjection/BitBagSyliusCmsExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ final class BitBagSyliusCmsExtension extends Extension implements PrependExtensi
2121

2222
public function load(array $configs, ContainerBuilder $container): void
2323
{
24+
$config = $this->processConfiguration($this->getConfiguration([], $container), $configs);
25+
26+
$container->setParameter('bitbag_sylius_cms_plugin.wysiwyg_editor', $config['wysiwyg_editor']);
2427
}
2528

2629
public function prepend(ContainerBuilder $container): void
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BitBag\SyliusCmsPlugin\DependencyInjection;
6+
7+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
8+
use Symfony\Component\Config\Definition\ConfigurationInterface;
9+
10+
final class Configuration implements ConfigurationInterface
11+
{
12+
public function getConfigTreeBuilder(): TreeBuilder
13+
{
14+
$treeBuilder = new TreeBuilder('bitbag_sylius_cms_plugin');
15+
$rootNode = $treeBuilder->getRootNode();
16+
17+
$rootNode
18+
->children()
19+
->enumNode('wysiwyg_editor')
20+
->values(['trix', 'ckeditor'])
21+
->defaultValue('ckeditor')
22+
;
23+
24+
return $treeBuilder;
25+
}
26+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file was created by developers working at BitBag
5+
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
6+
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace BitBag\SyliusCmsPlugin\Form\Type\Wysiwyg\Strategy;
12+
13+
use BitBag\SyliusCmsPlugin\Form\Type\Wysiwyg\WysiwygStrategyInterface;
14+
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
15+
use Symfony\Component\Form\FormInterface;
16+
use Symfony\Component\Form\FormView;
17+
use Symfony\Component\OptionsResolver\OptionsResolver;
18+
19+
abstract class AbstractWysiwygStrategy implements WysiwygStrategyInterface
20+
{
21+
public function configureOptions(OptionsResolver $resolver): void
22+
{
23+
}
24+
25+
public function buildView(FormView $view, FormInterface $form, array $options): void
26+
{
27+
}
28+
29+
public function getParent(): string
30+
{
31+
return TextareaType::class;
32+
}
33+
34+
public function getBlockPrefix(): string
35+
{
36+
return 'bitbag_wysiwyg';
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file was created by developers working at BitBag
5+
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
6+
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace BitBag\SyliusCmsPlugin\Form\Type\Wysiwyg\Strategy;
12+
13+
use FOS\CKEditorBundle\Form\Type\CKEditorType;
14+
use Symfony\Component\OptionsResolver\OptionsResolver;
15+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
16+
17+
class CkeditorStrategy extends AbstractWysiwygStrategy
18+
{
19+
public function __construct(private UrlGeneratorInterface $urlGenerator)
20+
{
21+
}
22+
23+
public function configureOptions(OptionsResolver $resolver): void
24+
{
25+
$resolver->setDefaults([
26+
'label' => 'bitbag_sylius_cms_plugin.ui.content',
27+
'config' => [
28+
'filebrowserUploadUrl' => $this->urlGenerator->generate('bitbag_sylius_cms_plugin_admin_upload_editor_image'),
29+
'bodyId' => 'bitbag-ckeditor',
30+
],
31+
]);
32+
}
33+
34+
public function getParent(): string
35+
{
36+
return CKEditorType::class;
37+
}
38+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file was created by developers working at BitBag
5+
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
6+
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace BitBag\SyliusCmsPlugin\Form\Type\Wysiwyg\Strategy;
12+
13+
use Symfony\Component\Form\FormInterface;
14+
use Symfony\Component\Form\FormView;
15+
16+
class TrixStrategy extends AbstractWysiwygStrategy
17+
{
18+
public function buildView(FormView $view, FormInterface $form, array $options): void
19+
{
20+
$view->vars['block_prefix'] = 'bitbag_sylius_cms_plugin_trix_strategy';
21+
}
22+
23+
public function getBlockPrefix(): string
24+
{
25+
return 'bitbag_sylius_cms_plugin_trix_strategy';
26+
}
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file was created by developers working at BitBag
5+
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
6+
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace BitBag\SyliusCmsPlugin\Form\Type\Wysiwyg;
12+
13+
use Symfony\Component\Form\FormInterface;
14+
use Symfony\Component\Form\FormView;
15+
use Symfony\Component\OptionsResolver\OptionsResolver;
16+
17+
interface WysiwygStrategyInterface
18+
{
19+
public function configureOptions(OptionsResolver $resolver): void;
20+
21+
public function buildView(FormView $view, FormInterface $form, array $options): void;
22+
23+
public function getParent(): string;
24+
25+
public function getBlockPrefix(): string;
26+
}

src/Form/Type/WysiwygType.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,43 @@
1010

1111
namespace BitBag\SyliusCmsPlugin\Form\Type;
1212

13-
use FOS\CKEditorBundle\Form\Type\CKEditorType;
13+
use BitBag\SyliusCmsPlugin\Form\Type\Wysiwyg\WysiwygStrategyInterface;
14+
use BitBag\SyliusCmsPlugin\Resolver\WysiwygStrategyResolverInterface;
1415
use Symfony\Component\Form\AbstractType;
16+
use Symfony\Component\Form\FormInterface;
17+
use Symfony\Component\Form\FormView;
1518
use Symfony\Component\OptionsResolver\OptionsResolver;
16-
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1719

1820
final class WysiwygType extends AbstractType
1921
{
20-
public function __construct(private UrlGeneratorInterface $urlGenerator)
22+
private WysiwygStrategyInterface $strategy;
23+
24+
public function __construct(private WysiwygStrategyResolverInterface $strategyResolver)
2125
{
2226
}
2327

2428
public function configureOptions(OptionsResolver $resolver): void
2529
{
26-
$resolver->setDefaults([
27-
'label' => 'bitbag_sylius_cms_plugin.ui.content',
28-
'config' => [
29-
'filebrowserUploadUrl' => $this->urlGenerator->generate('bitbag_sylius_cms_plugin_admin_upload_editor_image'),
30-
'bodyId' => 'bitbag-ckeditor',
31-
],
32-
]);
30+
$this->strategy->configureOptions($resolver);
31+
}
32+
33+
public function buildView(FormView $view, FormInterface $form, array $options): void
34+
{
35+
$this->strategy->buildView($view, $form, $options);
3336
}
3437

3538
public function getParent(): string
3639
{
37-
return CKEditorType::class;
40+
return $this->strategy->getParent();
3841
}
3942

4043
public function getBlockPrefix(): string
4144
{
42-
return 'bitbag_wysiwyg';
45+
return $this->strategy->getBlockPrefix();
46+
}
47+
48+
public function setStrategy(string $strategy): void
49+
{
50+
$this->strategy = $this->strategyResolver->getStrategy($strategy);
4351
}
4452
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BitBag\SyliusCmsPlugin\Resolver;
6+
7+
use BitBag\SyliusCmsPlugin\Form\Type\Wysiwyg\WysiwygStrategyInterface;
8+
9+
class WysiwygStrategyResolver implements WysiwygStrategyResolverInterface
10+
{
11+
public function __construct(
12+
private array $strategies,
13+
private string $default,
14+
) {
15+
}
16+
17+
public function getStrategy(string $wysiwygType): WysiwygStrategyInterface
18+
{
19+
return $this->strategies[$wysiwygType] ?? $this->strategies[$this->default];
20+
}
21+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BitBag\SyliusCmsPlugin\Resolver;
6+
7+
use BitBag\SyliusCmsPlugin\Form\Type\Wysiwyg\WysiwygStrategyInterface;
8+
9+
interface WysiwygStrategyResolverInterface
10+
{
11+
public function getStrategy(string $wysiwygType): WysiwygStrategyInterface;
12+
}

0 commit comments

Comments
 (0)