diff --git a/src/hyperlink.ts b/src/hyperlink.ts new file mode 100644 index 0000000..5cff82c --- /dev/null +++ b/src/hyperlink.ts @@ -0,0 +1,23 @@ +//Hyperlink related functions + +export function stringToHyperlink(rawData: any): any { + try { + //checks if the rawData provided is a valid url + const url = new URL(rawData); + //turns url into a hyperlink + const hyperlink = rawData.link(url); + return hyperlink; + } catch (e) { + return rawData; + } +} + +export function checkHyperlink(rawData: any): any { + try { + //checks if the rawData provided is a valid url + new URL(rawData); + return true; + } catch (e) { + return false; + } +} diff --git a/src/index.ts b/src/index.ts index 90aaf7f..b624a31 100644 --- a/src/index.ts +++ b/src/index.ts @@ -368,6 +368,14 @@ function addCommands( } }); + commands.addCommand(CommandIDs.openLinkContextMenu, { + label: 'Open Link', + execute: () => { + tracker.currentWidget && + tracker.currentWidget.content.commandSignal.emit('open-link'); + } + }); + commands.addCommand(CommandIDs.clearCells, { label: 'Clear Contents', execute: () => { @@ -397,6 +405,8 @@ function addCommands( 'cutContextMenu', 'copyContextMenu', 'pasteContextMenu', + 'separator', + 'openLinkContextMenu', //temporarily here to display the open link command on the context menu 'separator' ]; @@ -709,5 +719,6 @@ export const CommandIDs: { [key: string]: string } = { save: 'tde-save', clearCells: 'tde-clear-contents', clearColumns: 'tde-clear-columns', - clearRows: 'tde-clear-rows' + clearRows: 'tde-clear-rows', + openLinkContextMenu: 'tde:open-link' }; diff --git a/src/widget.ts b/src/widget.ts index 943b246..730198a 100644 --- a/src/widget.ts +++ b/src/widget.ts @@ -38,6 +38,7 @@ import { unsaveDialog } from './dialog'; import { PaintedGrid } from './grid'; import { HeaderTextRenderer } from './headercelleditor'; import { RichKeyHandler } from './keyhandler'; +import { checkHyperlink } from './hyperlink'; const CSV_CLASS = 'jp-CSVViewer'; const CSV_GRID_CLASS = 'jp-CSVViewer-grid'; @@ -553,6 +554,15 @@ export class DSVEditor extends Widget { let update: DSVEditor.ModelChangedArgs | null = null; switch (command) { + case 'open-link': { + const cellData = this.dataModel.data('body', r1, c1); + const checkedHyperlink = checkHyperlink(cellData); + if (checkedHyperlink) { + window.open(cellData, '_blank'); + } + break; + } + case 'insert-rows-above': { update = this.dataModel.addRows('body', r1, rowSpan); break; @@ -894,7 +904,8 @@ export namespace DSVEditor { | 'paste-cells' | 'undo' | 'redo' - | 'save'; + | 'save' + | 'open-link'; /** * The arguments emitted to the Editor when the datamodel changes