Skip to content

Commit

Permalink
display inflection rule descriptions (#1000)
Browse files Browse the repository at this point in the history
* load descriptions in deinflector

* description functions in deinflectors

* show descriptions in title

* use toaster

* use names without internal

* css lint

* reformat transform descriptors

* fix merge errors

* done?

* rename method
  • Loading branch information
StefanVukovic99 authored Jun 3, 2024
1 parent d2fce50 commit 7955fc8
Show file tree
Hide file tree
Showing 26 changed files with 1,188 additions and 801 deletions.
3 changes: 3 additions & 0 deletions ext/css/display.css
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,9 @@ button.action-button:active {
.inflection-source-icon[data-inflection-source='both']::after {
content: '🧩📖';
}
.inflection[title] {
cursor: pointer;
}


/* Headwords */
Expand Down
43 changes: 43 additions & 0 deletions ext/data/templates/anki-field-templates-upgrade-v38.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{{<<<<<<<}}
{{#*inline "conjugation"}}
{{~#if (op ">" definition.inflectionRuleChainCandidates.length 0)~}}
{{~set "multiple" false~}}
{{~#if (op ">" definition.inflectionRuleChainCandidates.length 1)~}}
{{~set "multiple" true~}}
{{~/if~}}
{{~#if (get "multiple")~}}<ul>{{/if~}}
{{~#each definition.inflectionRuleChainCandidates~}}
{{~#if (op ">" inflectionRules.length 0)~}}
{{~#if (get "multiple")~}}<li>{{/if~}}
{{~#each inflectionRules~}}
{{~#if (op ">" @index 0)}} « {{/if~}}
{{.}}
{{~/each~}}
{{~#if (get "multiple")~}}</li>{{/if~}}
{{~/if~}}
{{~/each~}}
{{~#if (get "multiple")~}}</ul>{{/if~}}
{{~/if~}}
{{/inline}}
{{=======}}
{{#*inline "conjugation"}}
{{~#if (op ">" definition.inflectionRuleChainCandidates.length 0)~}}
{{~set "multiple" false~}}
{{~#if (op ">" definition.inflectionRuleChainCandidates.length 1)~}}
{{~set "multiple" true~}}
{{~/if~}}
{{~#if (get "multiple")~}}<ul>{{/if~}}
{{~#each definition.inflectionRuleChainCandidates~}}
{{~#if (op ">" inflectionRules.length 0)~}}
{{~#if (get "multiple")~}}<li>{{/if~}}
{{~#each inflectionRules~}}
{{~#if (op ">" @index 0)}} « {{/if~}}
{{name}}
{{~/each~}}
{{~#if (get "multiple")~}}</li>{{/if~}}
{{~/if~}}
{{~/each~}}
{{~#if (get "multiple")~}}</ul>{{/if~}}
{{~/if~}}
{{/inline}}
{{>>>>>>>}}
2 changes: 1 addition & 1 deletion ext/data/templates/default-anki-field-templates.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@
{{~#if (get "multiple")~}}<li>{{/if~}}
{{~#each inflectionRules~}}
{{~#if (op ">" @index 0)}} « {{/if~}}
{{.}}
{{name}}
{{~/each~}}
{{~#if (get "multiple")~}}</li>{{/if~}}
{{~/if~}}
Expand Down
9 changes: 9 additions & 0 deletions ext/js/data/options-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ export class OptionsUtil {
this._updateVersion35,
this._updateVersion36,
this._updateVersion37,
this._updateVersion38,
];
/* eslint-enable @typescript-eslint/unbound-method */
if (typeof targetVersion === 'number' && targetVersion < result.length) {
Expand Down Expand Up @@ -1304,6 +1305,14 @@ export class OptionsUtil {
await this._applyAnkiFieldTemplatesPatch(options, '/data/templates/anki-field-templates-upgrade-v37.handlebars');
}

/**
* - Updated `conjugation` handlebars for new inflection chain format.
* @type {import('options-util').UpdateFunction}
*/
async _updateVersion38(options) {
await this._applyAnkiFieldTemplatesPatch(options, '/data/templates/anki-field-templates-upgrade-v38.handlebars');
}

/**
* @param {string} url
* @returns {Promise<chrome.tabs.Tab>}
Expand Down
8 changes: 5 additions & 3 deletions ext/js/display/display-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,14 +401,16 @@ export class DisplayGenerator {
}

/**
* @param {string} inflection
* @param {import('dictionary').InflectionRule} inflection
* @returns {DocumentFragment}
*/
_createTermInflection(inflection) {
const {name, description} = inflection;
const fragment = this._templates.instantiateFragment('inflection');
const node = this._querySelector(fragment, '.inflection');
this._setTextContent(node, inflection);
node.dataset.reason = inflection;
this._setTextContent(node, name);
if (description) { node.title = description; }
node.dataset.reason = name;
return fragment;
}

Expand Down
30 changes: 30 additions & 0 deletions ext/js/display/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ export class Display extends EventDispatcher {
this._contentTextScanner = null;
/** @type {?import('./display-notification.js').DisplayNotification} */
this._tagNotification = null;
/** @type {?import('./display-notification.js').DisplayNotification} */
this._inflectionNotification = null;
/** @type {HTMLElement} */
this._footerNotificationContainer = querySelectorNotNull(document, '#content-footer');
/** @type {OptionToggleHotkeyHandler} */
Expand All @@ -181,6 +183,8 @@ export class Display extends EventDispatcher {
/** @type {(event: MouseEvent) => void} */
this._onTagClickBind = this._onTagClick.bind(this);
/** @type {(event: MouseEvent) => void} */
this._onInflectionClickBind = this._onInflectionClick.bind(this);
/** @type {(event: MouseEvent) => void} */
this._onMenuButtonClickBind = this._onMenuButtonClick.bind(this);
/** @type {(event: import('popup-menu').MenuCloseEvent) => void} */
this._onMenuButtonMenuCloseBind = this._onMenuButtonMenuClose.bind(this);
Expand Down Expand Up @@ -1023,6 +1027,14 @@ export class Display extends EventDispatcher {
this._showTagNotification(node);
}

/**
* @param {MouseEvent} e
*/
_onInflectionClick(e) {
const node = /** @type {HTMLElement} */ (e.currentTarget);
this._showInflectionNotification(node);
}

/**
* @param {MouseEvent} e
*/
Expand Down Expand Up @@ -1085,6 +1097,21 @@ export class Display extends EventDispatcher {
this._tagNotification.open();
}

/**
* @param {HTMLSpanElement} inflectionNode
*/
_showInflectionNotification(inflectionNode) {
const description = inflectionNode.title;
if (!description || !(inflectionNode instanceof HTMLSpanElement)) { return; }

if (this._inflectionNotification === null) {
this._inflectionNotification = this.createNotification(true);
}

this._inflectionNotification.setContent(description);
this._inflectionNotification.open();
}

/**
* @param {boolean} animate
*/
Expand Down Expand Up @@ -1797,6 +1824,9 @@ export class Display extends EventDispatcher {
for (const node of entry.querySelectorAll('.headword-kanji-link')) {
eventListeners.addEventListener(node, 'click', this._onKanjiLookupBind);
}
for (const node of entry.querySelectorAll('.inflection[data-reason]')) {
eventListeners.addEventListener(node, 'click', this._onInflectionClickBind);
}
for (const node of entry.querySelectorAll('.tag-label')) {
eventListeners.addEventListener(node, 'click', this._onTagClickBind);
}
Expand Down
14 changes: 7 additions & 7 deletions ext/js/language/de/german-transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,43 +64,43 @@ export const germanTransforms = {
isDictionaryForm: true,
},
},
transforms: [
{
transforms: {
'nominalization': {
name: 'nominalization',
description: 'Noun formed from a verb',
rules: [
suffixInflection('ung', 'en', [], []),
suffixInflection('lung', 'eln', [], []),
],
},
{
'-bar': {
name: '-bar',
description: '-able adjective from a verb',
rules: [
suffixInflection('bar', 'en', [], ['v']),
suffixInflection('bar', 'n', [], ['v']), // Lieferbar
],
},
{
'negative': {
name: 'negative',
description: 'Negation',
rules: [
prefixInflection('un', '', [], ['adj']),
],
},
{
'separated prefix': {
name: 'separated prefix',
description: 'Separable prefix',
rules: [
...separatedPrefixInflections,
],
},
{
'zu-infinitive': {
name: 'zu-infinitive',
description: 'zu-infinitive',
rules: [
...zuInfinitiveInflections,
],
},
],
},
};
36 changes: 18 additions & 18 deletions ext/js/language/en/english-transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ export const englishTransforms = {
isDictionaryForm: true,
},
},
transforms: [
{
transforms: {
'plural': {
name: 'plural',
description: 'Plural form of a noun',
rules: [
Expand All @@ -160,53 +160,53 @@ export const englishTransforms = {
suffixInflection('ves', 'f', ['np'], ['ns']),
],
},
{
'possessive': {
name: 'possessive',
description: 'Possessive form of a noun',
rules: [
suffixInflection('\'s', '', ['n'], ['n']),
suffixInflection('s\'', 's', ['n'], ['n']),
],
},
{
'past': {
name: 'past',
description: 'Simple past tense of a verb',
rules: [
...pastSuffixInflections,
...createPhrasalVerbInflectionsFromSuffixInflections(pastSuffixInflections),
],
},
{
'ing': {
name: 'ing',
description: 'Present participle of a verb',
rules: [
...ingSuffixInflections,
...createPhrasalVerbInflectionsFromSuffixInflections(ingSuffixInflections),
],
},
{
'3rd pers. sing. pres': {
name: '3rd pers. sing. pres',
description: 'Third person singular present tense of a verb',
rules: [
...thirdPersonSgPresentSuffixInflections,
...createPhrasalVerbInflectionsFromSuffixInflections(thirdPersonSgPresentSuffixInflections),
],
},
{
'interposed object': {
name: 'interposed object',
description: 'Phrasal verb with interposed object',
rules: [
phrasalVerbInterposedObjectRule,
],
},
{
'archaic': {
name: 'archaic',
description: 'Archaic form of a word',
rules: [
suffixInflection('\'d', 'ed', ['v'], ['v']),
],
},
{
'adverb': {
name: 'adverb',
description: 'Adverb form of an adjective',
rules: [
Expand All @@ -215,7 +215,7 @@ export const englishTransforms = {
suffixInflection('ly', 'le', ['adv'], ['adj']), // 'humbly'
],
},
{
'comparative': {
name: 'comparative',
description: 'Comparative form of an adjective',
rules: [
Expand All @@ -225,7 +225,7 @@ export const englishTransforms = {
...doubledConsonantInflection('bdgmnt', 'er', ['adj'], ['adj']),
],
},
{
'superlative': {
name: 'superlative',
description: 'Superlative form of an adjective',
rules: [
Expand All @@ -235,14 +235,14 @@ export const englishTransforms = {
...doubledConsonantInflection('bdgmnt', 'est', ['adj'], ['adj']),
],
},
{
'dropped g': {
name: 'dropped g',
description: 'Dropped g in -ing form of a verb',
rules: [
suffixInflection('in\'', 'ing', ['v'], ['v']),
],
},
{
'-y': {
name: '-y',
description: 'Adjective formed from a verb or noun',
rules: [
Expand All @@ -251,34 +251,34 @@ export const englishTransforms = {
...doubledConsonantInflection('glmnprst', 'y', [], ['n', 'v']), // 'baggy', 'saggy'
],
},
{
'un-': {
name: 'un-',
description: 'Negative form of an adjective, adverb, or verb',
rules: [
prefixInflection('un', '', ['adj', 'adv', 'v'], ['adj', 'adv', 'v']),
],
},
{
'going-to future': {
name: 'going-to future',
description: 'Going-to future tense of a verb',
rules: [
prefixInflection('going to ', '', ['v'], ['v']),
],
},
{
'will future': {
name: 'will future',
description: 'Will-future tense of a verb',
rules: [
prefixInflection('will ', '', ['v'], ['v']),
],
},
{
'imperative negative': {
name: 'imperative negative',
description: 'Negative imperative form of a verb',
rules: [
prefixInflection('don\'t ', '', ['v'], ['v']),
prefixInflection('do not ', '', ['v'], ['v']),
],
},
],
},
};
Loading

0 comments on commit 7955fc8

Please sign in to comment.