Skip to content

Commit 780c9b1

Browse files
committed
improve auto parsing
1 parent a8ddb46 commit 780c9b1

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/main.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,10 @@ function main(): void {
299299
const word = importWordTextBox.value.trim();
300300
if (word === "") {
301301
showMessage(NO_WORD_MESSAGE);
302-
} else if (currentDictionary.isError()) {
302+
} else if (autoParse() && currentDictionary.isError()) {
303303
showMessage(DICTIONARY_ERROR_MESSAGE_ON_IMPORT);
304304
} else if (
305-
currentDictionary.unwrap()[0].has(word)
305+
autoParse() && currentDictionary.unwrap()[0].has(word)
306306
) {
307307
showMessage(WORD_ALREADY_IMPORTED_MESSAGE);
308308
} else {
@@ -316,36 +316,37 @@ function main(): void {
316316
0,
317317
customDictionaryTextBox.scrollHeight,
318318
);
319-
updateDictionary();
319+
updateIfCanAutoParse();
320320
} else {
321321
showMessage(WORD_NOT_FOUND_MESSAGE);
322322
}
323323
}
324324
}
325-
customDictionaryTextBox.addEventListener("input", () => {
326-
if (
327-
customDictionaryTextBox.value.length <= DICTIONARY_AUTO_PARSE_THRESHOLD
328-
) {
329-
updateDictionary();
330-
}
331-
});
325+
customDictionaryTextBox.addEventListener("input", updateIfCanAutoParse);
332326
discardButton.addEventListener("click", () => {
333327
customDictionaryTextBox.value = lastSavedText;
334328
currentDictionary = lastSavedDictionary;
335329
tryCloseDictionary();
336330
});
337331
saveButton.addEventListener("click", () => {
338-
if (
339-
customDictionaryTextBox.value.length > DICTIONARY_AUTO_PARSE_THRESHOLD
340-
) {
332+
if (!autoParse()) {
341333
updateDictionary();
342334
}
343335
tryCloseDictionary();
344336
});
337+
function autoParse(): boolean {
338+
return customDictionaryTextBox.value.length <=
339+
DICTIONARY_AUTO_PARSE_THRESHOLD;
340+
}
345341
function updateDictionary(): void {
346342
currentDictionary = dictionaryParser.parse(customDictionaryTextBox.value);
347343
showDictionaryError();
348344
}
345+
function updateIfCanAutoParse(): void {
346+
if (autoParse()) {
347+
updateDictionary();
348+
}
349+
}
349350
function tryCloseDictionary(): void {
350351
if (!currentDictionary.isError()) {
351352
lastSavedText = customDictionaryTextBox.value;

0 commit comments

Comments
 (0)