Jump to content

Autosize textareas in admin


tpr
 Share

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
Link to comment
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
Link to comment
Share on other sites

  • 6 months later...

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?

Link to comment
Share on other sites

  • 4 years later...
16 hours ago, tires said:

Do i have to throw the autosize.min.js manually into the "site" folder?
Or into the sites/templates? Or the root of the site?

Try @tpr's essential AdminOnSteroids module which packages everything needed for this to work. 

  • Like 2
Link to comment
Share on other sites

19 hours ago, adrian said:

Perhaps, but I would play around with it - you'll find many of its other features invaluable ?

I thought so as well for quite some time, but I also had several problems related to AOS and wasted too much time for debugging just to find out, that AOS was the reason... That's why I don't use it any more and I was surprised - I don't really miss it ? It's nice in many ways, but it's not essential imho and I personally don't want to take the risk of getting too used to a module that does not seem to be maintained any more and potentially brings in too many problems.

No AOS bashing here - just trying to support his concern ? 

22 hours ago, tires said:

But its a big module for such a little problem ...

I'd much more prefer to get some of AOS features into the core...

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

I just stumbled across this post and wanted to share a very simple solution if you create a InputfieldTextArea with the API and want it to automatically resize based on the content:

$f = $this('modules')->get('InputfieldTextArea');
$f->name = 'myText';
$f->label = 'Text';
$f->attr("oninput", "this.style.height = ''; this.style.height = this.scrollHeight +'px'"); // this line
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

  • 1 month later...

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...