Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 356283 - editor preview scrollbar jumps to beginning of document when saving
Summary: editor preview scrollbar jumps to beginning of document when saving
Status: RESOLVED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Mylyn (show other bugs)
Version: 1.5   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 1.6.0   Edit
Assignee: Nicolas Bros CLA
QA Contact: David Green CLA
URL:
Whiteboard:
Keywords: contributed
: 362090 (view as bug list)
Depends on:
Blocks:
 
Reported: 2011-08-31 03:38 EDT by Nicolas Bros CLA
Modified: 2012-03-21 12:10 EDT (History)
1 user (show)

See Also:


Attachments
mylyn/context/zip (1.41 KB, application/octet-stream)
2011-08-31 13:33 EDT, David Green CLA
no flags Details
mylyn/context/zip (16.83 KB, application/octet-stream)
2011-09-01 14:25 EDT, David Green CLA
no flags Details
patch (2.08 KB, patch)
2011-10-11 07:28 EDT, Nicolas Bros CLA
no flags Details | Diff
patch v2 (2.40 KB, patch)
2011-10-12 05:17 EDT, Nicolas Bros CLA
no flags Details | Diff
mylyn/context/zip (104.18 KB, application/octet-stream)
2011-10-28 18:17 EDT, David Green CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Nicolas Bros CLA 2011-08-31 03:38:08 EDT
When I save a mediawiki file I'm editing (in the edit tab) and I switch to the Preview tab, the scrollbar jumps to the beginning of the preview (html) document.
This is annoying whenever editing a section that is not at the beginning of the document.

To prevent this, maybe the MarkupEditor could save and restore the position of the scrollbar when refreshing the document during the save:

int verticalScrollbarPos = editorWidget.getVerticalBar().getSelection();
...
editorWidget.getVerticalBar().setSelection(verticalScrollbarPos);
editorWidget.getVerticalBar().notifyListeners(SWT.Selection, new Event());
Comment 1 David Green CLA 2011-08-31 13:33:23 EDT
The intended behaviour when updating the preview is to scroll to the region that corresponds to the caret position or selected text in the source editor.  The idea is that the author is most likely interested in the preview for the portion of the document that was most recently viewed/modified in source form.  

