@@ -9,9 +9,7 @@ if (LIVE_RELOAD) {
9
9
. addEventListener ( "change" , ( ) => location . reload ( ) ) ;
10
10
}
11
11
import { dictionary } from "../dictionary/dictionary.ts" ;
12
- import { flattenError } from "../misc/misc.ts" ;
13
12
import PROJECT_DATA from "../project_data.json" with { type : "json" } ;
14
- import { ArrayResultError , isArrayResult } from "./array_result.ts" ;
15
13
import { loadCustomDictionary } from "./dictionary.ts" ;
16
14
import { checkLocalStorage , setIgnoreError } from "./local_storage.ts" ;
17
15
import { translate } from "./mod.ts" ;
@@ -46,19 +44,15 @@ custom dictionary comes with
46
44
limitations. Press Help above to get
47
45
started.` ;
48
46
49
- const DICTIONARY_LOADING_FAILED_FIXABLE_MESSAGE =
47
+ const DICTIONARY_LOADING_FAILED_MESSAGE =
50
48
"Failed to load custom dictionary. This is mostly likely because the " +
51
49
"syntax has been updated and your custom dictionary still uses the old " +
52
50
"syntax. Please fix it. Apologies for the inconvenience." ;
53
- const DICTIONARY_LOADING_FAILED_UNFIXABLE_MESSAGE =
54
- "Failed to load custom dictionary. Please report this." ;
55
- const WORD_NOT_FOUND_MESSAGE = "Error: Word not found." ;
56
- const INVALID_WORD_ERROR =
57
- "Error: Invalid word to add (You may remove this line)." ;
58
- const DICTIONARY_ERROR_FIXABLE_MESSAGE =
51
+ const NO_WORD_MESSAGE = "Please provide a word" ;
52
+ const WORD_NOT_FOUND_MESSAGE = "Word not found" ;
53
+
54
+ const DICTIONARY_ERROR_MESSAGE =
59
55
"Please fix these errors before saving.\n(You may remove these when fixed)" ;
60
- const DICTIONARY_ERROR_UNFIXABLE_MESSAGE =
61
- "Unable to save dictionary due to error. Please report this." ;
62
56
63
57
// never change this
64
58
const DICTIONARY_KEY = "dictionary" ;
@@ -119,10 +113,34 @@ function main(): void {
119
113
"save-button" ,
120
114
) as HTMLButtonElement ;
121
115
116
+ const alertBox = document . getElementById ( "alert-box" ) as HTMLDialogElement ;
117
+ const message = document . getElementById ( "message" ) as HTMLParagraphElement ;
118
+ const closeButton = document . getElementById (
119
+ "close-button" ,
120
+ ) as HTMLButtonElement ;
121
+
122
+ const errorBox = document . getElementById ( "error-box" ) as HTMLDialogElement ;
123
+ const errorCode = document . getElementById ( "error-code" ) as HTMLElement ;
124
+ const errorCloseButton = document . getElementById (
125
+ "error-close-button" ,
126
+ ) as HTMLButtonElement ;
127
+
122
128
const versionDisplay = document . getElementById (
123
129
"version" ,
124
130
) as HTMLAnchorElement ;
125
131
132
+ // emulates `window.alert`
133
+ function showMessage ( useMessage : string ) : void {
134
+ message . innerText = useMessage ;
135
+ alertBox . showModal ( ) ;
136
+ }
137
+
138
+ // handle error
139
+ addEventListener ( "error" , ( event ) => {
140
+ errorCode . innerText = event . message ;
141
+ errorBox . showModal ( ) ;
142
+ } ) ;
143
+
126
144
// set version
127
145
const displayDate = PROJECT_DATA . onDevelopment
128
146
? "(on development)"
@@ -138,14 +156,8 @@ function main(): void {
138
156
? localStorage . getItem ( DICTIONARY_KEY ) ?? ""
139
157
: customDictionaryTextBox . value ;
140
158
if ( customDictionary . trim ( ) !== "" ) {
141
- try {
142
- loadCustomDictionary ( customDictionary ) ;
143
- } catch ( error ) {
144
- errorDisplay . innerText = isArrayResult ( flattenError ( error ) )
145
- ? DICTIONARY_LOADING_FAILED_FIXABLE_MESSAGE
146
- : DICTIONARY_LOADING_FAILED_UNFIXABLE_MESSAGE ;
147
- // deno-lint-ignore no-console
148
- console . error ( error ) ;
159
+ if ( loadCustomDictionary ( customDictionary ) != null ) {
160
+ showMessage ( DICTIONARY_LOADING_FAILED_MESSAGE ) ;
149
161
}
150
162
}
151
163
@@ -180,14 +192,15 @@ function main(): void {
180
192
outputList . innerHTML = "" ;
181
193
errorList . innerHTML = "" ;
182
194
errorDisplay . innerText = "" ;
183
- try {
184
- for ( const translation of translate ( inputTextBox . value ) ) {
195
+ const result = translate ( inputTextBox . value ) ;
196
+ if ( ! result . isError ( ) ) {
197
+ for ( const translation of result . array ) {
185
198
const list = document . createElement ( "li" ) ;
186
199
list . innerHTML = translation ;
187
200
outputList . appendChild ( list ) ;
188
201
}
189
- } catch ( error ) {
190
- const errors = flattenError ( error ) ;
202
+ } else {
203
+ const errors = result . errors ;
191
204
switch ( errors . length ) {
192
205
case 0 :
193
206
errorDisplay . innerText = UNKNOWN_ERROR_MESSAGE ;
@@ -200,15 +213,11 @@ function main(): void {
200
213
break ;
201
214
}
202
215
for ( const item of errors ) {
203
- const property = item instanceof ArrayResultError && item . isHtml
204
- ? "innerHTML"
205
- : "innerText" ;
216
+ const property = item . isHtml ? "innerHTML" : "innerText" ;
206
217
const list = document . createElement ( "li" ) ;
207
- list [ property ] = extractErrorMessage ( item ) ;
218
+ list [ property ] = item . message ;
208
219
errorList . appendChild ( list ) ;
209
220
}
210
- // deno-lint-ignore no-console
211
- console . error ( error ) ;
212
221
}
213
222
}
214
223
settingsButton . addEventListener ( "click" , ( ) => {
@@ -249,39 +258,40 @@ function main(): void {
249
258
}
250
259
function importWord ( ) : void {
251
260
const word = importWordTextBox . value . trim ( ) ;
252
- if ( / ^ [ a - z ] [ a - z A - Z ] * $ / . test ( word ) ) {
261
+ if ( word === "" ) {
262
+ showMessage ( NO_WORD_MESSAGE ) ;
263
+ } else {
253
264
const definitions = dictionary . get ( word ) ?. source ;
254
265
if ( definitions != null ) {
255
266
displayToCustomDictionary ( `${ word } :${ definitions } ` ) ;
256
267
} else {
257
- displayToCustomDictionary ( asComment ( WORD_NOT_FOUND_MESSAGE ) ) ;
268
+ showMessage ( WORD_NOT_FOUND_MESSAGE ) ;
258
269
}
259
- } else {
260
- displayToCustomDictionary ( asComment ( INVALID_WORD_ERROR ) ) ;
261
270
}
262
271
}
263
272
discardButton . addEventListener ( "click" , ( ) => {
264
273
customDictionaryDialogBox . close ( ) ;
265
274
} ) ;
266
275
saveButton . addEventListener ( "click" , ( ) => {
267
276
const { value } = customDictionaryTextBox ;
268
- try {
269
- loadCustomDictionary ( value ) ;
277
+ const errors = loadCustomDictionary ( value ) ;
278
+ if ( errors == null ) {
270
279
setIgnoreError ( DICTIONARY_KEY , value ) ;
271
280
customDictionaryDialogBox . close ( ) ;
272
- } catch ( error ) {
273
- const errors = flattenError ( error ) ;
274
- const message = isArrayResult ( errors )
275
- ? DICTIONARY_ERROR_FIXABLE_MESSAGE
276
- : DICTIONARY_ERROR_UNFIXABLE_MESSAGE ;
281
+ } else {
277
282
const errorListMessage = errors
278
- . map ( extractErrorMessage )
279
- . map ( ( message ) => `\n- ${ message . replaceAll ( / \r ? \n / g, "$& " ) } ` ) ;
280
- displayToCustomDictionary ( asComment ( `${ message } ${ errorListMessage } ` ) ) ;
281
- // deno-lint-ignore no-console
282
- console . error ( error ) ;
283
+ . map ( ( error ) => `\n- ${ error . message . replaceAll ( / \r ? \n / g, "$& " ) } ` ) ;
284
+ displayToCustomDictionary (
285
+ asComment ( `${ DICTIONARY_ERROR_MESSAGE } ${ errorListMessage } ` ) ,
286
+ ) ;
283
287
}
284
288
} ) ;
289
+ closeButton . addEventListener ( "click" , ( ) => {
290
+ alertBox . close ( ) ;
291
+ } ) ;
292
+ errorCloseButton . addEventListener ( "click" , ( ) => {
293
+ errorBox . close ( ) ;
294
+ } ) ;
285
295
addEventListener ( "beforeunload" , ( event ) => {
286
296
if ( customDictionaryDialogBox . open ) {
287
297
event . preventDefault ( ) ;
@@ -301,13 +311,6 @@ const unused = [...new Array(localStorage.length).keys()]
301
311
for ( const key of unused ) {
302
312
localStorage . removeItem ( key ) ;
303
313
}
304
- function extractErrorMessage ( error : unknown ) : string {
305
- if ( error instanceof Error ) {
306
- return error . message ;
307
- } else {
308
- return `${ error } ` ;
309
- }
310
- }
311
314
export function asComment ( text : string ) : string {
312
315
return text
313
316
. replaceAll ( / ^ / mg, "# " )
0 commit comments