Save and load from localStorage; fix csv arrays in edit modal
This commit is contained in:
parent
29488f2ed5
commit
22f8c0885a
|
@ -51,3 +51,5 @@ export const DEFAULT_DICTIONARY = {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const DEFAULT_PAGE_SIZE = 50;
|
export const DEFAULT_PAGE_SIZE = 50;
|
||||||
|
|
||||||
|
export const LOCAL_STORAGE_KEY = 'dictionary';
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
import './main.scss';
|
import './main.scss';
|
||||||
|
|
||||||
import { DEFAULT_DICTIONARY } from './constants';
|
|
||||||
import setupListeners from './js/setupListeners';
|
import setupListeners from './js/setupListeners';
|
||||||
import { renderAll } from './js/render';
|
import { renderAll } from './js/render';
|
||||||
import { cloneObject } from './helpers';
|
|
||||||
import { generateRandomWords } from './js/utilities';
|
import { generateRandomWords } from './js/utilities';
|
||||||
|
import { loadDictionary } from './js/dictionaryManagement';
|
||||||
|
|
||||||
function initialize() {
|
function initialize() {
|
||||||
console.log('initializing');
|
console.log('initializing');
|
||||||
window.currentDictionary = cloneObject(DEFAULT_DICTIONARY);
|
loadDictionary();
|
||||||
// generateRandomWords(100);
|
// generateRandomWords(100);
|
||||||
setupListeners();
|
setupListeners();
|
||||||
renderAll();
|
renderAll();
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { renderDictionaryDetails, renderPartsOfSpeech } from "./render";
|
import { renderDictionaryDetails, renderPartsOfSpeech } from "./render";
|
||||||
import { removeTags } from "../helpers";
|
import { removeTags, cloneObject } from "../helpers";
|
||||||
|
import { LOCAL_STORAGE_KEY, DEFAULT_DICTIONARY } from "../constants";
|
||||||
|
|
||||||
export function updateDictionary () {
|
export function updateDictionary () {
|
||||||
|
|
||||||
|
@ -38,18 +39,18 @@ export function openEditModal() {
|
||||||
document.getElementById('editModal').style.display = '';
|
document.getElementById('editModal').style.display = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
export function save() {
|
export function saveEditModal() {
|
||||||
window.currentDictionary.name = removeTags(document.getElementById('editName').value.trim());
|
window.currentDictionary.name = removeTags(document.getElementById('editName').value.trim());
|
||||||
window.currentDictionary.specification = removeTags(document.getElementById('editSpecification').value.trim());
|
window.currentDictionary.specification = removeTags(document.getElementById('editSpecification').value.trim());
|
||||||
window.currentDictionary.description = removeTags(document.getElementById('editDescription').value.trim());
|
window.currentDictionary.description = removeTags(document.getElementById('editDescription').value.trim());
|
||||||
window.currentDictionary.partsOfSpeech = document.getElementById('editPartsOfSpeech').value.split(',').map(val => val.trim());
|
window.currentDictionary.partsOfSpeech = document.getElementById('editPartsOfSpeech').value.split(',').map(val => val.trim()).filter(val => val !== '');
|
||||||
|
|
||||||
window.currentDictionary.details.phonology.consonants = document.getElementById('editConsonants').value.split(',').map(val => val.trim());
|
window.currentDictionary.details.phonology.consonants = document.getElementById('editConsonants').value.split(',').map(val => val.trim()).filter(val => val !== '');
|
||||||
window.currentDictionary.details.phonology.vowels = document.getElementById('editVowels').value.split(',').map(val => val.trim());
|
window.currentDictionary.details.phonology.vowels = document.getElementById('editVowels').value.split(',').map(val => val.trim()).filter(val => val !== '');
|
||||||
window.currentDictionary.details.phonology.blends = document.getElementById('editBlends').value.split(',').map(val => val.trim());
|
window.currentDictionary.details.phonology.blends = document.getElementById('editBlends').value.split(',').map(val => val.trim()).filter(val => val !== '');
|
||||||
window.currentDictionary.details.phonology.phonotactics.onset = document.getElementById('editOnset').value.split(',').map(val => val.trim());
|
window.currentDictionary.details.phonology.phonotactics.onset = document.getElementById('editOnset').value.split(',').map(val => val.trim()).filter(val => val !== '');
|
||||||
window.currentDictionary.details.phonology.phonotactics.nucleus = document.getElementById('editNucleus').value.split(',').map(val => val.trim());
|
window.currentDictionary.details.phonology.phonotactics.nucleus = document.getElementById('editNucleus').value.split(',').map(val => val.trim()).filter(val => val !== '');
|
||||||
window.currentDictionary.details.phonology.phonotactics.coda = document.getElementById('editCoda').value.split(',').map(val => val.trim());
|
window.currentDictionary.details.phonology.phonotactics.coda = document.getElementById('editCoda').value.split(',').map(val => val.trim()).filter(val => val !== '');
|
||||||
window.currentDictionary.details.phonology.phonotactics.exceptions = removeTags(document.getElementById('editExceptions').value.trim());
|
window.currentDictionary.details.phonology.phonotactics.exceptions = removeTags(document.getElementById('editExceptions').value.trim());
|
||||||
|
|
||||||
window.currentDictionary.details.orthography.notes = removeTags(document.getElementById('editOrthography').value.trim());
|
window.currentDictionary.details.orthography.notes = removeTags(document.getElementById('editOrthography').value.trim());
|
||||||
|
@ -61,15 +62,29 @@ export function save() {
|
||||||
window.currentDictionary.settings.isComplete = document.getElementById('editIsComplete').checked;
|
window.currentDictionary.settings.isComplete = document.getElementById('editIsComplete').checked;
|
||||||
window.currentDictionary.settings.isPublic = document.getElementById('editIsPublic').checked;
|
window.currentDictionary.settings.isPublic = document.getElementById('editIsPublic').checked;
|
||||||
|
|
||||||
|
saveDictionary();
|
||||||
renderDictionaryDetails();
|
renderDictionaryDetails();
|
||||||
renderPartsOfSpeech();
|
renderPartsOfSpeech();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function saveAndClose() {
|
export function saveAndCloseEditModal() {
|
||||||
save();
|
saveEditModal();
|
||||||
document.getElementById('editModal').style.display = 'none';
|
document.getElementById('editModal').style.display = 'none';
|
||||||
}
|
}
|
||||||
|
|
||||||
export function updateGeneralDetails() {
|
export function saveDictionary() {
|
||||||
|
window.localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(window.currentDictionary));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function loadDictionary() {
|
||||||
|
const storedDictionary = window.localStorage.getItem(LOCAL_STORAGE_KEY);
|
||||||
|
if (storedDictionary) {
|
||||||
|
window.currentDictionary = JSON.parse(storedDictionary);
|
||||||
|
} else {
|
||||||
|
clearDictionary();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function clearDictionary() {
|
||||||
|
window.currentDictionary = cloneObject(DEFAULT_DICTIONARY);
|
||||||
}
|
}
|
|
@ -3,7 +3,7 @@ import { renderWords, renderEditForm, renderMaximizedTextbox, renderInfoModal }
|
||||||
import { validateWord, addWord, confirmEditWord, cancelEditWord, confirmDeleteWord } from './wordManagement';
|
import { validateWord, addWord, confirmEditWord, cancelEditWord, confirmDeleteWord } from './wordManagement';
|
||||||
import { removeTags } from '../helpers';
|
import { removeTags } from '../helpers';
|
||||||
import { getNextId } from './utilities';
|
import { getNextId } from './utilities';
|
||||||
import { openEditModal, save, saveAndClose } from './dictionaryManagement';
|
import { openEditModal, saveEditModal, saveAndCloseEditModal } from './dictionaryManagement';
|
||||||
import { goToNextPage, goToPreviousPage, goToPage } from './pagination';
|
import { goToNextPage, goToPreviousPage, goToPage } from './pagination';
|
||||||
|
|
||||||
export default function setupListeners() {
|
export default function setupListeners() {
|
||||||
|
@ -74,8 +74,8 @@ function setupEditFormInteractions() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupEditFormButtons() {
|
function setupEditFormButtons() {
|
||||||
document.getElementById('editSave').addEventListener('click', () => save());
|
document.getElementById('editSave').addEventListener('click', () => saveEditModal());
|
||||||
document.getElementById('editSaveAndClose').addEventListener('click', () => saveAndClose());
|
document.getElementById('editSaveAndClose').addEventListener('click', () => saveAndCloseEditModal());
|
||||||
|
|
||||||
setupMaximizeButtons();
|
setupMaximizeButtons();
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ import { renderWords } from "./render";
|
||||||
import { wordExists } from "./utilities";
|
import { wordExists } from "./utilities";
|
||||||
import removeDiacritics from "./StackOverflow/removeDiacritics";
|
import removeDiacritics from "./StackOverflow/removeDiacritics";
|
||||||
import { removeTags } from "../helpers";
|
import { removeTags } from "../helpers";
|
||||||
|
import { saveDictionary } from "./dictionaryManagement";
|
||||||
|
|
||||||
export function validateWord(word, wordId = false) {
|
export function validateWord(word, wordId = false) {
|
||||||
const errorElementId = wordId === false ? 'wordErrorMessage' : 'wordErrorMessage_' + wordId,
|
const errorElementId = wordId === false ? 'wordErrorMessage' : 'wordErrorMessage_' + wordId,
|
||||||
|
@ -38,6 +39,8 @@ export function sortWords(render) {
|
||||||
return removeDiacritics(wordA[sortBy]).toLowerCase() > removeDiacritics(wordB[sortBy]).toLowerCase() ? 1 : -1;
|
return removeDiacritics(wordA[sortBy]).toLowerCase() > removeDiacritics(wordB[sortBy]).toLowerCase() ? 1 : -1;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
saveDictionary();
|
||||||
|
|
||||||
if (render) {
|
if (render) {
|
||||||
renderWords();
|
renderWords();
|
||||||
}
|
}
|
||||||
|
@ -50,10 +53,12 @@ export function addWord(word, render = true) {
|
||||||
|
|
||||||
export function deleteWord(wordId) {
|
export function deleteWord(wordId) {
|
||||||
const wordIndex = window.currentDictionary.words.findIndex(word => word.wordId === wordId);
|
const wordIndex = window.currentDictionary.words.findIndex(word => word.wordId === wordId);
|
||||||
if (wordIndex > -1) {
|
if (wordIndex < 0) {
|
||||||
|
console.error('Could not find word to delete');
|
||||||
|
} else {
|
||||||
window.currentDictionary.words.splice(wordIndex, 1);
|
window.currentDictionary.words.splice(wordIndex, 1);
|
||||||
|
sortWords(true);
|
||||||
}
|
}
|
||||||
sortWords(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function updateWord(word, wordId) {
|
export function updateWord(word, wordId) {
|
||||||
|
|
Loading…
Reference in New Issue