This repository has been archived by the owner on Jul 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
180 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 0 additions & 124 deletions
124
src/renderer/components/elements/diary-editor/editor-toolbar/EditorToolbar.tsx
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
src/renderer/components/elements/editor/editor-toolbar/editor-toolbar/EditorToolbar.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { EditorState } from "draft-js"; | ||
import React, { ReactElement } from "react"; | ||
|
||
import FormattingButtons from "../formatting-buttons/FormattingButtons"; | ||
import WordCountWrapper from "../word-count/WordCountWrapper"; | ||
|
||
export interface CustomProps { | ||
onTextChange: (textEditorState: EditorState) => void; | ||
textEditorState: EditorState; | ||
} | ||
|
||
type Props = CustomProps; | ||
|
||
export default function EditorToolbar(props: Props): ReactElement { | ||
const { onTextChange, textEditorState } = props; | ||
|
||
return ( | ||
<div | ||
className="editor-toolbar" | ||
onMouseDown={(e): void => { | ||
e.preventDefault(); // Keep focus on editor when a button is clicked | ||
}} | ||
role="none" | ||
> | ||
<FormattingButtons onTextChange={onTextChange} textEditorState={textEditorState} /> | ||
<WordCountWrapper /> | ||
</div> | ||
); | ||
} |
99 changes: 99 additions & 0 deletions
99
...nderer/components/elements/editor/editor-toolbar/formatting-buttons/FormattingButtons.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { EditorState, RichUtils } from "draft-js"; | ||
import BoldIcon from "feather-icons/dist/icons/bold.svg"; | ||
import ItalicIcon from "feather-icons/dist/icons/italic.svg"; | ||
import UlIcon from "feather-icons/dist/icons/list.svg"; | ||
import React, { ReactElement } from "react"; | ||
|
||
import OlIcon from "../../../../../assets/icons/ordered-list.svg"; | ||
import { translations } from "../../../../../utils/i18n"; | ||
import { iconProps } from "../../../../../utils/icons"; | ||
|
||
const STROKE_WIDTH_DEFAULT = 2; | ||
const STROKE_WIDTH_SELECTED = 3; | ||
|
||
export interface CustomProps { | ||
onTextChange: (textEditorState: EditorState) => void; | ||
textEditorState: EditorState; | ||
} | ||
|
||
type Props = CustomProps; | ||
|
||
/** | ||
* Toolbar buttons for changing the formatting of the diary entry (bold, italic, lists) | ||
*/ | ||
export default function FormattingButtons(props: Props): ReactElement { | ||
const { onTextChange, textEditorState } = props; | ||
|
||
const onBoldClick = (): void => { | ||
onTextChange(RichUtils.toggleInlineStyle(textEditorState, "BOLD")); | ||
}; | ||
|
||
const onItalicClick = (): void => { | ||
onTextChange(RichUtils.toggleInlineStyle(textEditorState, "ITALIC")); | ||
}; | ||
|
||
const onOlClick = (): void => { | ||
onTextChange(RichUtils.toggleBlockType(textEditorState, "ordered-list-item")); | ||
}; | ||
|
||
const onUlClick = (): void => { | ||
onTextChange(RichUtils.toggleBlockType(textEditorState, "unordered-list-item")); | ||
}; | ||
|
||
// Detect active inline/block styles | ||
const inlineStyle = textEditorState.getCurrentInlineStyle(); | ||
const blockType = RichUtils.getCurrentBlockType(textEditorState); | ||
const isBold = inlineStyle.has("BOLD"); | ||
const isItalic = inlineStyle.has("ITALIC"); | ||
const isOl = blockType === "ordered-list-item"; | ||
const isUl = blockType === "unordered-list-item"; | ||
|
||
return ( | ||
<div className="formatting-buttons"> | ||
<button | ||
type="button" | ||
className={`button button-invisible ${isBold ? "button-active" : ""}`} | ||
onClick={onBoldClick} | ||
> | ||
<BoldIcon | ||
{...iconProps} | ||
strokeWidth={isBold ? STROKE_WIDTH_SELECTED : STROKE_WIDTH_DEFAULT} | ||
title={translations.bold} | ||
/> | ||
</button> | ||
<button | ||
type="button" | ||
className={`button button-invisible ${isItalic ? "button-active" : ""}`} | ||
onClick={onItalicClick} | ||
> | ||
<ItalicIcon | ||
{...iconProps} | ||
strokeWidth={isItalic ? STROKE_WIDTH_SELECTED : STROKE_WIDTH_DEFAULT} | ||
title={translations.italic} | ||
/> | ||
</button> | ||
<button | ||
type="button" | ||
className={`button button-invisible ${isUl ? "button-active" : ""}`} | ||
onClick={onUlClick} | ||
> | ||
<UlIcon | ||
{...iconProps} | ||
strokeWidth={isUl ? STROKE_WIDTH_SELECTED : STROKE_WIDTH_DEFAULT} | ||
title={translations.bullets} | ||
/> | ||
</button> | ||
<button | ||
type="button" | ||
className={`button button-invisible ${isOl ? "button-active" : ""}`} | ||
onClick={onOlClick} | ||
> | ||
<OlIcon | ||
{...iconProps} | ||
strokeWidth={isOl ? STROKE_WIDTH_SELECTED : STROKE_WIDTH_DEFAULT} | ||
title={translations.list} | ||
/> | ||
</button> | ||
</div> | ||
); | ||
} |
30 changes: 30 additions & 0 deletions
30
src/renderer/components/elements/editor/editor-toolbar/word-count/WordCount.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React, { ReactElement } from "react"; | ||
import countWords from "word-count"; | ||
|
||
import { toIndexDate } from "../../../../../utils/dateFormat"; | ||
|
||
export interface StateProps { | ||
dateSelected: Date; | ||
entries: Entries; | ||
} | ||
|
||
type Props = StateProps; | ||
|
||
/** | ||
* Text component which indicates the number of words/characters (varies per language) for the | ||
* currently selected diary entry | ||
*/ | ||
export default function WordCount(props: Props): ReactElement { | ||
const { dateSelected, entries } = props; | ||
|
||
let wordCount = 0; | ||
|
||
const indexDate = toIndexDate(dateSelected); | ||
|
||
if (indexDate in entries) { | ||
const entry = entries[indexDate]; | ||
wordCount = countWords(`${entry.title ?? ""}\n${entry.text ?? ""}`); | ||
} | ||
|
||
return <p className="word-count">{wordCount}</p>; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/renderer/components/elements/editor/editor-toolbar/word-count/WordCountWrapper.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { connect } from "react-redux"; | ||
|
||
import { RootState } from "../../../../../store/store"; | ||
import WordCount, { StateProps } from "./WordCount"; | ||
|
||
const mapStateToProps = (state: RootState): StateProps => ({ | ||
dateSelected: state.diary.dateSelected, | ||
entries: state.file.entries, | ||
}); | ||
|
||
export default connect(mapStateToProps)(WordCount); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters