mirror of
				https://github.com/Alamantus/Lexiconga.git
				synced 2025-10-31 08:26:46 +01:00 
			
		
		
		
	Upload new, updated, and imported words/dictionaries
This commit is contained in:
		
							parent
							
								
									6a6b545979
								
							
						
					
					
						commit
						f753bd66f3
					
				
					 5 changed files with 55 additions and 28 deletions
				
			
		|  | @ -2,7 +2,7 @@ import '../../scss/Account/main.scss'; | ||||||
| 
 | 
 | ||||||
| import { renderLoginForm } from "./render"; | import { renderLoginForm } from "./render"; | ||||||
| import { triggerLoginChanges } from './login'; | import { triggerLoginChanges } from './login'; | ||||||
| import { syncDictionary, uploadWords } from './sync'; | import { syncDictionary, uploadWords, uploadDetails, uploadWholeDictionary } from './sync'; | ||||||
| 
 | 
 | ||||||
| export function showLoginForm() { | export function showLoginForm() { | ||||||
|   renderLoginForm(); |   renderLoginForm(); | ||||||
|  | @ -13,6 +13,18 @@ export function loginWithToken() { | ||||||
|   syncDictionary(); |   syncDictionary(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | export function syncImportedDictionary() { | ||||||
|  |   uploadWholeDictionary(true); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export function uploadDetailsDirect() { | ||||||
|  |   uploadDetails(); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| export function uploadWord(word) { | export function uploadWord(word) { | ||||||
|   uploadWords([word]); |   uploadWords([word]); | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | export function syncImportedWords(words) { | ||||||
|  |   uploadWords(words); | ||||||
|  | } | ||||||
|  | @ -1,6 +1,6 @@ | ||||||
| import { request, saveToken } from "./helpers"; | import { request, saveToken } from "./helpers"; | ||||||
| import { addMessage } from "../utilities"; | import { addMessage } from "../utilities"; | ||||||
| import { setupLogoutButton, setupEditFormButtonOverrides } from "./setupListeners"; | import { setupLogoutButton } from "./setupListeners"; | ||||||
| import { renderAccountSettings } from "./render"; | import { renderAccountSettings } from "./render"; | ||||||
| import { uploadWholeDictionary } from "./sync"; | import { uploadWholeDictionary } from "./sync"; | ||||||
| 
 | 
 | ||||||
|  | @ -119,7 +119,6 @@ export function triggerLoginChanges() { | ||||||
|   loginButton.parentElement.appendChild(logoutButton); |   loginButton.parentElement.appendChild(logoutButton); | ||||||
|   loginButton.parentElement.removeChild(loginButton); |   loginButton.parentElement.removeChild(loginButton); | ||||||
|   setupLogoutButton(logoutButton); |   setupLogoutButton(logoutButton); | ||||||
|   setupEditFormButtonOverrides(); |  | ||||||
| 
 | 
 | ||||||
|   renderAccountSettings(); |   renderAccountSettings(); | ||||||
| } | } | ||||||
|  | @ -1,6 +1,4 @@ | ||||||
| import { logIn, createAccount } from "./login"; | import { logIn, createAccount } from "./login"; | ||||||
| import { saveEditModal, saveAndCloseEditModal } from "../dictionaryManagement"; |  | ||||||
| import { saveEditModalAndSync, saveAndCloseEditModalAndSync } from "./dictionaryManagement"; |  | ||||||
| import { setCookie } from "../StackOverflow/cookie"; | import { setCookie } from "../StackOverflow/cookie"; | ||||||
| 
 | 
 | ||||||
| export function setupLoginModal(modal) { | export function setupLoginModal(modal) { | ||||||
|  | @ -21,18 +19,3 @@ export function setupLogoutButton(logoutButton) { | ||||||
|     window.location.reload(); |     window.location.reload(); | ||||||
|   }); |   }); | ||||||
| } | } | ||||||
| 
 |  | ||||||
| export function setupEditFormButtonOverrides() { |  | ||||||
|   document.getElementById('editSave').removeEventListener('click', saveEditModal); |  | ||||||
|   document.getElementById('editSave').addEventListener('click', saveEditModalAndSync); |  | ||||||
|   document.getElementById('editSaveAndClose').removeEventListener('click', saveAndCloseEditModal); |  | ||||||
|   document.getElementById('editSaveAndClose').addEventListener('click', saveAndCloseEditModalAndSync); |  | ||||||
| 
 |  | ||||||
|   // document.getElementById('importDictionaryFile').addEventListener('change', importDictionary);
 |  | ||||||
|   // document.getElementById('importWordsCSV').addEventListener('change', importWords);
 |  | ||||||
|   // document.getElementById('exportDictionaryButton').addEventListener('click', exportDictionary);
 |  | ||||||
|   // document.getElementById('exportWordsButton').addEventListener('click', exportWords);
 |  | ||||||
|   // document.getElementById('deleteDictionaryButton').addEventListener('click', confirmDeleteDictionary);
 |  | ||||||
| 
 |  | ||||||
|   // setupMaximizeButtons();
 |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -1,7 +1,7 @@ | ||||||
| import { renderDictionaryDetails, renderPartsOfSpeech, renderAll } from "./render"; | import { renderDictionaryDetails, renderPartsOfSpeech, renderAll } from "./render"; | ||||||
| import { removeTags, cloneObject, getTimestampInSeconds, download, slugify } from "../helpers"; | import { removeTags, cloneObject, getTimestampInSeconds, download, slugify } from "../helpers"; | ||||||
| import { LOCAL_STORAGE_KEY, DEFAULT_DICTIONARY, MIGRATE_VERSION } from "../constants"; | import { LOCAL_STORAGE_KEY, DEFAULT_DICTIONARY, MIGRATE_VERSION } from "../constants"; | ||||||
| import { addMessage, getNextId } from "./utilities"; | import { addMessage, getNextId, hasToken } from "./utilities"; | ||||||
| import { addWord } from "./wordManagement"; | import { addWord } from "./wordManagement"; | ||||||
| 
 | 
 | ||||||
| export function updateDictionary () { | export function updateDictionary () { | ||||||
|  | @ -68,6 +68,12 @@ export function saveEditModal() { | ||||||
|   saveDictionary(); |   saveDictionary(); | ||||||
|   renderDictionaryDetails(); |   renderDictionaryDetails(); | ||||||
|   renderPartsOfSpeech(); |   renderPartsOfSpeech(); | ||||||
|  | 
 | ||||||
|  |   if (hasToken()) { | ||||||
|  |     import('./account/index.js').then(account => { | ||||||
|  |       account.uploadDetailsDirect(); | ||||||
|  |     }) | ||||||
|  |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export function saveAndCloseEditModal() { | export function saveAndCloseEditModal() { | ||||||
|  | @ -131,6 +137,12 @@ export function importDictionary() { | ||||||
|           importDictionaryField.value = ''; |           importDictionaryField.value = ''; | ||||||
|           document.getElementById('editModal').style.display = 'none'; |           document.getElementById('editModal').style.display = 'none'; | ||||||
|           addMessage('Dictionary Imported Successfully'); |           addMessage('Dictionary Imported Successfully'); | ||||||
|  | 
 | ||||||
|  |           if (hasToken()) { | ||||||
|  |             import('./account/index.js').then(account => { | ||||||
|  |               account.syncImportedDictionary(); | ||||||
|  |             }); | ||||||
|  |           } | ||||||
|         } else { |         } else { | ||||||
|           addMessage('Dictionary could not be imported', 10000); |           addMessage('Dictionary could not be imported', 10000); | ||||||
|         } |         } | ||||||
|  | @ -148,7 +160,7 @@ export function importWords() { | ||||||
|     if (confirm('Importing a CSV file with words will add all of the words in the file to your dictionary regardless of duplication!\nDo you want to continue?')) { |     if (confirm('Importing a CSV file with words will add all of the words in the file to your dictionary regardless of duplication!\nDo you want to continue?')) { | ||||||
|       addMessage('Importing words...'); |       addMessage('Importing words...'); | ||||||
|       import('papaparse').then(papa => { |       import('papaparse').then(papa => { | ||||||
|         let wordsImported = 0; |         const importedWords = []; | ||||||
|         papa.parse(importWordsField.files[0], { |         papa.parse(importWordsField.files[0], { | ||||||
|           header: true, |           header: true, | ||||||
|           encoding: "utf-8", |           encoding: "utf-8", | ||||||
|  | @ -160,15 +172,16 @@ export function importWords() { | ||||||
|               }); |               }); | ||||||
|             } else { |             } else { | ||||||
|               const row = results.data[0]; |               const row = results.data[0]; | ||||||
|               addWord({ |               const importedWord = addWord({ | ||||||
|                 name: removeTags(row.word).trim(), |                 name: removeTags(row.word).trim(), | ||||||
|                 pronunciation: removeTags(row.pronunciation).trim(), |                 pronunciation: removeTags(row.pronunciation).trim(), | ||||||
|                 partOfSpeech: removeTags(row['part of speech']).trim(), |                 partOfSpeech: removeTags(row['part of speech']).trim(), | ||||||
|                 definition: removeTags(row.definition).trim(), |                 definition: removeTags(row.definition).trim(), | ||||||
|                 details: removeTags(row.explanation).trim(), |                 details: removeTags(row.explanation).trim(), | ||||||
|                 wordId: getNextId(), |                 wordId: getNextId(), | ||||||
|               }, false, false); |               }, false, false, false); | ||||||
|               wordsImported++; | 
 | ||||||
|  |               importedWords.push(importedWord); | ||||||
|             } |             } | ||||||
|           }, |           }, | ||||||
|           complete: () => { |           complete: () => { | ||||||
|  | @ -176,7 +189,13 @@ export function importWords() { | ||||||
|             renderAll(); |             renderAll(); | ||||||
|             importWordsField.value = ''; |             importWordsField.value = ''; | ||||||
|             document.getElementById('editModal').style.display = 'none'; |             document.getElementById('editModal').style.display = 'none'; | ||||||
|             addMessage(`Done Importing ${wordsImported} Words`); |             addMessage(`Done Importing ${importedWords.length} Words`); | ||||||
|  | 
 | ||||||
|  |             if (hasToken()) { | ||||||
|  |               import('./account/index.js').then(account => { | ||||||
|  |                 account.syncImportedWords(importedWords); | ||||||
|  |               }); | ||||||
|  |             } | ||||||
|           }, |           }, | ||||||
|           error: err => { |           error: err => { | ||||||
|             addMessage('Error Importing Words: ' + err); |             addMessage('Error Importing Words: ' + err); | ||||||
|  |  | ||||||
|  | @ -1,5 +1,5 @@ | ||||||
| import { renderWords } from "./render"; | import { renderWords } from "./render"; | ||||||
| import { wordExists, addMessage, getNextId } from "./utilities"; | import { wordExists, addMessage, getNextId, hasToken } from "./utilities"; | ||||||
| import removeDiacritics from "./StackOverflow/removeDiacritics"; | import removeDiacritics from "./StackOverflow/removeDiacritics"; | ||||||
| import { removeTags, getTimestampInSeconds } from "../helpers"; | import { removeTags, getTimestampInSeconds } from "../helpers"; | ||||||
| import { saveDictionary } from "./dictionaryManagement"; | import { saveDictionary } from "./dictionaryManagement"; | ||||||
|  | @ -78,7 +78,7 @@ export function clearWordForm() { | ||||||
|   document.getElementById('wordName').focus(); |   document.getElementById('wordName').focus(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export function addWord(word, render = true, message = true) { | export function addWord(word, render = true, message = true, upload = true) { | ||||||
|   const timestamp = getTimestampInSeconds(); |   const timestamp = getTimestampInSeconds(); | ||||||
|   word.lastUpdated = timestamp; |   word.lastUpdated = timestamp; | ||||||
|   word.createdOn = timestamp; |   word.createdOn = timestamp; | ||||||
|  | @ -87,6 +87,14 @@ export function addWord(word, render = true, message = true) { | ||||||
|     addMessage(`<a href="#${word.wordId}">${word.name}</a> Created Successfully`, 10000); |     addMessage(`<a href="#${word.wordId}">${word.name}</a> Created Successfully`, 10000); | ||||||
|   } |   } | ||||||
|   sortWords(render); |   sortWords(render); | ||||||
|  | 
 | ||||||
|  |   if (upload && hasToken()) { | ||||||
|  |     import('./account/index.js').then(account => { | ||||||
|  |       account.uploadWord(word); | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   return word; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export function deleteWord(wordId) { | export function deleteWord(wordId) { | ||||||
|  | @ -111,6 +119,12 @@ export function updateWord(word, wordId) { | ||||||
|     window.currentDictionary.words[wordIndex] = word; |     window.currentDictionary.words[wordIndex] = word; | ||||||
|     addMessage('Word Updated Successfully'); |     addMessage('Word Updated Successfully'); | ||||||
|     sortWords(true); |     sortWords(true); | ||||||
|  | 
 | ||||||
|  |     if (hasToken()) { | ||||||
|  |       import('./account/index.js').then(account => { | ||||||
|  |         account.uploadWord(word); | ||||||
|  |       }); | ||||||
|  |     } | ||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue