RememberEditPosition

Summary: Return the editing textarea to the previous position when previewing a page
Version: 2009-01-20
Prerequisites: 2.1.27
Status: beta
Maintainer: Petko (original author: Eemeli Aro)
License: Public domain
Categories: Editing
Users: +3 (View / Edit)

Questions answered by this recipe

Note that the NotSavedWarning recipe includes a feature similar to this one.

Quoting JL in PITS:01019:

During edition of a pmwiki page, we use the preview button, and the page is reloaded with the preview. Then the scrollbar of the textarea is set at the top and we scroll to return at the point of the last modification. Could you store the scrollbar position [...] to restore it on the load of edit/preview?

Description

Return the editing textarea to the previous position when previewing a page.

To implement

  1. Add the following to your config file:
if ( $action == 'edit' ) {
  $InputTags['e_scrolltop'][':html'] = 
    "<input type='hidden' name='prev_scroll' id='prev_scroll' value='".
    intval(@$_POST['prev_scroll'])."'/>";
  $HTMLHeaderFmt[] = "<script type='text/javascript'>
    function addEvent( obj, evt, fn ) {
      if (obj.addEventListener) obj.addEventListener( evt, fn, false );
      else if (obj.attachEvent) obj.attachEvent( 'on'+evt, fn );
    }
    var ps;
    addEvent( window, 'load', function() {
      ps = document.getElementById('prev_scroll');
      if ( ps.value > 0 ) document.getElementById('text').scrollTop = ps.value;
      addEvent( ps.form, 'submit', function() { ps.value = ps.form.text.scrollTop; } )
    } );
    </script>";
}
  1. Add the directive (:input e_scrolltop:) just after (:input e_form:) on your Site.EditForm page.

Notes

As this is mostly implemented using JavaScript, you may need to customise the script if you're already including a separate Javascript file and/or using a different event handler adder in your setup.

In quick testing, the script appears to work in everything from at least IE6 onwards.

See Also

  • PITS.01019  Store the position of scrollbar of textarea when preview
  • NotSavedWarning has a similar feature built-in. (Warn authors when they move away from a page without saving it; optionally request an edit summary or an author name)

Contributors

Comments

This is cool...but does not seem to work in IE6 or FF3... I think I have installed as prescribed. I am using fixflow skin. When I edit a page, the var "ps" is always null and gives an error on the "if ( ps.value > 0 )" line. JJ? 08/03/2009

FixFlowSkin? uses Site.Popup-EditForm instead of Site.EditForm so try adding (:input e_scrolltop:) to that page. —Eemeli Aro August 04, 2009, at 06:44 PM

Nice, thank you! --SteP December 14, 2009

Does someone have this cookbook working with (vista+)IE8 (and how ?) ? I don't (though it's working with Vista+Google Chrome and with XP+IE6 and with XP+Opera 9.64). Thank you.
gb December 16, 2009, at 04:48 PM

User notes +3: If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.