From 979e8eb89bb22cfb9c510a3ded6b31f9e37bf347 Mon Sep 17 00:00:00 2001 From: Yuri Katkov Date: Thu, 14 Nov 2024 22:49:40 +0100 Subject: [PATCH] #1439 fixed spacing --- ext/js/dictionary/dictionary-data-util.js | 36 +++++++++++++-- ext/js/display/display-generator.js | 54 ++++++++++++----------- ext/templates-display.html | 22 +++++++-- types/ext/dictionary-data-util.d.ts | 5 +++ 4 files changed, 85 insertions(+), 32 deletions(-) diff --git a/ext/js/dictionary/dictionary-data-util.js b/ext/js/dictionary/dictionary-data-util.js index 8ff1b625a8..72f145557b 100644 --- a/ext/js/dictionary/dictionary-data-util.js +++ b/ext/js/dictionary/dictionary-data-util.js @@ -346,18 +346,18 @@ export function compareRevisions(current, latest) { */ function findExistingGroupedPronunciation(reading, pronunciation, groupedPronunciationList) { const existingGroupedPronunciation = groupedPronunciationList.find((groupedPronunciation) => { - return groupedPronunciation.reading === reading && arePronunciationsEquivalent(groupedPronunciation, pronunciation); + return groupedPronunciation.reading === reading && arePronunciationsEquivalent(groupedPronunciation.pronunciation, pronunciation); }); return existingGroupedPronunciation || null; } /** - * @param {import('dictionary-data-util').GroupedPronunciationInternal} groupedPronunciation + * @param {import('dictionary').Pronunciation} pronunciation1 * @param {import('dictionary').Pronunciation} pronunciation2 * @returns {boolean} */ -function arePronunciationsEquivalent({pronunciation: pronunciation1}, pronunciation2) { +function arePronunciationsEquivalent(pronunciation1, pronunciation2) { if ( pronunciation1.type !== pronunciation2.type || !areTagListsEqual(pronunciation1.tags, pronunciation2.tags) @@ -463,3 +463,33 @@ function getSetIntersection(set1, set2) { function createMapKey(array) { return JSON.stringify(array); } + + +/** + * @param {import('dictionary-data-util').DictionaryGroupedPronunciations[]} groupedPronunciations + * @returns {import('dictionary-data-util').PronunciationsInSeveralDictionaries[]} + */ +export function groupByPronunciation(groupedPronunciations) { + const groupedList = []; + + for (const dictionaryGroup of groupedPronunciations) { + const {dictionary, pronunciations} = dictionaryGroup; + + for (const pronunciation of pronunciations) { + const existingEntry = groupedList.find((entry) => arePronunciationsEquivalent(entry.pronunciation.pronunciation, pronunciation.pronunciation)); + + if (existingEntry) { + if (!existingEntry.dictionaries.includes(dictionary)) { + existingEntry.dictionaries.push(dictionary); + } + } else { + groupedList.push({ + pronunciation, + dictionaries: [dictionary], + }); + } + } + } + + return groupedList; +} diff --git a/ext/js/display/display-generator.js b/ext/js/display/display-generator.js index fc9369d55a..27e0811f7f 100644 --- a/ext/js/display/display-generator.js +++ b/ext/js/display/display-generator.js @@ -17,7 +17,7 @@ */ import {ExtensionError} from '../core/extension-error.js'; -import {getDisambiguations, getGroupedPronunciations, getTermFrequency, groupKanjiFrequencies, groupTermFrequencies, groupTermTags, isNonNounVerbOrAdjective} from '../dictionary/dictionary-data-util.js'; +import {getDisambiguations, getGroupedPronunciations, getTermFrequency, groupKanjiFrequencies, groupTermFrequencies, groupTermTags, isNonNounVerbOrAdjective, groupByPronunciation} from '../dictionary/dictionary-data-util.js'; import {HtmlTemplateCollection} from '../dom/html-template-collection.js'; import {distributeFurigana, getKanaMorae, getPitchCategory, isCodePointKanji} from '../language/ja/japanese.js'; import {getLanguageFromText} from '../language/text-utilities.js'; @@ -119,7 +119,8 @@ export class DisplayGenerator { this._appendMultiple(inflectionRuleChainsContainer, this._createInflectionRuleChain.bind(this), inflectionRuleChainCandidates); this._appendMultiple(frequencyGroupListContainer, this._createFrequencyGroup.bind(this), groupedFrequencies, false); - this._appendMultiple(groupedPronunciationsContainer, this._createGroupedPronunciation.bind(this), groupedPronunciations); + const dictionariesGroupedByPronunciation = groupByPronunciation(groupedPronunciations); + this._appendMultiple(groupedPronunciationsContainer, this._createGroupedPronunciationByPronunciation.bind(this), dictionariesGroupedByPronunciation); this._appendMultiple(headwordTagsContainer, this._createTermTag.bind(this), termTags, headwords.length); for (const term of uniqueTerms) { @@ -659,37 +660,32 @@ export class DisplayGenerator { } /** - * @param {import('dictionary-data-util').DictionaryGroupedPronunciations} details + * @param {import('dictionary-data-util').PronunciationsInSeveralDictionaries} details * @returns {HTMLElement} */ - _createGroupedPronunciation(details) { - const {dictionary, dictionaryAlias, pronunciations} = details; + _createGroupedPronunciationByPronunciation({pronunciation, dictionaries}) { + const container = document.createElement('div'); + container.classList.add('grouped-pronunciation-container'); const node = this._instantiate('pronunciation-group'); - node.dataset.dictionary = dictionary; - node.dataset.pronunciationsMulti = 'true'; - node.dataset.pronunciationsCount = `${pronunciations.length}`; - - const n1 = this._querySelector(node, '.pronunciation-group-tag-list'); - const tag = this._createTag(this._createTagData(dictionaryAlias, 'pronunciation-dictionary')); - tag.dataset.details = dictionary; - n1.appendChild(tag); - let hasTags = false; - for (const {pronunciation: {tags}} of pronunciations) { - if (tags.length > 0) { - hasTags = true; - break; - } + const tagListNode = this._querySelector(node, '.pronunciation-group-tag-list'); + for (const dictionary of dictionaries) { + const tag = this._createTag(this._createTagData(dictionary, 'pronunciation-dictionary')); + tag.dataset.details = dictionary; + tagListNode.appendChild(tag); } - const n = this._querySelector(node, '.pronunciation-list'); - n.dataset.hasTags = `${hasTags}`; - this._appendMultiple(n, this._createPronunciation.bind(this), pronunciations); + const pronunciationListNode = this._querySelector(node, '.pronunciation-list'); - return node; + this._appendMultiple(pronunciationListNode, this._createPronunciation.bind(this), [pronunciation]); + + container.appendChild(node); + + return container; } + /** * @param {import('dictionary-data-util').GroupedPronunciation} details * @returns {HTMLElement} @@ -1067,14 +1063,20 @@ export class DisplayGenerator { * @returns {?string} */ _getPronunciationCategories(reading, termPronunciations, wordClasses, headwordIndex) { - if (termPronunciations.length === 0) { return null; } + if (termPronunciations.length === 0) { + return null; + } const isVerbOrAdjective = isNonNounVerbOrAdjective(wordClasses); /** @type {Set} */ const categories = new Set(); for (const termPronunciation of termPronunciations) { - if (termPronunciation.headwordIndex !== headwordIndex) { continue; } + if (termPronunciation.headwordIndex !== headwordIndex) { + continue; + } for (const pronunciation of termPronunciation.pronunciations) { - if (pronunciation.type !== 'pitch-accent') { continue; } + if (pronunciation.type !== 'pitch-accent') { + continue; + } const category = getPitchCategory(reading, pronunciation.position, isVerbOrAdjective); if (category !== null) { categories.add(category); diff --git a/ext/templates-display.html b/ext/templates-display.html index dbd0eddbae..7889de0937 100644 --- a/ext/templates-display.html +++ b/ext/templates-display.html @@ -105,9 +105,25 @@ - - - + + +