UNPKG

scriptable-testlab

Version:

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

173 lines (155 loc) 3.73 kB
import {AbsPasteboard} from 'scriptable-abstract'; /** * Interface representing the state of the pasteboard */ interface PasteboardState { text: string; items: Array<{ text?: string; image?: Image; url?: string; }>; } /** * Mock implementation of Scriptable's Pasteboard * * Note: This class extends AbsPasteboard which already provides: * - Singleton pattern implementation * - State management * - Basic property getters/setters * * Inherited methods from AbsPasteboard: * - copy(string) * - paste() * - copyString(string) * - pasteString() * - copyImage(image) * - pasteImage() */ export class MockPasteboard extends AbsPasteboard<PasteboardState> { /** * @inheritdoc * Inherited from ScriptableVariable, just adding type casting */ public static get instance(): MockPasteboard { return super.instance as MockPasteboard; } /** * @implements Scriptable.Pasteboard.copy * Copies text to the pasteboard */ public copy(text: string): void { this.setText(text); } /** * @implements Scriptable.Pasteboard.paste * Gets text from the pasteboard */ public paste(): string { return this.getText(); } /** * @implements Scriptable.Pasteboard.copyString * Copies string to the pasteboard */ public copyString(text: string): void { this.setText(text); } /** * @implements Scriptable.Pasteboard.pasteString * Gets string from the pasteboard */ public pasteString(): string { return this.getText(); } /** * @implements Scriptable.Pasteboard.copyImage * Copies image to the pasteboard */ public copyImage(image: Image): void { this.setItems([{image}]); } /** * @implements Scriptable.Pasteboard.pasteImage * Gets image from the pasteboard */ public pasteImage(): Image { const images = this.getImages(); if (images.length === 0) { throw new Error('No image on pasteboard'); } return images[0]; } /** * @additional * Additional helper method for getting text */ public getText(): string { return this.state.text; } /** * @additional * Additional helper method for setting text */ public setText(text: string): void { this.setState({ text, items: [{text}], }); } /** * @additional * Additional helper method for getting all items */ public getItems(): Array<{text?: string; image?: Image; url?: string}> { return [...this.state.items]; } /** * @additional * Additional helper method for setting items */ public setItems(items: Array<{text?: string; image?: Image; url?: string}>): void { this.setState({ text: items[0]?.text ?? this.state.text, items: [...items], }); } /** * @additional * Additional helper method for checking URLs */ public hasURLs(): boolean { return this.state.items.some(item => item.url !== undefined); } /** * @additional * Additional helper method for getting URLs */ public getURLs(): string[] { return this.state.items.map(item => item.url).filter((url): url is string => url !== undefined); } /** * @additional * Additional helper method for checking images */ public hasImages(): boolean { return this.state.items.some(item => item.image !== undefined); } /** * @additional * Additional helper method for getting images */ public getImages(): Image[] { return this.state.items.map(item => item.image).filter((image): image is Image => image !== undefined); } /** * @additional * Additional helper method for clearing pasteboard */ public clear(): void { this.setState({ text: '', items: [], }); } }