scrapbox-edit-emulation-2
UserScriptからScrapboxの編集を行うAPI
textを変更する操作のみを集めた
2021-05-09
14:33:14 行IDから行を削除する機能を追加した
dependencies
code:script.js
import {
goHead, goLine,
} from '../scrapbox-motion-emulation/script.js';
import {press} from '../scrapbox-keyboard-emulation-2/script.js';
import {insertText} from '../scrapbox-insert-text-2/script.js';
import {sleep} from '../sleep/script.js';
undoとredo
code:script.js
export function undo(count = 1) {
for (const _ of range(count)) {
press('z', {ctrlKey: true});
}
}
export function redo(count = 1) {
for (const _ of range(count)) {
press('z', {shiftKey: true, ctrlKey: true});
}
}
export function insertTimestamp(index = 1) {
for (const _ of range(count)) {
press('t', {altKey: true});
}
}
任意の行に挿入する
code:script.js
export async function insertLine(lineNo, text) {
await goLine(lineNo);
goHead();
press('Enter');
press('ArrowUp');
await insertText(text);
}
任意の行を削除する
cursorは一行下に移る
下に行がない場合は一行上に移る
引数指定を柔軟にした
valueが数値の時
countと合わせて、行番号から削除する行を特定する
valueが文字列 or 文字列の配列の時
行IDとみなして、それぞれの行を一つずつ消していく
code:script.js
export async function deleteLines(value, count = 1) {
if (typeof value === 'number') {
const from = value;
if (scrapbox.Page.lines.length === from + count) {
await goLine(from - 1);
press('ArrowRight', {shiftKey: true});
} else {
await goLine(from);
goHead();
}
for (let i = 0; i < count; i++) {
press('ArrowRight', {shiftKey: true});
press('End', {shiftKey: true});
}
press('ArrowRight', {shiftKey: true});
press('Delete');
return;
}
if (typeof value === 'string' || Array.isArray(value)) {
const ids = Array.isArray(value) ? value : value; for (const id of ids) {
await goLine(id);
press('Home', {shiftKey: true});
press('Home', {shiftKey: true});
press('Backspace');
press('Backspace');
}
return;
}
throw Error('The type of value must be number | string | string[]: ', value);
}
任意の行を書き換える
code:script.js
export async function replaceLine(lineNo, text) {
await goLine(lineNo);
press('Home', {shiftKey: true});
press('Home', {shiftKey: true});
await insertText(text);
}
インデントの上げ下げ
code:script.js
export function indentLines(count = 1) {
for (const _ of range(count)) {
press('ArrowRight', {ctrlKey: true});
}
}
export function deindentLines(count = 1) {
for (const _ of range(count)) {
press('ArrowLeft', {ctrlKey: true});
}
}
export function moveLines(count) {
if (count > 0) {
downLines(count);
} else {
upLines(-count);
}
}
// to行目の後ろに移動させる
export function moveLinesBefore({from: From, to}) {
const count = to - From;
if (count >= 0) {
downLines(count);
} else {
upLines(- count - 1);
}
}
export function upLines(count = 1) {
for (const _ of range(count)) {
press('ArrowUp', {ctrlKey: true});
}
}
export function downLines(count = 1) {
for (const _ of range(count)) {
press('ArrowDown', {ctrlKey: true});
}
}
export function indentBlocks(count = 1) {
for (const _ of range(count)) {
press('ArrowRight', {altKey: true});
}
}
export function deindentBlocks(count = 1) {
for (const _ of range(count)) {
press('ArrowLeft', {altKey: true});
}
}
export function moveBlocks(count) {
if (count > 0) {
downBlocks(count);
} else {
upBlocks(-count);
}
}
export function upBlocks(count = 1) {
for (const _ of range(count)) {
press('ArrowUp', {altKey: true});
}
}
export function downBlocks(count = 1) {
for (const _ of range(count)) {
press('ArrowDown', {altKey: true});
}
}