app-walk
Version:
An intuitive guided walkthrough library with UI highlighting and voice narration for web apps.
64 lines (63 loc) • 2.08 kB
JavaScript
import { findElement } from "./findElement";
import { handleDropdownAction } from "./handleDropdownAction";
import { handleInputAction } from "./handleInputAction";
import { setHighlight } from "./highlight";
import { speakTextAsync } from "./speech";
import { wait } from "./utils";
import { isUiAction, isUrlAction } from "./dataTypes";
export async function handleUrlAction(step, navigate) {
if (!isUrlAction(step))
return;
if (step.beforeMessage)
await speakTextAsync(step.beforeMessage);
navigate(step.urlString);
if (step.afterMessage)
await speakTextAsync(step.afterMessage);
await wait(2000);
}
function handleClickAction(element, step) {
if (["click", "clickRowAction", "clickTab"].includes(step.actionType)) {
element.click();
}
}
export async function handleUiAction(step) {
if (!isUiAction(step) || step.selectors.length === 0)
return false;
console.log("runActionStep selectors:", step.selectors);
const element = findElement(step.selectors, document);
if (!element) {
console.log("Element not found:", step.selectors);
return false;
}
console.log("Element found:", element);
setHighlight(element);
if (step.beforeMessage)
await speakTextAsync(step.beforeMessage);
handleStepAction(element, step);
element.blur();
if (step.afterMessage)
await speakTextAsync(step.afterMessage);
return true;
}
function handleStepAction(element, step) {
switch (step.actionType) {
case "input":
handleInputAction(element, step);
break;
case "select":
case "dropdownClick":
case "dropdownSearchClick":
case "multiSelect":
handleDropdownAction(element, step);
break;
case "click":
case "clickRowAction":
case "clickTab":
handleClickAction(element, step);
break;
// Future extensibility:
// case "select":
// handleSelectAction(element, step);
// break;
}
}