コードブロックを分割
from /villagepump/コードブロックをワンキーで分割したい
2024/5/26
script.js:10 Uncaught TypeError: Cannot read properties of undefined (reading 'addEventListener')
.addEventListener("keydown"の箇所
feat(browser): Keep listeners registered to #text-input by takker99 · Pull Request #172 · takker99/scrapbox-userscript-std · GitHubですでに対応されている?
/takker/✅️scrapbox-userscript-stdにaddTextInputEventListenerを実装する
v0.26.0-
addTextInputEventListenerを使うように変更
ScrapBindingsから使うためにexport
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");
}
scrapbox-jp/types
$ deno check --remote -r=https://scrapbox.io https://scrapbox.io/api/code/yosider-scripts/コードブロックを分割/script.ts