scrapbox-shortcut
動機
他のユーザー操作拡張系のUserScriptでショートカットキーの対応を簡単にしたい。
ショートカットキーの表記を分かりやすくしたい。
機能
ScrapboxのUserScriptにショートカットキーを割り当てる。
設定方法
自分のページに下記を貼り付けて編集する。
code:scrapbox(js)
code:script.js
import '/api/code/foldrr/scrapbox-shortcut/script.js'
// キーの指定方法は下記を参照
// 設定例: 閲覧中のショートカットを割り当てる場合
scrapboxShortcut.onBrowse('ctrl+s', e => {
alert('閲覧中にショートカットキーを押した時の動作')
});
// 設定例: 検索中のショートカットを割り当てる場合
scrapboxShortcut.onSearch('esc', e => {
alert('検索中にショートカットキーを押した時の動作')
});
// 設定例: 編集中のショートカットを割り当てる場合
scrapboxShortcut.onEdit('ctrl+s', e => {
alert('編集中にショートカットキーを押した時の動作')
});
コード
依存ライブラリ
code:script.js
/* mousetrap v1.6.2 craig.is/killing/mice */
(function(p,t,h){function u(a,b,d){a.addEventListener?a.addEventListener(b,d,!1):a.attachEvent("on"+b,d)}function y(a){if("keypress"==a.type){var b=String.fromCharCode(a.which);a.shiftKey||(b=b.toLowerCase());return b}return ma.which?ma.which:qa.which?qa.which:String.fromCharCode(a.which).toLowerCase()}function E(a){var b=[];a.shiftKey&&b.push("shift");a.altKey&&b.push("alt");a.ctrlKey&&b.push("ctrl");a.metaKey&&b.push("meta");return b}function v(a){return"shift"==a||"ctrl"==a||"alt"==a|| "meta"==a}function z(a,b){var d,e=[];var c=a;"+"===c?c="+":(c=c.replace(/\+{2}/g,"+plus"),c=c.split("+"));for(d=0;d<c.length;++d){var k=cd;Ak&&(k=Ak);b&&"keypress"!=b&&Bk&&(k=Bk,e.push("shift"));v(k)&&e.push(k)}c=k;d=b;if(!d){if(!n){n={};for(var h in m)95<h&&112>h||m.hasOwnProperty(h)&&(n[mh]=h)}d=nc?"keydown":"keypress"}"keypress"==d&&e.length&&(d="keydown");return{key:k,modifiers:e,action:d}}function C(a,b){return null===a||a===t?!1:a===b?!0:C(a.parentNode,b)}function e(a){function b(a){a= a||{};var b=!1,l;for(l in n)al?b=!0:nl=0;b||(w=!1)}function d(a,b,r,g,F,e){var l,D=[],h=r.type;if(!f._callbacksa)return[];"keyup"==h&&v(a)&&(b=a);for(l=0;l<f._callbacksa.length;++l){var d=f._callbacksal;if((g||!d.seq||nd.seq==d.level)&&h==d.action){var c;(c="keypress"==h&&!r.metaKey&&!r.ctrlKey)||(c=d.modifiers,c=b.sort().join(",")===c.sort().join(","));c&&(c=g&&d.seq==g&&d.level==e,(!g&&d.combo==F||c)&&f._callbacksa.splice(l,1),D.push(d))}}return D}function h(a,b,d,g){f.stopCallback(b, b.target||b.srcElement,d,g)||!1!==a(b,d)||(b.preventDefault?b.preventDefault():b.returnValue=!1,b.stopPropagation?b.stopPropagation():b.cancelBubble=!0)}function c(a){"number"!==typeof a.which&&(a.which=a.keyCode);var b=y(a);b&&("keyup"==a.type&&x===b?x=!1:f.handleKey(b,E(a),a))}function k(a,d,r,g){function l(d){return function(){w=d;++na;clearTimeout(p);p=setTimeout(b,1E3)}}function e(d){h(r,d,a);"keyup"!==g&&(x=y(d));setTimeout(b,10)}for(var c=na=0;c<d.length;++c){var f=c+1===d.length?e:l(g|| z(dc+1).action);m(dc,f,g,a,c)}}function m(a,b,c,g,e){f._directMapa+":"+c=b;a=a.replace(/\s+/g," ");var h=a.split(" ");1<h.length?k(a,h,b,c):(c=z(a,c),f._callbacksc.key=f._callbacksc.key||[],d(c.key,c.modifiers,{type:c.action},g,a,e),f._callbacksc.keyg?"unshift":"push"({callback:b,modifiers:c.modifiers,action:c.action,seq:g,level:e,combo:a}))}var f=this;a=a||t;if(!(f instanceof e))return new e(a);f.target=a;f._callbacks={};f._directMap={};var n={},p,x=!1,q=!1,w=!1;f._handleKey=function(a, c,e){var g=d(a,c,e),f;c={};var l=0,k=!1;for(f=0;f<g.length;++f)gf.seq&&(l=Math.max(l,gf.level));for(f=0;f<g.length;++f)gf.seq?gf.level==l&&(k=!0,c[gf.seq]=1,h(gf.callback,e,gf.combo,gf.seq)):k||h(gf.callback,e,gf.combo);g="keypress"==e.type&&q;e.type!=w||v(a)||g||b(c);q=k&&"keydown"==e.type};f._bindMultiple=function(a,b,c){for(var d=0;d<a.length;++d)m(ad,b,c)};u(a,"keypress",c);u(a,"keydown",c);u(a,"keyup",c)}if(p){var m={8:"backspace",9:"tab",13:"enter",16:"shift",17:"ctrl", 18:"alt",20:"capslock",27:"esc",32:"space",33:"pageup",34:"pagedown",35:"end",36:"home",37:"left",38:"up",39:"right",40:"down",45:"ins",46:"del",91:"meta",93:"meta",224:"meta"},q={106:"*",107:"+",109:"-",110:".",111:"/",186:";",187:"=",188:",",189:"-",190:".",191:"/",192:"",219:"[",220:"\\",221:"]",222:"'"},B={"~":"","!":"1","@":"2","#":"3",$:"4","%":"5","^":"6","&":"7","*":"8","(":"9",")":"0",_:"-","+":"=",":":";",'"':"'","<":",",">":".","?":"/","|":"\\"},A={option:"alt",command:"meta","return":"enter",
escape:"esc",plus:"+",mod:/Mac|iPod|iPhone|iPad/.test(navigator.platform)?"meta":"ctrl"},n;for(h=1;20>h;++h)m111+h="f"+h;for(h=0;9>=h;++h)mh+96=h.toString();e.prototype.bind=function(a,b,d){a=a instanceof Array?a:a;this._bindMultiple.call(this,a,b,d);return this};e.prototype.unbind=function(a,b){return this.bind.call(this,a,function(){},b)};e.prototype.trigger=function(a,b){if(this._directMapa+":"+b)this._directMapa+":"+b({},a);return this};e.prototype.reset=function(){this._callbacks={}; this._directMap={};return this};e.prototype.stopCallback=function(a,b){return-1<(" "+b.className+" ").indexOf(" mousetrap ")||C(b,this.target)?!1:"INPUT"==b.tagName||"SELECT"==b.tagName||"TEXTAREA"==b.tagName||b.isContentEditable};e.prototype.handleKey=function(){return this._handleKey.apply(this,arguments)};e.addKeycodes=function(a){for(var b in a)a.hasOwnProperty(b)&&(mb=ab);n=null};e.init=function(){var a=e(t),b;for(b in a)"_"!==b.charAt(0)&&(eb=function(b){return function(){return ab.apply(a, arguments)}}(b))};e.init();p.Mousetrap=e;"undefined"!==typeof module&&module.exports&&(module.exports=e);"function"===typeof define&&define.amd&&define(function(){return e})}})("undefined"!==typeof window?window:null,"undefined"!==typeof window?document:null);
scrapbox-shortcut本体
code:script.js
(() => {
let mousetrapOnBrowse = new Mousetrap();
let mousetrapOnSearch = new Mousetrap(document.querySelector('.search-form input'));
let mousetrapOnEdit = new Mousetrap(document.querySelector('#text-input'));
mousetrapOnEdit.stopCallback = (e, el) => false;
window.scrapboxShortcut = {
onBrowse: (key, f) => {
mousetrapOnBrowse.bind(key, f);
},
onSearch: (key, f) => {
mousetrapOnSearch.bind(key, e => {
let cancel = f(e) === false;
if (cancel) return false;
});
},
onEdit: (key, f) => {
mousetrapOnEdit.bind(key, e => {
let cancel = f(e) === false;
if (cancel) return false;
});
},
};
})();