Compare commits

..

12 Commits

13 changed files with 301 additions and 150 deletions

View File

@ -139,6 +139,17 @@
<label>Details<span class="red">*</span><a class="label-button maximize-button">Maximize</a><br> <label>Details<span class="red">*</span><a class="label-button maximize-button">Maximize</a><br>
<textarea id="wordDetails" placeholder="Markdown formatting allowed"></textarea> <textarea id="wordDetails" placeholder="Markdown formatting allowed"></textarea>
</label> </label>
<label>
<a id="expandAdvancedForm" class="small button expand-advanced-form">Show Advanced Fields</a>
</label>
<div id="advancedForm" class="advanced-word-form" style="display:none;">
<label>Etymology / Root Words<br>
<input id="wordEtymology" maxlength="2500" placeholder="comma,separated,root,words">
</label>
<label>Related Words<br>
<input id="wordRelated" maxlength="2500" placeholder="comma,separated,related,words">
</label>
</div>
<div id="wordErrorMessage"></div> <div id="wordErrorMessage"></div>
<a class="button" id="addWordButton">Add Word</a> <a class="button" id="addWordButton">Add Word</a>
</form> </form>

View File

@ -36,6 +36,8 @@ export const DEFAULT_DICTIONARY = {
definition: '', definition: '',
details: '', details: '',
etymology: [], etymology: [],
related: [],
principalParts: [],
wordId: 0 wordId: 0
}, */ }, */
], ],

View File

@ -9,7 +9,7 @@ import {
setupWordEditFormButtons, setupWordEditFormButtons,
} from '../setupListeners/words'; } from '../setupListeners/words';
import { getPaginationData } from '../pagination'; import { getPaginationData } from '../pagination';
import { getOpenEditForms, translateOrthography, parseReferences } from '../wordManagement'; import { getOpenEditForms, translateOrthography, parseReferences, getWordReferenceMarkdown } from '../wordManagement';
import { renderAd } from '../ads'; import { renderAd } from '../ads';
import { getPublicLink } from '../account/utilities'; import { getPublicLink } from '../account/utilities';
import { renderPartsOfSpeech } from './details'; import { renderPartsOfSpeech } from './details';
@ -60,6 +60,12 @@ export function renderWords() {
partOfSpeech: removeTags(originalWord.partOfSpeech), partOfSpeech: removeTags(originalWord.partOfSpeech),
definition: removeTags(originalWord.definition), definition: removeTags(originalWord.definition),
details: parseReferences(removeTags(originalWord.details)), details: parseReferences(removeTags(originalWord.details)),
etymology: typeof originalWord.etymology === 'undefined' || originalWord.etymology.length < 1 ? null
: originalWord.etymology.map(root => getWordReferenceMarkdown(removeTags(root))).join(', '),
related: typeof originalWord.related === 'undefined' || originalWord.related.length < 1 ? null
: originalWord.related.map(relatedWord => getWordReferenceMarkdown(removeTags(relatedWord))).join(', '),
principalParts: typeof originalWord.principalParts === 'undefined' || originalWord.principalParts.length < 1 ? null
: originalWord.principalParts.join(', '),
wordId: originalWord.wordId, wordId: originalWord.wordId,
}); });
const homonymnNumber = getHomonymnNumber(originalWord); const homonymnNumber = getHomonymnNumber(originalWord);
@ -72,6 +78,7 @@ export function renderWords() {
wordsHTML += `<article class="entry" id="${word.wordId}"> wordsHTML += `<article class="entry" id="${word.wordId}">
<header> <header>
<h4 class="word"><span class="orthographic-translation">${wordNameDisplay}</span>${homonymnNumber > 0 ? ' <sub>' + homonymnNumber.toString() + '</sub>' : ''}</h4> <h4 class="word"><span class="orthographic-translation">${wordNameDisplay}</span>${homonymnNumber > 0 ? ' <sub>' + homonymnNumber.toString() + '</sub>' : ''}</h4>
${word.principalParts === null ? '' : `<span class="principalParts">(${word.principalParts})</span>`}
<span class="pronunciation">${word.pronunciation}</span> <span class="pronunciation">${word.pronunciation}</span>
<span class="part-of-speech">${word.partOfSpeech}</span> <span class="part-of-speech">${word.partOfSpeech}</span>
${isPublic ? `<a class="small button share-link" href="${shareLink}" target="_blank" title="Public Link to Word">&#10150;</a>` : ''} ${isPublic ? `<a class="small button share-link" href="${shareLink}" target="_blank" title="Public Link to Word">&#10150;</a>` : ''}
@ -86,6 +93,15 @@ export function renderWords() {
<dd class="details"> <dd class="details">
${md(word.details)} ${md(word.details)}
</dd> </dd>
${word.etymology === null && word.related === null ? '' : `<hr>`}
${word.etymology === null ? '' : `<dt>Etymology <small>(Root Word${originalWord.etymology.length !== 1 ? 's' : ''})</small></dt>
<dd class="etymology">
${md(word.etymology).replace(/<\/?p>/g, '')}
</dd>`}
${word.related === null ? '' : `<dt>Related Word${originalWord.related.length !== 1 ? 's' : ''}</dt>
<dd class="related">
${md(word.related).replace(/<\/?p>/g, '')}
</dd>`}
</dl> </dl>
</article>`; </article>`;
}); });
@ -167,6 +183,12 @@ export function renderEditForm(wordId = false) {
<label>Etymology / Root Words<br> <label>Etymology / Root Words<br>
<input id="wordEtymology_${wordId}" maxlength="2500" placeholder="comma,separated,root,words" value="${word.hasOwnProperty('etymology') ? word.etymology : ''}"> <input id="wordEtymology_${wordId}" maxlength="2500" placeholder="comma,separated,root,words" value="${word.hasOwnProperty('etymology') ? word.etymology : ''}">
</label> </label>
<label>Related Words<br>
<input id="wordRelated_${wordId}" maxlength="2500" placeholder="comma,separated,related,words" value="${word.hasOwnProperty('related') ? word.related : ''}">
</label>
<label>Principal Parts<a href="https://en.wikipedia.org/wiki/Principal_parts" target="_blank" class="label-button">What's This?</a><br>
<input id="wordPrincipalParts_${wordId}" maxlength="2500" placeholder="comma,separated,principal,parts" value="${word.hasOwnProperty('principalParts') ? word.principalParts : ''}">
</label>
</div> </div>
<div id="wordErrorMessage_${wordId}"></div> <div id="wordErrorMessage_${wordId}"></div>
<a class="button edit-save-changes" id="editWordButton_${wordId}">Save Changes</a> <a class="button edit-save-changes" id="editWordButton_${wordId}">Save Changes</a>

View File

@ -63,15 +63,25 @@ export function getMatchingSearchWords() {
let details = filters.orthography ? parseReferences(word.details) : word.details; let details = filters.orthography ? parseReferences(word.details) : word.details;
details = filters.ignoreDiacritics ? removeDiacritics(details) : details; details = filters.ignoreDiacritics ? removeDiacritics(details) : details;
details = filters.caseSensitive ? details : details.toLowerCase(); 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 const isInName = filters.name && (filters.exact
? searchTerm == name ? searchTerm == name
: new RegExp(searchTerm, 'g').test(name)); : new RegExp(searchTerm, 'g').test(name)
);
const isInDefinition = filters.definition && (filters.exact const isInDefinition = filters.definition && (filters.exact
? searchTerm == definition ? searchTerm == definition
: new RegExp(searchTerm, 'g').test(definition)); : new RegExp(searchTerm, 'g').test(definition)
);
const isInDetails = filters.details && new RegExp(searchTerm, 'g').test(details); const isInDetails = filters.details && new RegExp(searchTerm, 'g').test(details);
return searchTerm === '' || isInName || isInDefinition || isInDetails; const isInPrincipalParts = filters.name && (filters.exact
? principalParts.includes(searchTerm)
: principalParts.some(part => new RegExp(searchTerm, 'g').test(part))
);
return searchTerm === '' || isInName || isInDefinition || isInDetails || isInPrincipalParts;
}); });
return matchingWords; return matchingWords;
} }
@ -99,6 +109,16 @@ export function highlightSearchTerm(word) {
+ '<mark>' + markedUpWord.name.substr(wordIndex, searchTermLength) + '</mark>' + '<mark>' + markedUpWord.name.substr(wordIndex, searchTermLength) + '</mark>'
+ markedUpWord.name.substr(wordIndex + searchTermLength); + markedUpWord.name.substr(wordIndex + searchTermLength);
}); });
if (markedUpWord.principalParts !== null) {
const part = getIndicesOf(searchTerm, removeDiacritics(markedUpWord.principalParts), filters.caseSensitive);
part.forEach((wordIndex, i) => {
wordIndex += '<mark></mark>'.length * i;
markedUpWord.principalParts = markedUpWord.principalParts.substring(0, wordIndex)
+ '<mark>' + markedUpWord.principalParts.substr(wordIndex, searchTermLength) + '</mark>'
+ markedUpWord.principalParts.substr(wordIndex + searchTermLength);
});
}
} }
if (filters.definition) { if (filters.definition) {
const definitionMatches = getIndicesOf(searchTerm, removeDiacritics(markedUpWord.definition), filters.caseSensitive); const definitionMatches = getIndicesOf(searchTerm, removeDiacritics(markedUpWord.definition), filters.caseSensitive);
@ -122,6 +142,10 @@ export function highlightSearchTerm(word) {
const regexMethod = 'g' + (filters.caseSensitive ? '' : 'i'); const regexMethod = 'g' + (filters.caseSensitive ? '' : 'i');
if (filters.name) { if (filters.name) {
markedUpWord.name = markedUpWord.name.replace(new RegExp(`(${searchTerm})`, regexMethod), `<mark>$1</mark>`); markedUpWord.name = markedUpWord.name.replace(new RegExp(`(${searchTerm})`, regexMethod), `<mark>$1</mark>`);
if (markedUpWord.principalParts !== null) {
markedUpWord.principalParts = markedUpWord.principalParts.replace(new RegExp(`(${searchTerm})`, regexMethod), `<mark>$1</mark>`);
}
} }
if (filters.definition) { if (filters.definition) {
markedUpWord.definition = markedUpWord.definition.replace(new RegExp(`(${searchTerm})`, regexMethod), `<mark>$1</mark>`); markedUpWord.definition = markedUpWord.definition.replace(new RegExp(`(${searchTerm})`, regexMethod), `<mark>$1</mark>`);

View File

@ -182,6 +182,12 @@ export function renderWords() {
partOfSpeech: removeTags(originalWord.partOfSpeech), partOfSpeech: removeTags(originalWord.partOfSpeech),
definition: removeTags(originalWord.definition), definition: removeTags(originalWord.definition),
details: originalWord.details, details: originalWord.details,
etymology: typeof originalWord.etymology === 'undefined' || originalWord.etymology.length < 1 ? null
: originalWord.etymology.join(', '),
related: typeof originalWord.related === 'undefined' || originalWord.related.length < 1 ? null
: originalWord.related.join(', '),
principalParts: typeof originalWord.principalParts === 'undefined' || originalWord.principalParts.length < 1 ? null
: originalWord.principalParts.join(', '),
wordId: originalWord.wordId, wordId: originalWord.wordId,
}); });
@ -193,6 +199,7 @@ export function renderWords() {
wordsHTML += `<article class="entry" id="${word.wordId}"> wordsHTML += `<article class="entry" id="${word.wordId}">
<header> <header>
<h4 class="word"><span class="orthographic-translation">${word.name}</span>${homonymnNumber > 0 ? ' <sub>' + homonymnNumber.toString() + '</sub>' : ''}</h4> <h4 class="word"><span class="orthographic-translation">${word.name}</span>${homonymnNumber > 0 ? ' <sub>' + homonymnNumber.toString() + '</sub>' : ''}</h4>
${word.principalParts === null ? '' : `<span class="principalParts">(${word.principalParts})</span>`}
<span class="pronunciation">${word.pronunciation}</span> <span class="pronunciation">${word.pronunciation}</span>
<span class="part-of-speech">${word.partOfSpeech}</span> <span class="part-of-speech">${word.partOfSpeech}</span>
<a href="${shareLink}" target="_blank" class="small button word-option-button" title="Link to Word">&#10150;</a> <a href="${shareLink}" target="_blank" class="small button word-option-button" title="Link to Word">&#10150;</a>
@ -202,6 +209,15 @@ export function renderWords() {
<dd class="details"> <dd class="details">
${md(word.details)} ${md(word.details)}
</dd> </dd>
${word.etymology === null && word.related === null ? '' : `<hr>`}
${word.etymology === null ? '' : `<dt>Etymology <small>(Root Word${originalWord.etymology.length !== 1 ? 's' : ''})</small></dt>
<dd class="etymology">
${md(word.etymology).replace(/<\/?p>/g, '')}
</dd>`}
${word.related === null ? '' : `<dt>Related Word${originalWord.related.length !== 1 ? 's' : ''}</dt>
<dd class="related">
${md(word.related).replace(/<\/?p>/g, '')}
</dd>`}
</dl> </dl>
</article>`; </article>`;
}); });

