Skip to content

Commit

Permalink
fix: fix sanitize function to return pure text and caching the 18n data
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarwbr committed Mar 25, 2024
1 parent 28e5f5f commit b6596a1
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
1 change: 0 additions & 1 deletion example/src/ExampleInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ const ExampleInput = () => {
onEnter={text => {
console.log("enter", text);
}}
language="fr"
placeholder="Type a message"
keepOpened
disableRecent
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-input-emoji",
"version": "5.6.8",
"version": "5.6.9",
"description": "A React input with an option to add an emoji with language support.",
"homepage": "https://cesarwbr.github.io/react-input-emoji/",
"author": "cesarwbr",
Expand Down
2 changes: 0 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ export default {
file: pkg.main,
format: 'cjs',
sourcemap: true,
inlineDynamicImports: true,
},
{
file: pkg.module,
format: 'es',
sourcemap: true,
inlineDynamicImports: true,
},
],
plugins: [
Expand Down
14 changes: 12 additions & 2 deletions src/components/emoji-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import React, { memo, useEffect, useMemo, useState } from "react";
import Picker from "@emoji-mart/react";

const EMOJI_MART_DATA_URL = "https://cdn.jsdelivr.net/npm/@emoji-mart/data";
const cacheI18n = {};

/**
* @typedef {object} Props
* @property {'light' | 'dark' | 'auto'} theme
Expand Down Expand Up @@ -46,11 +49,17 @@ function EmojiPicker(props) {

useEffect(() => {
if (!language) {
if (cacheI18n.en) {
setI18n(cacheI18n.en);
return;
}

// @ts-ignore
fetch(`https://cdn.jsdelivr.net/npm/@emoji-mart/data/i18n/en.json`)
fetch(`${EMOJI_MART_DATA_URL}/i18n/en.json`)
.then(async data => {
const translations = await data.json();
setI18n(translations);
cacheI18n.en = translations;
})
.catch(error => {
console.error("Failed to load translations:", error);
Expand All @@ -59,10 +68,11 @@ function EmojiPicker(props) {
}

// @ts-ignore
fetch(`https://cdn.jsdelivr.net/npm/@emoji-mart/data/i18n/${language}.json`)
fetch(`${EMOJI_MART_DATA_URL}/i18n/${language}.json`)
.then(async data => {
const translations = await data.json();
setI18n(translations);
cacheI18n[language] = translations;
})
.catch(error => {
console.error("Failed to load translations:", error);
Expand Down
5 changes: 4 additions & 1 deletion src/hooks/use-sanitize.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,8 @@ export function replaceAllHtmlToString(html, shouldReturn) {
// remove all ↵ for safari
text = text.replace(/\n/gi, "");

return text;
const tempContainer = document.createElement("div");
tempContainer.innerHTML = text;

return tempContainer.innerText || "";
}

0 comments on commit b6596a1

Please sign in to comment.