Skip to content

Support to scroll markdown preview with source scrolling #5047

Closed

Description

  • 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.

Activity

  1. added
    feature-request
    markdown-ext
    on Apr 7, 2016
  2. changed the title [-]Support locking Markdown preview to source file[/-] [+]Support to scroll markdown preview with source[/+] on Apr 7, 2016
  3. changed the title [-]Support to scroll markdown preview with source[/-] [+]Support to scroll markdown preview with source scrolling[/+] on Apr 7, 2016
  4. tedhudek commented on Jun 23, 2016

    @tedhudek
    Member

    hi Kai Maetzel (@kieferrm), just curious if you have an ETA for this feature. Greg Van Liew (@gregvanl) and I were discussing it. Thanks!

  5. rebornix commented on Jun 24, 2016

    @rebornix
    Contributor

    there is another duplicate issue #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.

  6. akillcool commented on Aug 14, 2016

    @akillcool

    It's really an user-friendly feature.

  7. helphi commented on Aug 27, 2016

    @helphi

    +1

  8. nadoo commented on Sep 23, 2016

    @nadoo

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

  9. 1 remaining item

  10. samherrmann commented on Nov 2, 2016

    @samherrmann

    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?

  11. added
    plan-item
    on Jan 3, 2017
  12. mjbvz commented on Jan 10, 2017

    @mjbvz
    Contributor

    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 Kai Maetzel (@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 Peng Lyu (@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 Martin Aeschlimann (@aeschli) Johannes Rieken (@jrieken) Benjamin Pasero (@bpasero) João Moreno (@joaomoreno) Erich Gamma (@egamma) Daniel Imms (@Tyriar)

  13. joaomoreno commented on Jan 10, 2017

    @joaomoreno
    Contributor

    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. 👍

  14. jrieken commented on Jan 10, 2017

    @jrieken
    Contributor

    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

  15. bpasero commented on Jan 10, 2017

    @bpasero
    Contributor

    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.

  16. Arnavion commented on Jan 10, 2017

    @Arnavion

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

  17. mjbvz commented on Jan 19, 2017

    @mjbvz
    Contributor

    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.

  18. mjbvz commented on Jan 23, 2017

    @mjbvz
    Contributor

    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.

  19. joaomoreno commented on Jan 26, 2017

    @joaomoreno
    Contributor

    Closing as this seems done.

  20. mjbvz commented on Jan 26, 2017

    @mjbvz
    Contributor

    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.

  21. locked and limited conversation to collaborators on Nov 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

feature-requestmarkdown-extplan-item

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions