From b5f877afa9e3d116a69c60013e34ffc4665dc382 Mon Sep 17 00:00:00 2001 From: Santosh-Bogi Date: Tue, 10 May 2022 12:15:10 +0530 Subject: [PATCH] Adding-Punctuation-For-Every-Suggestion --- README.md | 2 ++ src/index.tsx | 2 ++ src/interfaces/Props.ts | 7 +++++++ src/util/suggestions-util.ts | 33 ++++++++++++++++++++++++++++----- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b43623c..d481b0a 100644 --- a/README.md +++ b/README.md @@ -183,6 +183,7 @@ const data = await getTransliterateSuggestions( numOptions: 5, // number of suggestions to fetch showCurrentWordAsLastSuggestion: true, // add the word as the last suggestion lang: "hi", // target language + punctuationsToBeHandledAtEndOfWord: [",", '"'] // some punctuations may not be properly handled by transliterate pass them here }, ); ``` @@ -209,6 +210,7 @@ For a full example, take a look at the `example` folder | triggerKeys | | `KEY_SPACE, KEY_ENTER, KEY_TAB, KEY_RETURN` | Keys which when pressed, input the current selection to the textbox | | insertCurrentSelectionOnBlur | | `true` | Should the current selection be inserted when `blur` event occurs | | showCurrentWordAsLastSuggestion | | `true` | Show current input as the last option in the suggestion box | +| punctuationsToBeHandledAtEndOfWord | | `[',', '"']` | Last punctation mark will be truncated and added back when the user types the punctation | ### Supported Languages diff --git a/src/index.tsx b/src/index.tsx index 6931a80..dcd9fb2 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -39,6 +39,7 @@ export const ReactTransliterate = ({ ], insertCurrentSelectionOnBlur = true, showCurrentWordAsLastSuggestion = true, + punctuationsToBeHandledAtEndOfWord = [",", '"'], enabled = true, ...rest }: ReactTransliterateProps): JSX.Element => { @@ -110,6 +111,7 @@ export const ReactTransliterate = ({ numOptions, showCurrentWordAsLastSuggestion, lang, + punctuationsToBeHandledAtEndOfWord, }); setOptions(data); }; diff --git a/src/interfaces/Props.ts b/src/interfaces/Props.ts index 0081b24..88751c6 100644 --- a/src/interfaces/Props.ts +++ b/src/interfaces/Props.ts @@ -87,6 +87,13 @@ export interface ReactTransliterateProps */ showCurrentWordAsLastSuggestion?: boolean; + /** + * Last punctation mark will be truncated and added back when the user types the punctation + * @type string[] + */ + punctuationsToBeHandledAtEndOfWord?: string[]; + + /** * Control whether suggestions should be shown * @type boolean diff --git a/src/util/suggestions-util.ts b/src/util/suggestions-util.ts index 2500f5e..df154a1 100644 --- a/src/util/suggestions-util.ts +++ b/src/util/suggestions-util.ts @@ -4,35 +4,58 @@ type Config = { numOptions?: number; showCurrentWordAsLastSuggestion?: boolean; lang?: Language; + punctuationsToBeHandledAtEndOfWord?: string[]; }; export const getTransliterateSuggestions = async ( word: string, config?: Config, ): Promise => { - const { numOptions, showCurrentWordAsLastSuggestion, lang } = config || { + const { + numOptions, + showCurrentWordAsLastSuggestion, + lang, + punctuationsToBeHandledAtEndOfWord, + } = config || { numOptions: 5, showCurrentWordAsLastSuggestion: true, lang: "hi", + punctuationsToBeHandledAtEndOfWord: [",", '"'], }; // fetch suggestion from api // const url = `https://www.google.com/inputtools/request?ime=transliteration_en_${lang}&num=5&cp=0&cs=0&ie=utf-8&oe=utf-8&app=jsapi&text=${word}`; + let punctuation = word.slice(-1); + if (punctuationsToBeHandledAtEndOfWord?.includes(punctuation)) { + word = word.slice(0, -1); + } else { + punctuation = ""; + } + const url = `https://inputtools.google.com/request?text=${word}&itc=${lang}-t-i0-und&num=${numOptions}&cp=0&cs=1&ie=utf-8&oe=utf-8&app=demopage`; try { const res = await fetch(url); const data = await res.json(); + let suggestions; if (data && data[0] === "SUCCESS") { - const found = showCurrentWordAsLastSuggestion + suggestions = showCurrentWordAsLastSuggestion ? [...data[1][0][1], word] : data[1][0][1]; - return found; } else { if (showCurrentWordAsLastSuggestion) { - return [word]; + suggestions = [word]; } - return []; + suggestions = []; } + + // doublequotes are returned as " by google-transliterate + suggestions = suggestions.map((suggestion: string) => + suggestion.replace(/"/g, '"'), + ); + + return punctuation + ? suggestions.map((suggestion: string) => suggestion + punctuation) + : suggestions; } catch (e) { // catch error console.error("There was an error with transliteration", e);