
基盤#キーボード#キーバインド#エディタ#module
cosense-keyboard-events
Cosense既存のキーボードショートカット(インデント移動、行末での改行、ページ先頭・末尾への移動など)を、プログラムから呼び出せるようにまとめた部品です。Vim風キーバインドのような独自のキー操作スクリプトを書くときの土台になります。
デモ

インストール
設定ページの code:script.js に貼る
import { moveLineIndentTo } from "/api/code/cosense-toolbox/cosense-keyboard-events/module.js";
ソースコード全文を見る(JS)
let TextEditor;
function getTextEditor() {
if (!TextEditor) {
TextEditor = document.querySelector("#text-input");
}
return TextEditor;
}
/**
* @param position {string} 👈️か👉のどっちかをいれる Left|Rgiht
**/
function moveLineIndentTo(position) {
const ctrlBEvent = new KeyboardEvent("keydown", {
isTrusted: true,
altKey: true,
bubbles: true,
cancelable: true,
charCode: 0,
code: "Arrow" + position,
composed: true,
ctrlKey: false,
currentTarget: $("div#app-container"),
defaultPrevented: true,
key: "Arrow" + position,
keyCode: position === "Right" ? 39 : 37,
//which: 66
});
getTextEditor().dispatchEvent(ctrlBEvent);
}
function moveCursorToEndOfLineAndBreakLine() {
let ctrlBEvent = new KeyboardEvent("keydown", {
isTrusted: true,
altKey: false,
bubbles: true,
cancelable: true,
charCode: 0,
code: "KeyE",
composed: true,
ctrlKey: true,
currentTarget: $("div#app-container"),
defaultPrevented: true,
key: "e",
keyCode: 69,
});
getTextEditor().dispatchEvent(ctrlBEvent);
ctrlBEvent = new KeyboardEvent("keydown", {
isTrusted: true,
altKey: false,
bubbles: true,
cancelable: true,
charCode: 0,
code: "Enter",
composed: true,
ctrlKey: false,
currentTarget: $("div#app-container"),
defaultPrevented: true,
key: "Enter",
keyCode: 13,
});
getTextEditor().dispatchEvent(ctrlBEvent);
}
function moveCursorToEndOfPage() {
let ctrlBEvent = new KeyboardEvent("keydown", {
isTrusted: true,
altKey: false,
bubbles: true,
cancelable: true,
charCode: 0,
code: "ArrowDown",
composed: true,
ctrlKey: false,
currentTarget: $("div#app-container"),
defaultPrevented: true,
key: "ArrowDown",
metaKey: true,
keyCode: 40,
});
getTextEditor().dispatchEvent(ctrlBEvent);
}
function moveCursorToStartOfPage() {
let ctrlBEvent = new KeyboardEvent("keydown", {
isTrusted: true,
altKey: false,
bubbles: true,
cancelable: true,
charCode: 0,
code: "ArrowDown",
composed: true,
ctrlKey: false,
currentTarget: $("div#app-container"),
defaultPrevented: true,
key: "ArrowUp",
metaKey: true,
keyCode: 38,
});
getTextEditor().dispatchEvent(ctrlBEvent);
}
import { moveCursorToLeft } from "/api/code/cosense-toolbox/move-cursor/module.js";
export {
getTextEditor,
moveCursorToLeft,
moveLineIndentTo,
moveCursorToEndOfLineAndBreakLine,
moveCursorToEndOfPage,
moveCursorToStartOfPage,
};