Skip to content

the button "go here"#9

Merged
dzylikecode merged 7 commits intomasterfrom
editor-sync
Oct 13, 2022
Merged

the button "go here"#9
dzylikecode merged 7 commits intomasterfrom
editor-sync

Conversation

@dzylikecode
Copy link
Copy Markdown
Owner

@dzylikecode dzylikecode commented Oct 12, 2022

#8

  • The editor syncs with the preview when scrolling the preview window.
  • The editor opens the corresponding file when the preview navigates to a page.

@dzylikecode
Copy link
Copy Markdown
Owner Author

dzylikecode commented Oct 12, 2022

editor scroll

https://github.com/shd101wyy/vscode-markdown-preview-enhanced/blob/40ddd2b756aa3052dd6220e2325a0b2fd0b3997b/src/extension.ts#L799-L823

function revealLine(uri, line) {
  const sourceUri = vscode.Uri.parse(uri);

  vscode.window.visibleTextEditors
    .filter(
      (editor) =>
        isMarkdownFile(editor.document) &&
        editor.document.uri.fsPath === sourceUri.fsPath,
    )
    .forEach((editor) => {
      const sourceLine = Math.min(
        Math.floor(line),
        editor.document.lineCount - 1,
      );
      const fraction = line - sourceLine;
      const text = editor.document.lineAt(sourceLine).text;
      const start = Math.floor(fraction * text.length);
      editorScrollDelay = Date.now() + 500;
      editor.revealRange(
        new vscode.Range(sourceLine, start, sourceLine + 1, 0),
        vscode.TextEditorRevealType.InCenter,
      );
      editorScrollDelay = Date.now() + 500;
    });
}

@dzylikecode
Copy link
Copy Markdown
Owner Author

open the corresponding file

https://github.com/microsoft/vscode/blob/2fe62e7dfec6fcff6e037e5fbc12082d43d98c0e/extensions/markdown-language-features/src/commands/openDocumentLink.ts#L46-L59

	public execute(args: OpenDocumentLinkArgs) {
		const fromResource = vscode.Uri.parse(decodeURIComponent(args.fromResource));
		const targetPath = decodeURIComponent(args.path);
		const column = this.getViewColumn(fromResource);
		return this.tryOpen(targetPath, args, column).catch(() => {
			if (targetPath && extname(targetPath) === '') {
				return this.tryOpen(targetPath + '.md', args, column);
			}
			const targetResource = vscode.Uri.file(targetPath);
			return Promise.resolve(undefined)
				.then(() => vscode.commands.executeCommand('vscode.open', targetResource, column))
				.then(() => undefined);
		});
	}

@dzylikecode
Copy link
Copy Markdown
Owner Author

These functions may help:

example

VSCode API

  • tryRevealLine
  • tryOpen
    • vscode.workspace.openTextDocument
    • vscode.window.showTextDocument

@dzylikecode
Copy link
Copy Markdown
Owner Author

the process of message passing

flowchart LR

scroll["preview:scroll message/open a link"]
webview
vscode

scroll --> webview --> vscode

Loading

@dzylikecode
Copy link
Copy Markdown
Owner Author

docsifyjs/docsify#1905

To keep the sidebar closed if you closed it before when reloading the window

@dzylikecode
Copy link
Copy Markdown
Owner Author

I think using the button "go here" to replace the feature that the editor syncs with the preview is better. The editor will be crazy if syncing with the preview when you switch the preview page. Maybe sometimes, we just want the editor to go to the corresponding position of markdown instead of syncing with preview.

@dzylikecode dzylikecode changed the title editor syncs with preview the button "go here" Oct 13, 2022
@dzylikecode dzylikecode merged commit a425a1c into master Oct 13, 2022
@dzylikecode dzylikecode deleted the editor-sync branch October 13, 2022 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant