Created
December 2, 2020 08:08
-
-
Save birtles/584cdbf3a7673e7131d4cdc5889b7656 to your computer and use it in GitHub Desktop.
Patch for applying misc labels to group headings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/popup.ts b/src/popup.ts | |
index ca9d850..12b70da 100644 | |
--- a/src/popup.ts | |
+++ b/src/popup.ts | |
@@ -1,4 +1,8 @@ | |
-import { KanjiResult, NameTranslation } from '@birchill/hikibiki-data'; | |
+import { | |
+ KanjiResult, | |
+ MiscType, | |
+ NameTranslation, | |
+} from '@birchill/hikibiki-data'; | |
import { countMora, moraSubstring } from '@birchill/normal-jp'; | |
import { | |
@@ -461,44 +465,50 @@ function renderDefinitions(entry: WordResult, options: PopupOptions) { | |
// Group heading | |
const groupHeading = document.createElement('p'); | |
groupHeading.classList.add('w-group-head'); | |
- if (group.pos) { | |
- for (const pos of group.pos) { | |
- const posSpan = document.createElement('span'); | |
- posSpan.classList.add('w-pos', 'tag'); | |
- posSpan.lang = getLangTag(); | |
- posSpan.textContent = | |
- browser.i18n.getMessage(`pos_label_${pos}`) || pos; | |
- groupHeading.append(posSpan); | |
- } | |
- } else { | |
+ for (const pos of group.pos) { | |
+ const posSpan = document.createElement('span'); | |
+ posSpan.classList.add('w-pos', 'tag'); | |
+ posSpan.lang = getLangTag(); | |
+ posSpan.textContent = | |
+ browser.i18n.getMessage(`pos_label_${pos}`) || pos; | |
+ groupHeading.append(posSpan); | |
+ } | |
+ | |
+ for (const misc of group.misc) { | |
+ const miscSpan = document.createElement('span'); | |
+ miscSpan.classList.add('w-misc', 'tag'); | |
+ miscSpan.lang = getLangTag(); | |
+ miscSpan.textContent = | |
+ browser.i18n.getMessage(`misc_label_${misc}`) || misc; | |
+ groupHeading.append(miscSpan); | |
+ } | |
+ | |
+ if (!group.pos.length && !group.misc.length) { | |
const posSpan = document.createElement('span'); | |
posSpan.classList.add('w-pos', 'tag'); | |
posSpan.textContent = '-'; | |
groupHeading.append(posSpan); | |
} | |
+ | |
definitionsDiv.append(groupHeading); | |
// Group items | |
const definitionList = document.createElement('ol'); | |
definitionList.start = startIndex; | |
- let previousSense: ExtendedSense | undefined; | |
for (const sense of group.senses) { | |
const listItem = document.createElement('li'); | |
- listItem.append(renderSense(sense, options, previousSense)); | |
+ listItem.append(renderSense(sense, options)); | |
definitionList.append(listItem); | |
- previousSense = sense; | |
startIndex++; | |
} | |
definitionsDiv.append(definitionList); | |
} | |
} else { | |
const definitionList = document.createElement('ol'); | |
- let previousSense: ExtendedSense | undefined; | |
for (const sense of entry.s) { | |
const listItem = document.createElement('li'); | |
- listItem.append(renderSense(sense, options, previousSense)); | |
+ listItem.append(renderSense(sense, options)); | |
definitionList.append(listItem); | |
- previousSense = sense; | |
} | |
definitionsDiv.append(definitionList); | |
} | |
@@ -509,6 +519,7 @@ function renderDefinitions(entry: WordResult, options: PopupOptions) { | |
interface PosGroup { | |
pos: Array<PartOfSpeech>; | |
+ misc: Array<MiscType>; | |
senses: Array<ExtendedSense>; | |
} | |
@@ -552,7 +563,7 @@ function groupSenses(senses: Array<ExtendedSense>): Array<PosGroup> { | |
// If there was no match, start a new group | |
const thisPos = sense.pos && sense.pos.length ? sense.pos[0] : undefined; | |
const pos = thisPos ? [thisPos] : []; | |
- groups.push({ pos, senses: [dropPos(sense, thisPos)] }); | |
+ groups.push({ pos, misc: [], senses: [dropPos(sense, thisPos)] }); | |
previousPos = thisPos; | |
} | |
} | |
@@ -580,13 +591,37 @@ function groupSenses(senses: Array<ExtendedSense>): Array<PosGroup> { | |
} | |
} | |
+ // Hoist any common misc readings | |
+ for (const group of groups) { | |
+ let commonMisc = group.senses[0].misc; | |
+ if (!commonMisc) { | |
+ continue; | |
+ } | |
+ | |
+ for (const sense of group.senses.slice(1)) { | |
+ commonMisc = commonMisc.filter( | |
+ (misc) => sense.misc && sense.misc.includes(misc) | |
+ ); | |
+ if (!commonMisc.length) { | |
+ break; | |
+ } | |
+ } | |
+ | |
+ if (commonMisc.length) { | |
+ group.misc = commonMisc; | |
+ group.senses = group.senses.map((sense) => ({ | |
+ ...sense, | |
+ misc: sense.misc?.filter((misc) => !commonMisc!.includes(misc)), | |
+ })); | |
+ } | |
+ } | |
+ | |
return groups; | |
} | |
function renderSense( | |
sense: ExtendedSense, | |
- options: PopupOptions, | |
- previousSense?: ExtendedSense | |
+ options: PopupOptions | |
): string | DocumentFragment { | |
const fragment = document.createDocumentFragment(); | |
@@ -620,25 +655,13 @@ function renderSense( | |
} | |
if (sense.misc) { | |
- if ( | |
- previousSense && | |
- previousSense.misc && | |
- JSON.stringify(previousSense.misc) === JSON.stringify(sense.misc) | |
- ) { | |
+ for (const misc of sense.misc) { | |
const miscSpan = document.createElement('span'); | |
miscSpan.classList.add('w-misc', 'tag'); | |
- miscSpan.textContent = '〃'; | |
miscSpan.lang = getLangTag(); | |
+ miscSpan.textContent = | |
+ browser.i18n.getMessage(`misc_label_${misc}`) || misc; | |
fragment.append(miscSpan); | |
- } else { | |
- for (const misc of sense.misc) { | |
- const miscSpan = document.createElement('span'); | |
- miscSpan.classList.add('w-misc', 'tag'); | |
- miscSpan.lang = getLangTag(); | |
- miscSpan.textContent = | |
- browser.i18n.getMessage(`misc_label_${misc}`) || misc; | |
- fragment.append(miscSpan); | |
- } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment