From 04af3e161a815b4244d866d88558746c2747ecad Mon Sep 17 00:00:00 2001 From: Vasu Bhog Date: Tue, 6 Apr 2021 11:51:53 -0700 Subject: [PATCH] See and Edit Selected Links in Callout Dialog (#14987) * Add URL label to linkCallout * add test for file link --- .../calloutDialog/linkCalloutDialog.ts | 2 ++ .../cellViews/markdownToolbar.component.ts | 11 ++++++- .../calloutDialog/linkCalloutDialog.test.ts | 29 +++++++++++++++++-- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/sql/workbench/contrib/notebook/browser/calloutDialog/linkCalloutDialog.ts b/src/sql/workbench/contrib/notebook/browser/calloutDialog/linkCalloutDialog.ts index e3e4e486da..13e4913236 100644 --- a/src/sql/workbench/contrib/notebook/browser/calloutDialog/linkCalloutDialog.ts +++ b/src/sql/workbench/contrib/notebook/browser/calloutDialog/linkCalloutDialog.ts @@ -45,6 +45,7 @@ export class LinkCalloutDialog extends Modal { dialogPosition: DialogPosition, dialogProperties: IDialogProperties, private readonly _defaultLabel: string = '', + private readonly _defaultLinkUrl: string = '', @IContextViewService private readonly _contextViewService: IContextViewService, @IThemeService themeService: IThemeService, @ILayoutService layoutService: ILayoutService, @@ -134,6 +135,7 @@ export class LinkCalloutDialog extends Modal { placeholder: constants.linkAddressPlaceholder, ariaLabel: constants.linkAddressLabel }); + this._linkUrlInputBox.value = this._defaultLinkUrl; DOM.append(linkAddressRow, linkAddressInputContainer); } diff --git a/src/sql/workbench/contrib/notebook/browser/cellViews/markdownToolbar.component.ts b/src/sql/workbench/contrib/notebook/browser/cellViews/markdownToolbar.component.ts index e6ea31f4e4..fab47a1d1c 100644 --- a/src/sql/workbench/contrib/notebook/browser/cellViews/markdownToolbar.component.ts +++ b/src/sql/workbench/contrib/notebook/browser/cellViews/markdownToolbar.component.ts @@ -296,7 +296,8 @@ export class MarkdownToolbarComponent extends AngularDisposable { if (type === MarkdownButtonType.LINK_PREVIEW) { const defaultLabel = this.getCurrentSelectionText(); - this._linkCallout = this._instantiationService.createInstance(LinkCalloutDialog, this.insertLinkHeading, dialogPosition, dialogProperties, defaultLabel); + const defaultLinkUrl = this.getCurrentLinkUrl(); + this._linkCallout = this._instantiationService.createInstance(LinkCalloutDialog, this.insertLinkHeading, dialogPosition, dialogProperties, defaultLabel, defaultLinkUrl); this._linkCallout.render(); calloutOptions = await this._linkCallout.open(); } @@ -318,6 +319,14 @@ export class MarkdownToolbarComponent extends AngularDisposable { } } + private getCurrentLinkUrl(): string { + if (document.getSelection().anchorNode.parentNode['protocol'] === 'file:') { + return document.getSelection().anchorNode.parentNode['pathname'] || ''; + } else { + return document.getSelection().anchorNode.parentNode['href'] || ''; + } + } + private getCellEditorControl(): IEditor | undefined { // If control doesn't exist, editor may have been destroyed previously when switching edit modes if (!this._cellEditor?.getEditor()?.getControl()) { diff --git a/src/sql/workbench/contrib/notebook/test/calloutDialog/linkCalloutDialog.test.ts b/src/sql/workbench/contrib/notebook/test/calloutDialog/linkCalloutDialog.test.ts index 53954fb586..c26b5ee5dc 100644 --- a/src/sql/workbench/contrib/notebook/test/calloutDialog/linkCalloutDialog.test.ts +++ b/src/sql/workbench/contrib/notebook/test/calloutDialog/linkCalloutDialog.test.ts @@ -29,7 +29,7 @@ suite('Link Callout Dialog', function (): void { }); test('Should return empty markdown on cancel', async function (): Promise { - let linkCalloutDialog = new LinkCalloutDialog('Title', 'below', defaultDialogProperties, 'defaultLabel', + let linkCalloutDialog = new LinkCalloutDialog('Title', 'below', defaultDialogProperties, 'defaultLabel', 'defaultLinkLabel', undefined, themeService, layoutService, telemetryService, contextKeyService, undefined, undefined, undefined); linkCalloutDialog.render(); @@ -50,7 +50,7 @@ suite('Link Callout Dialog', function (): void { test('Should return expected values on insert', async function (): Promise { const defaultLabel = 'defaultLabel'; const sampleUrl = 'https://www.aka.ms/azuredatastudio'; - let linkCalloutDialog = new LinkCalloutDialog('Title', 'below', defaultDialogProperties, defaultLabel, + let linkCalloutDialog = new LinkCalloutDialog('Title', 'below', defaultDialogProperties, defaultLabel, sampleUrl, undefined, themeService, layoutService, telemetryService, contextKeyService, undefined, undefined, undefined); linkCalloutDialog.render(); @@ -73,7 +73,7 @@ suite('Link Callout Dialog', function (): void { test('Should return expected values on insert when escape necessary', async function (): Promise { const defaultLabel = 'default[]Label'; const sampleUrl = 'https://www.aka.ms/azuredatastudio()'; - let linkCalloutDialog = new LinkCalloutDialog('Title', 'below', defaultDialogProperties, defaultLabel, + let linkCalloutDialog = new LinkCalloutDialog('Title', 'below', defaultDialogProperties, defaultLabel, sampleUrl, undefined, themeService, layoutService, telemetryService, contextKeyService, undefined, undefined, undefined); linkCalloutDialog.render(); @@ -106,4 +106,27 @@ suite('Link Callout Dialog', function (): void { assert.equal(escapeUrl('<>&()'), '<>&%28%29', 'URL test known escaped characters failed'); assert.equal(escapeUrl('<>&()[]'), '<>&%28%29[]', 'URL test all escaped characters failed'); }); + + test('Should return file link properly', async function (): Promise { + const defaultLabel = 'defaultLabel'; + const sampleUrl = 'C:/Test/Test.ipynb'; + let linkCalloutDialog = new LinkCalloutDialog('Title', 'below', defaultDialogProperties, defaultLabel, sampleUrl, + undefined, themeService, layoutService, telemetryService, contextKeyService, undefined, undefined, undefined); + linkCalloutDialog.render(); + + let deferred = new Deferred(); + // When I first open the callout dialog + linkCalloutDialog.open().then(value => { + deferred.resolve(value); + }); + linkCalloutDialog.url = sampleUrl; + + // And insert the dialog + linkCalloutDialog.insert(); + let result = await deferred.promise; + assert.equal(result.insertUnescapedLinkLabel, defaultLabel, 'Label not returned correctly'); + assert.equal(result.insertUnescapedLinkUrl, sampleUrl, 'URL not returned correctly'); + assert.equal(result.insertEscapedMarkdown, `[${defaultLabel}](${sampleUrl})`, 'Markdown not returned correctly'); + }); + });