View File

@ -22,7 +22,7 @@ export function getSearchFilters() {
caseSensitive: document.getElementById('searchCaseSensitive').checked, caseSensitive: document.getElementById('searchCaseSensitive').checked,
ignoreDiacritics: document.getElementById('searchIgnoreDiacritics').checked, ignoreDiacritics: document.getElementById('searchIgnoreDiacritics').checked,
exact: document.getElementById('searchExactWords').checked, exact: document.getElementById('searchExactWords').checked,
orthography: document.getElementById('searchOrthography').checked, // orthography is removed my default because it is already rendered on the backend.
name: document.getElementById('searchIncludeName').checked, name: document.getElementById('searchIncludeName').checked,
definition: document.getElementById('searchIncludeDefinition').checked, definition: document.getElementById('searchIncludeDefinition').checked,
details: document.getElementById('searchIncludeDetails').checked, details: document.getElementById('searchIncludeDetails').checked,
@ -54,23 +54,30 @@ export function getMatchingSearchWords() {
}).filter(word => { }).filter(word => {
searchTerm = filters.ignoreDiacritics ? removeDiacritics(searchTerm) : searchTerm; searchTerm = filters.ignoreDiacritics ? removeDiacritics(searchTerm) : searchTerm;
searchTerm = filters.caseSensitive ? searchTerm : searchTerm.toLowerCase(); searchTerm = filters.caseSensitive ? searchTerm : searchTerm.toLowerCase();
let name = filters.orthography ? translateOrthography(word.name) : word.name; let name = filters.ignoreDiacritics ? removeDiacritics(word.name) : word.name;
name = filters.ignoreDiacritics ? removeDiacritics(name) : name;
name = filters.caseSensitive ? name : name.toLowerCase(); name = filters.caseSensitive ? name : name.toLowerCase();
let definition = filters.ignoreDiacritics ? removeDiacritics(word.definition) : word.definition; let definition = filters.ignoreDiacritics ? removeDiacritics(word.definition) : word.definition;
definition = filters.caseSensitive ? definition : definition.toLowerCase(); definition = filters.caseSensitive ? definition : definition.toLowerCase();
let details = filters.orthography ? parseReferences(word.details) : word.details; let details = filters.ignoreDiacritics ? removeDiacritics(word.details) : word.details;
details = filters.ignoreDiacritics ? removeDiacritics(details) : details;
details = filters.caseSensitive ? details : details.toLowerCase(); details = filters.caseSensitive ? details : details.toLowerCase();
let principalParts = typeof word.principalParts === 'undefined' ? [] : word.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 const isInName = filters.name && (filters.exact
? searchTerm == name ? searchTerm == name
: new RegExp(searchTerm, 'g').test(name)); : new RegExp(searchTerm, 'g').test(name)
);
const isInDefinition = filters.definition && (filters.exact const isInDefinition = filters.definition && (filters.exact
? searchTerm == definition ? searchTerm == definition
: new RegExp(searchTerm, 'g').test(definition)); : new RegExp(searchTerm, 'g').test(definition)
);
const isInDetails = filters.details && new RegExp(searchTerm, 'g').test(details); const isInDetails = filters.details && new RegExp(searchTerm, 'g').test(details);
return searchTerm === '' || isInName || isInDefinition || isInDetails; const isInPrincipalParts = filters.name && (filters.exact
? principalParts.includes(searchTerm)
: principalParts.some(part => new RegExp(searchTerm, 'g').test(part))
);
return searchTerm === '' || isInName || isInDefinition || isInDetails || isInPrincipalParts;
}); });
return matchingWords; return matchingWords;
} }
@ -83,10 +90,6 @@ export function highlightSearchTerm(word) {
if (searchTerm) { if (searchTerm) {
const filters = getSearchFilters(); const filters = getSearchFilters();
const markedUpWord = cloneObject(word); const markedUpWord = cloneObject(word);
if (filters.orthography) {
markedUpWord.name = translateOrthography(markedUpWord.name);
markedUpWord.details = parseReferences(markedUpWord.details);
}
if (filters.ignoreDiacritics) { if (filters.ignoreDiacritics) {
const searchTermLength = searchTerm.length; const searchTermLength = searchTerm.length;
searchTerm = removeDiacritics(searchTerm); searchTerm = removeDiacritics(searchTerm);
@ -98,6 +101,16 @@ export function highlightSearchTerm(word) {
+ '<mark>' + markedUpWord.name.substr(wordIndex, searchTermLength) + '</mark>' + '<mark>' + markedUpWord.name.substr(wordIndex, searchTermLength) + '</mark>'
+ markedUpWord.name.substr(wordIndex + searchTermLength); + markedUpWord.name.substr(wordIndex + searchTermLength);
}); });
if (markedUpWord.principalParts !== null) {
const part = getIndicesOf(searchTerm, removeDiacritics(markedUpWord.principalParts), filters.caseSensitive);
part.forEach((wordIndex, i) => {
wordIndex += '<mark></mark>'.length * i;
markedUpWord.principalParts = markedUpWord.principalParts.substring(0, wordIndex)
+ '<mark>' + markedUpWord.principalParts.substr(wordIndex, searchTermLength) + '</mark>'
+ markedUpWord.principalParts.substr(wordIndex + searchTermLength);
});
}
} }
if (filters.definition) { if (filters.definition) {
const definitionMatches = getIndicesOf(searchTerm, removeDiacritics(markedUpWord.definition), filters.caseSensitive); const definitionMatches = getIndicesOf(searchTerm, removeDiacritics(markedUpWord.definition), filters.caseSensitive);
@ -121,6 +134,10 @@ export function highlightSearchTerm(word) {
const regexMethod = 'g' + (filters.caseSensitive ? '' : 'i'); const regexMethod = 'g' + (filters.caseSensitive ? '' : 'i');
if (filters.name) { if (filters.name) {
markedUpWord.name = markedUpWord.name.replace(new RegExp(`(${searchTerm})`, regexMethod), `<mark>$1</mark>`); markedUpWord.name = markedUpWord.name.replace(new RegExp(`(${searchTerm})`, regexMethod), `<mark>$1</mark>`);
if (markedUpWord.principalParts !== null) {
markedUpWord.principalParts = markedUpWord.principalParts.replace(new RegExp(`(${searchTerm})`, regexMethod), `<mark>$1</mark>`);
}
} }
if (filters.definition) { if (filters.definition) {
markedUpWord.definition = markedUpWord.definition.replace(new RegExp(`(${searchTerm})`, regexMethod), `<mark>$1</mark>`); markedUpWord.definition = markedUpWord.definition.replace(new RegExp(`(${searchTerm})`, regexMethod), `<mark>$1</mark>`);

View File

@ -1,4 +1,3 @@
import { wordExists, getHomonymnIndexes } from "./utilities";
import removeDiacritics from "../StackOverflow/removeDiacritics"; import removeDiacritics from "../StackOverflow/removeDiacritics";
export function sortWords() { export function sortWords() {
@ -10,46 +9,3 @@ export function sortWords() {
return removeDiacritics(wordA[sortBy]).toLowerCase() > removeDiacritics(wordB[sortBy]).toLowerCase() ? 1 : -1; return removeDiacritics(wordA[sortBy]).toLowerCase() > removeDiacritics(wordB[sortBy]).toLowerCase() ? 1 : -1;
}); });
} }
export function parseReferences(detailsMarkdown) {
const references = detailsMarkdown.match(/\{\{.+?\}\}/g);
if (references && Array.isArray(references)) {
new Set(references).forEach(reference => {
let wordToFind = reference.replace(/\{\{|\}\}/g, '');
let homonymn = 0;
if (wordToFind.includes(':')) {
const separator = wordToFind.indexOf(':');
homonymn = wordToFind.substr(separator + 1);
wordToFind = wordToFind.substring(0, separator);
if (homonymn && homonymn.trim()
&& !isNaN(parseInt(homonymn.trim())) && parseInt(homonymn.trim()) > 0) {
homonymn = parseInt(homonymn.trim());
} else {
homonymn = false;
}
}
let existingWordId = false;
const homonymnIndexes = getHomonymnIndexes({ name: wordToFind, wordId: -1 });
if (homonymn !== false && homonymn > 0) {
if (typeof homonymnIndexes[homonymn - 1] !== 'undefined') {
existingWordId = window.currentDictionary.words[homonymnIndexes[homonymn - 1]].wordId;
}
} else if (homonymn !== false) {
existingWordId = wordExists(wordToFind, true);
}
if (existingWordId !== false) {
if (homonymn < 1 && homonymnIndexes.length > 0) {
homonymn = 1;
}
const homonymnSubHTML = homonymn > 0 ? '<sub>' + homonymn.toString() + '</sub>' : '';
const wordMarkdownLink = `[${wordToFind}${homonymnSubHTML}](#${existingWordId})`;
detailsMarkdown = detailsMarkdown.replace(new RegExp(reference, 'g'), wordMarkdownLink);
}
});
}
return detailsMarkdown;
}

View File

@ -116,7 +116,17 @@ export function parseReferences(detailsMarkdown) {
const references = detailsMarkdown.match(/\{\{.+?\}\}/g); const references = detailsMarkdown.match(/\{\{.+?\}\}/g);
if (references && Array.isArray(references)) { if (references && Array.isArray(references)) {
new Set(references).forEach(reference => { new Set(references).forEach(reference => {
let wordToFind = reference.replace(/\{\{|\}\}/g, ''); const wordMarkdownLink = getWordReferenceMarkdown(reference);
if (wordMarkdownLink !== reference) {
detailsMarkdown = detailsMarkdown.replace(new RegExp(reference, 'g'), wordMarkdownLink);
}
});
}
return detailsMarkdown;
}
export function getWordReferenceMarkdown(reference) {
const wordToFind = reference.replace(/\{\{|\}\}/g, '');
let homonymn = 0; let homonymn = 0;
if (wordToFind.includes(':')) { if (wordToFind.includes(':')) {
@ -147,12 +157,10 @@ export function parseReferences(detailsMarkdown) {
homonymn = 1; homonymn = 1;
} }
const homonymnSubHTML = homonymnIndexes.length > 1 && homonymn - 1 >= 0 ? '<sub>' + homonymn.toString() + '</sub>' : ''; const homonymnSubHTML = homonymnIndexes.length > 1 && homonymn - 1 >= 0 ? '<sub>' + homonymn.toString() + '</sub>' : '';
const wordMarkdownLink = `<span class="word-reference">[<span class="orthographic-translation">${translateOrthography(wordToFind)}</span>${homonymnSubHTML}](#${existingWordId})</span>`; return `<span class="word-reference">[<span class="orthographic-translation">${translateOrthography(wordToFind)}</span>${homonymnSubHTML}](#${existingWordId})</span>`;
detailsMarkdown = detailsMarkdown.replace(new RegExp(reference, 'g'), wordMarkdownLink);
} }
});
} return reference;
return detailsMarkdown;
} }
export function expandAdvancedForm(id = false) { export function expandAdvancedForm(id = false) {
@ -174,7 +182,9 @@ export function submitWordForm() {
partOfSpeech = document.getElementById('wordPartOfSpeech').value, partOfSpeech = document.getElementById('wordPartOfSpeech').value,
definition = document.getElementById('wordDefinition').value, definition = document.getElementById('wordDefinition').value,
details = document.getElementById('wordDetails').value, details = document.getElementById('wordDetails').value,
etymology = document.getElementById('wordEtymology').value; etymology = document.getElementById('wordEtymology').value,
related = document.getElementById('wordRelated').value,
principalParts = document.getElementById('wordPrincipalParts').value;
const word = { const word = {
name: removeTags(name).trim(), name: removeTags(name).trim(),
@ -189,6 +199,14 @@ export function submitWordForm() {
word.etymology = removeTags(etymology).split(',').map(w => w.trim()).filter(w => w.length > 0); word.etymology = removeTags(etymology).split(',').map(w => w.trim()).filter(w => w.length > 0);
} }
if (removeTags(related).trim() !== '') {
word.related = removeTags(related).split(',').map(w => w.trim()).filter(w => w.length > 0);
}
if (removeTags(principalParts).trim() !== '') {
word.principalParts = removeTags(principalParts).split(',').map(w => w.trim()).filter(w => w.length > 0);
}
if (validateWord(word)) { if (validateWord(word)) {
addWord(word); addWord(word);
sortWords(true); sortWords(true);
@ -272,7 +290,9 @@ export function confirmEditWord(id) {
partOfSpeech = document.getElementById('wordPartOfSpeech_' + wordId).value, partOfSpeech = document.getElementById('wordPartOfSpeech_' + wordId).value,
definition = document.getElementById('wordDefinition_' + wordId).value, definition = document.getElementById('wordDefinition_' + wordId).value,
details = document.getElementById('wordDetails_' + wordId).value, details = document.getElementById('wordDetails_' + wordId).value,
etymology = document.getElementById('wordEtymology_' + wordId).value; etymology = document.getElementById('wordEtymology_' + wordId).value,
related = document.getElementById('wordRelated_' + wordId).value,
principalParts = document.getElementById('wordPrincipalParts_' + wordId).value;
const word = { const word = {
name: removeTags(name).trim(), name: removeTags(name).trim(),
@ -287,6 +307,14 @@ export function confirmEditWord(id) {
word.etymology = removeTags(etymology).split(',').map(w => w.trim()).filter(w => w.length > 0); word.etymology = removeTags(etymology).split(',').map(w => w.trim()).filter(w => w.length > 0);
} }
if (removeTags(related).trim() !== '') {
word.related = removeTags(related).split(',').map(w => w.trim()).filter(w => w.length > 0);
}
if (removeTags(principalParts).trim() !== '') {
word.principalParts = removeTags(principalParts).split(',').map(w => w.trim()).filter(w => w.length > 0);
}
if (validateWord(word, wordId)) { if (validateWord(word, wordId)) {
if (confirm(`Are you sure you want to save changes to "${word.name}"?`)) { if (confirm(`Are you sure you want to save changes to "${word.name}"?`)) {
document.getElementById('editForm_' + wordId).classList.add('done'); document.getElementById('editForm_' + wordId).classList.add('done');

View File

@ -215,8 +215,8 @@ WHERE dictionary=$dictionary";
} }
public function getWords ($user, $dictionary) { public function getWords ($user, $dictionary) {
$query = "SELECT words.*, words_advanced.etymology FROM words $query = "SELECT words.*, wa.etymology, wa.related, wa.principal_parts FROM words
LEFT JOIN words_advanced ON words_advanced.dictionary = words.dictionary AND words_advanced.word_id = words.word_id LEFT JOIN words_advanced wa ON wa.dictionary = words.dictionary AND wa.word_id = words.word_id
JOIN dictionaries ON dictionaries.id = words.dictionary JOIN dictionaries ON dictionaries.id = words.dictionary
WHERE words.dictionary=$dictionary AND dictionaries.user=$user"; WHERE words.dictionary=$dictionary AND dictionaries.user=$user";
$results = $this->db->query($query)->fetchAll(); $results = $this->db->query($query)->fetchAll();
@ -233,10 +233,18 @@ WHERE words.dictionary=$dictionary AND dictionaries.user=$user";
'wordId' => intval($row['word_id']), 'wordId' => intval($row['word_id']),
); );
if (!is_null($row['etymology'])) { if (!is_null($row['etymology']) && $row['etymology'] !== '') {
$word['etymology'] = explode(',', $row['etymology']); $word['etymology'] = explode(',', $row['etymology']);
} }
if (!is_null($row['related']) && $row['related'] !== '') {
$word['related'] = explode(',', $row['related']);
}
if (!is_null($row['principal_parts']) && $row['principal_parts'] !== '') {
$word['principalParts'] = explode(',', $row['principal_parts']);
}
return $word; return $word;
}, $results); }, $results);
} }
@ -263,7 +271,7 @@ WHERE words.dictionary=$dictionary AND dictionaries.user=$user";
} }
$query1 = 'INSERT INTO words (dictionary, word_id, name, pronunciation, part_of_speech, definition, details, last_updated, created_on) VALUES '; $query1 = 'INSERT INTO words (dictionary, word_id, name, pronunciation, part_of_speech, definition, details, last_updated, created_on) VALUES ';
$query2 = 'INSERT INTO words_advanced (dictionary, word_id, etymology) VALUES '; $query2 = 'INSERT INTO words_advanced (dictionary, word_id, etymology, related, principal_parts) VALUES ';
$params1 = array(); $params1 = array();
$params2 = array(); $params2 = array();
$word_ids = array(); $word_ids = array();
@ -285,10 +293,12 @@ WHERE words.dictionary=$dictionary AND dictionaries.user=$user";
$params1[] = $last_updated; $params1[] = $last_updated;
$params1[] = $word['createdOn']; $params1[] = $word['createdOn'];
$query2 .= "(?, ?, ?), "; $query2 .= "(?, ?, ?, ?, ?), ";
$params2[] = $dictionary; $params2[] = $dictionary;
$params2[] = $word['wordId']; $params2[] = $word['wordId'];
$params2[] = isset($word['etymology']) ? implode(',', $word['etymology']) : null; $params2[] = isset($word['etymology']) ? implode(',', $word['etymology']) : '';
$params2[] = isset($word['related']) ? implode(',', $word['related']) : '';
$params2[] = isset($word['principalParts']) ? implode(',', $word['principalParts']) : '';
} }
$query1 = trim($query1, ', ') . ' ON DUPLICATE KEY UPDATE $query1 = trim($query1, ', ') . ' ON DUPLICATE KEY UPDATE
name=VALUES(name), name=VALUES(name),
@ -299,7 +309,9 @@ details=VALUES(details),
last_updated=VALUES(last_updated), last_updated=VALUES(last_updated),
created_on=VALUES(created_on)'; created_on=VALUES(created_on)';
$query2 = trim($query2, ', ') . ' ON DUPLICATE KEY UPDATE $query2 = trim($query2, ', ') . ' ON DUPLICATE KEY UPDATE
etymology=VALUES(etymology)'; etymology=VALUES(etymology),
related=VALUES(related),
principal_parts=VALUES(principal_parts)';
$results1 = $this->db->execute($query1, $params1); $results1 = $this->db->execute($query1, $params1);

View File

@ -86,14 +86,32 @@ class PublicDictionary {
'pronunciation' => $row['pronunciation'], 'pronunciation' => $row['pronunciation'],
'partOfSpeech' => $row['part_of_speech'], 'partOfSpeech' => $row['part_of_speech'],
'definition' => $row['definition'], 'definition' => $row['definition'],
'details' => $this->parseReferences(strip_tags($row['details']), $dictionary), 'details' => $this->parseReferences(strip_tags($row['details']), $dictionary, false),
'lastUpdated' => is_null($row['last_updated']) ? intval($row['created_on']) : intval($row['last_updated']), 'lastUpdated' => is_null($row['last_updated']) ? intval($row['created_on']) : intval($row['last_updated']),
'createdOn' => intval($row['created_on']), 'createdOn' => intval($row['created_on']),
'wordId' => intval($row['word_id']), 'wordId' => intval($row['word_id']),
); );
if (!is_null($row['etymology'])) { if (!is_null($row['etymology'])) {
$word['etymology'] = $row['etymology']; if (strlen($row['etymology']) > 0) {
$word['etymology'] = array_map(function ($root) use($dictionary) {
return $this->getWordReferenceHTML(strip_tags($root), $dictionary, false);
}, explode(',', $row['etymology']));
}
}
if (!is_null($row['related'])) {
if (strlen($row['related']) > 0) {
$word['related'] = array_map(function ($root) use($dictionary) {
return $this->getWordReferenceHTML(strip_tags($root), $dictionary, false);
}, explode(',', $row['related']));
}
}
if (!is_null($row['principal_parts'])) {
if (strlen($row['principal_parts']) > 0) {
$word['principalParts'] = explode(',', $row['principal_parts']);
}
} }
return $word; return $word;
@ -105,10 +123,13 @@ class PublicDictionary {
public function getSpecificPublicDictionaryWord ($dictionary, $word) { public function getSpecificPublicDictionaryWord ($dictionary, $word) {
if (is_numeric($dictionary) && is_numeric($word)) { if (is_numeric($dictionary) && is_numeric($word)) {
$query = "SELECT words.* FROM words JOIN dictionaries ON id = dictionary WHERE dictionary=? AND word_id=? AND is_public=1"; $query = "SELECT words.*, wa.etymology, wa.related, wa.principal_parts FROM words
LEFT JOIN words_advanced wa ON wa.dictionary = words.dictionary AND wa.word_id = words.word_id
JOIN dictionaries ON dictionaries.id = words.dictionary
WHERE words.dictionary=? AND words.word_id=? AND dictionaries.is_public=1";
$result = $this->db->query($query, array($dictionary, $word))->fetch(); $result = $this->db->query($query, array($dictionary, $word))->fetch();
if ($result) { if ($result) {
return array( $word = array(
'name' => $this->translateOrthography($result['name'], $dictionary), 'name' => $this->translateOrthography($result['name'], $dictionary),
'pronunciation' => $result['pronunciation'], 'pronunciation' => $result['pronunciation'],
'partOfSpeech' => $result['part_of_speech'], 'partOfSpeech' => $result['part_of_speech'],
@ -118,6 +139,30 @@ class PublicDictionary {
'createdOn' => intval($result['created_on']), 'createdOn' => intval($result['created_on']),
'wordId' => intval($result['word_id']), 'wordId' => intval($result['word_id']),
); );
if (!is_null($result['etymology'])) {
if (strlen($result['etymology']) > 0) {
$word['etymology'] = array_map(function ($root) use($dictionary) {
return $this->getWordReferenceHTML(strip_tags($root), $dictionary);
}, explode(',', $result['etymology']));
}
}
if (!is_null($result['related'])) {
if (strlen($result['related']) > 0) {
$word['related'] = array_map(function ($root) use($dictionary) {
return $this->getWordReferenceHTML(strip_tags($root), $dictionary);
}, explode(',', $result['related']));
}
}
if (!is_null($result['principal_parts'])) {
if (strlen($result['principal_parts']) > 0) {
$word['principalParts'] = explode(',', $result['principal_parts']);
}
}
return $word;
} }
} }
return false; return false;
@ -125,7 +170,10 @@ class PublicDictionary {
private function getWordsAsEntered() { private function getWordsAsEntered() {
if (!isset($this->original_words)) { if (!isset($this->original_words)) {
$query = "SELECT words.* FROM words JOIN dictionaries ON id = dictionary WHERE dictionary=? AND is_public=1"; $query = "SELECT words.*, wa.etymology, wa.related, wa.principal_parts FROM words
LEFT JOIN words_advanced wa ON wa.dictionary = words.dictionary AND wa.word_id = words.word_id
JOIN dictionaries ON dictionaries.id = words.dictionary
WHERE words.dictionary=? AND is_public=1";
$this->original_words = $this->db->query($query, array($this->details['externalID']))->fetchAll(); $this->original_words = $this->db->query($query, array($this->details['externalID']))->fetchAll();
} }
return $this->original_words; return $this->original_words;
@ -189,6 +237,17 @@ class PublicDictionary {
if (preg_match_all('/\{\{.+?\}\}/', $details, $references) !== false) { if (preg_match_all('/\{\{.+?\}\}/', $details, $references) !== false) {
$references = array_unique($references[0]); $references = array_unique($references[0]);
foreach($references as $reference) { foreach($references as $reference) {
$reference_link = $this->getWordReferenceHTML($reference, $dictionary_id);
if ($reference_link !== $reference) {
$details = str_replace($reference, $markdown_link, $details);
}
}
}
return $details;
}
private function getWordReferenceHTML($reference, $dictionary_id, $direct_link = true) {
$word_to_find = preg_replace('/\{\{|\}\}/', '', $reference); $word_to_find = preg_replace('/\{\{|\}\}/', '', $reference);
$homonymn = 0; $homonymn = 0;
@ -221,16 +280,15 @@ class PublicDictionary {
} }
$homonymn_sub_html = count($reference_ids) > 1 && $homonymn - 1 >= 0 ? '<sub>' . $homonymn . '</sub>' : ''; $homonymn_sub_html = count($reference_ids) > 1 && $homonymn - 1 >= 0 ? '<sub>' . $homonymn . '</sub>' : '';
$site_root = substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], $dictionary_id)); $site_root = substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], $dictionary_id));
$markdown_link = '<span class="word-reference"><a href="' . $site_root . $dictionary_id . '/' . $target_id .'" target="_blank" title="Link to Reference">' return '<span class="word-reference"><a href="'
. ($direct_link ? $site_root . $dictionary_id . '/' : '#') . $target_id .'" '
. ($direct_link ? 'target="_blank" ' : '') . 'title="Link to Reference">'
. '<span class="orthographic-translation">' . $this->translateOrthography($word_to_find, $dictionary_id) . '</span>' . $homonymn_sub_html . '<span class="orthographic-translation">' . $this->translateOrthography($word_to_find, $dictionary_id) . '</span>' . $homonymn_sub_html
. '</a></span>'; . '</a></span>';
$details = str_replace($reference, $markdown_link, $details);
}
}
} }
} }
return $details; return $reference;
} }
private function getWordIdsWithName($dictionary, $word_name) { private function getWordIdsWithName($dictionary, $word_name) {

View File

@ -99,5 +99,7 @@ CREATE TABLE IF NOT EXISTS `words_advanced` (
`dictionary` int(11) NOT NULL, `dictionary` int(11) NOT NULL,
`word_id` int(11) NOT NULL, `word_id` int(11) NOT NULL,
`etymology` text NOT NULL COMMENT 'Comma-separated', `etymology` text NOT NULL COMMENT 'Comma-separated',
`related` text NOT NULL COMMENT 'Comma-separated',
`principal_parts` text NOT NULL COMMENT 'Comma-separated',
UNIQUE KEY `dictionary_word_id` (`dictionary`,`word_id`) UNIQUE KEY `dictionary_word_id` (`dictionary`,`word_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8; ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

View File

@ -146,6 +146,12 @@
<label>Etymology / Root Words<br> <label>Etymology / Root Words<br>
<input id="wordEtymology" maxlength="2500" placeholder="comma,separated,root,words"> <input id="wordEtymology" maxlength="2500" placeholder="comma,separated,root,words">
</label> </label>
<label>Related Words<br>
<input id="wordRelated" maxlength="2500" placeholder="comma,separated,related,words">
</label>
<label>Principal Parts<a href="https://en.wikipedia.org/wiki/Principal_parts" target="_blank" class="label-button">What's This?</a><br>
<input id="wordPrincipalParts" maxlength="2500" placeholder="comma,separated,principal,parts">
</label>
</div> </div>
<div id="wordErrorMessage"></div> <div id="wordErrorMessage"></div>
<a class="button" id="addWordButton">Add Word</a> <a class="button" id="addWordButton">Add Word</a>

View File

@ -81,9 +81,6 @@
<label>Exact Words <label>Exact Words
<input type="checkbox" id="searchExactWords"> <input type="checkbox" id="searchExactWords">
</label> </label>
<label style="display:none;">Translations
<input type="checkbox" id="searchOrthography">
</label>
</div> </div>
</div> </div>
<div class="split"> <div class="split">