From 6560832421e7e6123252416e2f19a1599adf4e35 Mon Sep 17 00:00:00 2001 From: Wang Zhuochun Date: Wed, 4 Dec 2013 02:47:24 +0800 Subject: [PATCH] refactoring code --- extension/packages/commands.coffee | 28 +++++++--------------------- extension/packages/find-link.coffee | 29 ++++++++++++----------------- extension/packages/prefs.coffee | 2 ++ 3 files changed, 21 insertions(+), 38 deletions(-) diff --git a/extension/packages/commands.coffee b/extension/packages/commands.coffee index 803784b..22827db 100644 --- a/extension/packages/commands.coffee +++ b/extension/packages/commands.coffee @@ -220,31 +220,17 @@ command_follow_in_tab = helper_follow.bind(undefined, {inTab: true}) # Follow multiple links with hint markers command_follow_multiple = helper_follow.bind(undefined, {inTab: true, multiple: true}) -# TODO : move these patterns to settings -# -# NOTE : If a page contains both a single angle-bracket link and a double angle-bracket link, then in -# most cases the single bracket link will be "prev/next page" and the double bracket link will be -# "first/last page", so we put the single bracket first in the pattern string so that it gets searched -# for first. -# -# "\bprev\b,\bprevious\b,\bback\b,<,←,«,≪,<<" -previousPatterns = "prev,previous,back,<,\u2190,\xab,\u226a,<<" -# "\bnext\b,\bmore\b,>,→,»,≫,>>" -nextPatterns = "next,more,>,\u2192,\xbb,\u226b,>>" +helper_follow_link = ({ type, inTab }, vim) -> + pattern = getPref("#{ type }_patterns") || "" + strings = pattern.split(",").filter( (s) -> s.trim().length ) + link = find_link.find(vim.window.document, type, strings) + utils.simulateClick(link, {metaKey: inTab, ctrlKey: inTab}) if link # Follow previous page -command_follow_prev = (vim) -> - #previousPatterns = getPref('previous_link_patterns') || "" - previousStrings = previousPatterns.split(",").filter( (s) -> s.trim().length ) - link = find_link.find vim.window.document, "prev", previousStrings - utils.simulateClick(link, {metaKey: false, ctrlKey: false}) if link +command_follow_prev = helper_follow_link.bind(undefined, { type: "prev", inTab: false }) # Follow next page -command_follow_next = (vim) -> - #nextPatterns = getPref('next_link_patterns') || "" - nextStrings = nextPatterns.split(",").filter( (s) -> s.trim().length ) - link = find_link.find vim.window.document, "next", nextStrings - utils.simulateClick(link, {metaKey: false, ctrlKey: false}) if link +command_follow_next = helper_follow_link.bind(undefined, { type: "next", inTab: false }) # Move current tab to the left command_tab_move_left = (vim) -> diff --git a/extension/packages/find-link.coffee b/extension/packages/find-link.coffee index 90418dc..24877d5 100644 --- a/extension/packages/find-link.coffee +++ b/extension/packages/find-link.coffee @@ -47,28 +47,24 @@ findLinkPattern = (document, patterns) -> isElementMatchPattern(link, patterns)) candidateLinks.push(link) - console.log(candidateLinks) - return if (candidateLinks.length == 0) for link in candidateLinks link.wordCount = link.textContent.trim().split(/\s+/).length - console.log(candidateLinks) - # We can use this trick to ensure that Array.sort is stable. We need this property to retain the reverse # in-page order of the links. candidateLinks.forEach((a,i) -> a.originalIndex = i) # favor shorter links, and ignore those that are more than one word longer than the shortest link candidateLinks = - candidateLinks - .sort((a, b) -> - if (a.wordCount == b.wordCount) then a.originalIndex - b.originalIndex else a.wordCount - b.wordCount - ) - .filter((a) -> a.wordCount <= candidateLinks[0].wordCount + 1) - - console.log(candidateLinks) + candidateLinks.sort((a, b) -> + if (a.wordCount == b.wordCount) + a.originalIndex - b.originalIndex + else + a.wordCount - b.wordCount + ).filter((a) -> + a.wordCount <= candidateLinks[0].wordCount + 1) for pattern in patterns exactWordRegex = @@ -112,21 +108,20 @@ getLinkElements = do -> isVisibleElement = (element) -> document = element.ownerDocument window = document.defaultView - docElem = document.documentElement - body = document.body # element that isn't visible on the page computedStyle = window.getComputedStyle(element, null) - if (computedStyle && (computedStyle.getPropertyValue('visibility') != 'visible' || - computedStyle.getPropertyValue('display') == 'none' || - computedStyle.getPropertyValue('opacity') == '0')) + if (computedStyle.getPropertyValue('visibility') != 'visible' || + computedStyle.getPropertyValue('display') == 'none' || + computedStyle.getPropertyValue('opacity') == '0') return false + # element that has 0 dimension clientRect = element.getBoundingClientRect() if (clientRect.width == 0 || clientRect.height == 0) return false - return true + true # Determine the link has a pattern matched diff --git a/extension/packages/prefs.coffee b/extension/packages/prefs.coffee index f55d4a1..38fd955 100644 --- a/extension/packages/prefs.coffee +++ b/extension/packages/prefs.coffee @@ -10,6 +10,8 @@ PREF_BRANCH = 'extensions.VimFx.' DEFAULT_PREF_VALUES = addon_id: 'VimFx@akhodakivskiy.github.com' hint_chars: 'fjdkslaghrueiwovncm' # preferably use letters only + prev_patterns: 'prev,previous,back,<,\xab,<<' + next_patterns: 'next,more,>,\xbb,>>' disabled: false scroll_step_lines: 6 black_list: '*mail.google.com*' -- 2.39.3