Jump to content
tpr

Autosize textareas in admin

Recommended Posts

This is a simple addition to the admin to automatically resize textareas according to their content. It doesn't work with CKE fields tough.

All it needs is a hook in ready.php + copying autosize.min.js to the "site" folder (or elsewhere if you feel so).

/site/ready.php:

// autosize textareas in admin
$page->addHookAfter('render', function ($event) {

    if ($this->page->template != 'admin') return;

    $autoSizeJsUrl = wire('config')->urls->site . 'autosize.min.js';

    $js = <<< HTML
 <script>
var autosizeTextareas = document.querySelectorAll('textarea');

if (autosizeTextareas.length) {
    $.getScript("$autoSizeJsUrl", function () {
        autosize(autosizeTextareas);
    });
}

$('.langTabs').on('tabsactivate', function(event, ui) {
    var textareas = ui.newPanel.get(0).querySelectorAll('textarea');
    
    if(textareas.length && window.autosize && window.autosize.update) {
        autosize.update(textareas);
    }
});
</script>
HTML;

    $event->return = str_replace('</body>', $js . PHP_EOL . '</body>', $event->return);
});

post-3156-0-28991300-1459947770_thumb.gi

Edit:

fixed updating textareas on activating language tabs (heights weren't updated)

  • Like 10

Share this post


Link to post
Share on other sites

Thanks netcarver. I have an idea to collect such mini-modules into a one admin helper module but as usual I don't know when will I have to do that :)

  • Like 1

Share this post


Link to post
Share on other sites

Just tried the AutoGrow CKEditor plugin and works fine (PW 3.013, CKEditor with Lightwire theme).

You can add options like these to the "Custom Config Options" to the CKEditor PW field settings:

autoGrow_onStartup: true
autoGrow_bottomSpace: 20

Or to "/site/modules/InputfieldCKEditor/config.js" if you prefer:

CKEDITOR.editorConfig = function (config) {
    config.autoGrow_onStartup = true;
    config.autoGrow_bottomSpace = 20;
};
  • Like 5

Share this post


Link to post
Share on other sites

Yes, it was made using PW3. netcarver's module should work fine too, or you can use AdminOnSteroids in which there are some improvements too.

  • Like 1

Share this post


Link to post
Share on other sites

My best guess is that you try I on a CKEditor field and not on a simple textarea. In this case you need the Auto Grow plugin (see above).

If not, do you have any js error in the browser console?

Share this post


Link to post
Share on other sites

Have you copied autosize.min.js to the "site" folder?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...