Skip to content

Commit

Permalink
#1439 fixed spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
ganqqwerty committed Nov 14, 2024
1 parent 746bed5 commit 979e8eb
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 32 deletions.
36 changes: 33 additions & 3 deletions ext/js/dictionary/dictionary-data-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 = [];

Check failure on line 473 in ext/js/dictionary/dictionary-data-util.js

View workflow job for this annotation

GitHub Actions / Static Analysis

Variable 'groupedList' implicitly has type 'any[]' in some locations where its type cannot be determined.

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));

Check failure on line 479 in ext/js/dictionary/dictionary-data-util.js

View workflow job for this annotation

GitHub Actions / Static Analysis

Variable 'groupedList' implicitly has an 'any[]' type.

if (existingEntry) {
if (!existingEntry.dictionaries.includes(dictionary)) {
existingEntry.dictionaries.push(dictionary);
}
} else {
groupedList.push({
pronunciation,
dictionaries: [dictionary],
});
}
}
}

return groupedList;

Check failure on line 494 in ext/js/dictionary/dictionary-data-util.js

View workflow job for this annotation

GitHub Actions / Static Analysis

Type '{ pronunciation: GroupedPronunciation; dictionaries: string[]; }[]' is not assignable to type 'PronunciationsInSeveralDictionaries[]'.
}
54 changes: 28 additions & 26 deletions ext/js/display/display-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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]);

Check failure on line 681 in ext/js/display/display-generator.js

View workflow job for this annotation

GitHub Actions / Static Analysis

Type 'DictionaryGroupedPronunciations' is missing the following properties from type 'GroupedPronunciation': pronunciation, terms, reading, exclusiveTerms, exclusiveReadings

container.appendChild(node);

return container;
}


/**
* @param {import('dictionary-data-util').GroupedPronunciation} details
* @returns {HTMLElement}
Expand Down Expand Up @@ -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<import('japanese-util').PitchCategory>} */
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);
Expand Down
22 changes: 19 additions & 3 deletions ext/templates-display.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,25 @@
</span></span></template>

<!-- Pitch accent -->
<template id="pronunciation-group-template"><li class="pronunciation-group"><span class="pronunciation-group-tag-list tag-list"></span><ul class="pronunciation-list"></ul></li></template>
<template id="pronunciation-disambiguation-template"><span class="pronunciation-disambiguation"></span></template>
<template id="pronunciation-template"><li class="pronunciation"><span class="pronunciation-tag-list tag-list"></span><span class="pronunciation-disambiguation-list"></span><span class="pronunciation-representation-list"><span class="pronunciation-text-container"></span><span class="pronunciation-downstep-notation-container"></span><span class="pronunciation-graph-container"></span></span></li></template>
<template id="pronunciation-group-template">
<li class="pronunciation-group">
<ul class="pronunciation-list"></ul>
<span class="pronunciation-group-tag-list tag-list"></span>
</li>
</template>
<template id="pronunciation-disambiguation-template">
<span class="pronunciation-disambiguation"></span>
</template>
<template id="pronunciation-template">
<li class="pronunciation">
<span class="pronunciation-tag-list tag-list"></span>
<span class="pronunciation-disambiguation-list"></span>
<span class="pronunciation-representation-list">
<span class="pronunciation-text-container"></span>
<span class="pronunciation-downstep-notation-container"></span>
<span class="pronunciation-graph-container"></span></span>
</li>
</template>

<!-- Kanji entry -->
<template id="kanji-entry-template" data-remove-whitespace-text="true"><div class="entry" data-type="kanji">
Expand Down
5 changes: 5 additions & 0 deletions types/ext/dictionary-data-util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ export type DictionaryGroupedPronunciations = {
pronunciations: GroupedPronunciation[];
};

export type PronunciationsInSeveralDictionaries = {
dictionaries: string[];
pronunciation: DictionaryGroupedPronunciations;
};

export type TagGroup = {
tag: Dictionary.Tag;
headwordIndices: number[];
Expand Down

0 comments on commit 979e8eb

Please sign in to comment.