From 82466a292d99c003ea78b29ab22135f80aaf12b7 Mon Sep 17 00:00:00 2001 From: anijanyan Date: Fri, 28 Oct 2022 14:11:55 +0400 Subject: [PATCH] Namings fixed due to code review --- src/commands/default_commands.js | 4 ++-- src/editor.js | 23 +++++++++++++---------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/commands/default_commands.js b/src/commands/default_commands.js index acb1e374f75..d7c6413247d 100644 --- a/src/commands/default_commands.js +++ b/src/commands/default_commands.js @@ -745,9 +745,9 @@ exports.commands = [{ scrollIntoView: "cursor", readOnly: true }, { - name: "jumptodef", + name: "openlink", bindKey: bindKey("Ctrl+F3", "F3"), - exec: function(editor) { editor.jumptodef(); } + exec: function(editor) { editor.openLink(); } }, { name: "joinlines", description: "Join lines", diff --git a/src/editor.js b/src/editor.js index fb9d940491f..276893fb527 100644 --- a/src/editor.js +++ b/src/editor.js @@ -1733,32 +1733,35 @@ Editor.$uid = 0; * Finds link at defined {row} and {column} * @returns {String} **/ - this.findLinkAt = function (row,column) { + this.findLinkAt = function (row, column) { var line = this.session.getLine(row); var wordParts = line.split(/((?:https?|ftp):\/\/[\S]+)/); - var delta = column; - if (delta < 0) delta = 0; - var curLength = 0, itLength = 0, match; - wordParts.forEach(function (item) { - itLength = curLength + item.length; - if (delta >= curLength && delta <= itLength) { + 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; } } - curLength = itLength; - }); + previousPosition = currentPosition; + } return match; }; /** * Open valid url under cursor in another tab + * @returns {Boolean} **/ - this.jumptodef = function () { + 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; }; /**