@ingglish/dom 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +155 -0
- package/dist/index.d.ts +155 -0
- package/dist/index.js +601 -0
- package/dist/index.mjs +572 -0
- package/package.json +67 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { OutputFormat, TranslatedToken } from '@ingglish/phonemes';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Apply pre-computed translations to DOM.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Options for applying pre-computed translations.
|
|
8
|
+
*/
|
|
9
|
+
interface ApplyTranslationsOptions {
|
|
10
|
+
/** Number of text nodes to process per animation frame */
|
|
11
|
+
chunkSize?: number;
|
|
12
|
+
/** Callback for progress updates */
|
|
13
|
+
onProgress?: (processed: number, total: number) => void;
|
|
14
|
+
/** Whether to show tooltips with original text on hover */
|
|
15
|
+
showTooltips?: boolean;
|
|
16
|
+
/** Pre-collected text nodes (avoids re-traversing DOM) */
|
|
17
|
+
textNodes?: Text[];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Applies pre-computed translations to a DOM tree.
|
|
21
|
+
* This is designed for use cases where translations are fetched externally
|
|
22
|
+
* (e.g., from a service worker via message passing).
|
|
23
|
+
*
|
|
24
|
+
* @param root The root element to translate
|
|
25
|
+
* @param translations Map of lowercase words to their translations
|
|
26
|
+
* @param options Configuration options
|
|
27
|
+
*/
|
|
28
|
+
declare function applyTranslationsMap(root: Document | Element, translations: Record<string, string>, options?: ApplyTranslationsOptions): Promise<void>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* DOM restoration utilities.
|
|
32
|
+
*/
|
|
33
|
+
/**
|
|
34
|
+
* Restores original text content that was translated.
|
|
35
|
+
* Replaces tooltip spans with their original text to preserve DOM structure.
|
|
36
|
+
*
|
|
37
|
+
* @param root The root element to restore
|
|
38
|
+
*/
|
|
39
|
+
declare function restoreDOM(root: Document | Element): void;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Shared types for @ingglish/dom
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Configuration options for DOM translation.
|
|
47
|
+
*/
|
|
48
|
+
interface DOMTranslatorOptions {
|
|
49
|
+
/**
|
|
50
|
+
* Enable chunked DOM updates using requestAnimationFrame for smooth rendering.
|
|
51
|
+
* Prevents UI freezes on large pages.
|
|
52
|
+
* @default false
|
|
53
|
+
*/
|
|
54
|
+
chunked?: boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Number of text nodes to process per animation frame when chunked is enabled.
|
|
57
|
+
* @default 100
|
|
58
|
+
*/
|
|
59
|
+
chunkSize?: number;
|
|
60
|
+
/**
|
|
61
|
+
* Callback for progress updates during translation.
|
|
62
|
+
*/
|
|
63
|
+
onProgress?: (processed: number, total: number) => void;
|
|
64
|
+
/**
|
|
65
|
+
* Output format for translations.
|
|
66
|
+
* @default 'ingglish'
|
|
67
|
+
*/
|
|
68
|
+
outputFormat?: OutputFormat;
|
|
69
|
+
/**
|
|
70
|
+
* Whether to show tooltips with original English words on hover.
|
|
71
|
+
* When enabled, translated words are wrapped in spans with data-original attributes.
|
|
72
|
+
* @default false
|
|
73
|
+
*/
|
|
74
|
+
showTooltips?: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* CSS class names to skip during translation.
|
|
77
|
+
* @default ['no-translate', 'notranslate']
|
|
78
|
+
*/
|
|
79
|
+
skipClasses?: string[];
|
|
80
|
+
/**
|
|
81
|
+
* HTML tag names to skip during translation.
|
|
82
|
+
* @default ['SCRIPT', 'STYLE', 'CODE', 'PRE', 'KBD', 'SAMP', 'VAR', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SVG', 'MATH', 'CANVAS']
|
|
83
|
+
*/
|
|
84
|
+
skipTags?: string[];
|
|
85
|
+
/**
|
|
86
|
+
* Whether to translate attributes like title, alt, placeholder, aria-label.
|
|
87
|
+
* @default true
|
|
88
|
+
*/
|
|
89
|
+
translateAttributes?: boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Custom token-mapping translation function. When provided, used instead of
|
|
92
|
+
* the built-in English→Ingglish translateSyncWithMapping(). Returns structured
|
|
93
|
+
* tokens with original/translated text and match status. Enables foreign
|
|
94
|
+
* language translation with full tooltip support.
|
|
95
|
+
*/
|
|
96
|
+
translateWithMappingFn?: (text: string, format: OutputFormat) => TranslatedToken[];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Core DOM translation functionality.
|
|
101
|
+
*/
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Translates all text content within a DOM element (async, auto-loads dictionary).
|
|
105
|
+
* This is the recommended entry point for DOM translation.
|
|
106
|
+
*/
|
|
107
|
+
declare function translateDOM(root: Document | Element, options?: DOMTranslatorOptions): Promise<void>;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Extracts all unique words from an array of text nodes.
|
|
111
|
+
* Uses a single Set to collect and deduplicate in one pass.
|
|
112
|
+
*
|
|
113
|
+
* @param textNodes - Array of DOM text nodes to extract words from
|
|
114
|
+
* @returns Array of unique lowercase words across all nodes
|
|
115
|
+
*/
|
|
116
|
+
declare function extractWordsFromNodes(textNodes: Text[]): string[];
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Skip rules for DOM translation.
|
|
120
|
+
* Determines which elements and text nodes should be skipped.
|
|
121
|
+
*/
|
|
122
|
+
/**
|
|
123
|
+
* Default tags to skip during translation.
|
|
124
|
+
* These typically contain code, scripts, or non-translatable content.
|
|
125
|
+
*/
|
|
126
|
+
declare const DEFAULT_SKIP_TAGS: string[];
|
|
127
|
+
/**
|
|
128
|
+
* Default CSS classes to skip during translation.
|
|
129
|
+
* Common conventions for marking content as non-translatable.
|
|
130
|
+
*/
|
|
131
|
+
declare const DEFAULT_SKIP_CLASSES: string[];
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Text node collection utilities.
|
|
135
|
+
*/
|
|
136
|
+
/**
|
|
137
|
+
* Collects all translatable text nodes from a DOM tree.
|
|
138
|
+
* Skips elements based on tags, classes, and data attributes.
|
|
139
|
+
*
|
|
140
|
+
* Optimized to use FILTER_REJECT on skip elements, which skips entire subtrees
|
|
141
|
+
* instead of checking each text node's parent chain individually.
|
|
142
|
+
*/
|
|
143
|
+
declare function collectTextNodes(root: Document | Element, skipTags?: string[], skipClasses?: string[]): Text[];
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Injects tooltip behavior: creates tooltip divs on hover,
|
|
147
|
+
* positioned with position:fixed to escape overflow:hidden ancestors.
|
|
148
|
+
*/
|
|
149
|
+
declare function injectTooltipBehavior(targetDoc?: Document): void;
|
|
150
|
+
/**
|
|
151
|
+
* Injects tooltip styles into the document if not already present.
|
|
152
|
+
*/
|
|
153
|
+
declare function injectTooltipStyles(targetDoc?: Document): void;
|
|
154
|
+
|
|
155
|
+
export { DEFAULT_SKIP_CLASSES, DEFAULT_SKIP_TAGS, applyTranslationsMap, collectTextNodes, extractWordsFromNodes, injectTooltipBehavior, injectTooltipStyles, restoreDOM, translateDOM };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { OutputFormat, TranslatedToken } from '@ingglish/phonemes';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Apply pre-computed translations to DOM.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Options for applying pre-computed translations.
|
|
8
|
+
*/
|
|
9
|
+
interface ApplyTranslationsOptions {
|
|
10
|
+
/** Number of text nodes to process per animation frame */
|
|
11
|
+
chunkSize?: number;
|
|
12
|
+
/** Callback for progress updates */
|
|
13
|
+
onProgress?: (processed: number, total: number) => void;
|
|
14
|
+
/** Whether to show tooltips with original text on hover */
|
|
15
|
+
showTooltips?: boolean;
|
|
16
|
+
/** Pre-collected text nodes (avoids re-traversing DOM) */
|
|
17
|
+
textNodes?: Text[];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Applies pre-computed translations to a DOM tree.
|
|
21
|
+
* This is designed for use cases where translations are fetched externally
|
|
22
|
+
* (e.g., from a service worker via message passing).
|
|
23
|
+
*
|
|
24
|
+
* @param root The root element to translate
|
|
25
|
+
* @param translations Map of lowercase words to their translations
|
|
26
|
+
* @param options Configuration options
|
|
27
|
+
*/
|
|
28
|
+
declare function applyTranslationsMap(root: Document | Element, translations: Record<string, string>, options?: ApplyTranslationsOptions): Promise<void>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* DOM restoration utilities.
|
|
32
|
+
*/
|
|
33
|
+
/**
|
|
34
|
+
* Restores original text content that was translated.
|
|
35
|
+
* Replaces tooltip spans with their original text to preserve DOM structure.
|
|
36
|
+
*
|
|
37
|
+
* @param root The root element to restore
|
|
38
|
+
*/
|
|
39
|
+
declare function restoreDOM(root: Document | Element): void;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Shared types for @ingglish/dom
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Configuration options for DOM translation.
|
|
47
|
+
*/
|
|
48
|
+
interface DOMTranslatorOptions {
|
|
49
|
+
/**
|
|
50
|
+
* Enable chunked DOM updates using requestAnimationFrame for smooth rendering.
|
|
51
|
+
* Prevents UI freezes on large pages.
|
|
52
|
+
* @default false
|
|
53
|
+
*/
|
|
54
|
+
chunked?: boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Number of text nodes to process per animation frame when chunked is enabled.
|
|
57
|
+
* @default 100
|
|
58
|
+
*/
|
|
59
|
+
chunkSize?: number;
|
|
60
|
+
/**
|
|
61
|
+
* Callback for progress updates during translation.
|
|
62
|
+
*/
|
|
63
|
+
onProgress?: (processed: number, total: number) => void;
|
|
64
|
+
/**
|
|
65
|
+
* Output format for translations.
|
|
66
|
+
* @default 'ingglish'
|
|
67
|
+
*/
|
|
68
|
+
outputFormat?: OutputFormat;
|
|
69
|
+
/**
|
|
70
|
+
* Whether to show tooltips with original English words on hover.
|
|
71
|
+
* When enabled, translated words are wrapped in spans with data-original attributes.
|
|
72
|
+
* @default false
|
|
73
|
+
*/
|
|
74
|
+
showTooltips?: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* CSS class names to skip during translation.
|
|
77
|
+
* @default ['no-translate', 'notranslate']
|
|
78
|
+
*/
|
|
79
|
+
skipClasses?: string[];
|
|
80
|
+
/**
|
|
81
|
+
* HTML tag names to skip during translation.
|
|
82
|
+
* @default ['SCRIPT', 'STYLE', 'CODE', 'PRE', 'KBD', 'SAMP', 'VAR', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SVG', 'MATH', 'CANVAS']
|
|
83
|
+
*/
|
|
84
|
+
skipTags?: string[];
|
|
85
|
+
/**
|
|
86
|
+
* Whether to translate attributes like title, alt, placeholder, aria-label.
|
|
87
|
+
* @default true
|
|
88
|
+
*/
|
|
89
|
+
translateAttributes?: boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Custom token-mapping translation function. When provided, used instead of
|
|
92
|
+
* the built-in English→Ingglish translateSyncWithMapping(). Returns structured
|
|
93
|
+
* tokens with original/translated text and match status. Enables foreign
|
|
94
|
+
* language translation with full tooltip support.
|
|
95
|
+
*/
|
|
96
|
+
translateWithMappingFn?: (text: string, format: OutputFormat) => TranslatedToken[];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Core DOM translation functionality.
|
|
101
|
+
*/
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Translates all text content within a DOM element (async, auto-loads dictionary).
|
|
105
|
+
* This is the recommended entry point for DOM translation.
|
|
106
|
+
*/
|
|
107
|
+
declare function translateDOM(root: Document | Element, options?: DOMTranslatorOptions): Promise<void>;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Extracts all unique words from an array of text nodes.
|
|
111
|
+
* Uses a single Set to collect and deduplicate in one pass.
|
|
112
|
+
*
|
|
113
|
+
* @param textNodes - Array of DOM text nodes to extract words from
|
|
114
|
+
* @returns Array of unique lowercase words across all nodes
|
|
115
|
+
*/
|
|
116
|
+
declare function extractWordsFromNodes(textNodes: Text[]): string[];
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Skip rules for DOM translation.
|
|
120
|
+
* Determines which elements and text nodes should be skipped.
|
|
121
|
+
*/
|
|
122
|
+
/**
|
|
123
|
+
* Default tags to skip during translation.
|
|
124
|
+
* These typically contain code, scripts, or non-translatable content.
|
|
125
|
+
*/
|
|
126
|
+
declare const DEFAULT_SKIP_TAGS: string[];
|
|
127
|
+
/**
|
|
128
|
+
* Default CSS classes to skip during translation.
|
|
129
|
+
* Common conventions for marking content as non-translatable.
|
|
130
|
+
*/
|
|
131
|
+
declare const DEFAULT_SKIP_CLASSES: string[];
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Text node collection utilities.
|
|
135
|
+
*/
|
|
136
|
+
/**
|
|
137
|
+
* Collects all translatable text nodes from a DOM tree.
|
|
138
|
+
* Skips elements based on tags, classes, and data attributes.
|
|
139
|
+
*
|
|
140
|
+
* Optimized to use FILTER_REJECT on skip elements, which skips entire subtrees
|
|
141
|
+
* instead of checking each text node's parent chain individually.
|
|
142
|
+
*/
|
|
143
|
+
declare function collectTextNodes(root: Document | Element, skipTags?: string[], skipClasses?: string[]): Text[];
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Injects tooltip behavior: creates tooltip divs on hover,
|
|
147
|
+
* positioned with position:fixed to escape overflow:hidden ancestors.
|
|
148
|
+
*/
|
|
149
|
+
declare function injectTooltipBehavior(targetDoc?: Document): void;
|
|
150
|
+
/**
|
|
151
|
+
* Injects tooltip styles into the document if not already present.
|
|
152
|
+
*/
|
|
153
|
+
declare function injectTooltipStyles(targetDoc?: Document): void;
|
|
154
|
+
|
|
155
|
+
export { DEFAULT_SKIP_CLASSES, DEFAULT_SKIP_TAGS, applyTranslationsMap, collectTextNodes, extractWordsFromNodes, injectTooltipBehavior, injectTooltipStyles, restoreDOM, translateDOM };
|