UNPKG

@mirawision/copily

Version:

A comprehensive clipboard manipulation library for TypeScript, providing functionalities for copying/pasting text, HTML, JSON, images, files, and smart content detection.

64 lines (63 loc) 2.55 kB
import { ClipboardSmartResult, CopyInterceptHandler, PasteInterceptHandler } from './types'; /** * Intelligently detect clipboard content and return structured results. * * Aggregates multiple clipboard reads (text, HTML, image, file) and classifies * the content into typed results like `otp`, `url`, `email`, `json`, `html`, * `text`, `image`, and `file`. * * @returns Resolves to an array of detected clipboard items. * @example * const items = await pasteSmart(); */ declare function pasteSmart(): Promise<ClipboardSmartResult[]>; /** * Intercept native `copy` events and optionally modify clipboard data. * * Use the handler return value to prevent the default or override text/HTML * placed on the clipboard. * * @param handler Function invoked on each `copy` event. * @returns Cleanup function to remove the listener. * @example * const off = interceptCopy(({ selection }) => ({ overrideText: selection.trim() })); */ declare function interceptCopy(handler: CopyInterceptHandler): () => void; /** * Intercept native `paste` events and optionally modify inserted content. * * The handler can prevent the default paste, or override with custom * plain text or HTML content. Text overrides are inserted into inputs, * textareas, and contenteditable elements; HTML overrides are inserted for * contenteditable contexts when possible. * * @param handler Function invoked on each `paste` event. * @returns Cleanup function to remove the listener. * @example * const off = interceptPaste(() => ({ overrideText: 'plain text only' })); */ declare function interceptPaste(handler: PasteInterceptHandler): () => void; /** * Listen to `paste` events and emit smart-detected clipboard data. * * Internally calls {@link pasteSmart} on paste and passes the results to the * provided callback. * * @param callback Receives the array of detected clipboard results. * @returns Cleanup function to remove the listener. */ declare function listenPaste(callback: (data: ClipboardSmartResult[]) => void): () => void; /** * Listen to `copy` events and emit selection details. * * Provides the raw event, the current text selection, and the event target. * * @param callback Receives `{ event, selection, target }` on each copy. * @returns Cleanup function to remove the listener. */ declare function listenCopy(callback: (params: { event: ClipboardEvent; selection: string; target: HTMLElement | null; }) => void): () => void; export { pasteSmart, interceptCopy, interceptPaste, listenPaste, listenCopy, };