Skip to content

Commit 580751a

Browse files
author
HugoFara
committed
chore: removes LWT_DATA.
1 parent 9d1ce38 commit 580751a

21 files changed

+429
-719
lines changed

.eslintrc.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"semi": 0
1313
},
1414
"globals": {
15-
"$": true,
16-
"LWT_DATA": true
15+
"$": true
1716
}
1817
}

src/frontend/js/core/language_config.ts

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
/**
22
* Language Configuration Module - Provides type-safe access to language settings.
33
*
4-
* This module replaces direct LWT_DATA.language access with explicit functions.
4+
* This module provides explicit functions to access language configuration.
55
* Configuration is loaded once from DOM data attributes or initial config.
66
*
7-
* For backward compatibility, getter functions fall back to reading from
8-
* the legacy LWT_DATA global when this module hasn't been initialized.
9-
*
107
* @license Unlicense <http://unlicense.org/>
118
* @since 3.1.0
129
*/
1310

14-
// Import LWT_DATA type from globals
15-
import type { LwtData } from '../types/globals.d';
16-
1711
export interface LanguageConfig {
1812
id: number;
1913
name?: string;
@@ -40,26 +34,6 @@ const defaultConfig: LanguageConfig = {
4034
let currentConfig: LanguageConfig = { ...defaultConfig };
4135
let isInitialized = false;
4236

43-
/**
44-
* Get language config from legacy LWT_DATA for backward compatibility.
45-
*/
46-
function getFromLegacy(): LanguageConfig {
47-
const lwtData = typeof window !== 'undefined' ? (window as { LWT_DATA?: LwtData }).LWT_DATA : undefined;
48-
if (lwtData?.language) {
49-
return {
50-
id: lwtData.language.id || 0,
51-
dictLink1: lwtData.language.dict_link1 || '',
52-
dictLink2: lwtData.language.dict_link2 || '',
53-
translatorLink: lwtData.language.translator_link || '',
54-
delimiter: lwtData.language.delimiter || '',
55-
wordParsing: lwtData.language.word_parsing || '',
56-
rtl: lwtData.language.rtl || false,
57-
ttsVoiceApi: lwtData.language.ttsVoiceApi || ''
58-
};
59-
}
60-
return defaultConfig;
61-
}
62-
6337
/**
6438
* Initialize language configuration from a config object.
6539
*
@@ -105,65 +79,52 @@ export function initLanguageConfigFromDOM(): void {
10579
initLanguageConfig(config);
10680
}
10781

108-
/**
109-
* Get the effective config (module state or legacy fallback).
110-
*/
111-
function getEffectiveConfig(): LanguageConfig {
112-
return isInitialized ? currentConfig : getFromLegacy();
113-
}
11482

11583
/**
11684
* Get the current language configuration.
11785
* Returns a copy to prevent external mutation.
118-
* Falls back to LWT_DATA if not initialized.
11986
*/
12087
export function getLanguageConfig(): Readonly<LanguageConfig> {
121-
return { ...getEffectiveConfig() };
88+
return { ...currentConfig };
12289
}
12390

12491
/**
12592
* Get the language ID.
126-
* Falls back to LWT_DATA.language.id if not initialized.
12793
*/
12894
export function getLanguageId(): number {
129-
return getEffectiveConfig().id;
95+
return currentConfig.id;
13096
}
13197

13298
/**
13399
* Get dictionary links.
134-
* Falls back to LWT_DATA.language if not initialized.
135100
*/
136101
export function getDictionaryLinks(): { dict1: string; dict2: string; translator: string } {
137-
const config = getEffectiveConfig();
138102
return {
139-
dict1: config.dictLink1,
140-
dict2: config.dictLink2,
141-
translator: config.translatorLink
103+
dict1: currentConfig.dictLink1,
104+
dict2: currentConfig.dictLink2,
105+
translator: currentConfig.translatorLink
142106
};
143107
}
144108

145109
/**
146110
* Check if the language uses right-to-left script.
147-
* Falls back to LWT_DATA.language.rtl if not initialized.
148111
*/
149112
export function isRtl(): boolean {
150-
return getEffectiveConfig().rtl;
113+
return currentConfig.rtl;
151114
}
152115

153116
/**
154117
* Get the term translation delimiter.
155-
* Falls back to LWT_DATA.language.delimiter if not initialized.
156118
*/
157119
export function getDelimiter(): string {
158-
return getEffectiveConfig().delimiter;
120+
return currentConfig.delimiter;
159121
}
160122

161123
/**
162124
* Get the TTS voice API identifier.
163-
* Falls back to LWT_DATA.language.ttsVoiceApi if not initialized.
164125
*/
165126
export function getTtsVoiceApi(): string {
166-
return getEffectiveConfig().ttsVoiceApi;
127+
return currentConfig.ttsVoiceApi;
167128
}
168129

169130
/**

src/frontend/js/core/lwt_state.ts

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/**
2-
* LWT State Management - Core data structures and legacy globals
2+
* LWT State Management - Core data structures and state modules
33
*
4-
* This module provides the legacy LWT_DATA global object for backwards
5-
* compatibility. New code should use the focused state modules instead:
4+
* This module re-exports the focused state modules:
65
*
76
* - reading_state.ts - Reading position state
87
* - language_config.ts - Language configuration
@@ -16,7 +15,7 @@
1615
*/
1716

1817
// Import types from globals.d.ts to ensure consistency
19-
import type { LwtData, LwtLanguage, LwtText, LwtWord, LwtTest, LwtSettings } from '../types/globals.d';
18+
import type { LwtLanguage, LwtText, LwtWord, LwtTest, LwtSettings } from '../types/globals.d';
2019

2120
// Re-export new state modules for easier migration
2221
export * from './reading_state';
@@ -28,56 +27,3 @@ export * from './test_state';
2827
// Re-export types for backward compatibility
2928
export type { LwtLanguage, LwtText, LwtWord, LwtTest, LwtSettings };
3029

31-
// LwtDataInterface is now an alias to LwtData for consistency
32-
export type LwtDataInterface = LwtData;
33-
34-
/**
35-
* Legacy LWT_DATA global object.
36-
*
37-
* @deprecated Use the focused state modules instead:
38-
* - getReadingPosition() / setReadingPosition() from reading_state
39-
* - getLanguageConfig() / getLanguageId() from language_config
40-
* - getTextId() / getAnnotations() from text_config
41-
* - getHtsMode() / isApiModeEnabled() from settings_config
42-
* - getTestSolution() / isAnswerOpened() from test_state
43-
*/
44-
export const LWT_DATA: LwtDataInterface = {
45-
/** Language data */
46-
language: {
47-
id: 0,
48-
/** First dictionary URL */
49-
dict_link1: '',
50-
/** Second dictionary URL */
51-
dict_link2: '',
52-
/** Translator URL */
53-
translator_link: '',
54-
55-
delimiter: '',
56-
57-
/** Word parsing strategy, usually regular expression or 'mecab' */
58-
word_parsing: '',
59-
60-
rtl: false,
61-
/** Third-party voice API */
62-
ttsVoiceApi: ''
63-
},
64-
text: {
65-
id: 0,
66-
reading_position: -1,
67-
// annotations can be either a number or a Record
68-
annotations: 0
69-
},
70-
word: {
71-
id: 0
72-
},
73-
test: {
74-
solution: '',
75-
answer_opened: false
76-
},
77-
settings: {
78-
hts: 0,
79-
word_status_filter: ''
80-
}
81-
};
82-
83-

src/frontend/js/core/reading_state.ts

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,39 @@
11
/**
22
* Reading State Module - Manages mutable reading state.
33
*
4-
* This module replaces the LWT_DATA.text.reading_position and related
5-
* mutable state with explicit getter/setter functions.
6-
* For backward compatibility, getter functions fall back to reading from
7-
* the legacy LWT_DATA global when this module hasn't been explicitly set.
4+
* This module provides explicit getter/setter functions for reading position.
85
*
96
* @license Unlicense <http://unlicense.org/>
107
* @since 3.1.0
118
*/
129

13-
// Import LWT_DATA type from globals
14-
import type { LwtData } from '../types/globals.d';
15-
1610
/**
1711
* Current reading position in the text (word index).
1812
* -1 means no position is set.
1913
*/
2014
let readingPosition = -1;
21-
let isInitialized = false;
2215

2316
/**
2417
* Get the current reading position.
25-
* Falls back to LWT_DATA.text.reading_position if not set.
2618
*/
2719
export function getReadingPosition(): number {
28-
if (!isInitialized) {
29-
const lwtData = typeof window !== 'undefined' ? (window as { LWT_DATA?: LwtData }).LWT_DATA : undefined;
30-
if (lwtData?.text?.reading_position !== undefined) {
31-
return lwtData.text.reading_position;
32-
}
33-
}
3420
return readingPosition;
3521
}
3622

3723
/**
3824
* Set the current reading position.
39-
* Also updates LWT_DATA.text.reading_position for backward compatibility.
4025
*
4126
* @param position The new reading position (-1 to reset)
4227
*/
4328
export function setReadingPosition(position: number): void {
4429
readingPosition = position;
45-
isInitialized = true;
46-
47-
// Sync to legacy LWT_DATA for backward compatibility
48-
const lwtData = typeof window !== 'undefined' ? (window as { LWT_DATA?: LwtData }).LWT_DATA : undefined;
49-
if (lwtData?.text) {
50-
lwtData.text.reading_position = position;
51-
}
5230
}
5331

5432
/**
5533
* Reset the reading position to -1.
56-
* Also updates LWT_DATA.text.reading_position for backward compatibility.
5734
*/
5835
export function resetReadingPosition(): void {
5936
readingPosition = -1;
60-
isInitialized = true;
61-
62-
// Sync to legacy LWT_DATA for backward compatibility
63-
const lwtData = typeof window !== 'undefined' ? (window as { LWT_DATA?: LwtData }).LWT_DATA : undefined;
64-
if (lwtData?.text) {
65-
lwtData.text.reading_position = -1;
66-
}
6737
}
6838

6939
/**

0 commit comments

Comments
 (0)