app-walkthrough
Version:
An intuitive guided walkthrough library with UI highlighting and voice narration for web apps.
58 lines (57 loc) • 1.85 kB
JavaScript
import { popupEvent } from "./utils";
import { handleAntDropdownSearch } from "./handleAntdAction";
// 可移至 constants 文件以便未来在多个 handler 中复用
export const DROPDOWN_VALID_ACTION_TYPES = new Set([
"select",
"dropdownClick",
"dropdownSearchClick",
"multiSelect",
]);
export function handleDropdownAction(element, step) {
if (!DROPDOWN_VALID_ACTION_TYPES.has(step.actionType) || !("input" in step)) {
return;
}
console.log("Dropdown element found:", element);
const input = extractInputValue(step).toString();
if (element instanceof HTMLSelectElement) {
handleNativeSelect(element, input);
return;
}
if (step.actionType === "dropdownClick") {
handleAntDropdownSearch(element, input);
return;
}
console.log("handleGenericDropdown");
handleGenericDropdown(element, input);
}
function extractInputValue(step) {
if (typeof step.input === "string") {
return step.input;
}
if (Array.isArray(step.input) && step.input.length > 0) {
return step.input[0];
}
return "";
}
function handleNativeSelect(element, input) {
for (const option of element.options) {
if (option.value === input || option.text === input) {
option.selected = true;
popupEvent(element, "change");
break;
}
}
}
function handleGenericDropdown(element, input) {
element.click();
const inputElement = element.querySelector("input");
const finalElement = inputElement !== null && inputElement !== void 0 ? inputElement : element;
if (finalElement instanceof HTMLInputElement ||
finalElement instanceof HTMLTextAreaElement) {
finalElement.value = input;
}
else {
finalElement.innerText = input;
}
popupEvent(finalElement);
}