Passed
Pull Request — 2.x (#1360)
by Harings
11:27
created

Wysiwyg   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 59
c 1
b 0
f 0
dl 0
loc 97
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 41 1
B render() 0 33 6
1
<?php
2
3
namespace A17\Twill\View\Components;
4
5
class Wysiwyg extends TwillFormComponent
6
{
7
    public $translated;
8
    public $required;
9
    public $maxlength;
10
    public $options;
11
    public $placeholder;
12
    public $note;
13
    public $disabled;
14
    public $readonly;
15
    public $editSource;
16
    public $toolbarOptions;
17
    public $inModal;
18
    public $hideCounter;
19
    public $type;
20
    public $limitHeight;
21
    public $syntax;
22
    public $customTheme;
23
    public $customOptions;
24
    public $default;
25
26
    public function __construct(
27
        $name,
28
        $label,
29
        $translated = false,
30
        $required = false,
31
        $maxlength = null,
32
        $options = null,
33
        $placeholder = null,
34
        $note = null,
35
        $default = null,
36
        $disabled = false,
37
        $readonly = false,
38
        $editSource = false,
39
        $toolbarOptions = null,
40
        $inModal = false,
41
        $hideCounter = false,
42
        $type = 'quill',
43
        $limitHeight = false,
44
        $syntax = false,
45
        $customTheme = 'github',
46
        $customOptions = null
47
    ) {
48
        parent::__construct($name, $label);
49
        $this->translated = $translated;
50
        $this->required = $required;
51
        $this->maxlength = $maxlength;
52
        $this->options = $options;
53
        $this->placeholder = $placeholder;
54
        $this->note = $note;
55
        $this->disabled = $disabled;
56
        $this->readonly = $readonly;
57
        $this->editSource = $editSource;
58
        $this->toolbarOptions = $toolbarOptions;
59
        $this->inModal = $inModal;
60
        $this->hideCounter = $hideCounter;
61
        $this->type = $type;
62
        $this->limitHeight = $limitHeight;
63
        $this->syntax = $syntax;
64
        $this->customTheme = $customTheme;
65
        $this->customOptions = $customOptions;
66
        $this->default = $default;
67
    }
68
69
    public function render()
70
    {
71
        if ($this->toolbarOptions) {
72
            $toolbarOptions = array_map(static function ($option) {
73
                if ($option === 'list-unordered') {
74
                    return (object)['list' => 'bullet'];
75
                }
76
                if ($option === 'list-ordered') {
77
                    return (object)['list' => 'ordered'];
78
                }
79
                if ($option === 'h1') {
80
                    return (object)['header' => 1];
81
                }
82
                if ($option === 'h2') {
83
                    return (object)['header' => 2];
84
                }
85
                return $option;
86
            }, $this->toolbarOptions);
87
88
            $toolbarOptions = [
89
                'modules' => [
90
                    'toolbar' => $toolbarOptions,
91
                    'syntax' => $this->syntax,
92
                ],
93
            ];
94
        }
95
96
        $options = $this->customOptions ?? $toolbarOptions ?? false;
97
98
        return view('twill::partials.form._wysiwyg', [
99
            'theme' => $this->customTheme,
100
            'activeSyntax' => $this->syntax,
101
            'options' => $options,
102
        ]);
103
    }
104
}
105