Skip to content

Commit

Permalink
Add onload word count functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
1-Harshit committed Nov 17, 2023
1 parent e042475 commit ceda2b5
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/views/editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,28 @@ import {
import WordCountCard from "components/card/WordCountCard";
import EditorOptions from "components/editor/EditorOptions";
import EditorArea from "components/editor/EditorArea";
import { useState } from "react";
import { useEffect, useState } from "react";

export default function EditorHome() {
const [wordCount, setWrordCount] = useState(0);

const setWrordCountFromText = (text: string) => {
text = text.trim();
const value = text === "" ? 0 : text.split(/\s+/).length;
setWrordCount(value);
};

const handleContentChange = (e: React.SyntheticEvent) => {
const target = e.target as HTMLDivElement;
console.log(target.innerHTML);
const text = target.innerText.trim();
if(text === "") setWrordCount(0);
else setWrordCount(text.split(/\s+/).length);
setWrordCountFromText(target.innerText);
};

useEffect(() => {
const target = document.getElementById("editor-main") as HTMLDivElement;
setWrordCountFromText(target.innerText);
}, []);

return (
<Box pt={{ base: "130px", md: "80px", xl: "80px" }}>
<Grid
Expand Down

0 comments on commit ceda2b5

Please sign in to comment.