form/tinymce
< >

return function () {
    $self = url('/@object/@action/' . urlencode('form/tinymce'));
    $form_id = 'docs_tinymce_demo';
    // A second form that htmx replaces: the starter removes the old editor before the swap and starts the new one after it
    $note_form = function ($n) use ($self) {
        $n = max(1, min(1000, (int) $n));
        return '<div id="tinymce_htmx">' . form(['tm_note' => '<p>Note ' . $n . '</p>'], ['id' => 'tinymce_note_form', '__form_id__' => 'docs_tinymce_note'])
            ->tinymce('tm_note', 'Note')
            . '<button type="button" class="btn btn-default btn-outline-secondary" id="tinymce_htmx_reload" hx-get="' . $self . '?note=' . ($n + 1)
            . '" hx-target="#tinymce_htmx" hx-swap="outerHTML">Replace with htmx (note ' . ($n + 1) . ')</button></div>';
    };
    $htmx = _class('htmx');
    header('Vary: HX-Request, HX-History-Restore-Request');
    if ($htmx->_wants_fragment() && isset($_GET['note'])) {
        return $htmx->_result($note_form($_GET['note']));
    }
    asset('htmx');
    $intro = '<p class="docs-demo-intro">The <code>tinymce()</code> control: a textarea that TinyMCE 8 turns into a rich text editor
        (plugins lists, link, image, table, code, autoresize). Without JavaScript it stays a plain textarea. The editor copies its HTML
        into the textarea as you type, so a normal submit sends it; below, the server shows what it received, escaped. TinyMCE 8 is
        GPL-2.0-or-later: the editor runs with <code>license_key: \'gpl\'</code> unless
        <code>$PROJECT_CONF[\'form2_tinymce\'][\'LICENSE_KEY\']</code> holds a commercial Tiny key. Scripts, <code>on*</code> handlers and
        <code>javascript:</code> links are dropped by the editor; image uploads are off (images by URL only). Under Bootstrap 5 it follows the page
        colour mode: switch to Dark and reload for the dark skin. The second form is replaced by htmx: the old editor is removed and the
        new one started. CKEditor stays the default editor of the admin modules. The demo stores nothing.
        Classes: <code>yf_form2</code>, <code>yf_form2_tinymce</code>, <code>yf_htmx</code>.</p>';
    $result = '';
    $value = '<h2>Release notes</h2><p>Hello <strong>world</strong>, this is <em>TinyMCE</em>.</p><ul><li>one</li><li>two</li></ul>'
        . '<p><a href="https://yfix.net/">yfix.net</a></p>';
    $form = form((array) $_POST + ['tm_text' => $value], ['csrf' => true, '__form_id__' => $form_id, 'id' => 'tinymce_form'])
        ->validate(['__form_id__' => $form_id, 'tm_text' => 'trim|required'])
        ->on_validate_ok(function ($data) use (&$result) {
            $html = (string) $data['tm_text'];
            $result = '<div class="alert alert-success" id="tinymce_result"><b>Received ' . strlen($html) . ' bytes</b> (nothing stored):'
                . '<pre class="tinymce-demo-received">' . _prepare_html($html, 0) . '</pre></div>';
        })
        ->tinymce('tm_text', 'Text')
        ->save();
    // A failed CSRF check logs into the log_csrf_errors table; the demo writes no rows
    $guard = _class('csrf_guard');
    $log_errors = $guard->LOG_ERRORS;
    $guard->LOG_ERRORS = false;
    try {
        $html = (string) $form;
    } finally {
        $guard->LOG_ERRORS = $log_errors;
    }
    css('.tinymce-demo-received { white-space: pre-wrap; margin: 8px 0 0; }');
    return $intro . $result . $html . '<h4>Replaced by htmx</h4>' . $note_form(1);
};

The tinymce() control: a textarea that TinyMCE 8 turns into a rich text editor (plugins lists, link, image, table, code, autoresize). Without JavaScript it stays a plain textarea. The editor copies its HTML into the textarea as you type, so a normal submit sends it; below, the server shows what it received, escaped. TinyMCE 8 is GPL-2.0-or-later: the editor runs with license_key: 'gpl' unless $PROJECT_CONF['form2_tinymce']['LICENSE_KEY'] holds a commercial Tiny key. Scripts, on* handlers and javascript: links are dropped by the editor; image uploads are off (images by URL only). Under Bootstrap 5 it follows the page colour mode: switch to Dark and reload for the dark skin. The second form is replaced by htmx: the old editor is removed and the new one started. CKEditor stays the default editor of the admin modules. The demo stores nothing. Classes: yf_form2, yf_form2_tinymce, yf_htmx.

Replaced by htmx