Created
December 2, 2020 08:20
-
-
Save birtles/a01ff073bc71a8904ec26f4be31092e0 to your computer and use it in GitHub Desktop.
Patch to display common misc labels at the top
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..1272482 100644 | |
--- a/src/popup.ts | |
+++ b/src/popup.ts | |
@@ -449,6 +449,39 @@ function renderDefinitions(entry: WordResult, options: PopupOptions) { | |
if (entry.s.length === 1) { | |
definitionsDiv.append(renderSense(entry.s[0], options)); | |
} else { | |
+ // Extract any misc labels that apply to the whole set of definitions | |
+ let commonMisc = entry.s[0].misc; | |
+ if (commonMisc && commonMisc.length) { | |
+ for (const sense of entry.s.slice(1)) { | |
+ commonMisc = commonMisc.filter( | |
+ (misc) => sense.misc && sense.misc.includes(misc) | |
+ ); | |
+ if (!commonMisc.length) { | |
+ break; | |
+ } | |
+ } | |
+ } | |
+ | |
+ // Remove any common misc labels from the senses | |
+ if (commonMisc && commonMisc.length) { | |
+ entry.s = entry.s.map((sense) => ({ | |
+ ...sense, | |
+ misc: sense.misc?.filter((misc) => !commonMisc!.includes(misc)), | |
+ })); | |
+ | |
+ const miscHeading = document.createElement('p'); | |
+ miscHeading.classList.add('w-group-head'); | |
+ for (const misc of commonMisc) { | |
+ const miscSpan = document.createElement('span'); | |
+ miscSpan.classList.add('w-misc', 'tag'); | |
+ miscSpan.lang = getLangTag(); | |
+ miscSpan.textContent = | |
+ browser.i18n.getMessage(`misc_label_${misc}`) || misc; | |
+ miscHeading.append(miscSpan); | |
+ } | |
+ definitionsDiv.append(miscHeading); | |
+ } | |
+ | |
// Try grouping the definitions by part-of-speech. | |
const posGroups = options.posDisplay !== 'none' ? groupSenses(entry.s) : []; | |
const largestGroupSize = Math.max( | |
@@ -461,16 +494,16 @@ 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); | |
+ } | |
+ | |
+ if (!group.pos.length) { | |
const posSpan = document.createElement('span'); | |
posSpan.classList.add('w-pos', 'tag'); | |
posSpan.textContent = '-'; | |
@@ -481,24 +514,20 @@ function renderDefinitions(entry: WordResult, options: PopupOptions) { | |
// 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); | |
} | |
@@ -585,8 +614,7 @@ function groupSenses(senses: Array<ExtendedSense>): Array<PosGroup> { | |
function renderSense( | |
sense: ExtendedSense, | |
- options: PopupOptions, | |
- previousSense?: ExtendedSense | |
+ options: PopupOptions | |
): string | DocumentFragment { | |
const fragment = document.createDocumentFragment(); | |
@@ -619,27 +647,13 @@ function renderSense( | |
} | |
} | |
- if (sense.misc) { | |
- if ( | |
- previousSense && | |
- previousSense.misc && | |
- JSON.stringify(previousSense.misc) === JSON.stringify(sense.misc) | |
- ) { | |
- const miscSpan = document.createElement('span'); | |
- miscSpan.classList.add('w-misc', 'tag'); | |
- miscSpan.textContent = '〃'; | |
- miscSpan.lang = getLangTag(); | |
- 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); | |
- } | |
- } | |
+ 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); | |
} | |
if (sense.dial) { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment