Skip to content

Commit

Permalink
Show metadata when clicking on term, kanji, and frequency dictionary …
Browse files Browse the repository at this point in the history
…tags (#1608)

* Show metadata when clicking on term dict tags

* Show frequency count when clicking on frequency dict tags

* Show metadata when clicking on kanji dict tags

* Add URL to term and kanji dict
  • Loading branch information
Kuuuube authored Nov 27, 2024
1 parent f0b7009 commit 51842b2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
14 changes: 10 additions & 4 deletions ext/js/dictionary/dictionary-data-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ export function groupTermTags(dictionaryEntry) {

/**
* @param {import('dictionary').TermDictionaryEntry} dictionaryEntry
* @param {import('dictionary-importer').Summary[]} dictionaryInfo
* @returns {import('dictionary-data-util').DictionaryFrequency<import('dictionary-data-util').TermFrequency>[]}
*/
export function groupTermFrequencies(dictionaryEntry) {
export function groupTermFrequencies(dictionaryEntry, dictionaryInfo) {
const {headwords, frequencies: sourceFrequencies} = dictionaryEntry;

/** @type {import('dictionary-data-util').TermFrequenciesMap1} */
Expand Down Expand Up @@ -92,16 +93,19 @@ export function groupTermFrequencies(dictionaryEntry) {
values: [...values.values()],
});
}
results.push({dictionary, frequencies, dictionaryAlias});
const currentDictionaryInfo = dictionaryInfo.find(({title}) => title === dictionary);
const freqCount = currentDictionaryInfo?.counts.termMeta.freq ?? 0;
results.push({dictionary, frequencies, dictionaryAlias, freqCount});
}
return results;
}

