scriptable-testlab
Version:
A lightweight, efficient tool designed to manage and update scripts for Scriptable.
168 lines • 5.95 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var alert_exports = {};
__export(alert_exports, {
MockAlert: () => MockAlert
});
module.exports = __toCommonJS(alert_exports);
var import_scriptable_abstract = require("scriptable-abstract");
var import_system = require("../../system");
var import_text_field = require("./text-field");
const DEFAULT_STATE = {
title: "",
message: "",
actions: [],
textFields: [],
destructiveActionIndex: -1,
cancelActionIndex: -1,
presentedResult: 0
};
class MockAlert extends import_scriptable_abstract.AbsAlert {
constructor() {
super(DEFAULT_STATE);
}
get title() {
return this.state.title;
}
set title(value) {
if (value === null) throw new Error("Title cannot be null");
this.setState({ title: value });
}
get message() {
return this.state.message;
}
set message(value) {
if (value === null) throw new Error("Message cannot be null");
this.setState({ message: value });
}
get actions() {
return [...this.state.actions];
}
get textFields() {
return new Proxy([...this.state.textFields], {
get(target, prop) {
if (typeof prop === "string" && !isNaN(Number(prop))) {
const index = Number(prop);
if (index < 0 || index >= target.length) {
throw new Error("Invalid text field index");
}
}
return Reflect.get(target, prop);
}
});
}
get destructiveAction() {
return this.state.destructiveActionIndex >= 0 ? this.state.actions[this.state.destructiveActionIndex] : void 0;
}
get cancelAction() {
return this.state.cancelActionIndex >= 0 ? this.state.actions[this.state.cancelActionIndex] : void 0;
}
addAction(title) {
if (!title) throw new Error("Action text cannot be empty");
const actions = [...this.state.actions];
const insertIndex = Math.min(
this.state.cancelActionIndex >= 0 ? this.state.cancelActionIndex : actions.length,
this.state.destructiveActionIndex >= 0 ? this.state.destructiveActionIndex : actions.length
);
actions.splice(insertIndex, 0, title);
const newState = { actions };
if (this.state.cancelActionIndex >= insertIndex) {
newState.cancelActionIndex = this.state.cancelActionIndex + 1;
}
if (this.state.destructiveActionIndex >= insertIndex) {
newState.destructiveActionIndex = this.state.destructiveActionIndex + 1;
}
this.setState(newState);
}
addDestructiveAction(title) {
if (!title) throw new Error("Action text cannot be empty");
const actions = [...this.state.actions];
const insertIndex = this.state.cancelActionIndex >= 0 ? this.state.cancelActionIndex : actions.length;
actions.splice(insertIndex, 0, title);
const newState = {
actions,
destructiveActionIndex: insertIndex
};
if (this.state.cancelActionIndex >= insertIndex) {
newState.cancelActionIndex = this.state.cancelActionIndex + 1;
}
this.setState(newState);
}
addCancelAction(title) {
if (!title) throw new Error("Action text cannot be empty");
const actions = [...this.state.actions, title];
this.setState({
actions,
cancelActionIndex: actions.length - 1
});
}
addTextField(placeholder, text) {
const textField = new import_text_field.MockTextField();
if (placeholder) textField.placeholder = placeholder;
if (text) textField.text = text;
const textFields = [...this.state.textFields, textField];
this.setState({ textFields });
return textField;
}
addSecureTextField(placeholder, text) {
const textField = new import_text_field.MockTextField();
textField.isSecure = true;
if (placeholder) textField.placeholder = placeholder;
if (text) textField.text = text;
const textFields = [...this.state.textFields, textField];
this.setState({ textFields });
return textField;
}
textFieldValue(index) {
if (index < 0 || index >= this.state.textFields.length) return "";
return this.state.textFields[index]?.text ?? "";
}
setPresentedResult(result) {
this.setState({ presentedResult: result });
}
async presentAlert() {
const systemResponse = import_system.SystemState.getAlertResponse();
if (systemResponse !== void 0) return systemResponse;
if (this.state.presentedResult !== 0) return this.state.presentedResult;
if (this.state.actions.length === 0) return -1;
return this.state.presentedResult;
}
async presentSheet() {
return this.presentAlert();
}
async present() {
return this.presentAlert();
}
setState(newState) {
if (newState === null || newState === void 0) {
throw new Error("State cannot be null or undefined");
}
if ("title" in newState && (newState.title === null || newState.title === void 0)) {
throw new Error("Title cannot be null or undefined");
}
if ("message" in newState && (newState.message === null || newState.message === void 0)) {
throw new Error("Message cannot be null or undefined");
}
return super.setState(newState);
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
MockAlert
});
//# sourceMappingURL=alert.js.map