Skip to content

Instantly share code, notes, and snippets.

@Armaxis
Last active March 16, 2017 18:33
Show Gist options
  • Save Armaxis/5377f011f17ff59a36fb5fa31ee3d7f9 to your computer and use it in GitHub Desktop.
Save Armaxis/5377f011f17ff59a36fb5fa31ee3d7f9 to your computer and use it in GitHub Desktop.
Custom JS for Bitbucket that adds few buttons
//This custom JS adds few missing features to Bitbucket's code review tool
//1) emoji buttons to Edit page to easily mark Pull Request
//2) 'No whitespaces' button is added next to 'Commits' and 'Activity' and reloads the page with whitespaces diff turned off.
//I'm not familiar with JS or jQuery, so this code is ugly and could be much better. But it works, so I have that going for me which is nice
//I used Chrome Extension to enable usage of this code: https://chrome.google.com/webstore/detail/custom-javascript-for-web/poakhlngfciodnhlhhgnaaelnpjljija?hl=en
//Reference to last focused text field
var lastFocused;
$("input[type='text']").focus(function() {
lastFocused = document.activeElement;
});
//http://stackoverflow.com/questions/1064089/inserting-a-text-where-cursor-is-using-javascript-jquery
function insertText(text) {
var input = lastFocused;
console.log(input);
if (input == undefined) { return; }
var scrollPos = input.scrollTop;
var pos = 0;
var browser = ((input.selectionStart || input.selectionStart == "0") ?
"ff" : (document.selection ? "ie" : false ) );
if (browser == "ie") {
input.focus();
var range = document.selection.createRange();
range.moveStart ("character", -input.value.length);
pos = range.text.length;
}
else if (browser == "ff") { pos = input.selectionStart };
var front = (input.value).substring(0, pos);
var back = (input.value).substring(pos, input.value.length);
input.value = front+text+back;
pos = pos + text.length;
if (browser == "ie") {
input.focus();
var range = document.selection.createRange();
range.moveStart ("character", -input.value.length);
range.moveStart ("character", pos);
range.moveEnd ("character", 0);
range.select();
}
else if (browser == "ff") {
input.selectionStart = pos;
input.selectionEnd = pos;
input.focus();
}
input.scrollTop = scrollPos;
}
$(function() {
//If on edit page of PR - add custom buttons to panel
if (window.location.href.indexOf("pull-requests/update") > -1) {
var zap = $('<li/>').attr(
{ type: 'button',
class: 'markItUpButton markdown',
value:'',
title:'Needs changes',
style: 'background-image: url(https://d301sr5gafysq2.cloudfront.net/739f014be764/emoji/img/zap.svg);height: 26px;width: 26px;background-size: 18px 18px;background-repeat: no-repeat;background-position: 3px 3px;'
}).click(function (){ insertText(':zap:');});
var ready = $('<li/>').attr(
{ type: 'button',
class: 'markItUpButton markdown',
value:'',
title:'Ready for merge',
style: 'background-image: url(https://d301sr5gafysq2.cloudfront.net/739f014be764/emoji/img/white_check_mark.svg);height: 26px;width: 26px;background-size: 18px 18px;background-repeat: no-repeat;background-position: 3px 3px;'
}).click(function (){ insertText(':white_check_mark:');});
var conflict = $('<li/>').attr(
{ type: 'button',
class: 'markItUpButton markdown',
value:'',
title:'Merge conflict',
style: 'background-image: url(https://d301sr5gafysq2.cloudfront.net/739f014be764/emoji/img/warning.svg);height: 26px;width: 26px;background-size: 18px 18px;background-repeat: no-repeat;background-position: 3px 3px;'
}).click(function (){ insertText(':warning:');});
var hold = $('<li/>').attr(
{ type: 'button',
class: 'markItUpButton markdown',
value:'',
title:'Do not merge yet',
style: 'background-image: url(https://d301sr5gafysq2.cloudfront.net/739f014be764/emoji/img/no_entry_sign.svg);height: 26px;width: 26px;background-size: 18px 18px;background-repeat: no-repeat;background-position: 3px 3px;'
}).click(function (){ insertText(':no_entry_sign:');});
var m = $('<li/>').attr(
{ type: 'button',
class: 'markItUpButton markdown',
value:'',
title:'Merge person',
style: 'background-image: url(https://d301sr5gafysq2.cloudfront.net/739f014be764/emoji/img/m.svg);height: 26px;width: 26px;background-size: 18px 18px;background-repeat: no-repeat;background-position: 3px 3px;'
}).click(function (){ insertText('[:m: ]');});
var beetle = $('<li/>').attr(
{ type: 'button',
class: 'markItUpButton markdown',
value:'',
title:'Mark as bug',
style: 'background-image: url(https://d301sr5gafysq2.cloudfront.net/739f014be764/emoji/img/beetle.svg);height: 26px;width: 26px;background-size: 18px 18px;background-repeat: no-repeat;background-position: 3px 3px;'
}).click(function (){ insertText(':beetle:');});
var nma = $('<li/>').attr(
{ type: 'button',
class: 'markItUpButton markdown',
title:'Insert JIRA reference',
style: 'background-image: url(https://jira5.mobitv.corp/s/en_US-kdfcs0-418945332/854/3/_/images/icon-jira-logo.png);height: 26px;width: 51px;background-color:#326ca6;background-size: 47px 24px;background-repeat: no-repeat;background-position: 1px 1px;'
}).click(function (){ insertText('[NMA-]');});
$(".markItUpHeader").find("ul").append(zap);
$(".markItUpHeader").find("ul").append(ready);
$(".markItUpHeader").find("ul").append(conflict);
$(".markItUpHeader").find("ul").append(hold);
$(".markItUpHeader").find("ul").append(m);
$(".markItUpHeader").find("ul").append(beetle);
$(".markItUpHeader").find("ul").append(nma);
} else if (window.location.href.indexOf("pull-requests") > -1 && window.location.href.indexOf("?w=1") == -1) {
//Add 'No whitespaces' button if on viewing page and this feature is not turned on yet.
var nospace = $('<input/>').attr(
{ type: 'button',
class: 'aui-tabs',
value: 'No whitespaces',
title: 'Show pull request without whitespace changes',
style: 'border-top-right-radius: 3px;height: 30px;background: #eee;float: left;margin: 0px 0px 0px -1px;width: 120px;border-right: 1px solid #ccc;border-top: 1px solid #ccc;border-left: 1px solid #ccc;border-bottom: 0px;padding: 5px 10px 4px;'
}).click(function (){ window.open(window.location.href + "?w=1","_self");});
$(".pr-tab-links").append(nospace);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment