diff --git a/src/constants.js b/src/constants.js index d005963..6382690 100644 --- a/src/constants.js +++ b/src/constants.js @@ -50,4 +50,6 @@ export const DEFAULT_DICTIONARY = { createdOn: 0, }; -export const DEFAULT_PAGE_SIZE = 50; \ No newline at end of file +export const DEFAULT_PAGE_SIZE = 50; + +export const LOCAL_STORAGE_KEY = 'dictionary'; diff --git a/src/index.js b/src/index.js index 2965f89..10ab518 100644 --- a/src/index.js +++ b/src/index.js @@ -1,14 +1,13 @@ import './main.scss'; -import { DEFAULT_DICTIONARY } from './constants'; import setupListeners from './js/setupListeners'; import { renderAll } from './js/render'; -import { cloneObject } from './helpers'; import { generateRandomWords } from './js/utilities'; +import { loadDictionary } from './js/dictionaryManagement'; function initialize() { console.log('initializing'); - window.currentDictionary = cloneObject(DEFAULT_DICTIONARY); + loadDictionary(); // generateRandomWords(100); setupListeners(); renderAll(); diff --git a/src/js/dictionaryManagement.js b/src/js/dictionaryManagement.js index f6dddda..c49b77f 100644 --- a/src/js/dictionaryManagement.js +++ b/src/js/dictionaryManagement.js @@ -1,5 +1,6 @@ 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 () { @@ -38,18 +39,18 @@ export function openEditModal() { document.getElementById('editModal').style.display = ''; } -export function save() { +export function saveEditModal() { window.currentDictionary.name = removeTags(document.getElementById('editName').value.trim()); window.currentDictionary.specification = removeTags(document.getElementById('editSpecification').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.vowels = document.getElementById('editVowels').value.split(',').map(val => val.trim()); - window.currentDictionary.details.phonology.blends = document.getElementById('editBlends').value.split(',').map(val => val.trim()); - window.currentDictionary.details.phonology.phonotactics.onset = document.getElementById('editOnset').value.split(',').map(val => val.trim()); - window.currentDictionary.details.phonology.phonotactics.nucleus = document.getElementById('editNucleus').value.split(',').map(val => val.trim()); - window.currentDictionary.details.phonology.phonotactics.coda = document.getElementById('editCoda').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()).filter(val => val !== ''); + 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()).filter(val => val !== ''); + 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()).filter(val => val !== ''); window.currentDictionary.details.phonology.phonotactics.exceptions = removeTags(document.getElementById('editExceptions').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.isPublic = document.getElementById('editIsPublic').checked; + saveDictionary(); renderDictionaryDetails(); renderPartsOfSpeech(); } -export function saveAndClose() { - save(); +export function saveAndCloseEditModal() { + saveEditModal(); document.getElementById('editModal').style.display = 'none'; } -export function updateGeneralDetails() { +export function saveDictionary() { + window.localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(window.currentDictionary)); +} -} \ No newline at end of file +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); +} diff --git a/src/js/setupListeners.js b/src/js/setupListeners.js index c976f79..031f5d3 100644 --- a/src/js/setupListeners.js +++ b/src/js/setupListeners.js @@ -3,7 +3,7 @@ import { renderWords, renderEditForm, renderMaximizedTextbox, renderInfoModal } import { validateWord, addWord, confirmEditWord, cancelEditWord, confirmDeleteWord } from './wordManagement'; import { removeTags } from '../helpers'; import { getNextId } from './utilities'; -import { openEditModal, save, saveAndClose } from './dictionaryManagement'; +import { openEditModal, saveEditModal, saveAndCloseEditModal } from './dictionaryManagement'; import { goToNextPage, goToPreviousPage, goToPage } from './pagination'; export default function setupListeners() { @@ -74,8 +74,8 @@ function setupEditFormInteractions() { } function setupEditFormButtons() { - document.getElementById('editSave').addEventListener('click', () => save()); - document.getElementById('editSaveAndClose').addEventListener('click', () => saveAndClose()); + document.getElementById('editSave').addEventListener('click', () => saveEditModal()); + document.getElementById('editSaveAndClose').addEventListener('click', () => saveAndCloseEditModal()); setupMaximizeButtons(); } diff --git a/src/js/wordManagement.js b/src/js/wordManagement.js index 26f227f..4ebaa8e 100644 --- a/src/js/wordManagement.js +++ b/src/js/wordManagement.js @@ -2,6 +2,7 @@ import { renderWords } from "./render"; import { wordExists } from "./utilities"; import removeDiacritics from "./StackOverflow/removeDiacritics"; import { removeTags } from "../helpers"; +import { saveDictionary } from "./dictionaryManagement"; export function validateWord(word, wordId = false) { const errorElementId = wordId === false ? 'wordErrorMessage' : 'wordErrorMessage_' + wordId, @@ -37,6 +38,8 @@ export function sortWords(render) { if (removeDiacritics(wordA[sortBy]).toLowerCase() === removeDiacritics(wordB[sortBy]).toLowerCase()) return 0; return removeDiacritics(wordA[sortBy]).toLowerCase() > removeDiacritics(wordB[sortBy]).toLowerCase() ? 1 : -1; }); + + saveDictionary(); if (render) { renderWords(); @@ -50,10 +53,12 @@ export function addWord(word, render = true) { export function deleteWord(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); + sortWords(true); } - sortWords(true); } export function updateWord(word, wordId) {