Skip to content

Instantly share code, notes, and snippets.

@thesved
Last active February 23, 2022 23:51
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thesved/e66a32af296925ba306b121bb516e1c8 to your computer and use it in GitHub Desktop.
Save thesved/e66a32af296925ba306b121bb516e1c8 to your computer and use it in GitHub Desktop.
/*
* Viktor's Roam Mobile Double tap to Exluce Filters and Right click on bullets
* version: 0.2
* author: @ViktorTabori
*
* How to install it:
* - go to page [[roam/js]]
* - create a node with: { {[[roam/js]]}}
* - create a clode block under it, and change its type from clojure to javascript
* - allow the running of the javascript on the {{[[roam/js]]}} node
* - double tap a filter and it gets excluded
* - double tap on bullets to simulate right click
*/
window.ViktorMobileTap = window.ViktorMobileTap || (function(){
// max wait for second tap in ms
var maxWait = 200;
var added;
addListener();
return {
added: added,
addListener: addListener,
removeListener: removeListener,
};
function addListener() {
if (added) return;
added = true;
document.addEventListener('touchstart', process, {passive: false, capture: true});
console.log('** double tap installed **');
}
function removeListener() {
if (!added) return;
added = false;
document.removeEventListener('touchstart', process, {passive: false, capture: true});
console.log('** double tap STOPPED **');
}
function process(e) {
var target = e.target;
// check click on elements we plan to modify
var action = [];
try {
// filter double tap to exclude
if (target.classList.contains('bp3-button') && target.parentNode.parentNode.parentNode.parentNode.classList.contains('bp3-popover-content')) {
action = [target, ['mousedown', 'click', 'mouseup'], true, {shiftKey:true}];
} else
// bullet double tap to right click
if (target.className && target.className.match(/simple-bullet-(inner|outer)|controls|block-expand|rm-caret/i)) {
// make clicking on the controls element
while (target.classList && !target.classList.contains('controls') && target.parentNode) target = target.parentNode;
var bound = target.getBoundingClientRect();
action = [target, ['contextmenu'], false, {clientX:(bound.left+bound.width/2) , clientY:(bound.top+bound.height/2)}];
}
} catch(_) {
return;
}
if (!action.length) return;
// prevent propagation of event
e.preventDefault();
e.stopPropagation();
// first tap
if(!target.dataset.doubleTap) {
target.dataset.doubleTap = 1;
setTimeout( function() {
if (target.dataset.doubleTap) {
// click on the original target if no double tap happened
simulateClick(e.target, ['mousedown', 'click', 'mouseup'], true);
}
delete target.dataset.doubleTap;
}, maxWait );
return false;
}
//action on double tap goes below
//console.log('*** Double tap detected ***',target);
delete target.dataset.doubleTap;
simulateClick.apply(null, action);
// mouse click emulation
function simulateClick(element, events, leftButton, opts) {
events.forEach(function(type){
element.dispatchEvent(new MouseEvent(type, {
view: window,
bubbles: true,
cancelable: true,
buttons: leftButton?1:2,
...opts,
}))
});
}
}
})();
@LukasCY
Copy link

LukasCY commented May 28, 2021

I cannot collapse a block with one click after installation of the plugin. Is it the idea in oder to enable double click?

@thesved
Copy link
Author

thesved commented May 30, 2021

The double-tap plugin was the first iteration, long-tap from ViktoRoam is recommended: https://github.com/thesved/ViktoRoam

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment