Skip to content

Instantly share code, notes, and snippets.

@ma3tk
Created January 5, 2021 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ma3tk/d39361ce3ff7e6be5ac601a4dda3eef6 to your computer and use it in GitHub Desktop.
Save ma3tk/d39361ce3ff7e6be5ac601a4dda3eef6 to your computer and use it in GitHub Desktop.
GitHub issue タブとシフトタブでスペース挿入 / tampermonkey
// ==UserScript==
// @name タブとシフトタブでスペース挿入
// @namespace http://tampermonkey.net/
// @version 0.1
// @description issue書く時にタブ使うと項目移動しちゃうので、無効にしてスペースが入るようになりました
// @author @ma3tk
// @match https://github.com/*
// @grant none
// @see https://qiita.com/n-ishida/items/da1cc0cfdaebe789d047
// @see https://qiita.com/laineus/items/12a220d2ab086931232d
// ==/UserScript==
(function() {
'use strict';
const TAB_STR = ' ';
const element = document.getElementById("issue_body");
if (element) {
element.addEventListener("keydown", inputTab);
}
function inputTab(e) {
if (e.target.tagName !== 'TEXTAREA' || e.keyCode !== 9) return false;
e.preventDefault();
const selection = { left: e.target.selectionStart, right: e.target.selectionEnd };
const lineStart = e.target.value.substr(0, selection.left).split('\n').length - 1;
const lineEnd = e.target.value.substr(0, selection.right).split('\n').length - 1;
const lines = e.target.value.split('\n');
for (const i in lines) {
if (i < lineStart || i > lineEnd || lines[i] === '') {
continue;
}
if (!e.shiftKey) {
// 行頭にタブ挿入
lines[i] = TAB_STR + lines[i];
selection.left += i == lineStart ? TAB_STR.length : 0;
selection.right += TAB_STR.length;
} else if (lines[i].substr(0, TAB_STR.length) === TAB_STR) {
// 行頭のタブ削除
lines[i] = lines[i].substr(TAB_STR.length);
selection.left -= i == lineStart ? TAB_STR.length : 0;
selection.right -= TAB_STR.length;
}
}
e.target.value = lines.join('\n');
e.target.setSelectionRange(selection.left, selection.right);
return false;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment