Newer
Older
Deleephan K S
committed
import React, { useMemo, useState, useRef, useContext } from 'react';
import styled from 'styled-components';
Deleephan K S
committed
import { v4 as uuidv4 } from 'uuid';
import {
WaxContext,
useOnClickOutside,
MenuButton,
} from 'wax-prosemirror-core';
Deleephan K S
committed
const Wrapper = styled.div`
font-size: 0;
position: relative;
z-index: 2;
Deleephan K S
committed
`;
const DropWrapper = styled.div`
Deleephan K S
committed
margin-top: ${grid(1)};
position: absolute;
top: 32px;
width: max-content;
Deleephan K S
committed
`;
const TextHighlightComponent = styled.div`
Deleephan K S
committed
display: flex;
flex-direction: column;
`;
const Highlighter = styled.div`
border: 1px solid gray;
cursor: pointer;
display: inline-grid;
Deleephan K S
committed
height: 25px;
Deleephan K S
committed
`;
const TextHighlightingTool = ({ view: { dispatch, state }, item }) => {
const { icon, title, select } = item;
const [isOpen, setIsOpen] = useState(false);
const highlightDropDownOptions = [
{ name: 'red', value: 'red' },
{ name: 'Light blue', value: '#add8e6' },
{ name: 'yellow', value: 'yellow' },
{ name: 'green', value: '#90EE90' },
{ name: 'gray', value: '#d3d3d3' },
{ name: 'orange', value: 'orange' },
{ name: 'brown', value: 'brown' },
{ name: 'aquamarine', value: 'aquamarine' },
{ name: 'remove highlight', value: 'transparent' },
];
Deleephan K S
committed
const ref = useRef();
const {
activeViewId,
activeView,
pmViews: { main },
} = useContext(WaxContext);
const isEditable = main.props.editable(editable => {
Deleephan K S
committed
useOnClickOutside(ref, () => setIsOpen(false));
const renderList = () => {
const lists = [];
Deleephan K S
committed
lists.push(
data-style={highlightDropDownOptions[key].value}
key={uuidv4()}
onMouseDown={e =>
handleMouseDown(e, highlightDropDownOptions[key].value)
}
style={{ backgroundColor: highlightDropDownOptions[key].value }}
Deleephan K S
committed
);
});
return <div>{lists}</div>;
};
Deleephan K S
committed
e.preventDefault();
item.run(activeView.state, activeView.dispatch, color);
if (color !== 'transparent') localStorage.setItem('lastBgColor', color);
Deleephan K S
committed
setIsOpen(false);
const color = localStorage.getItem('lastBgColor')
? localStorage.getItem('lastBgColor')
: highlightDropDownOptions[0].value;
Deleephan K S
committed
let isDisabled = !select(state, activeViewId, activeView);
if (!isEditable) isDisabled = true;
Deleephan K S
committed
const MenuButtonComponent = useMemo(
() => (
? 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>
Deleephan K S
committed
close={() => {
setIsOpen(false);
Deleephan K S
committed
</DropWrapper>
)}
</Wrapper>
),
Deleephan K S
committed
);
return MenuButtonComponent;
};
export default TextHighlightingTool;