diff --git a/src/commands/default_commands.js b/src/commands/default_commands.js index 6dfa827a625..d7c6413247d 100644 --- a/src/commands/default_commands.js +++ b/src/commands/default_commands.js @@ -744,6 +744,10 @@ exports.commands = [{ multiSelectAction: "forEach", scrollIntoView: "cursor", readOnly: true +}, { + name: "openlink", + bindKey: bindKey("Ctrl+F3", "F3"), + exec: function(editor) { editor.openLink(); } }, { name: "joinlines", description: "Join lines", diff --git a/src/editor.js b/src/editor.js index 47580fc7493..276893fb527 100644 --- a/src/editor.js +++ b/src/editor.js @@ -1729,6 +1729,41 @@ Editor.$uid = 0; } }; + /** + * Finds link at defined {row} and {column} + * @returns {String} + **/ + this.findLinkAt = function (row, column) { + var line = this.session.getLine(row); + var wordParts = line.split(/((?:https?|ftp):\/\/[\S]+)/); + var columnPosition = column; + if (columnPosition < 0) columnPosition = 0; + var previousPosition = 0, currentPosition = 0, match; + for (let item of wordParts) { + currentPosition = previousPosition + item.length; + if (columnPosition >= previousPosition && columnPosition <= currentPosition) { + if (item.match(/((?:https?|ftp):\/\/[\S]+)/)) { + match = item.replace(/[\s:.,'";}\]]+$/, ""); + break; + } + } + previousPosition = currentPosition; + } + return match; + }; + + /** + * Open valid url under cursor in another tab + * @returns {Boolean} + **/ + this.openLink = function () { + var cursor = this.selection.getCursor(); + var url = this.findLinkAt(cursor.row, cursor.column); + if (url) + window.open(url, '_blank'); + return url != null; + }; + /** * Removes all the lines in the current selection * @related EditSession.remove diff --git a/src/editor_commands_test.js b/src/editor_commands_test.js index ce9fc657d9d..e74e5e7c2c2 100644 --- a/src/editor_commands_test.js +++ b/src/editor_commands_test.js @@ -5,8 +5,6 @@ if (typeof process !== "undefined") { "use strict"; -var ace = require("./ace"); -var EditSession = require("./edit_session").EditSession; var Editor = require("./editor").Editor; var UndoManager = require("./undomanager").UndoManager; var MockRenderer = require("./test/mockrenderer").MockRenderer; @@ -533,6 +531,16 @@ module.exports = { exec("selectlineend", 1); editor.execCommand(editor.commands.byName.joinlines); assert.equal(editor.getValue(), "foo for foo foo foo for foo foo\nfoo for foo foo"); + }, + "test findlink": function() { + editor = new Editor(new MockRenderer()); + + editor.setValue("foo for foo foo\nhttps://www.google.com/", 1); + var url = editor.findLinkAt(0, 1); + assert.equal(url, null); + + url = editor.findLinkAt(1, 5); + assert.equal(url, "https://www.google.com/"); } };