Skip to content
Snippets Groups Projects
Commit 7353e92f authored by chris's avatar chris
Browse files

add current selection word count

parent 8188f81a
No related branches found
No related tags found
1 merge request!536Counters
import React, { useMemo, useState, useRef } from 'react'; import React, { useMemo, useState, useRef, useContext, useEffect } from 'react';
import styled from 'styled-components'; import styled from 'styled-components';
import { grid, override } from '@pubsweet/ui-toolkit'; import { grid, override } from '@pubsweet/ui-toolkit';
import { MenuButton, useOnClickOutside } from 'wax-prosemirror-core'; import {
MenuButton,
useOnClickOutside,
WaxContext,
} from 'wax-prosemirror-core';
import { isEmpty } from 'lodash'; import { isEmpty } from 'lodash';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
...@@ -81,17 +86,25 @@ const Counter = styled.div` ...@@ -81,17 +86,25 @@ const Counter = styled.div`
`; `;
const EditorInfoTool = ({ view: { state }, item }) => { const EditorInfoTool = ({ view: { state }, item }) => {
const { activeView } = useContext(WaxContext);
const { t, i18n } = useTranslation(); const { t, i18n } = useTranslation();
const ref = useRef(); const ref = useRef();
const [currentWordCount, setCurrentWordCount] = useState(0);
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
useOnClickOutside(ref, () => setIsOpen(false)); useOnClickOutside(ref, () => setIsOpen(false));
const infoDropDownOptions = useMemo(() => { useEffect(() => {
if (!isOpen) return []; const { from, to } = activeView.state.selection;
const currentSelection = activeView.state.doc.textBetween(from, to);
setCurrentWordCount(
currentSelection.split(' ').filter(word => word !== '').length,
);
}, [activeView.state.selection]);
const docText = state.doc.textBetween( const infoDropDownOptions = () => {
const docText = activeView.state.doc.textBetween(
0, 0,
state.doc.content.size, activeView.state.doc.content.size,
undefined, undefined,
' ', ' ',
); );
...@@ -172,7 +185,7 @@ const EditorInfoTool = ({ view: { state }, item }) => { ...@@ -172,7 +185,7 @@ const EditorInfoTool = ({ view: { state }, item }) => {
}`, }`,
}, },
]; ];
}, [state]); };
const MenuButtonComponent = useMemo( const MenuButtonComponent = useMemo(
() => ( () => (
...@@ -180,12 +193,14 @@ const EditorInfoTool = ({ view: { state }, item }) => { ...@@ -180,12 +193,14 @@ const EditorInfoTool = ({ view: { state }, item }) => {
<MenuButton <MenuButton
active={isOpen} active={isOpen}
disabled={false} disabled={false}
label={ label={`${currentWordCount} ${
!isEmpty(i18n) && i18n.exists(`Wax.Counters.Word`) !isEmpty(i18n) && i18n.exists(`Wax.Counters.Word`)
? t(`Wax.Counters.Word`) ? t(`Wax.Counters.Word`)
: 'Word' : 'Word'
} }${currentWordCount > 1 ? 's' : ''}
onMouseDown={() => { `}
onMouseDown={e => {
e.preventDefault();
setIsOpen(!isOpen); setIsOpen(!isOpen);
}} }}
title={item.title} title={item.title}
...@@ -198,7 +213,7 @@ const EditorInfoTool = ({ view: { state }, item }) => { ...@@ -198,7 +213,7 @@ const EditorInfoTool = ({ view: { state }, item }) => {
item={item} item={item}
view={state} view={state}
> >
{infoDropDownOptions.map(option => ( {infoDropDownOptions().map(option => (
<Counter key={option.name} title={option.name}> <Counter key={option.name} title={option.name}>
<span>{option.name}</span> <span>{option.name}</span>
</Counter> </Counter>
...@@ -208,7 +223,7 @@ const EditorInfoTool = ({ view: { state }, item }) => { ...@@ -208,7 +223,7 @@ const EditorInfoTool = ({ view: { state }, item }) => {
)} )}
</Wrapper> </Wrapper>
), ),
[isOpen], [currentWordCount, isOpen],
); );
return MenuButtonComponent; return MenuButtonComponent;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment