1 | <?php |
||
2 | |||
3 | namespace LeKoala\FormElements; |
||
4 | |||
5 | use SilverStripe\Forms\FormField; |
||
6 | use SilverStripe\View\Requirements; |
||
7 | use SilverStripe\Forms\HTMLEditor\HTMLEditorField; |
||
8 | use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig; |
||
9 | |||
10 | /** |
||
11 | * @link https://tiptap.dev/ |
||
12 | */ |
||
13 | class TipTapEditor extends HTMLEditorField |
||
14 | { |
||
15 | use BaseElement; |
||
16 | |||
17 | /** |
||
18 | * @config |
||
19 | * @var array |
||
20 | */ |
||
21 | private static $default_config = []; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
22 | |||
23 | /** |
||
24 | * @config |
||
25 | * @var boolean |
||
26 | */ |
||
27 | private static $enable_requirements = true; |
||
0 ignored issues
–
show
|
|||
28 | |||
29 | /** |
||
30 | * ID or instance of editorconfig |
||
31 | * |
||
32 | * @var string|TipTapEditorConfig |
||
33 | */ |
||
34 | protected $editorConfig = null; |
||
35 | |||
36 | public function __construct($name, $title = null, $value = '', $config = null) |
||
37 | { |
||
38 | parent::__construct($name, $title, $value); |
||
39 | $this->mergeDefaultConfig(); |
||
40 | |||
41 | if (!$config) { |
||
42 | $this->editorConfig = new TipTapEditorConfig; |
||
43 | |||
44 | // Required for sanitizer |
||
45 | $defaultConfig = HTMLEditorConfig::get(); |
||
46 | $this->editorConfig->setOption('valid_elements', $defaultConfig->getOption('valid_elements')); |
||
47 | $this->editorConfig->setOption('extended_valid_elements', $defaultConfig->getOption('extended_valid_elements')); |
||
48 | } |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Get the config (always as json object) |
||
53 | * @return string |
||
54 | */ |
||
55 | public function getConfigAsJson() |
||
56 | { |
||
57 | if (empty($this->config)) { |
||
58 | return '{}'; |
||
59 | } |
||
60 | $json = json_encode($this->editorConfig->getSettings()); |
||
61 | return $json; |
||
62 | } |
||
63 | |||
64 | public function Type() |
||
65 | { |
||
66 | return 'tiptap'; |
||
67 | } |
||
68 | |||
69 | public function extraClass() |
||
70 | { |
||
71 | return 'text ' . parent::extraClass(); |
||
72 | } |
||
73 | |||
74 | public function getAttributes() |
||
75 | { |
||
76 | // Fix CSS height based on rows |
||
77 | $rowHeight = $this->config()->get('fixed_row_height'); |
||
78 | $attributes = []; |
||
79 | if ($rowHeight) { |
||
80 | $height = $this->getRows() * $rowHeight; |
||
81 | $attributes['style'] = sprintf('height: %dpx;', $height); |
||
82 | } |
||
83 | |||
84 | return FormField::getAttributes(); |
||
85 | } |
||
86 | |||
87 | public function getElementAttributes() |
||
88 | { |
||
89 | $attrs = $this->elementAttributes; |
||
90 | $extraAttrs = $this->editorConfig->getExtraAttributes(); |
||
91 | return array_merge($attrs, $extraAttrs); |
||
92 | } |
||
93 | |||
94 | public function Field($properties = array()) |
||
95 | { |
||
96 | return $this->wrapInElement('tiptap-editor', $properties); |
||
97 | } |
||
98 | |||
99 | public static function requirements() |
||
100 | { |
||
101 | Requirements::javascript("lekoala/silverstripe-form-elements: client/custom-elements/tiptap-editor.min.js"); |
||
102 | } |
||
103 | } |
||
104 |