Fix tag disambiguation (#1556)

* Update display generator to use new data format for tag disambiguation

* Add separator for multiple disambiguations
This commit is contained in:
toasted-nutbread 2021-03-25 20:54:39 -04:00 committed by GitHub
parent 4be5c8fd9f
commit 6af0ee26b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 26 deletions

View File

@ -1823,6 +1823,9 @@ button.footer-notification-close-button {
height: calc(16em / var(--font-size-no-units)); height: calc(16em / var(--font-size-no-units));
background-color: var(--button-content-color); background-color: var(--button-content-color);
} }
.tag-details-disambiguation-list>.tag-details-disambiguation:not(:last-child)::after {
content: var(--disambiguation-separator);
}
/* Overlays */ /* Overlays */

View File

@ -163,7 +163,7 @@ class DisplayGenerator {
return this._templates.instantiate('footer-notification'); return this._templates.instantiate('footer-notification');
} }
createTagFooterNotificationDetails(tagNode) { createTagFooterNotificationDetails(tagNode, dictionaryEntry) {
const node = this._templates.instantiateFragment('footer-notification-tag-details'); const node = this._templates.instantiateFragment('footer-notification-tag-details');
let details = tagNode.dataset.details; let details = tagNode.dataset.details;
@ -173,32 +173,35 @@ class DisplayGenerator {
} }
this._setTextContent(node.querySelector('.tag-details'), details); this._setTextContent(node.querySelector('.tag-details'), details);
let disambiguation = null; if (dictionaryEntry !== null) {
try { const {headwords} = dictionaryEntry;
let a = tagNode.dataset.disambiguation; const disambiguationHeadwords = [];
if (typeof a !== 'undefined') { const {headwords: headwordIndices} = tagNode.dataset;
a = JSON.parse(a); if (typeof headwordIndices === 'string' && headwordIndices.length > 0) {
if (Array.isArray(a)) { disambiguation = a; } for (let headwordIndex of headwordIndices.split(' ')) {
headwordIndex = Number.parseInt(headwordIndex, 10);
if (!Number.isNaN(headwordIndex) && headwordIndex >= 0 && headwordIndex < headwords.length) {
disambiguationHeadwords.push(headwords[headwordIndex]);
}
}
} }
} catch (e) {
// NOP
}
if (disambiguation !== null) { if (disambiguationHeadwords.length > 0 && disambiguationHeadwords.length < headwords.length) {
const disambiguationContainer = node.querySelector('.tag-details-disambiguation-list'); const disambiguationContainer = node.querySelector('.tag-details-disambiguation-list');
const copyAttributes = ['totalExpressionCount', 'matchedExpressionCount', 'unmatchedExpressionCount']; const copyAttributes = ['totalExpressionCount', 'matchedExpressionCount', 'unmatchedExpressionCount'];
for (const attribute of copyAttributes) { for (const attribute of copyAttributes) {
const value = tagNode.dataset[attribute]; const value = tagNode.dataset[attribute];
if (typeof value === 'undefined') { continue; } if (typeof value === 'undefined') { continue; }
disambiguationContainer.dataset[attribute] = value; disambiguationContainer.dataset[attribute] = value;
} }
for (const {expression, reading} of disambiguation) { for (const {term, reading} of disambiguationHeadwords) {
const disambiguationItem = document.createElement('span'); const disambiguationItem = document.createElement('span');
disambiguationItem.className = 'tag-details-disambiguation'; disambiguationItem.className = 'tag-details-disambiguation';
this._appendFurigana(disambiguationItem, expression, reading, (container, text) => { this._appendFurigana(disambiguationItem, term, reading, (container, text) => {
container.appendChild(document.createTextNode(text)); container.appendChild(document.createTextNode(text));
}); });
disambiguationContainer.appendChild(disambiguationItem); disambiguationContainer.appendChild(disambiguationItem);
}
} }
} }

View File

@ -806,7 +806,10 @@ class Display extends EventDispatcher {
this._tagNotification = new DisplayNotification(this._footerNotificationContainer, node); this._tagNotification = new DisplayNotification(this._footerNotificationContainer, node);
} }
const content = this._displayGenerator.createTagFooterNotificationDetails(tagNode); const index = this._getClosestDefinitionIndex(tagNode);
const dictionaryEntry = (index >= 0 && index < this._definitions.length ? this._definitions[index] : null);
const content = this._displayGenerator.createTagFooterNotificationDetails(tagNode, dictionaryEntry);
this._tagNotification.setContent(content); this._tagNotification.setContent(content);
this._tagNotification.open(); this._tagNotification.open();
} }