vscode icon indicating copy to clipboard operation
vscode copied to clipboard

Support to scroll markdown preview with source scrolling

Open gregvanl opened this issue 10 years ago • 17 comments

  • VSCode Version:0.10.14 code-insiders
  • OS Version:Windows 10

Steps to Reproduce:

  1. Open a markdown preview to the side of markdown source
  2. Scroll source file

It would be nice to have a way to lock the preview and scroll at the same time. Especially nice for longer markdown files.

This came in from an internal MS writer using VS Code for Markdown.

gregvanl avatar Apr 06 '16 18:04 gregvanl

hi @kieferrm, just curious if you have an ETA for this feature. @gregvanl and I were discussing it. Thanks!

tedhudek avatar Jun 23 '16 16:06 tedhudek

there is another duplicate issue https://github.com/Microsoft/vscode/issues/6992 and I've put my knowledge there. markdown-it (our current markdown engine) does support generating scroll map for Editor's to implement sync-preview. The only thing left here is adding handlers inside TextEditor.

rebornix avatar Jun 24 '16 13:06 rebornix

really need it

ghost avatar Aug 11 '16 13:08 ghost

It's really an user-friendly feature.

akillcool avatar Aug 14 '16 15:08 akillcool

+1

helphi avatar Aug 27 '16 12:08 helphi

It's a necessary feature to throw my current markdown editor away.

nadoo avatar Sep 23 '16 06:09 nadoo

Perhaps a way to implement this is adding an scrolling action in the preview to the line that corresponds to the one where the cursor is placed every time the cursor moves

canda avatar Oct 21 '16 14:10 canda

I am not familiar with the code, but is the implementation of this any different than the Git integration where the diffs are shown side by side and the scrolling is synchronized?

samherrmann avatar Nov 02 '16 14:11 samherrmann

I've been considering a few ways to implement this feature, and want to toss out three possible ideas to see if anyone on the VSCode team or externally has any thoughts on which direction to go in.

(Sorry for the long writeup)

Scroll Events

My initial thought was to create a new API for monitoring when a text editor is scrolled. or more generally has its viewport changed. The viewport would likely be expressed in terms of line numbers instead of pixels, and the markdown extension would subscribe to these events for markdown source documents to implement scrolling synchronization.

The API here would look something like this:

export const onDidChangeViewport: Event<ViewportChangedEvent>;

export interface ViewportChangedEvent {
	document: TextDocument;
	topLine: number; 
	bottomLine: number;
}

Advantages

  • Relatively easy to implement.
  • Flexible for extension authors.

Disadvantages

  • Scrolling events are much closer to underlying the dom than anything that our current APIs expose.
  • Each extension/file type would have to implement its own scrolling synchronization logic.
  • Performance concerns. This could generate a lot of events.
  • Would require another API to sync scrolling from the preview to the editor.

Internal Editor Scrolling Associations

While discussing the purposed scroll event API with @kieferrm , its many drawbacks became clear. Since exposing scroll events felt too low level, we talked about how we may be able to create a more declarative API that hides most of the scrolling synchronization implementation details.

In this model, the workbench would keep track of the text editor associated with a markdown preview view. When the source text editor is scrolled, the workbench would map the source's location to a target position in the preview. The scroll mapping would be computed using html attributes on the preview's generated html:

<p data-line="1">...</p>
<img data-line="3" ... />
<p data-line="4">...</p>

Advantages

  • Easy for extension authors.
  • No new API.
  • Consistent behavior across extensions.
  • Could add new features more easily, such as bi-direction scrolling synchronization.

Disadvantages

  • More difficult to implement.
  • Performance concerns. We should only generate scrolling events when someone cares about them.
  • More limited applications for extension authors.
  • Entirely implicit API that is not easy to debug or customize.

Preview View

I liked this second approach much better, but while talking with @rebornix about this, he presented another idea that may be easier to implement and also offers some useful functionality for the rest of VSCode: a preview view.

The preview view would visually look somewhat like the current diff view, with a split pane layout. One side would have a text editor, while the other side would have have a rendered preview of the editor's content. The scrolling synchronization implementation would be similar to that described before.

The main advantage of the preview view is that it could be used to implement consistent preview experiences for multiple extensions and file types. I imagine the api would be similar to current TextDocumentContentProvider api:

export function registerPreviewProvider(scheme: string, provider: PreviewProvider): Disposable;

export interface PreviewProvider {
	providePreview(uri: Uri, token: CancellationToken): ProviderResult<string>;
}

Advantages

  • Same advantages as "Internal Editor Scrolling Associations" for scrolling synchronization.
  • Direct association between editor and preview view allows us to avoid some potential design and perf issues.
  • Makes previews a more well defined concept in VSCode.

Disadvantages

  • Same disadvantages as "Internal Editor Scrolling Associations"
  • More generic, so it would require some new APIs.
  • In this model, the preview would no longer be a separate view that can be detached from the editor and moved about on its own. The editor and preview would always be bound to each other.

I think that we can probably rule out exposing scroll events directly. That leaves either the "Internal Editor Scrolling Associations", which uses our current model to synchronize scrolling between the preview and the editor, or the preview view, which introduces a new concept to do the same while also bringing potentially new extensibility to VSCode. I know that there's been some noise around better supporting previews recently.

Please let me know if you have any thoughts on either approach, or if you have another idea that is worth considering.

Thanks

CC @aeschli @jrieken @bpasero @joaomoreno @egamma @Tyriar

mjbvz avatar Jan 10 '17 05:01 mjbvz

This won't help deciding on the approach but... I can see this being quite useful for other use cases; what if I were to open a TypeScript file and wanted to see the matching JavaScript next to it in some sort of "TypeScript preview"? It would be cool to have them linked too. Or anything else which produces sourcemaps. 👍

joaomoreno avatar Jan 10 '17 08:01 joaomoreno

Re Scroll Events:

Many people have already asked us to expose the current state of the view port. Unless we decide to tell them that won't ever happen we should seriously consider that. Also, with some debouncing tricks we should be able to avoid performance issues.

Re Internal Editor Scrolling Associations:

That sounds like a lot of magic to me

Re Preview View:

The idea has appeal but will add lots of new API. It's not enough to associate a string to a scheme using a provider indirection. Because for markdown-cases that string should be rendered as html but for cases like the one Joao pointed out, JS from TS should be shown using an editor. That means we need to be able to say what UI piece is supposed to be created, like create PreviewView and create Editor which requires more API

jrieken avatar Jan 10 '17 08:01 jrieken

I really would like having markdown source and preview in ONE editor at least when I am authoring the markdown. If I am reading markdown, I would not expect the source of the file to be visible. It seems to me that we then have 2 modes how to read markdown, or we need a way to allow the preview to span the full size of the editor if the user does not care about the sources.

For now, going with the scroll events API approach seems like a good first step though.

bpasero avatar Jan 10 '17 09:01 bpasero

The Internal Editor Scrolling Associations idea is similar to sourcemaps. It would probably map to other uses that have explicit sourcemaps.

Arnavion avatar Jan 10 '17 16:01 Arnavion

Just merged in #18762 which prototypes similar functionality and will be in tomorrow's insiders build. We still can't do scroll sync yet since we have no api for getting the vscode editor viewport, but the change adds the following:

  • When opening the markdown preview, automatically scroll to view the current selected line from the markdown editor.
  • When changing the selection in the markdown editor, scrolls the markdown preview to reveal the corresponding element.
  • When double clicking an element on the markdown preview page, opens an editor and reveals this line in the center.
  • Indicator in preview shows the currently selected line in the markdown editor

jan-18-2017 18-07-29

This can be disabled using markdown.preview.experimentalSyncronizationEnabled

We would really appreciate any feedback on the behavior of these features and other additions that may be helpful. Again, proper synchronization based on scrolling of the text editor is still something we are investigating, but these new features should be helpful when working with markdown documents.

mjbvz avatar Jan 19 '17 02:01 mjbvz

Next 1.9 insiders also adds markdown preview to editor scroll synchronization:

jan-22-2017 00-48-51

Again, let us know if you have any feedback or run into any bugs.

mjbvz avatar Jan 23 '17 20:01 mjbvz

Closing as this seems done.

joaomoreno avatar Jan 26 '17 20:01 joaomoreno

I've opened #19459 to track scroll sync from the editor to the markdown preview. The reverse works, but for editor to preview we currently only track the selection in the editor, not the viewport itself.

mjbvz avatar Jan 26 '17 20:01 mjbvz