Cosense Toolbox
userscript 一覧テーマ作成使い方パーサーGitHub ↗
cosense-keyboard-events
基盤#キーボード#キーバインド#エディタ#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,
};
元のCosenseページを見る ↗

Cosense Toolbox / Cosense をもっと面白く、もっと強力に使うためのツール群

GitHub ↗

マイ・ツールボックス

    積んだ import を1つにまとめてコピー → 自分の設定ページ(ユーザー名ページ)に貼るだけ。