UNPKG

scriptable-testlab

Version:

A lightweight, efficient tool designed to manage and update scripts for Scriptable.

275 lines (243 loc) 5.76 kB
import {AbsUITableCell} from 'scriptable-abstract'; import {MockImage} from '../../media/image'; import {MockColor} from '../color'; const DEFAULT_ON_TAP = () => {}; interface UITableCellState { title: string; subtitle: string | null; titleColor: Color | null; subtitleColor: Color | null; titleFont: Font | null; subtitleFont: Font | null; widthWeight: number; image: Image | null; dismissOnTap: boolean; onTap: () => void; alignment: 'left' | 'center' | 'right'; } /** * Mock implementation of Scriptable's UITableCell * Represents a cell in a UITableRow */ export class MockUITableCell extends AbsUITableCell<UITableCellState> { constructor() { super({ title: '', subtitle: null, titleColor: null, subtitleColor: null, titleFont: null, subtitleFont: null, widthWeight: 1, image: null, dismissOnTap: true, onTap: DEFAULT_ON_TAP, alignment: 'left', }); } /** * Title text of the cell */ get title(): string { return this.state.title; } /** * Sets the title text of the cell */ set title(value: string) { if (typeof value !== 'string') { throw new Error('Title must be a string'); } this.setState({title: value}); } /** * Subtitle text of the cell */ get subtitle(): string | null { return this.state.subtitle; } /** * Sets the subtitle text of the cell */ set subtitle(value: string | null) { if (value !== null && typeof value !== 'string') { throw new Error('Subtitle must be a string or null'); } this.setState({subtitle: value}); } /** * Color of the title text */ get titleColor(): Color { return this.state.titleColor as Color; } /** * Sets the color of the title text */ set titleColor(value: Color) { if (!value) { throw new Error('Title color cannot be null'); } this.setState({titleColor: value as MockColor}); } /** * Color of the subtitle text */ get subtitleColor(): Color { return this.state.subtitleColor as Color; } /** * Sets the color of the subtitle text */ set subtitleColor(value: Color) { if (!value) { throw new Error('Subtitle color cannot be null'); } this.setState({subtitleColor: value as MockColor}); } /** * Font of the title text */ get titleFont(): Font { return this.state.titleFont; } /** * Sets the font of the title text */ set titleFont(value: Font) { if (!value) { throw new Error('Title font cannot be null'); } this.setState({titleFont: value}); } /** * Font of the subtitle text */ get subtitleFont(): Font { return this.state.subtitleFont; } /** * Sets the font of the subtitle text */ set subtitleFont(value: Font) { if (!value) { throw new Error('Subtitle font cannot be null'); } this.setState({subtitleFont: value}); } /** * Width weight of the cell */ get widthWeight(): number { return this.state.widthWeight; } /** * Sets the width weight of the cell */ set widthWeight(value: number) { if (typeof value !== 'number' || value <= 0) { throw new Error('Width weight must be a positive number'); } this.setState({widthWeight: value}); } /** * Image of the cell */ get image(): Image { return this.state.image as Image; } /** * Sets the image of the cell */ set image(value: Image) { if (!value) { throw new Error('Image cannot be null'); } this.setState({image: value as MockImage}); } /** * Whether to dismiss the table when the cell is tapped */ get dismissOnTap(): boolean { return this.state.dismissOnTap; } /** * Sets whether to dismiss the table when the cell is tapped */ set dismissOnTap(value: boolean) { if (typeof value !== 'boolean') { throw new Error('DismissOnTap must be a boolean'); } this.setState({dismissOnTap: value}); } /** * Function to call when the cell is tapped */ get onTap(): () => void { return this.state.onTap; } /** * Sets the function to call when the cell is tapped */ set onTap(value: () => void) { if (typeof value !== 'function') { throw new Error('OnTap must be a function'); } this.setState({onTap: value}); } /** * Left aligns the content of the cell */ leftAligned(): void { this.setState({alignment: 'left'}); } /** * Right aligns the content of the cell */ rightAligned(): void { this.setState({alignment: 'right'}); } /** * Center aligns the content of the cell */ centerAligned(): void { this.setState({alignment: 'center'}); } static text(title?: string, subtitle?: string): MockUITableCell { const cell = new MockUITableCell(); if (title !== undefined) { cell.title = title; } if (subtitle !== undefined) { cell.subtitle = subtitle; } return cell; } static image(image: Image): MockUITableCell { if (!image) { throw new Error('Image cannot be null'); } const cell = new MockUITableCell(); cell.image = image; return cell; } static imageAtURL(url: string): MockUITableCell { if (!url) { throw new Error('URL cannot be empty'); } const cell = new MockUITableCell(); const image = MockImage.fromFile(url); cell.image = image; return cell; } static button(title: string): MockUITableCell { if (!title) { throw new Error('Button title cannot be empty'); } const cell = new MockUITableCell(); cell.title = title; // 按钮单元格默认居中对齐 cell.centerAligned(); return cell; } }