Newer
Older
Deleephan K S
committed
import React, { useMemo, useState, useRef, useContext } from 'react';
import styled from 'styled-components';
import { grid } from '@pubsweet/ui-toolkit';
import { v4 as uuidv4 } from 'uuid';
Deleephan K S
committed
import MenuButton from '../../ui/buttons/MenuButton';
import useOnClickOutside from '../../helpers/useOnClickOutside';
const Wrapper = styled.div`
font-size: 0;
position: relative;
z-index: 2;
Deleephan K S
committed
`;
const DropWrapper = styled.div`
margin-top: ${grid(1)};
position: absolute;
background: white;
top: 32px;
width: max-content;
`;
const TextHighlightComponent = styled.div`
display: flex;
flex-direction: column;
background:white
border:1px solid gray
`;
const Highlighter = styled.div`
min-width: 25px;
height: 25px;
Deleephan K S
committed
display: inline-grid;
cursor: pointer;
Deleephan K S
committed
`;
const TextHighlightingTool = ({ view: { dispatch, state }, item }) => {
const { icon, title, select } = item;
const [isOpen, setIsOpen] = useState(false);
Deleephan K S
committed
const ref = useRef();
const {
view: { main },
activeViewId,
activeView,
} = useContext(WaxContext);
useOnClickOutside(ref, () => setIsOpen(false));
let lastSaveColor;
Deleephan K S
committed
const highlightDropDownOptions = [
{ name: 'red', value: 'color:red' },
{ name: 'blue', value: 'color:blue' },
{ name: 'yellow', value: 'color:yellow' },
{ name: 'black', value: 'color:black' },
{ name: 'green', value: 'color:green' },
{ name: 'gray', value: 'color:gray' },
{ name: 'orange', value: 'color:orange' },
{ name: 'brown', value: 'color:brown' },
{ name: 'aquamarine', value: 'color:aquamarine' },
{ name: 'transparent', value: 'color:transparent' },
];
const renderList = () => {
const lists = [];
Deleephan K S
committed
lists.push(
<Highlighter
onMouseDown={e => handleMouseDown(e)}
key={uuidv4()}
title={highlightDropDownOptions[key].name}
data-style={highlightDropDownOptions[key].value}
style={{ backgroundColor: highlightDropDownOptions[key].name }}
/>,
Deleephan K S
committed
);
});
return <div>{lists}</div>;
};
Deleephan K S
committed
const handleMouseDown = e => {
e.preventDefault();
item.run(
activeView.state,
activeView.dispatch,
e.target.getAttribute('style'),
);
lastSaveColor = e.target.getAttribute('style');
localStorage.setItem('lastColor', lastSaveColor);
const btnBackground = e.target.title;
localStorage.setItem('lastBgColor', btnBackground);
Deleephan K S
committed
setIsOpen(false);
};
const handleDblClk = () => {
item.run(state, dispatch, localStorage.getItem('lastColor'));
};
Deleephan K S
committed
const isDisabled = !select(state, activeViewId, activeView);
const MenuButtonComponent = useMemo(
() => (
<Wrapper ref={ref} onDoubleClick={handleDblClk}>
<div
style={{
backgroundColor:
localStorage.getItem('lastBgColor') != undefined
? localStorage.getItem('lastBgColor')
: highlightDropDownOptions[0].name,
}}
>
<MenuButton
active={isOpen}
disabled={isDisabled}
iconName={icon}
onMouseDown={() => {
setIsOpen(!isOpen);
}}
title={title}
/>
Deleephan K S
committed
</div>
{isOpen && (
<DropWrapper>
<TextHighlightComponent
key={uuidv4()}
item={item}
view={(dispatch, state)}
Deleephan K S
committed
close={() => {
setIsOpen(false);
Deleephan K S
committed
</DropWrapper>
)}
</Wrapper>
),
Deleephan K S
committed
);
return MenuButtonComponent;
};
export default TextHighlightingTool;