Browse Source

Mousedown shortcut

Alexandre Dieulot 5 years ago
parent
commit
f9dbd966f1
1 changed files with 37 additions and 3 deletions
  1. 37 3
      instantpage.js

+ 37 - 3
instantpage.js

@@ -9,11 +9,14 @@ const isSupported = prefetchElement.relList && prefetchElement.relList.supports
 const allowQueryString = 'instantAllowQueryString' in document.body.dataset
 const allowQueryString = 'instantAllowQueryString' in document.body.dataset
 const allowExternalLinks = 'instantAllowExternalLinks' in document.body.dataset
 const allowExternalLinks = 'instantAllowExternalLinks' in document.body.dataset
 const useWhitelist = 'instantWhitelist' in document.body.dataset
 const useWhitelist = 'instantWhitelist' in document.body.dataset
+const mousedownShortcut = !('instantNoMousedownShortcut' in document.body.dataset)
+const DELAY_TO_NOT_BE_CONSIDERED_A_TOUCH_INITIATED_ACTION = 1111
 
 
 let delayOnHover = 65
 let delayOnHover = 65
 let useMousedown = false
 let useMousedown = false
 let useMousedownOnly = false
 let useMousedownOnly = false
 let useViewport = false
 let useViewport = false
+
 if ('instantIntensity' in document.body.dataset) {
 if ('instantIntensity' in document.body.dataset) {
   const intensity = document.body.dataset.instantIntensity
   const intensity = document.body.dataset.instantIntensity
 
 
@@ -59,8 +62,12 @@ if (isSupported) {
   if (!useMousedown) {
   if (!useMousedown) {
     document.addEventListener('mouseover', mouseoverListener, eventListenersOptions)
     document.addEventListener('mouseover', mouseoverListener, eventListenersOptions)
   }
   }
-  else {
-    document.addEventListener('mousedown', mousedownListener, eventListenersOptions)
+  else if (!mousedownShortcut) {
+      document.addEventListener('mousedown', mousedownListener, eventListenersOptions)
+  }
+
+  if (mousedownShortcut) {
+    document.addEventListener('mousedown', mousedownShortcutListener, eventListenersOptions)
   }
   }
 
 
   if (useViewport) {
   if (useViewport) {
@@ -113,7 +120,7 @@ function touchstartListener(event) {
 }
 }
 
 
 function mouseoverListener(event) {
 function mouseoverListener(event) {
-  if (performance.now() - lastTouchTimestamp < 1100) {
+  if (performance.now() - lastTouchTimestamp < DELAY_TO_NOT_BE_CONSIDERED_A_TOUCH_INITIATED_ACTION) {
     return
     return
   }
   }
 
 
@@ -152,6 +159,33 @@ function mouseoutListener(event) {
   }
   }
 }
 }
 
 
+function mousedownShortcutListener(event) {
+  if (performance.now() - lastTouchTimestamp < DELAY_TO_NOT_BE_CONSIDERED_A_TOUCH_INITIATED_ACTION) {
+    return
+  }
+
+  const linkElement = event.target.closest('a')
+
+  if (!linkElement) {
+    return
+  }
+
+  linkElement.addEventListener('click', function (event) {
+    if (event.which > 1 || event.metaKey || event.ctrlKey) {
+      return
+    }
+
+    if (event.detail == 1337) {
+      return
+    }
+
+    event.preventDefault()
+  }, {capture: true, passive: false, once: true})
+
+  const customEvent = new MouseEvent('click', {view: window, bubbles: true, cancelable: false, detail: 1337})
+  linkElement.dispatchEvent(customEvent)
+}
+
 function isPreloadable(linkElement) {
 function isPreloadable(linkElement) {
   if (!linkElement || !linkElement.href) {
   if (!linkElement || !linkElement.href) {
     return
     return