コードブロックを分割
2024/5/26
.addEventListener("keydown"の箇所
v0.26.0-
code:script.ts
import { Scrapbox } from "../scrapbox-jp%2Ftypes/userscript.ts";
import { addTextInputEventListener, insertText, press, takeCursor } from "../scrapbox-userscript-std/dom.ts";
declare const scrapbox: Scrapbox;
addTextInputEventListener("keydown", async (e: KeyboardEvent) => {
if (e.key !== "Enter" || e.metaKey || e.altKey || !e.ctrlKey || e.shiftKey) return;
await splitCodeBlock();
});
export async function splitCodeBlock() {
if (scrapbox.Layout !== "page") return;
const cursor = takeCursor()
const line = cursor.getPosition().line;
const cursorLine = scrapbox.Page.linesline; if (!("codeBlock" in cursorLine)
|| cursorLine.codeBlock.start
|| cursorLine.codeBlock.end) return;
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");
}