mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 18:46:40 -05:00
Fix markdown in Jupyter books not working (#6420)
Issue here is that for some reason click in tree causes 2 events: - Selection Changed - Resource Opened Due to this, we get the markdown event 2 times and this misbehaves. I am not confident in changing markdown so implemented similar pattern: - Throttle requests if they're for the same resource inside 300ms - This means 2nd event is ignored Testing: - Manual, clicked a bunch of markdowns - It "just works" for me now
This commit is contained in:
@@ -21,6 +21,8 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
|||||||
private _tableOfContentsPath: string[];
|
private _tableOfContentsPath: string[];
|
||||||
private _allNotebooks = new Map<string, BookTreeItem>();
|
private _allNotebooks = new Map<string, BookTreeItem>();
|
||||||
private _extensionContext: vscode.ExtensionContext;
|
private _extensionContext: vscode.ExtensionContext;
|
||||||
|
private _throttleTimer: any;
|
||||||
|
private _resource: string;
|
||||||
|
|
||||||
constructor(private workspaceRoot: string, extensionContext: vscode.ExtensionContext) {
|
constructor(private workspaceRoot: string, extensionContext: vscode.ExtensionContext) {
|
||||||
if (workspaceRoot !== '') {
|
if (workspaceRoot !== '') {
|
||||||
@@ -57,13 +59,34 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
|
|||||||
}
|
}
|
||||||
|
|
||||||
openMarkdown(resource: string): void {
|
openMarkdown(resource: string): void {
|
||||||
|
this.runThrottledAction(resource, () => {
|
||||||
try {
|
try {
|
||||||
vscode.commands.executeCommand('markdown.showPreview', vscode.Uri.file(resource));
|
vscode.commands.executeCommand('markdown.showPreview', vscode.Uri.file(resource));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
vscode.window.showErrorMessage(localize('openMarkdownError', 'Open file {0} failed: {1}',
|
vscode.window.showErrorMessage(localize('openMarkdownError', "Open file {0} failed: {1}",
|
||||||
resource,
|
resource,
|
||||||
e instanceof Error ? e.message : e));
|
e instanceof Error ? e.message : e));
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private runThrottledAction(resource: string, action: () => void) {
|
||||||
|
const isResourceChange = resource !== this._resource;
|
||||||
|
if (isResourceChange) {
|
||||||
|
clearTimeout(this._throttleTimer);
|
||||||
|
this._throttleTimer = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._resource = resource;
|
||||||
|
|
||||||
|
// Schedule update if none is pending
|
||||||
|
if (!this._throttleTimer) {
|
||||||
|
if (isResourceChange) {
|
||||||
|
action();
|
||||||
|
} else {
|
||||||
|
this._throttleTimer = setTimeout(() => action(), 300);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
openExternalLink(resource: string): void {
|
openExternalLink(resource: string): void {
|
||||||
|
|||||||
Reference in New Issue
Block a user