Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions browser/dom/caret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ interface ReactFiber {
* @return カーソルと選択範囲の情報
* @throws {Error} #text-inputとReact Componentの隠しpropertyが見つからなかった
*/
export function caret(): CaretInfo {
export const caret = (): CaretInfo => {
const textarea = textInput();
if (!textarea) {
throw Error(`#text-input is not found.`);
Expand All @@ -59,4 +59,4 @@ export function caret(): CaretInfo {
return (textarea[
reactKey
] as ReactFiber).return.return.stateNode.props;
}
};
12 changes: 6 additions & 6 deletions browser/dom/click.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export interface ClickOptions {
}

/** Emulate click event sequences */
export async function click(
export const click = async (
element: HTMLElement,
options: ClickOptions,
): Promise<void> {
): Promise<void> => {
const mouseOptions: MouseEventInit = {
button: options.button ?? 0,
clientX: options.X,
Expand All @@ -37,17 +37,17 @@ export async function click(
// ScrapboxのReactの処理が終わるまで少し待つ
// 待ち時間は感覚で決めた
await sleep(10);
}
};

export interface HoldDownOptions extends ClickOptions {
holding?: number;
}

/** Emulate long tap event sequence */
export async function holdDown(
export const holdDown = async (
element: HTMLElement,
options: HoldDownOptions,
): Promise<void> {
): Promise<void> => {
const touch = new Touch({
identifier: 0,
target: element,
Expand Down Expand Up @@ -79,4 +79,4 @@ export async function holdDown(
// ScrapboxのReactの処理が終わるまで少し待つ
// 待ち時間は感覚で決めた
await sleep(10);
}
};
86 changes: 48 additions & 38 deletions browser/dom/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,58 @@ import { textInput } from "./dom.ts";
import { isArray, isNumber, isString } from "../../is.ts";
import { sleep } from "../../sleep.ts";

export function undo(count = 1) {
export const undo = (count = 1): void => {
for (const _ of range(0, count)) {
press("z", { ctrlKey: true });
}
}
export function redo(count = 1) {
};
export const redo = (count = 1): void => {
for (const _ of range(0, count)) {
press("z", { shiftKey: true, ctrlKey: true });
}
}
};

export function insertIcon(count = 1) {
export const insertIcon = (count = 1): void => {
for (const _ of range(0, count)) {
press("i", { ctrlKey: true });
}
}
};

export function insertTimestamp(index = 1) {
export const insertTimestamp = (index = 1): void => {
for (const _ of range(0, index)) {
press("t", { altKey: true });
}
}
};

export async function insertLine(lineNo: number, text: string) {
export const insertLine = async (
lineNo: number,
text: string,
): Promise<void> => {
await goLine(lineNo);
goHead();
press("Enter");
press("ArrowUp");
await insertText(text);
}
};

export async function replaceLines(start: number, end: number, text: string) {
export const replaceLines = async (
start: number,
end: number,
text: string,
): Promise<void> => {
await goLine(start);
goHead();
for (const _ of range(start, end)) {
press("ArrowDown", { shiftKey: true });
}
press("End", { shiftKey: true });
await insertText(text);
}
};

export async function deleteLines(from: number | string | string[], count = 1) {
export const deleteLines = async (
from: number | string | string[],
count = 1,
): Promise<void> => {
if (isNumber(from)) {
if (getLineCount() === from + count) {
await goLine(from - 1);
Expand Down Expand Up @@ -78,74 +88,74 @@ export async function deleteLines(from: number | string | string[], count = 1) {
throw new TypeError(
`The type of value must be number | string | string[] but actual is "${typeof from}"`,
);
}
};

export function indentLines(count = 1) {
export const indentLines = (count = 1): void => {
for (const _ of range(0, count)) {
press("ArrowRight", { ctrlKey: true });
}
}
export function outdentLines(count = 1) {
};
export const outdentLines = (count = 1): void => {
for (const _ of range(0, count)) {
press("ArrowLeft", { ctrlKey: true });
}
}
export function moveLines(count: number) {
};
export const moveLines = (count: number): void => {
if (count > 0) {
downLines(count);
} else {
upLines(-count);
}
}
};
// to行目の後ろに移動させる
export function moveLinesBefore(from: number, to: number) {
export const moveLinesBefore = (from: number, to: number): void => {
const count = to - from;
if (count >= 0) {
downLines(count);
} else {
upLines(-count - 1);
}
}
export function upLines(count = 1) {
};
export const upLines = (count = 1): void => {
for (const _ of range(0, count)) {
press("ArrowUp", { ctrlKey: true });
}
}
export function downLines(count = 1) {
};
export const downLines = (count = 1): void => {
for (const _ of range(0, count)) {
press("ArrowDown", { ctrlKey: true });
}
}
};

export function indentBlocks(count = 1) {
export const indentBlocks = (count = 1): void => {
for (const _ of range(0, count)) {
press("ArrowRight", { altKey: true });
}
}
export function outdentBlocks(count = 1) {
};
export const outdentBlocks = (count = 1): void => {
for (const _ of range(0, count)) {
press("ArrowLeft", { altKey: true });
}
}
export function moveBlocks(count: number) {
};
export const moveBlocks = (count: number): void => {
if (count > 0) {
downBlocks(count);
} else {
upBlocks(-count);
}
}
export function upBlocks(count = 1) {
};
export const upBlocks = (count = 1): void => {
for (const _ of range(0, count)) {
press("ArrowUp", { altKey: true });
}
}
export function downBlocks(count = 1) {
};
export const downBlocks = (count = 1): void => {
for (const _ of range(0, count)) {
press("ArrowDown", { altKey: true });
}
}
};

export async function insertText(text: string) {
export const insertText = async (text: string): Promise<void> => {
const cursor = textInput();
if (!cursor) {
throw Error("#text-input is not ditected.");
Expand All @@ -156,4 +166,4 @@ export async function insertText(text: string) {
const event = new InputEvent("input", { bubbles: true });
cursor.dispatchEvent(event);
await sleep(1); // 待ち時間は感覚で決めた
}
};
4 changes: 2 additions & 2 deletions browser/dom/isHeightViewable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/// <reference lib="esnext"/>
/// <reference lib="dom" />

export function isHeightViewable(element: HTMLElement) {
export const isHeightViewable = (element: HTMLElement): boolean => {
const { top, bottom } = element.getBoundingClientRect();
return top >= 0 && bottom <= window.innerHeight;
}
};
Loading