@@ -9,9 +9,7 @@ if (LIVE_RELOAD) {
99 . addEventListener ( "change" , ( ) => location . reload ( ) ) ;
1010}
1111import { dictionary } from "../dictionary/dictionary.ts" ;
12- import { flattenError } from "../misc/misc.ts" ;
1312import PROJECT_DATA from "../project_data.json" with { type : "json" } ;
14- import { ArrayResultError , isArrayResult } from "./array_result.ts" ;
1513import { loadCustomDictionary } from "./dictionary.ts" ;
1614import { checkLocalStorage , setIgnoreError } from "./local_storage.ts" ;
1715import { translate } from "./mod.ts" ;
@@ -46,19 +44,15 @@ custom dictionary comes with
4644limitations. Press Help above to get
4745started.` ;
4846
49- const DICTIONARY_LOADING_FAILED_FIXABLE_MESSAGE =
47+ const DICTIONARY_LOADING_FAILED_MESSAGE =
5048 "Failed to load custom dictionary. This is mostly likely because the " +
5149 "syntax has been updated and your custom dictionary still uses the old " +
5250 "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 =
5955 "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." ;
6256
6357// never change this
6458const DICTIONARY_KEY = "dictionary" ;
@@ -119,10 +113,34 @@ function main(): void {
119113 "save-button" ,
120114 ) as HTMLButtonElement ;
121115
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+
122128 const versionDisplay = document . getElementById (
123129 "version" ,
124130 ) as HTMLAnchorElement ;
125131
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+
126144 // set version
127145 const displayDate = PROJECT_DATA . onDevelopment
128146 ? "(on development)"
@@ -138,14 +156,8 @@ function main(): void {
138156 ? localStorage . getItem ( DICTIONARY_KEY ) ?? ""
139157 : customDictionaryTextBox . value ;
140158 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 ) ;
149161 }
150162 }
151163
@@ -180,14 +192,15 @@ function main(): void {
180192 outputList . innerHTML = "" ;
181193 errorList . innerHTML = "" ;
182194 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 ) {
185198 const list = document . createElement ( "li" ) ;
186199 list . innerHTML = translation ;
187200 outputList . appendChild ( list ) ;
188201 }
189- } catch ( error ) {
190- const errors = flattenError ( error ) ;
202+ } else {
203+ const errors = result . errors ;
191204 switch ( errors . length ) {
192205 case 0 :
193206 errorDisplay . innerText = UNKNOWN_ERROR_MESSAGE ;
@@ -200,15 +213,11 @@ function main(): void {
200213 break ;
201214 }
202215 for ( const item of errors ) {
203- const property = item instanceof ArrayResultError && item . isHtml
204- ? "innerHTML"
205- : "innerText" ;
216+ const property = item . isHtml ? "innerHTML" : "innerText" ;
206217 const list = document . createElement ( "li" ) ;
207- list [ property ] = extractErrorMessage ( item ) ;
218+ list [ property ] = item . message ;
208219 errorList . appendChild ( list ) ;
209220 }
210- // deno-lint-ignore no-console
211- console . error ( error ) ;
212221 }
213222 }
214223 settingsButton . addEventListener ( "click" , ( ) => {
@@ -249,39 +258,40 @@ function main(): void {
249258 }
250259 function importWord ( ) : void {
251260 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 {
253264 const definitions = dictionary . get ( word ) ?. source ;
254265 if ( definitions != null ) {
255266 displayToCustomDictionary ( `${ word } :${ definitions } ` ) ;
256267 } else {
257- displayToCustomDictionary ( asComment ( WORD_NOT_FOUND_MESSAGE ) ) ;
268+ showMessage ( WORD_NOT_FOUND_MESSAGE ) ;
258269 }
259- } else {
260- displayToCustomDictionary ( asComment ( INVALID_WORD_ERROR ) ) ;
261270 }
262271 }
263272 discardButton . addEventListener ( "click" , ( ) => {
264273 customDictionaryDialogBox . close ( ) ;
265274 } ) ;
266275 saveButton . addEventListener ( "click" , ( ) => {
267276 const { value } = customDictionaryTextBox ;
268- try {
269- loadCustomDictionary ( value ) ;
277+ const errors = loadCustomDictionary ( value ) ;
278+ if ( errors == null ) {
270279 setIgnoreError ( DICTIONARY_KEY , value ) ;
271280 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 {
277282 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+ ) ;
283287 }
284288 } ) ;
289+ closeButton . addEventListener ( "click" , ( ) => {
290+ alertBox . close ( ) ;
291+ } ) ;
292+ errorCloseButton . addEventListener ( "click" , ( ) => {
293+ errorBox . close ( ) ;
294+ } ) ;
285295 addEventListener ( "beforeunload" , ( event ) => {
286296 if ( customDictionaryDialogBox . open ) {
287297 event . preventDefault ( ) ;
@@ -301,13 +311,6 @@ const unused = [...new Array(localStorage.length).keys()]
301311for ( const key of unused ) {
302312 localStorage . removeItem ( key ) ;
303313}
304- function extractErrorMessage ( error : unknown ) : string {
305- if ( error instanceof Error ) {
306- return error . message ;
307- } else {
308- return `${ error } ` ;
309- }
310- }
311314export function asComment ( text : string ) : string {
312315 return text
313316 . replaceAll ( / ^ / mg, "# " )
0 commit comments