From 07258ecc35c1a05aa1581a54c9f47a40ce3d76c9 Mon Sep 17 00:00:00 2001 From: StefanVukovic99 Date: Sun, 21 Apr 2024 17:15:08 +0200 Subject: rework text processors (#793) * rework text processors * rename text-preprocessors file * Fix search header left margins on small screens (#839) * Refocuses search input on backspace (#840) Fixes #775. Note that this behavior gets overridden if backspace is set as a shortcut action. * Change hotkey triggering condition to account for IME usage (#837) _isKeyCharacterInput only worked when not using an IME, as inside of an IME when a keydown event is fired, the key is reported as "Process", which does not have a key.length equal to 1. This resulted in hotkeys being triggered while typing, which this commit fixes. --------- Co-authored-by: James Maa Co-authored-by: Kuuuube <61125188+Kuuuube@users.noreply.github.com> Co-authored-by: Andrew Thomas Sartor --- types/ext/language-descriptors.d.ts | 145 +++++++++++++++++++++++++----------- types/ext/language.d.ts | 26 +++---- types/ext/translation-internal.d.ts | 12 ++- 3 files changed, 124 insertions(+), 59 deletions(-) (limited to 'types') diff --git a/types/ext/language-descriptors.d.ts b/types/ext/language-descriptors.d.ts index 6674b28c..37da106c 100644 --- a/types/ext/language-descriptors.d.ts +++ b/types/ext/language-descriptors.d.ts @@ -15,13 +15,17 @@ * along with this program. If not, see . */ -import type {TextPreprocessor, BidirectionalConversionPreprocessor} from './language'; +import type {TextProcessor, BidirectionalConversionPreprocessor} from './language'; import type {LanguageTransformDescriptor} from './language-transformer'; import type {SafeAny} from './core'; export type IsTextLookupWorthyFunction = (text: string) => boolean; -type LanguageDescriptor = { +type LanguageDescriptor< + TIso extends string, + TTextPreprocessorDescriptor extends TextProcessorDescriptor = Record, + TTextPostprocessorDescriptor extends TextProcessorDescriptor = Record, +> = { iso: TIso; name: string; exampleText: string; @@ -32,75 +36,126 @@ type LanguageDescriptor; +type TextProcessorDescriptor = { + [key: string]: TextProcessor; }; type LanguageDescriptorObjectMap = { - [key in keyof AllTextPreprocessors]: LanguageDescriptor; + [key in keyof AllTextProcessors]: LanguageDescriptor< + key, + AllTextProcessors[key] extends {pre: TextProcessorDescriptor} ? AllTextProcessors[key]['pre'] : Record, + AllTextProcessors[key] extends {post: TextProcessorDescriptor} ? AllTextProcessors[key]['post'] : Record + >; }; export type LanguageDescriptorAny = LanguageDescriptorObjectMap[keyof LanguageDescriptorObjectMap]; type CapitalizationPreprocessors = { - capitalizeFirstLetter: TextPreprocessor; - decapitalize: TextPreprocessor; + capitalizeFirstLetter: TextProcessor; + decapitalize: TextProcessor; }; /** - * This is a mapping of the iso tag to all of the preprocessors for that language. + * This is a mapping of the iso tag to all of the text processors for that language. * Any new language should be added to this object. */ -type AllTextPreprocessors = { +type AllTextProcessors = { ar: { - removeArabicScriptDiacritics: TextPreprocessor; + pre: { + removeArabicScriptDiacritics: TextProcessor; + }; }; - de: CapitalizationPreprocessors & { - eszettPreprocessor: BidirectionalConversionPreprocessor; + de: { + pre: CapitalizationPreprocessors & { + eszettPreprocessor: BidirectionalConversionPreprocessor; + }; + }; + el: { + pre: CapitalizationPreprocessors; + }; + en: { + pre: CapitalizationPreprocessors; + }; + es: { + pre: CapitalizationPreprocessors; }; - el: CapitalizationPreprocessors; - en: CapitalizationPreprocessors; - es: CapitalizationPreprocessors; fa: { - removeArabicScriptDiacritics: TextPreprocessor; + pre: { + removeArabicScriptDiacritics: TextProcessor; + }; + }; + fr: { + pre: CapitalizationPreprocessors; + }; + grc: { + pre: CapitalizationPreprocessors & { + removeAlphabeticDiacritics: TextProcessor; + }; }; - fr: CapitalizationPreprocessors; - grc: CapitalizationPreprocessors & { - removeAlphabeticDiacritics: TextPreprocessor; + hu: { + pre: CapitalizationPreprocessors; }; - hu: CapitalizationPreprocessors; - id: CapitalizationPreprocessors; - it: CapitalizationPreprocessors; - la: CapitalizationPreprocessors & { - removeAlphabeticDiacritics: TextPreprocessor; + id: { + pre: CapitalizationPreprocessors; + }; + it: { + pre: CapitalizationPreprocessors; + }; + la: { + pre: CapitalizationPreprocessors & { + removeAlphabeticDiacritics: TextProcessor; + }; }; ja: { - convertHalfWidthCharacters: TextPreprocessor; - convertNumericCharacters: TextPreprocessor; - convertAlphabeticCharacters: TextPreprocessor; - convertHiraganaToKatakana: BidirectionalConversionPreprocessor; - collapseEmphaticSequences: TextPreprocessor<[collapseEmphatic: boolean, collapseEmphaticFull: boolean]>; + pre: { + convertHalfWidthCharacters: TextProcessor; + convertNumericCharacters: TextProcessor; + convertAlphabeticCharacters: TextProcessor; + convertHiraganaToKatakana: BidirectionalConversionPreprocessor; + collapseEmphaticSequences: TextProcessor<[collapseEmphatic: boolean, collapseEmphaticFull: boolean]>; + }; }; km: Record; - pl: CapitalizationPreprocessors; - pt: CapitalizationPreprocessors; - ro: CapitalizationPreprocessors; - ru: CapitalizationPreprocessors & { - yoToE: TextPreprocessor; - removeRussianDiacritics: TextPreprocessor; - }; - sga: CapitalizationPreprocessors & { - removeAlphabeticDiacritics: TextPreprocessor; - }; - sh: CapitalizationPreprocessors; - sq: CapitalizationPreprocessors; - sv: CapitalizationPreprocessors; + pl: { + pre: CapitalizationPreprocessors; + }; + pt: { + pre: CapitalizationPreprocessors; + }; + ro: { + pre: CapitalizationPreprocessors; + }; + ru: { + pre: CapitalizationPreprocessors & { + yoToE: TextProcessor; + removeRussianDiacritics: TextProcessor; + }; + }; + sga: { + pre: CapitalizationPreprocessors & { + removeAlphabeticDiacritics: TextProcessor; + }; + }; + sh: { + pre: CapitalizationPreprocessors; + }; + sq: { + pre: CapitalizationPreprocessors; + }; + sv: { + pre: CapitalizationPreprocessors; + }; th: Record; - tr: CapitalizationPreprocessors; - vi: CapitalizationPreprocessors; + tr: { + pre: CapitalizationPreprocessors; + }; + vi: { + pre: CapitalizationPreprocessors; + }; zh: Record; }; diff --git a/types/ext/language.d.ts b/types/ext/language.d.ts index 8fa6f0e7..c708f6e7 100644 --- a/types/ext/language.d.ts +++ b/types/ext/language.d.ts @@ -15,32 +15,32 @@ * along with this program. If not, see . */ -import type {TextSourceMap} from '../../ext/js/general/text-source-map.js'; import type {LanguageTransformDescriptor} from './language-transformer.js'; -export type TextPreprocessorOptions = T[]; +export type TextProcessorOptions = T[]; -export type TextPreprocessorFunction = (str: string, setting: T, sourceMap: TextSourceMap) => string; +export type TextProcessorFunction = (str: string, setting: T) => string; /** - * Text preprocessors are used during the translation process to create alternate versions of the input text to search for. + * Text pre- and post-processors are used during the translation process to create alternate versions of the input text to search for. * This is helpful when the input text doesn't exactly match the term or expression found in the database. - * When a language has multiple preprocessors, the translator will generate variants of the text by applying all combinations of the preprocessors. + * When a language has multiple processors, the translator will generate variants of the text by applying all combinations of the processors. */ -export type TextPreprocessor = { +export type TextProcessor = { name: string; description: string; - options: TextPreprocessorOptions; - process: TextPreprocessorFunction; + options: TextProcessorOptions; + process: TextProcessorFunction; }; export type BidirectionalPreprocessorOptions = 'off' | 'direct' | 'inverse'; -export type BidirectionalConversionPreprocessor = TextPreprocessor; +export type BidirectionalConversionPreprocessor = TextProcessor; -export type LanguageAndPreprocessors = { +export type LanguageAndProcessors = { iso: string; - textPreprocessors: TextPreprocessorWithId[]; + textPreprocessors?: TextProcessorWithId[]; + textPostprocessors?: TextProcessorWithId[]; }; export type LanguageAndTransforms = { @@ -48,9 +48,9 @@ export type LanguageAndTransforms = { languageTransforms: LanguageTransformDescriptor; }; -export type TextPreprocessorWithId = { +export type TextProcessorWithId = { id: string; - textPreprocessor: TextPreprocessor; + textProcessor: TextProcessor; }; export type LanguageSummary = { diff --git a/types/ext/translation-internal.d.ts b/types/ext/translation-internal.d.ts index 7006221e..00056562 100644 --- a/types/ext/translation-internal.d.ts +++ b/types/ext/translation-internal.d.ts @@ -49,4 +49,14 @@ export type DatabaseDeinflection = { databaseEntries: DictionaryDatabase.TermEntry[]; }; -export type PreprocessorOptionsSpace = Map>; +export type TextProcessorOptionsSpace = Map>; + +export type TextProcessorMap = Map< + string, + { + textPreprocessors: Language.TextProcessorWithId[]; + preprocessorOptionsSpace: TextProcessorOptionsSpace; + textPostprocessors: Language.TextProcessorWithId[]; + postprocessorOptionsSpace: TextProcessorOptionsSpace; + } +>; -- cgit v1.2.3