pepebe
5/26/2015 - 8:20 AM

Add ACE code editor to your textarea TVs.

Add ACE code editor to your textarea TVs.

<?php
/*
Add ACE code editor to your textarea TVs.
Author: evd (hello@evd.me)
Source: https://github.com/danyaPostfactum/modx-ace/pull/9#issuecomment-76466239
Trigger on ONDocFormRender with a priority of ~10 (higher than ace...)
*/

//Apply ace editor to TV with id 5 and 7
$tvs = array(
    array(
        'id' => 31,
        'height' => '600px',
        'mimeType' => 'text/html'
    ),
    array(
        'id' => 32,
        'height' => '600px',
        'mimeType' => 'text/html'
    )
);

if ($modx->event->name != 'OnDocFormRender') {
    return;
}

if ($modx->getOption('which_element_editor', null, 'Ace') !== 'Ace') {
    return;
}

$script = '';
foreach ($tvs as $tv) {
    $id = is_array($tv) ? $tv['id'] : $tv;
    $height = isset($tv['height']) ? $tv['height'] : '';
    $mimeType = isset($tv['mimeType']) ? $tv['mimeType'] : '';
    $tvId = '#tv' . $id;
    $setHeightScript = !empty($height) ? 'this.style.height="' . $height . '";' : '';
    $aceScript = 'MODx.ux.Ace.replaceTextAreas([this], "' . $mimeType . '");';
    $script .= 'Ext.each(Ext.query("' . $tvId . '"), function() { ' . $setHeightScript . $aceScript . '});' . PHP_EOL;
}

if ($script) {
    $modx->controller->addHtml('<script>Ext.onReady(function() {' . $script . '});</script>');
}