@kitn.ai/ui
Version:
Framework-agnostic, Shadow-DOM web components for building AI chat interfaces — works in React, Vue, Angular, Svelte, or plain HTML. Authored in SolidJS.
49 lines (48 loc) • 1.99 kB
TypeScript
import { HighlightRule } from './composer';
export interface HighlightMatch {
start: number;
end: number;
class: string | undefined;
}
/**
* Find all non-overlapping highlight ranges in `text` for the given rules.
*
* - A string rule is matched as a case-insensitive literal (regex metachars are escaped).
* - An object rule uses `new RegExp(pattern, flags ?? 'gi')`.
* - Overlapping ranges are resolved in earliest-start order; later ranges that
* overlap an already-claimed span are dropped.
* - Zero-width matches are skipped to avoid infinite loops.
* - Returns `[]` when nothing matches.
*/
export declare function findHighlightMatches(text: string, rules: HighlightRule[]): HighlightMatch[];
declare global {
interface CSSNamespace {
highlights?: HighlightRegistry;
}
interface HighlightRegistry {
set(name: string, highlight: CSSHighlight): void;
delete(name: string): boolean;
has(name: string): boolean;
}
class CSSHighlight {
constructor(...ranges: Range[]);
}
}
/**
* Feature-detect whether the CSS Custom Highlight API is available in the
* current environment. Returns false in jsdom (decoration no-op).
*/
export declare function supportsHighlightAPI(): boolean;
/**
* Apply highlight matches to a contenteditable element using the CSS Custom
* Highlight API. Maps text offsets (ZWSP-stripped concatenated text) to DOM
* Ranges spanning across text nodes, builds a `Highlight`, and registers it.
*
* If the API is unavailable (jsdom, older browsers), this is a strict no-op.
*
* @param root The contenteditable element.
* @param matches Pre-computed non-overlapping `{start, end}` ranges.
* @param zwsp The ZWSP character used in the editable (passed to avoid
* a circular import; callers pass ZWSP from composer-dom).
*/
export declare function applyHighlights(root: HTMLElement, matches: HighlightMatch[], zwsp: string, name?: string): void;