Newer
Older
Deleephan K S
committed
import React, { useMemo, useState, useRef, useContext } from 'react';
import { isEmpty } from 'lodash';
import { useTranslation } from 'react-i18next';
Deleephan K S
committed
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;
`;
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);
{ name: 'yellow', value: '#F3E95C' },
{ name: 'blue', value: '#7DE5DB' },
{ name: 'green', value: '#C0EB96' },
{ name: 'pink', value: '#F2C4D1' },
{ name: 'orange', value: '#FFB56B' },
{ name: 'red', value: '#FF8080' },
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 line = document.getElementById('trait');
if (line)
line.style.fill =
localStorage.getItem('lastBgColor') !== null
? localStorage.getItem('lastBgColor')
: highlightDropDownOptions[0].name;
Deleephan K S
committed
const MenuButtonComponent = useMemo(
() => (
<MenuButton
active={isOpen}
disabled={isDisabled}
iconName={icon}
onMouseDown={() => {
setIsOpen(!isOpen);
}}
title={
!isEmpty(i18n) && i18n.exists(`Wax.Various.${title}`)
? t(`Wax.Various.${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;