Skip to content

Commit

Permalink
Added 'isEditorContentWithinLimit' utility function (#968)
Browse files Browse the repository at this point in the history
* Added 'isEditorContentWithinLimit' utility function

* Used isNotPresent instead of custom isNilOrEmpty

---------

Co-authored-by: Roshan R <[email protected]>
  • Loading branch information
roshanrajeev and Roshan R authored Dec 19, 2023
1 parent c6ca916 commit 67d5b99
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -19,6 +19,7 @@ export {
EditorContent,
Menu,
isEditorEmpty,
isEditorContentWithinLimit,
substituteVariables,
FormikEditor,
Attachments,
Expand Down
19 changes: 14 additions & 5 deletions src/utils/common.js
Original file line number Diff line number Diff line change
@@ -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;
};
2 changes: 2 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 67d5b99

Please sign in to comment.