UNPKG

@reaktly-js/tone-accessibility

Version:

A vanilla JS Accessability Widget compliant with ADA and WCAG 2.2.

63 lines (62 loc) 2.36 kB
/** * @class ContentManager * @description Scans the DOM, manages text nodes, and uses a MutationObserver * to handle dynamically added content for translation efficiently. */ export declare class ContentManager { private originalTextMap; private textNodes; private observer; constructor(); /** * Starts observing the document body for added nodes. */ observe(): void; /** * Stops the MutationObserver from watching for changes. */ disconnect(): void; /** * The callback executed when the observer detects DOM mutations. * It identifies new nodes and dispatches an event with only the new content. * @param mutations - An array of mutation records. */ private handleMutations; /** * Scans a given DOM node and its descendants for translatable text. * It stores the nodes and their text internally and returns the newly found nodes. * @param rootNode - The HTML element to start the scan from. * @returns An array of newly found text nodes. */ private scanNodeForText; /** * Gets the original, trimmed text for a specific set of new nodes. * @param newNodes - An array of newly discovered text nodes from a mutation. * @returns An array of clean text strings ready for translation. */ getNewTexts(newNodes: Node[]): string[]; /** * Replaces only the provided nodes with their new translated text using a map. * @param nodesToUpdate - The specific text nodes that need updating. * @param translations - A map where keys are original, trimmed strings and values are translations. */ applyTranslationsToNodes(nodesToUpdate: Node[], translations: { [originalText: string]: string; }): void; /** * Restores the page content to its original, untranslated state. */ revertToOriginals(): void; /** * Returns all text nodes currently being tracked. * @returns An array of text nodes. */ getAllTextNodes(): Node[]; /** * Returns the parent HTML elements for a given array of text nodes. * This is used to apply the flashing animation during translation. * @param nodes - The text nodes to get parent elements for. * @returns An array of unique HTMLElements. */ getElementsForNodes(nodes: Node[]): HTMLElement[]; }