Remove edited words if not matching search

This commit is contained in:
Robbie Antenesse 2020-08-03 15:50:20 -06:00
parent 15ad17e1d7
commit 5e803d988e
2 changed files with 70 additions and 42 deletions

View File

@ -42,17 +42,17 @@ export function getSearchFilters() {
return filters;
}
export function getMatchingSearchWords() {
let searchTerm = getSearchTerm();
const filters = getSearchFilters();
if (searchTerm !== '' || !filters.allPartsOfSpeechChecked) {
const matchingWords = window.currentDictionary.words.slice().filter(word => {
function wordMatchesPartsOfSpeechFilter(word, filters) {
if (!filters.allPartsOfSpeechChecked) {
const partOfSpeech = word.partOfSpeech === '' ? 'Unclassified' : word.partOfSpeech;
return filters.partsOfSpeech.hasOwnProperty(partOfSpeech) && filters.partsOfSpeech[partOfSpeech];
}
return true;
}).filter(word => {
}
function wordMatchesSearchTermAndOptions(word, searchTerm, filters) {
if (searchTerm === '') return true; // If searchTerm is blank, don't process word.
searchTerm = filters.ignoreDiacritics ? removeDiacritics(searchTerm) : searchTerm;
searchTerm = filters.caseSensitive ? searchTerm : searchTerm.toLowerCase();
let name = filters.orthography ? translateOrthography(word.name) : word.name;
@ -81,12 +81,33 @@ export function getMatchingSearchWords() {
? principalParts.includes(searchTerm)
: principalParts.some(part => new RegExp(searchTerm, 'g').test(part))
);
return searchTerm === '' || isInName || isInDefinition || isInDetails || isInPrincipalParts;
});
return isInName || isInDefinition || isInDetails || isInPrincipalParts;
}
export function getMatchingSearchWords() {
const searchTerm = getSearchTerm();
const filters = getSearchFilters();
if (searchTerm !== '' || !filters.allPartsOfSpeechChecked) {
const matchingWords = window.currentDictionary.words.slice()
.filter(word => wordMatchesPartsOfSpeechFilter(word, filters))
.filter(word => wordMatchesSearchTermAndOptions(word, searchTerm, filters));
return matchingWords;
}
return window.currentDictionary.words
return window.currentDictionary.words;
}
export function wordMatchesSearch(word) {
const searchTerm = getSearchTerm();
const filters = getSearchFilters();
if (searchTerm !== '' || !filters.allPartsOfSpeechChecked) {
if (searchTerm === '') {
return wordMatchesPartsOfSpeechFilter(word, filters);
}
return wordMatchesPartsOfSpeechFilter(word, filters)
&& wordMatchesSearchTermAndOptions(word, searchTerm, filters);
}
return true;
}
export function highlightSearchTerm(word) {

View File

@ -4,6 +4,7 @@ import removeDiacritics from "./StackOverflow/removeDiacritics";
import { removeTags, getTimestampInSeconds } from "../helpers";
import { saveDictionary } from "./dictionaryManagement";
import { setupWordOptionButtons, setupWordOptionSelections } from "./setupListeners/words";
import { wordMatchesSearch } from "./search";
export function validateWord(word, wordId = false) {
const errorElementId = wordId === false ? 'wordErrorMessage' : 'wordErrorMessage_' + wordId,
@ -288,10 +289,16 @@ export function updateWord(word, wordId) {
sortWords(true);
} else {
saveDictionary(false);
const entry = document.getElementById(wordId.toString());
if (!wordMatchesSearch(word)) {
entry.parentElement.removeChild(entry);
} else {
console.log('matches search, updating in place');
document.getElementById(wordId.toString()).outerHTML = renderWord(window.currentDictionary.words[wordIndex], isPublic);
setupWordOptionButtons();
setupWordOptionSelections();
}
}
if (hasToken()) {
import('./account/index.js').then(account => {