コードブロックをワンキーで分割したい
この操作を1つの操作でやりたいyosider.icon
code:test
line 1| ←ここにカーソル
line 2
↓
code:test
line 1
| ←ここにカーソル
code:test
line 2
わかる〜基素.icon
できた~takker.icon
早い基素.iconyosider.icon*2wogikaze.icon
ありがてぇ…yosider.icon
https://gyazo.com/4398f623c6d14c1ae6064dc93133e397
Ctrl+Enterで分割
code:sample.ts
import { press, insertText, takeCursor, textInput } from "../../takker/scrapbox-userscript-std/dom.ts";
import { Scrapbox } from "../../takker/scrapbox-jp%2Ftypes/userscript.ts";
declare const scrapbox: Scrapbox;
メインコード
code:sample.ts
textInput()!.addEventListener("keydown", async (e) => {
if (e.key !== "Enter" || e.metaKey || e.altKey || !e.ctrlKey || e.shiftKey) return;
code:sample.ts
if (scrapbox.Layout !== "page") return;
コードブロック確認
code:sample.ts
const cursor = takeCursor()
const line = cursor.getPosition().line;
const cursorLine = scrapbox.Page.linesline; if (!("codeBlock" in cursorLine)
|| cursorLine.codeBlock.start
|| cursorLine.codeBlock.end) return;
分割実行
code:sample.ts
press("End")
press("Enter");
cursor.setPosition({ line: cursor.getPosition().line, char: cursorLine.codeBlock.indent - 1 });
press("End", { shiftKey: true });
await insertText(code:${cursorLine.codeBlock.filename ?? ""});
press("Home");
press("Enter");
press("ArrowUp");
code:sample.ts
});
依存コードをHTTP importからJSR importに変更したけど、ちゃんとbuildできてるようだねtakker.icon
よかったよかった
code:sample.min.js
var d=(e,t)=>{if(!(e instanceof HTMLTextAreaElement))throw new TypeError("${t}" must be HTMLTextAreaElement but actual is "${e}")};var o=()=>{let e=document.getElementById("text-input");if(!!e)return d(e,"textarea#text-input"),e};var s=(e,t)=>{let{noModifiedKeys:i=!1,...n}=t??{},c={bubbles:!0,cancelable:!0,keyCode:xe,...i?{}:{...n}},r=o();if(!r)throw Error("#text-input must exist.");r.dispatchEvent(new KeyboardEvent("keydown",c)),r.dispatchEvent(new KeyboardEvent("keyup",c))},x={Backspace:8,Tab:9,Enter:13,Delete:46,Escape:27," ":32,PageUp:33,PageDown:34,End:35,Home:36,ArrowLeft:37,ArrowUp:38,ArrowRight:39,ArrowDown:40,a:65,A:65,b:66,B:66,c:67,C:67,d:68,D:68,e:69,E:69,f:70,F:70,g:71,G:71,h:72,H:72,i:73,I:73,j:74,J:74,k:75,K:75,l:76,L:76,m:77,M:77,n:78,N:78,o:79,O:79,p:80,P:80,q:81,Q:81,r:82,R:82,s:83,S:83,t:84,T:84,u:85,U:85,v:86,V:86,w:87,W:87,x:88,X:88,y:89,Y:89,z:90,Z:90,0:48,1:49,2:50,3:51,4:52,5:53,6:54,7:55,8:56,9:57,F1:113,F2:114,F3:115,F4:116,F5:117,F6:118,F7:119,F8:120,F9:121,F10:122,F11:123,F12:124,":":186,"*":186,";":187,"+":187,"-":189,"=":189,".":190,">":190,"/":191,"?":191,"@":192,"":192,"[":219,"{":219,"\\":220,"|":220,"]":221,"}":221,"^":222,"~":222,_:226};var a=e=>new Promise(t=>setTimeout(()=>t(),e));var m=async e=>{let t=o();if(!t)throw Error("#text-input is not ditected.");t.focus(),t.value=e;let i=new InputEvent("input",{bubbles:!0});t.dispatchEvent(i),await a(1)};var u=()=>{let e=o();if(!e)throw Error("#text-input is not found.");let t=Object.keys(e).find(r=>r.startsWith("__reactFiber"));if(!t)throw Error('#text-input must has the property whose name starts with "__reactFiber"');let i=e[t].return.return.stateNode._stores,n=i.find(r=>r.constructor.name==="Cursor");if(!n)throw Error('#text-input must has a "Cursor" store.');let c=i.find(r=>r.constructor.name==="Selection");if(!c)throw Error('#text-input must has a "Selection" store.');return{cursor:n,selection:c}};var p=()=>u().cursor;o().addEventListener("keydown",async e=>{if(e.key!=="Enter"||e.metaKey||e.altKey||!e.ctrlKey||e.shiftKey||scrapbox.Layout!=="page")return;let t=p(),i=t.getPosition().line,n=scrapbox.Page.lines[i];!("codeBlock"in n)||n.codeBlock.start||n.codeBlock.end||(s("End"),s("Enter"),t.setPosition({line:t.getPosition().line,char:n.codeBlock.indent-1}),s("End",{shiftKey:!0}),await m(code:${n.codeBlock.filename??""}`),s("Home"),s("Enter"),s("ArrowUp"))});