Skip to content

Commit

Permalink
Revert "♻️ refactor: calculateFleschReadingFromText"
Browse files Browse the repository at this point in the history
This reverts commit 2c511d0.
  • Loading branch information
jnaraujo committed Dec 14, 2023
1 parent e098a3a commit dac7856
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 32 deletions.
41 changes: 41 additions & 0 deletions src/components/Editor/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import TextAlign from "@tiptap/extension-text-align";
import Placeholder from "@tiptap/extension-placeholder";
import History from "@tiptap/extension-history";

import * as ReadingEase from "@/libs/ReadingEase";
import { TextAnalysisHL } from "./TextAnalysisHL/textAnalysisHL";
import { IEase } from "@/store/readingStore";
import { splitPhrases } from "./TextAnalysisHL/helper";
import { calculateFleschEase } from "@/libs/ReadingEase/helper";

export const EditorExtensions = [
Heading,
Expand All @@ -39,3 +43,40 @@ export const EditorExtensions = [
types: ["heading", "paragraph"],
}),
];

export const handleContentEase = (
text: string,
setEase: (obj: IEase) => void,
) => {
const phrases = splitPhrases(text);

let textAnalyses = {
result: 0,
syllables: 0,
words: 0,
sentences: 0,
};
for (let i = 0; i < phrases.length; i++) {
const phraseAnalyses = ReadingEase.calculateFleschReading(phrases[i]);
textAnalyses.syllables += phraseAnalyses.syllables;
textAnalyses.words += phraseAnalyses.words;
textAnalyses.sentences += phraseAnalyses.sentences;
}

setEase({
index: calculateFleschEase(
textAnalyses.words,
textAnalyses.sentences,
textAnalyses.syllables,
),
syllables: textAnalyses.syllables,
words: textAnalyses.words,
sentences: textAnalyses.sentences,
});
};

export const calculateReadingEase = (text: string) => {
const textAnalyses = ReadingEase.calculateFleschReading(text);

return textAnalyses.result;
};
13 changes: 2 additions & 11 deletions src/components/Editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import { useEffect, useRef, useState } from "react";
import { useEditor, EditorContent } from "@tiptap/react";
import { useWindowSize } from "react-use";
import { useReadingStore } from "@/store/readingStore";
import { EditorExtensions } from "./helper";
import { EditorExtensions, handleContentEase } from "./helper";
import { useContentStore } from "@/store/contentStore";
import { useConfigStore } from "@/store/configStore";
import clsx from "clsx";
import { useStatsStore } from "@/store/statsStore";
import { BubbleMenu } from "./BubbleMenu";
import { cn } from "@/libs/utils";
import Toolbar from "./Toolbar";
import { calculateFleschReadingFromText } from "@/libs/ReadingEase";

type ComponentPropsType = {
html?: string;
Expand Down Expand Up @@ -46,19 +45,11 @@ export default function TextEditor({
state.editor.commands.setContent(html);
return;
}
const text = state.editor.getText();
const ease = calculateFleschReadingFromText(text);

setEase(ease);
handleContentEase(state.editor.getText(), setEase);
},
onUpdate: (state) => {
setContent(state.editor.getHTML());

const text = state.editor.getText();
const ease = calculateFleschReadingFromText(text);

setEase(ease);

if (startedWritingAt.current === 0) {
startedWritingAt.current = Date.now();
}
Expand Down
31 changes: 10 additions & 21 deletions src/libs/ReadingEase/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,18 @@ export function calculateFleschReading(phrase: string) {
}

export function calculateFleschReadingFromText(text: string) {
const phrases = splitPhrases(text);
const words = getWords(text);
const totalSyllables = countSyllables(words);
const sentences = splitPhrases(text).length;

const result = {
index: 0,
syllables: 0,
words: 0,
sentences: 0,
return {
words: words.length,
sentences: sentences,
syllables: totalSyllables,
result: calculateResult(
calculateFleschEase(words.length, sentences, totalSyllables),
),
};

for (let i = 0; i < phrases.length; i++) {
const phraseAnalyses = calculateFleschReading(phrases[i]);
result.syllables += phraseAnalyses.syllables;
result.words += phraseAnalyses.words;
result.sentences += phraseAnalyses.sentences;
}

result.index = calculateFleschEase(
result.words,
result.sentences,
result.syllables,
);

return result;
}

export function easeToLabel(ease: number) {
Expand Down

0 comments on commit dac7856

Please sign in to comment.