Is the preview scrolling to the top only when document is saved, or does it happen all of the time?
Comment 2 David Green CLA 2011-08-31 13:33:25 EDT
Created attachment 202548 [details]
mylyn/context/zip
Comment 3 Nicolas Bros CLA 2011-09-01 05:05:59 EDT
(In reply to comment #1)
> The intended behaviour when updating the preview is to scroll to the region
> that corresponds to the caret position or selected text in the source editor.

I'm surprised. I've never seen the editor do this.

> Is the preview scrolling to the top only when document is saved, or does it
> happen all of the time?

Yes, it happens all the time when switching to the Preview tab after editing in the Mediawiki Source tab.
Comment 4 David Green CLA 2011-09-01 14:25:50 EDT
After looking deeper I can see that this behaviour was disabled on the Windows platform due to differences in how IE handles JavaScript (see related bug 270293).  I've pushed a fix for it to Git, please let me know if that fixes your problem.  Note that scrolling occurs to the nearest heading -- if you have lots of text without headings it may not work as expected.
Comment 5 David Green CLA 2011-09-01 14:25:52 EDT
Created attachment 202628 [details]
mylyn/context/zip
Comment 6 Nicolas Bros CLA 2011-10-11 07:28:39 EDT
Created attachment 204944 [details]
patch

Here is a patch that I propose to fix the problem.
With this patch, when I save in the source tab and click on the preview tab, my previous position in the preview is restored.
And when I open the same markup editor twice (using "Window > New Editor"), with the source editor on the left and the preview editor on the right, when I save in the source editor I see my changes in the preview editor without any vertical position change.
Comment 7 David Green CLA 2011-10-11 14:55:06 EDT
Nicolas, looks like a great approach!  What platform/browsers have you tested this with?  Also, would it make sense to move revealItem() back into the progress listener?  It looks like there could be a race condition where the browser might not have finished loading the new content before it's asked to reveal the item.
Comment 8 Nicolas Bros CLA 2011-10-12 05:17:27 EDT
Created attachment 205007 [details]
patch v2

(In reply to comment #7)
> What platform/browsers have you tested this with?
I have tested this on my system only : Windows 7 x64. It uses Internet Explorer for the SWT Browser (judging by the context menu and properties), but I don't know which version. Probably 9.0 which I have installed.

I have tried executing this bit of javascript in IE, Firefox and Google Chrome:
  javascript:window.scrollTo(0,100);
It works in all three browsers.

I have tried this as well:
  javascript:alert(document.body.scrollTop);
But it doesn't work in all browsers. It works in Chrome, but not in Firefox nor in IE (strangely, since it works in the integrated browser)

So I adapted the patch to handle all browsers, according to:
http://stackoverflow.com/questions/871399/cross-browser-method-for-detecting-the-scrolltop-of-the-browser-window

I tested in IE, Firefox and Google Chrome, and this code works in all three browsers.

> Also, would it make sense to move revealItem() back into the
> progress listener?  It looks like there could be a race condition where the
> browser might not have finished loading the new content before it's asked to
> reveal the item.

I don't understand how that would happen. The code to restore the vertical position is in ProgressAdapter#completed, which is only called when the browser has finished loading.
Comment 9 David Green CLA 2011-10-12 14:37:40 EDT
Thanks for the updated patch, it looks good.  Pushed review: http://review.mylyn.org/81
Comment 10 David Green CLA 2011-10-14 14:19:53 EDT
There's some loss of functionality with this patch.  Please see comments on the review: http://review.mylyn.org/81
Comment 11 Nicolas Bros CLA 2011-10-19 10:44:34 EDT
I haven't found a way to reconcile the two mechanisms. Maybe a new preference could be made: to either keep the existing behavior (try to jump to the edited section), or always keep the user's position in the document.
What do you think?
Comment 12 David Green CLA 2011-10-19 13:45:00 EDT
I'd hate to see a new preference for something like this.   

How about this for an idea: we could give every block (paragraph, div, table, etc.) an ID in the generated HTML.  When switching to the preview we could scroll to the block corresponding to the current caret position in the editor.
Comment 13 Nicolas Bros CLA 2011-10-20 05:20:25 EDT
(In reply to comment #12)
> How about this for an idea: we could give every block (paragraph, div, table,
> etc.) an ID in the generated HTML.  When switching to the preview we could
> scroll to the block corresponding to the current caret position in the editor.

This is a good way to improve the current implementation. But as a user, I don't like the wikitext editor to try to decide what I want to see. For example, I am currently working with diagrams (as images in the wikitext) with a description below. While I am editing the description, I want to see the diagram at the same time. But if the wikitext editor scrolls to the beginning of the text paragraph, the image will get out of the screen and I will lose my context.

Since I believe it is impossible for the wikitext editor to always find the best part of the content to display, I would prefer to decide it for myself and let the editor get out of my way. This is a way of empowering users by giving them control over things they are in the best position to decide.


So, a way your idea could be implemented while leaving the user in control:
- in the Edit tab, add a context menu action "Edit this paragraph" : clicking this would both switch to the edit tab and scroll to the corresponding paragraph
- in the Preview tab, each paragraph could have an "edit" link (like on Wikipedia). Clicking this link would switch to the corresponding position in the Edit tab.
Comment 14 David Green CLA 2011-10-20 13:45:40 EDT
(In reply to comment #13)

That's a good point Nicolas.  The current implementation was designed as a crutch to overcome scrolling being reset to the top of the document each time the preview was updated.  That was anoying for obvious reasons.  

I like your ideas for making it more edit-friendly.  How about this for an approach:

* we go with the maintain-current-scroll-position approach of your patch
* we add a context menu to the editor, something like "Preview here" that opens the preview tab and scrolls as near as possible to the caret position
* users can continue to use the outline view to scroll the preview

Adding "edit this paragraph" links is a good idea.  We should look at the implementation carefully, since we don't want to alter the preview too much.  Perhaps an "edit this paragraph" link that only displays on hover would do the trick.  We could consider this one as a future enhancement if it's too much to take on right away.
Comment 15 Nicolas Bros CLA 2011-10-24 10:59:15 EDT
(In reply to comment #14)
> * we go with the maintain-current-scroll-position approach of your patch
> * we add a context menu to the editor, something like "Preview here" that opens
> the preview tab and scrolls as near as possible to the caret position
> * users can continue to use the outline view to scroll the preview

I think this is a good approach.
Comment 16 David Green CLA 2011-10-26 16:01:34 EDT
*** Bug 362090 has been marked as a duplicate of this bug. ***
Comment 17 David Green CLA 2011-10-28 18:17:40 EDT
Patch applied, thanks Nicolas!

Summary of work done:
* patch applied - scroll position is restored whenever the preview content is updated
* context menu action added in source editor: *Preview at "Heading Name"* causes switch to preview tab, scrolls to reveal heading
Comment 18 David Green CLA 2011-10-28 18:17:44 EDT
Created attachment 206158 [details]
mylyn/context/zip