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,51 +42,72 @@ export function getSearchFilters() {
return filters;
}
function wordMatchesPartsOfSpeechFilter(word, filters) {
if (!filters.allPartsOfSpeechChecked) {
const partOfSpeech = word.partOfSpeech === '' ? 'Unclassified' : word.partOfSpeech;
return filters.partsOfSpeech.hasOwnProperty(partOfSpeech) && filters.partsOfSpeech[partOfSpeech];
}
return true;
}
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;
name = filters.ignoreDiacritics ? removeDiacritics(name) : name;
name = filters.caseSensitive ? name : name.toLowerCase();
let definition = filters.ignoreDiacritics ? removeDiacritics(word.definition) : word.definition;
definition = filters.caseSensitive ? definition : definition.toLowerCase();
let details = filters.orthography ? parseReferences(word.details) : word.details;
details = filters.ignoreDiacritics ? removeDiacritics(details) : details;
details = filters.caseSensitive ? details : details.toLowerCase();
let principalParts = typeof word.principalParts === 'undefined' ? [] : word.principalParts;
principalParts = filters.orthography ? principalParts.map(part => translateOrthography(part)) : principalParts;
principalParts = filters.ignoreDiacritics ? principalParts.map(part => removeDiacritics(part)) : principalParts;
principalParts = filters.caseSensitive ? principalParts : principalParts.map(part => part.toLowerCase());
const isInName = filters.name && (filters.exact
? searchTerm == name
: new RegExp(searchTerm, 'g').test(name)
);
const isInDefinition = filters.definition && (filters.exact
? searchTerm == definition
: new RegExp(searchTerm, 'g').test(definition)
);
const isInDetails = filters.details && new RegExp(searchTerm, 'g').test(details);
const isInPrincipalParts = filters.name && (filters.exact
? principalParts.includes(searchTerm)
: principalParts.some(part => new RegExp(searchTerm, 'g').test(part))
);
return isInName || isInDefinition || isInDetails || isInPrincipalParts;
}
export function getMatchingSearchWords() {
let searchTerm = getSearchTerm();
const searchTerm = getSearchTerm();
const filters = getSearchFilters();
if (searchTerm !== '' || !filters.allPartsOfSpeechChecked) {
const matchingWords = window.currentDictionary.words.slice().filter(word => {
if (!filters.allPartsOfSpeechChecked) {
const partOfSpeech = word.partOfSpeech === '' ? 'Unclassified' : word.partOfSpeech;
return filters.partsOfSpeech.hasOwnProperty(partOfSpeech) && filters.partsOfSpeech[partOfSpeech];
}
return true;
}).filter(word => {
searchTerm = filters.ignoreDiacritics ? removeDiacritics(searchTerm) : searchTerm;
searchTerm = filters.caseSensitive ? searchTerm : searchTerm.toLowerCase();
let name = filters.orthography ? translateOrthography(word.name) : word.name;
name = filters.ignoreDiacritics ? removeDiacritics(name) : name;
name = filters.caseSensitive ? name : name.toLowerCase();
let definition = filters.ignoreDiacritics ? removeDiacritics(word.definition) : word.definition;
definition = filters.caseSensitive ? definition : definition.toLowerCase();
let details = filters.orthography ? parseReferences(word.details) : word.details;
details = filters.ignoreDiacritics ? removeDiacritics(details) : details;
details = filters.caseSensitive ? details : details.toLowerCase();
let principalParts = typeof word.principalParts === 'undefined' ? [] : word.principalParts;
principalParts = filters.orthography ? principalParts.map(part => translateOrthography(part)) : principalParts;
principalParts = filters.ignoreDiacritics ? principalParts.map(part => removeDiacritics(part)) : principalParts;
principalParts = filters.caseSensitive ? principalParts : principalParts.map(part => part.toLowerCase());
const isInName = filters.name && (filters.exact
? searchTerm == name
: new RegExp(searchTerm, 'g').test(name)
);
const isInDefinition = filters.definition && (filters.exact
? searchTerm == definition
: new RegExp(searchTerm, 'g').test(definition)
);
const isInDetails = filters.details && new RegExp(searchTerm, 'g').test(details);
const isInPrincipalParts = filters.name && (filters.exact
? principalParts.includes(searchTerm)
: principalParts.some(part => new RegExp(searchTerm, 'g').test(part))
);
return searchTerm === '' || isInName || isInDefinition || isInDetails || isInPrincipalParts;
});
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,9 +289,15 @@ export function updateWord(word, wordId) {
sortWords(true);
} else {
saveDictionary(false);
document.getElementById(wordId.toString()).outerHTML = renderWord(window.currentDictionary.words[wordIndex], isPublic);
setupWordOptionButtons();
setupWordOptionSelections();
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()) {