Ability to see and edit links in split view/markdown mode (#15150)

* callout dialog selects link properly in split/markdown mode

* use regex for links
This commit is contained in:
Vasu Bhog
2021-04-20 23:33:14 -07:00
committed by GitHub
parent a82be2f014
commit 6951bf3b90

View File

@@ -24,6 +24,7 @@ import { URI } from 'vs/base/common/uri';
import { escape } from 'vs/base/common/strings'; import { escape } from 'vs/base/common/strings';
export const MARKDOWN_TOOLBAR_SELECTOR: string = 'markdown-toolbar-component'; export const MARKDOWN_TOOLBAR_SELECTOR: string = 'markdown-toolbar-component';
const linksRegex = /\[(?<text>.+)\]\((?<url>[^ ]+)(?: "(?<title>.+)")?\)/;
@Component({ @Component({
selector: MARKDOWN_TOOLBAR_SELECTOR, selector: MARKDOWN_TOOLBAR_SELECTOR,
@@ -298,7 +299,7 @@ export class MarkdownToolbarComponent extends AngularDisposable {
let calloutOptions; let calloutOptions;
if (type === MarkdownButtonType.LINK_PREVIEW) { if (type === MarkdownButtonType.LINK_PREVIEW) {
const defaultLabel = this.getCurrentSelectionText(); const defaultLabel = this.getCurrentLinkLabel();
const defaultLinkUrl = this.getCurrentLinkUrl(); const defaultLinkUrl = this.getCurrentLinkUrl();
this._linkCallout = this._instantiationService.createInstance(LinkCalloutDialog, this.insertLinkHeading, dialogPosition, dialogProperties, defaultLabel, defaultLinkUrl); this._linkCallout = this._instantiationService.createInstance(LinkCalloutDialog, this.insertLinkHeading, dialogPosition, dialogProperties, defaultLabel, defaultLinkUrl);
this._linkCallout.render(); this._linkCallout.render();
@@ -307,7 +308,7 @@ export class MarkdownToolbarComponent extends AngularDisposable {
return calloutOptions; return calloutOptions;
} }
private getCurrentSelectionText(): string { private getCurrentLinkLabel(): string {
if (this.cellModel.currentMode === CellEditModes.WYSIWYG) { if (this.cellModel.currentMode === CellEditModes.WYSIWYG) {
return document.getSelection()?.toString() || ''; return document.getSelection()?.toString() || '';
} else { } else {
@@ -316,18 +317,31 @@ export class MarkdownToolbarComponent extends AngularDisposable {
if (selection && !selection.isEmpty()) { if (selection && !selection.isEmpty()) {
const textModel = editorControl?.getModel() as TextModel; const textModel = editorControl?.getModel() as TextModel;
const value = textModel?.getValueInRange(selection); const value = textModel?.getValueInRange(selection);
return value || ''; let linkMatches = value?.match(linksRegex);
return linkMatches?.groups.text || value || '';
} }
return ''; return '';
} }
} }
private getCurrentLinkUrl(): string { private getCurrentLinkUrl(): string {
if (this.cellModel.currentMode === CellEditModes.WYSIWYG) {
if (document.getSelection().anchorNode.parentNode['protocol'] === 'file:') { if (document.getSelection().anchorNode.parentNode['protocol'] === 'file:') {
return document.getSelection().anchorNode.parentNode['pathname'] || ''; return document.getSelection().anchorNode.parentNode['pathname'] || '';
} else { } else {
return document.getSelection().anchorNode.parentNode['href'] || ''; return document.getSelection().anchorNode.parentNode['href'] || '';
} }
} else {
const editorControl = this.getCellEditorControl();
const selection = editorControl?.getSelection();
if (selection && !selection.isEmpty()) {
const textModel = editorControl?.getModel() as TextModel;
const value = textModel?.getValueInRange(selection);
let linkMatches = value?.match(linksRegex);
return linkMatches?.groups.url || '';
}
return '';
}
} }
private getCellEditorControl(): IEditor | undefined { private getCellEditorControl(): IEditor | undefined {