Rich text editors: TinyMCE and CKEditor form()
Which rich text editor a form2 textarea can have, how to turn each one on, and how they follow Bootstrap 3, 5 and the dark mode.
Опції
| Опція | Значення |
|---|---|
tinymce($name, $desc, $extra) | TinyMCE 8, the newer editor: a textarea with data-yf-tinymce that the yf_tinymce starter turns into the editor. $extra['tinymce'] = ['config' => [...]] sets tinymce.init() options. See tinymce(). |
'tinymce' => ['config' => [...]] of textarea() | TinyMCE 4 of before while yf_form2_tinymce::$LEGACY_TEXTAREA is on (default); ['legacy' => false] gives this one field TinyMCE 8. |
'ckeditor' => ['config' => [...]] of textarea() | CKEditor 4.21 (the pinned ckeditor asset): CKEDITOR.replace() with these options: an array (sent as JSON), a script string, or a callable returning the script. CKEditor is the editor of the admin modules. |
'ckeditor' => true | CKEditor with the site template ckeditor_config (the admin theme has one), else with CKEditor's own defaults (it starts every textarea of class ckeditor). |
'ckeditor' => ['hidden_id' => ...] | Inline CKEditor on a container() block: its HTML is copied into that hidden input when the form is sent. |
yf_form2_tinymce::$CONFIG | TinyMCE 8 options of every editor of the site ($PROJECT_CONF['form2_tinymce']['CONFIG']); a field's config wins over them. |
yf_form2_tinymce::$LICENSE_KEY | gpl (default, TinyMCE 8 is GPL-2.0-or-later) or a commercial Tiny key. |
yf_form2_tinymce::$DARK_SKIN | TinyMCE dark skin: null (default) loads it when the page has a Bootstrap 5 colour mode, true always, false never. |
yf_form2::$CLASS_CKEDITOR | Class a CKEditor field gets (ckeditor). |
Приклад
Код
return form(['tm_body' => '<p>TinyMCE: <b>bold</b>, <i>italic</i>, a <a href="https://yfix.net/">link</a>.</p>'])
->tinymce('tm_body', 'TinyMCE 8', ['tinymce' => ['config' => ['min_height' => 180]]]);Примітки
- Pick TinyMCE 8 for new forms: it needs no jQuery, sanitises what is typed or pasted (no scripts,
on*handlers orjavascript:links), follows the Bootstrap 5 dark mode and works with htmx swaps. CKEditor 4 stays for the admin modules and the sites that use it; its version is pinned and does not change. - Both editors keep a plain textarea under them: without JavaScript the field still works, and the form sends the HTML of the editor as the field value.
- The value is escaped into the textarea and the editor reads it as HTML; what comes back is HTML typed by the user: clean or escape it on the server before you show it to others.
- Neither editor uploads files on the user side: images are inserted by URL. In the admin both editors upload images to the same server handler: CKEditor through its file browser, TinyMCE with the field option
'tinymce' => ['upload' => true](the admin docs describe both). On the user side that option adds nothing. - Dark mode: TinyMCE takes its dark skin on a dark Bootstrap 5 page (
data-bs-theme="dark", the colour mode switcher). CKEditor 4 has no dark skin: it stays light on a dark page.
Bootstrap 3 and 5
The textarea has the form-control class under both. TinyMCE 8 follows the Bootstrap 5 colour mode (oxide-dark skin under data-bs-theme="dark"); under Bootstrap 3 it uses the light skin. CKEditor looks the same under both (moono-lisa skin).
Див. також: form/tinymce, form/textarea, form/container, demo:form/tinymce, demo:form/ckeditor