Skip to content

Commit

Permalink
Words count (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
manhtai authored Apr 4, 2022
1 parent e66cb27 commit ab241e4
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 27 deletions.
4 changes: 2 additions & 2 deletions release/app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion release/app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bookless",
"version": "0.1.2",
"version": "0.2.0",
"description": "Hassle-free book-writing app",
"main": "./dist/main/main.js",
"author": {
Expand Down
10 changes: 9 additions & 1 deletion src/helpers/string.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
const numformatter = new Intl.NumberFormat('en-US');

export const pathToName = (pathname: string) =>
pathname.split('\\').pop()?.split('/').pop();

export const truncate = (str: string, num = 60) => {
if (str.length > num) {
return `${str.slice(0, num)}...`;
const remain = str.slice(0, num).split(' ');
remain.pop();
return `${remain.join(' ')}...`;
}
return str;
};

export const words = (str: string): string => {
return str ? numformatter.format(str.split(' ').length) : '0';
};
8 changes: 5 additions & 3 deletions src/main/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { readFile, rename, unlink, writeFile } from 'fs/promises';
import path from 'path';
import { clipboard, dialog } from 'electron';
import { nanoid } from 'nanoid';
import { truncate, pathToName } from '../helpers/string';
import { words, truncate, pathToName } from '../helpers/string';
import { Doc, DocFile } from '../renderer/state/AppState';

const mdExtensions = ['md', 'txt', 'markdown'];
Expand Down Expand Up @@ -48,12 +48,14 @@ export const loadFiles = async (
const docFiles = await Promise.all(
filenames.map(async (name) => {
try {
const content = await readFile(path.join(dir, name), 'utf-8');
return {
name,
body: truncate(await readFile(path.join(dir, name), 'utf-8')),
body: truncate(content),
count: words(content),
};
} catch (err) {
return { name: '', body: '' };
return { name: '', body: '', count: '' };
}
})
);
Expand Down
42 changes: 24 additions & 18 deletions src/renderer/components/Explorer/Explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -257,29 +257,35 @@ const Explorer = (props: AppStateProps) => {
onClick={() => chooseFile(state.dir, file.name)}
onDoubleClick={() => renameFile(state.dir, file.name)}
onKeyPress={() => chooseFile(state.dir, file.name)}
className="flex flex-col items-stretch justify-between p-4 text-xs h-28 w-60"
className="flex flex-col items-stretch justify-between h-32 p-4 text-xs w-60"
>
<section className="space-y-1">
<h1 className="font-semibold">{file.name}</h1>
<p>{file.body}</p>
</section>
<span className="flex items-center justify-start mt-2 space-x-2 transition-opacity duration-300 opacity-0 hover:opacity-100">
<IoTrashOutline
className="w-4 h-4 transition-opacity duration-300 hover:opacity-70"
title="Delete"
onClick={(e) => {
e.stopPropagation();
deleteFile(state.dir, file.name);
}}
/>
<IoCreateOutline
className="w-4 h-4 transition-opacity duration-300 hover:opacity-70"
title="Rename"
onClick={(e) => {
e.stopPropagation();
renameFile(state.dir, file.name);
}}
/>
<span className="flex items-center justify-between opacity-70">
<span>
{file.count} word
{file.count === '1' ? '' : 's'}
</span>
<span className="flex items-center justify-end flex-1 space-x-2 transition-opacity duration-300 opacity-0 hover:opacity-100">
<IoTrashOutline
className="w-4 h-4 transition-opacity duration-300 hover:opacity-70"
title="Delete"
onClick={(e) => {
e.stopPropagation();
deleteFile(state.dir, file.name);
}}
/>
<IoCreateOutline
className="w-4 h-4 transition-opacity duration-300 hover:opacity-70"
title="Rename"
onClick={(e) => {
e.stopPropagation();
renameFile(state.dir, file.name);
}}
/>
</span>
</span>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/renderer/state/AppState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface Doc {
export interface DocFile {
name: string;
body: string;
count: string;
}

export const ConfigKey = {
Expand Down
8 changes: 6 additions & 2 deletions src/renderer/state/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { truncate } from 'helpers/string';
import { truncate, words } from 'helpers/string';
import { Dispatch } from 'react';
import { AppState } from './AppState';
import { Action } from './Action';
Expand Down Expand Up @@ -38,7 +38,11 @@ const appStateReducer = (state: AppState, action: Action): AppState => {
};
const files = state.files.map((file) => {
if (file.name === doc.fileName) {
return { ...file, body: truncate(doc.md) };
return {
...file,
body: truncate(doc.md),
count: words(doc.md),
};
}
return file;
});
Expand Down

0 comments on commit ab241e4

Please sign in to comment.