Skip to content

Commit

Permalink
fix: note stats (#743)
Browse files Browse the repository at this point in the history
* fix: note stats

* fix: show word count if note is plain or markdown

Co-authored-by: Johnny Almonte <[email protected]>
  • Loading branch information
johnny243 and johnny243 authored Dec 7, 2021
1 parent de0b04b commit 306441e
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions app/assets/javascripts/components/NotesOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
DisclosureButton,
DisclosurePanel,
} from '@reach/disclosure';
import { SNNote } from '@standardnotes/snjs/dist/@types';
import { SNApplication, SNNote } from '@standardnotes/snjs/dist/@types';
import { WebApplication } from '@/ui_models/application';
import { KeyboardModifier } from '@/services/ioService';
import { FunctionComponent } from 'preact';
Expand All @@ -35,6 +35,20 @@ const DeletePermanentlyButton = ({
</button>
);

const getWordCount = (text: string) => {
if (text.trim().length === 0) {
return 0;
}
return text.split(/\s+/).length;
};

const getParagraphCount = (text: string) => {
if (text.trim().length === 0) {
return 0;
}
return text.replace(/\n$/gm, '').split(/\n/).length;
};

const countNoteAttributes = (text: string) => {
try {
JSON.parse(text);
Expand All @@ -44,12 +58,9 @@ const countNoteAttributes = (text: string) => {
paragraphs: 'N/A',
};
} catch {
const removeTags = text.replace(/<[^>]*>/g," ").replace(/\s+/g, ' ').trim();
text = removeTags;

const characters = text.length;
const words = text.split(" ")?.length;
const paragraphs = text.replace(/\n$/gm, '').split(/\n/).length;
const words = getWordCount(text);
const paragraphs = getParagraphCount(text);

return {
characters,
Expand All @@ -73,7 +84,7 @@ const formatDate = (date: Date | undefined) => {
return `${date.toDateString()} ${date.toLocaleTimeString()}`;
};

const NoteAttributes: FunctionComponent<{ note: SNNote }> = ({ note }) => {
const NoteAttributes: FunctionComponent<{ application: SNApplication, note: SNNote }> = ({ application, note }) => {
const { words, characters, paragraphs } = useMemo(
() => countNoteAttributes(note.text),
[note.text]
Expand All @@ -94,9 +105,12 @@ const NoteAttributes: FunctionComponent<{ note: SNNote }> = ({ note }) => {
[note.created_at]
);

const editor = application.componentManager.editorForNote(note);
const format = editor?.package_info?.file_type || 'txt';

return (
<div className="px-3 pt-1.5 pb-1 text-xs color-neutral font-medium">
{typeof words === 'number' ? (
{typeof words === 'number' && (format === 'txt' || format === 'md') ? (
<>
<div className="mb-1">
{words} words · {characters} characters · {paragraphs} paragraphs
Expand Down Expand Up @@ -473,7 +487,7 @@ export const NotesOptions = observer(
{notes.length === 1 ? (
<>
<div className="min-h-1px my-2 bg-border"></div>
<NoteAttributes note={notes[0]} />
<NoteAttributes application={application} note={notes[0]} />
</>
) : null}
</>
Expand Down

0 comments on commit 306441e

Please sign in to comment.