/**
* @param {import('dictionary').KanjiFrequency[]} sourceFrequencies
* @param {import('dictionary-importer').Summary[]} dictionaryInfo
* @returns {import('dictionary-data-util').DictionaryFrequency<import('dictionary-data-util').KanjiFrequency>[]}
*/
export function groupKanjiFrequencies(sourceFrequencies) {
export function groupKanjiFrequencies(sourceFrequencies, dictionaryInfo) {
/** @type {import('dictionary-data-util').KanjiFrequenciesMap1} */
const map1 = new Map();
/** @type {Map<string, string>} */
Expand Down Expand Up @@ -133,7 +137,9 @@ export function groupKanjiFrequencies(sourceFrequencies) {
values: [...values.values()],
});
}
results.push({dictionary, frequencies, dictionaryAlias});
const currentDictionaryInfo = dictionaryInfo.find(({title}) => title === dictionary);
const freqCount = currentDictionaryInfo?.counts.kanjiMeta.freq ?? 0;
results.push({dictionary, frequencies, dictionaryAlias, freqCount});
}
return results;
}
Expand Down
55 changes: 50 additions & 5 deletions ext/js/display/display-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ export class DisplayGenerator {

/**
* @param {import('dictionary').TermDictionaryEntry} dictionaryEntry
* @param {import('dictionary-importer').Summary[]} dictionaryInfo
* @returns {HTMLElement}
*/
createTermEntry(dictionaryEntry) {
createTermEntry(dictionaryEntry, dictionaryInfo) {
const node = this._instantiate('term-entry');

const headwordsContainer = this._querySelector(node, '.headword-list');
Expand All @@ -81,7 +82,7 @@ export class DisplayGenerator {
const {headwords, type, inflectionRuleChainCandidates, definitions, frequencies, pronunciations} = dictionaryEntry;
const groupedPronunciations = getGroupedPronunciations(dictionaryEntry);
const pronunciationCount = groupedPronunciations.reduce((i, v) => i + v.pronunciations.length, 0);
const groupedFrequencies = groupTermFrequencies(dictionaryEntry);
const groupedFrequencies = groupTermFrequencies(dictionaryEntry, dictionaryInfo);
const termTags = groupTermTags(dictionaryEntry);

/** @type {Set<string>} */
Expand Down Expand Up @@ -143,6 +144,28 @@ export class DisplayGenerator {
dictionaryTag.dictionaries.push(dictionary);
dictionaryTag.name = dictionaryAlias;
dictionaryTag.content = [dictionary];

const currentDictionaryInfo = dictionaryInfo.find(({title}) => title === dictionary);
if (currentDictionaryInfo) {
const dictionaryContentArray = [];
dictionaryContentArray.push(currentDictionaryInfo.title);
if (currentDictionaryInfo.author) {
dictionaryContentArray.push('Author: ' + currentDictionaryInfo.author);
}
if (currentDictionaryInfo.description) {
dictionaryContentArray.push('Description: ' + currentDictionaryInfo.description);
}
if (currentDictionaryInfo.url) {
dictionaryContentArray.push('URL: ' + currentDictionaryInfo.url);
}

const totalTerms = currentDictionaryInfo.counts.terms.total;
if (totalTerms > 0) {
dictionaryContentArray.push('Term Count: ' + totalTerms.toString());
}

dictionaryTag.content = dictionaryContentArray;
}
}

const node2 = this._createTermDefinition(definition, dictionaryTag, headwords, uniqueTerms, uniqueReadings);
Expand All @@ -156,9 +179,10 @@ export class DisplayGenerator {

/**
* @param {import('dictionary').KanjiDictionaryEntry} dictionaryEntry
* @param {import('dictionary-importer').Summary[]} dictionaryInfo
* @returns {HTMLElement}
*/
createKanjiEntry(dictionaryEntry) {
createKanjiEntry(dictionaryEntry, dictionaryInfo) {
const node = this._instantiate('kanji-entry');
node.dataset.dictionary = dictionaryEntry.dictionary;

Expand All @@ -175,11 +199,32 @@ export class DisplayGenerator {

this._setTextContent(glyphContainer, dictionaryEntry.character, this._language);
if (this._language === 'ja') { glyphContainer.style.fontFamily = 'kanji-stroke-orders, sans-serif'; }
const groupedFrequencies = groupKanjiFrequencies(dictionaryEntry.frequencies);
const groupedFrequencies = groupKanjiFrequencies(dictionaryEntry.frequencies, dictionaryInfo);

const dictionaryTag = this._createDictionaryTag('');
dictionaryTag.name = dictionaryEntry.dictionaryAlias;
dictionaryTag.content = [dictionaryEntry.dictionary];
const currentDictionaryInfo = dictionaryInfo.find(({title}) => title === dictionaryEntry.dictionary);
if (currentDictionaryInfo) {
const dictionaryContentArray = [];
dictionaryContentArray.push(currentDictionaryInfo.title);
if (currentDictionaryInfo.author) {
dictionaryContentArray.push('Author: ' + currentDictionaryInfo.author);
}
if (currentDictionaryInfo.description) {
dictionaryContentArray.push('Description: ' + currentDictionaryInfo.description);
}
if (currentDictionaryInfo.url) {
dictionaryContentArray.push('URL: ' + currentDictionaryInfo.url);
}

const totalKanji = currentDictionaryInfo.counts.kanji.total;
if (totalKanji > 0) {
dictionaryContentArray.push('Kanji Count: ' + totalKanji.toString());
}

dictionaryTag.content = dictionaryContentArray;
}

this._appendMultiple(frequencyGroupListContainer, this._createFrequencyGroup.bind(this), groupedFrequencies, true);
this._appendMultiple(tagContainer, this._createTag.bind(this), [...dictionaryEntry.tags, dictionaryTag]);
Expand Down Expand Up @@ -828,7 +873,7 @@ export class DisplayGenerator {
body.dataset.count = `${ii}`;
node.dataset.count = `${ii}`;
node.dataset.details = dictionary;
tag.dataset.details = dictionary;
tag.dataset.details = dictionary + '\nFrequency Count: ' + details.freqCount?.toString();
return node;
}

Expand Down
8 changes: 6 additions & 2 deletions ext/js/display/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ export class Display extends EventDispatcher {
this._themeController = new ThemeController(document.documentElement);
/** @type {import('language').LanguageSummary[]} */
this._languageSummaries = [];
/** @type {import('dictionary-importer').Summary[]} */
this._dictionaryInfo = [];

/* eslint-disable @stylistic/no-multi-spaces */
this._hotkeyHandler.registerActions([
Expand Down Expand Up @@ -322,6 +324,8 @@ export class Display extends EventDispatcher {

this._languageSummaries = await this._application.api.getLanguageSummaries();

this._dictionaryInfo = await this._application.api.getDictionaryInfo();

// Prepare
await this._hotkeyHelpController.prepare(this._application.api);
await this._displayGenerator.prepare();
Expand Down Expand Up @@ -1415,8 +1419,8 @@ export class Display extends EventDispatcher {
const dictionaryEntry = dictionaryEntries[i];
const entry = (
dictionaryEntry.type === 'term' ?
this._displayGenerator.createTermEntry(dictionaryEntry) :
this._displayGenerator.createKanjiEntry(dictionaryEntry)
this._displayGenerator.createTermEntry(dictionaryEntry, this._dictionaryInfo) :
this._displayGenerator.createKanjiEntry(dictionaryEntry, this._dictionaryInfo)
);
entry.dataset.index = `${i}`;
this._dictionaryEntryNodes.push(entry);
Expand Down
1 change: 1 addition & 0 deletions types/ext/dictionary-data-util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export type DictionaryFrequency<T = unknown> = {
dictionary: string;
dictionaryAlias: string;
frequencies: T[];
freqCount: number;
};

export type TermFrequency = {
Expand Down

0 comments on commit 51842b2

Please sign in to comment.