From 67d5b999ad3a10e4156d67b8c372bce99e4c8765 Mon Sep 17 00:00:00 2001 From: Roshan Rajeev <52269241+roshanrajeev@users.noreply.github.com> Date: Tue, 19 Dec 2023 12:47:47 +0530 Subject: [PATCH] Added 'isEditorContentWithinLimit' utility function (#968) * Added 'isEditorContentWithinLimit' utility function * Used isNotPresent instead of custom isNilOrEmpty --------- Co-authored-by: Roshan R --- src/index.js | 3 ++- src/utils/common.js | 19 ++++++++++++++----- types.d.ts | 2 ++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index 26587751..b58ffe80 100644 --- a/src/index.js +++ b/src/index.js @@ -5,7 +5,7 @@ import { isEmojiSuggestionsMenuActive, } from "components/Editor/utils"; import { substituteVariables } from "components/EditorContent/utils"; -import { isEditorEmpty } from "utils/common"; +import { isEditorEmpty, isEditorContentWithinLimit } from "utils/common"; import Attachments from "./components/Attachments"; import Editor from "./components/Editor"; @@ -19,6 +19,7 @@ export { EditorContent, Menu, isEditorEmpty, + isEditorContentWithinLimit, substituteVariables, FormikEditor, Attachments, diff --git a/src/utils/common.js b/src/utils/common.js index 0e45b697..a060617c 100644 --- a/src/utils/common.js +++ b/src/utils/common.js @@ -1,18 +1,27 @@ -import { isNil, isEmpty } from "ramda"; +import { isNotPresent } from "neetocist"; import { NON_EMPTY_TAGS } from "./constants"; -export const isNilOrEmpty = object => isNil(object) || isEmpty(object); - export const isEditorEmpty = htmlContent => { - if (isNilOrEmpty(htmlContent)) return true; + if (isNotPresent(htmlContent)) return true; if (NON_EMPTY_TAGS.some(tag => htmlContent.includes(tag))) return false; const element = document.createElement("div"); element.innerHTML = htmlContent; - const editorIsEmpty = isNilOrEmpty(element.textContent?.trim()); + const editorIsEmpty = isNotPresent(element.textContent?.trim()); element.remove(); return editorIsEmpty; }; + +export const isEditorContentWithinLimit = (htmlContent, maxLength) => { + if (isNotPresent(htmlContent)) return true; + + const element = document.createElement("div"); + element.innerHTML = htmlContent; + const isLengthWithinLimit = element.textContent.length <= maxLength; + element.remove(); + + return isLengthWithinLimit; +}; diff --git a/types.d.ts b/types.d.ts index 8d56c07e..d40362e9 100644 --- a/types.d.ts +++ b/types.d.ts @@ -163,6 +163,8 @@ export function Menu(props: MenuProps): JSX.Element; export function isEditorEmpty(htmlContent: string | null | undefined): boolean; +export function isEditorContentWithinLimit(htmlContent: string | null | undefined, maxLength: number): boolean; + export function isEditorOverlaysActive(): boolean; export function isEmojiSuggestionsMenuActive(): boolean;