|
|
@ -1,18 +1,17 @@ |
|
|
|
import { useState, useEffect, useCallback, useRef } from 'react' |
|
|
|
import { useState, useEffect, useCallback, useRef } from "react"; |
|
|
|
import { useSelector } from 'react-redux' |
|
|
|
import { useSelector } from "react-redux"; |
|
|
|
// components
|
|
|
|
// components
|
|
|
|
import Wrapper from 'components/contextMenu/Wrapper' |
|
|
|
import Wrapper from "components/contextMenu/Wrapper"; |
|
|
|
import ColorItem from 'components/contextMenu/ColorItem' |
|
|
|
import ColorItem from "components/contextMenu/ColorItem"; |
|
|
|
import Item from 'components/contextMenu/Item' |
|
|
|
import Item from "components/contextMenu/Item"; |
|
|
|
// utils
|
|
|
|
// utils
|
|
|
|
import { getParagraphCfi } from 'lib/utils/commonUtil' |
|
|
|
import { getParagraphCfi } from "lib/utils/commonUtil"; |
|
|
|
// slices
|
|
|
|
// slices
|
|
|
|
import { contextmenuWidth } from 'lib/styles/viewerLayout' |
|
|
|
import { contextmenuWidth } from "lib/styles/viewerLayout"; |
|
|
|
// types
|
|
|
|
// types
|
|
|
|
import { RootState } from 'slices' |
|
|
|
import { RootState } from "slices"; |
|
|
|
import Highlight, { Color } from 'types/highlight' |
|
|
|
import Highlight, { Color } from "types/highlight"; |
|
|
|
import Selection from 'types/selection' |
|
|
|
import Selection from "types/selection"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ContextMenu = ({ |
|
|
|
const ContextMenu = ({ |
|
|
|
active, |
|
|
|
active, |
|
|
@ -21,10 +20,14 @@ const ContextMenu = ({ |
|
|
|
onAddHighlight, |
|
|
|
onAddHighlight, |
|
|
|
onRemoveHighlight, |
|
|
|
onRemoveHighlight, |
|
|
|
onUpdateHighlight, |
|
|
|
onUpdateHighlight, |
|
|
|
onContextmMenuRemove |
|
|
|
onContextmMenuRemove, |
|
|
|
}: Props) => { |
|
|
|
}: Props) => { |
|
|
|
const highlights = useSelector<RootState, Highlight[]>(state => state.book.highlights); |
|
|
|
const highlights = useSelector<RootState, Highlight[]>( |
|
|
|
const colorList = useSelector<RootState, Color[]>(state => state.book.colorList); |
|
|
|
(state) => state.book.highlights |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
const colorList = useSelector<RootState, Color[]>( |
|
|
|
|
|
|
|
(state) => state.book.colorList |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const menuRef = useRef<HTMLDivElement | null>(null); |
|
|
|
const menuRef = useRef<HTMLDivElement | null>(null); |
|
|
|
|
|
|
|
|
|
|
@ -37,16 +40,18 @@ const ContextMenu = ({ |
|
|
|
const [height, setHeight] = useState<number>(0); |
|
|
|
const [height, setHeight] = useState<number>(0); |
|
|
|
const [y, setY] = useState<number>(selection.y); |
|
|
|
const [y, setY] = useState<number>(selection.y); |
|
|
|
|
|
|
|
|
|
|
|
const ColorList = colorList.map( |
|
|
|
const ColorList = colorList.map((color) => ( |
|
|
|
color => <ColorItem key={color.code} |
|
|
|
<ColorItem |
|
|
|
|
|
|
|
key={color.code} |
|
|
|
name={color.name} |
|
|
|
name={color.name} |
|
|
|
color={color.code} |
|
|
|
color={color.code} |
|
|
|
onClick={selection.update |
|
|
|
onClick={ |
|
|
|
|
|
|
|
selection.update |
|
|
|
? () => onUpdateHighlight(highlight, color.code) |
|
|
|
? () => onUpdateHighlight(highlight, color.code) |
|
|
|
: () => onAddHighlight(color.code) |
|
|
|
: () => onAddHighlight(color.code) |
|
|
|
} /> |
|
|
|
} |
|
|
|
); |
|
|
|
/> |
|
|
|
|
|
|
|
)); |
|
|
|
|
|
|
|
|
|
|
|
/** Remove highlight */ |
|
|
|
/** Remove highlight */ |
|
|
|
const onRemoveHighlight_ = useCallback(() => { |
|
|
|
const onRemoveHighlight_ = useCallback(() => { |
|
|
@ -63,23 +68,28 @@ const ContextMenu = ({ |
|
|
|
* @param e Mouse Event |
|
|
|
* @param e Mouse Event |
|
|
|
* @param e.path |
|
|
|
* @param e.path |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
const onRemove = useCallback(({ path }: any) => { |
|
|
|
const onRemove = useCallback( |
|
|
|
|
|
|
|
({ path }: any) => { |
|
|
|
if (!menuRef.current) return; |
|
|
|
if (!menuRef.current) return; |
|
|
|
|
|
|
|
|
|
|
|
if ([...path].includes(menuRef.current)) return; |
|
|
|
if ([...path].includes(menuRef.current)) return; |
|
|
|
onContextmMenuRemove(); |
|
|
|
onContextmMenuRemove(); |
|
|
|
}, [menuRef, onContextmMenuRemove]); |
|
|
|
}, |
|
|
|
|
|
|
|
[menuRef, onContextmMenuRemove] |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Arrow event |
|
|
|
* Arrow event |
|
|
|
* @param e Keyboard Event |
|
|
|
* @param e Keyboard Event |
|
|
|
* @param e.key |
|
|
|
* @param e.key |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
const onKeyPress = useCallback(({ key }: any) => { |
|
|
|
const onKeyPress = useCallback( |
|
|
|
|
|
|
|
({ key }: any) => { |
|
|
|
key && key === "ArrowLeft" && onContextmMenuRemove(); |
|
|
|
key && key === "ArrowLeft" && onContextmMenuRemove(); |
|
|
|
key && key === "ArrowRight" && onContextmMenuRemove(); |
|
|
|
key && key === "ArrowRight" && onContextmMenuRemove(); |
|
|
|
}, [onContextmMenuRemove]); |
|
|
|
}, |
|
|
|
|
|
|
|
[onContextmMenuRemove] |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
/** Check whether the menu button is visible */ |
|
|
|
/** Check whether the menu button is visible */ |
|
|
|
useEffect(() => { |
|
|
|
useEffect(() => { |
|
|
@ -88,7 +98,9 @@ const ContextMenu = ({ |
|
|
|
const paragraphCfi = getParagraphCfi(selection.cfiRange); |
|
|
|
const paragraphCfi = getParagraphCfi(selection.cfiRange); |
|
|
|
if (!paragraphCfi) return; |
|
|
|
if (!paragraphCfi) return; |
|
|
|
|
|
|
|
|
|
|
|
const filtered = highlights.filter(highlight => highlight.key === paragraphCfi + selection.cfiRange); |
|
|
|
const filtered = highlights.filter( |
|
|
|
|
|
|
|
(highlight) => highlight.key === paragraphCfi + selection.cfiRange |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
if (!filtered.length) return; |
|
|
|
if (!filtered.length) return; |
|
|
|
const highlight_ = filtered[0]; |
|
|
|
const highlight_ = filtered[0]; |
|
|
@ -99,49 +111,41 @@ const ContextMenu = ({ |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
setIsEraseBtn(false); |
|
|
|
setIsEraseBtn(false); |
|
|
|
} |
|
|
|
} |
|
|
|
}, [ |
|
|
|
}, [active, highlights, selection.cfiRange, selection.update]); |
|
|
|
active, |
|
|
|
|
|
|
|
highlights,
|
|
|
|
|
|
|
|
selection.cfiRange, |
|
|
|
|
|
|
|
selection.update |
|
|
|
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register contextmenu events */ |
|
|
|
/** Register contextmenu events */ |
|
|
|
useEffect(() => { |
|
|
|
useEffect(() => { |
|
|
|
if (!viewerRef.current) return; |
|
|
|
if (!viewerRef.current) return; |
|
|
|
|
|
|
|
|
|
|
|
const iframe = document.querySelector('iframe'); |
|
|
|
const iframe = document.querySelector("iframe"); |
|
|
|
const node = iframe && iframe.contentWindow && iframe.contentWindow.document; |
|
|
|
const node = |
|
|
|
const scrolledTarget = viewerRef.current.querySelector('div'); |
|
|
|
iframe && iframe.contentWindow && iframe.contentWindow.document; |
|
|
|
|
|
|
|
const scrolledTarget = viewerRef.current.querySelector("div"); |
|
|
|
|
|
|
|
|
|
|
|
if (active) { |
|
|
|
if (active) { |
|
|
|
setDisplay(true); |
|
|
|
setDisplay(true); |
|
|
|
scrolledTarget && scrolledTarget.addEventListener('scroll', onContextmMenuRemove); |
|
|
|
scrolledTarget && |
|
|
|
node && node.addEventListener('mousedown', onRemove); |
|
|
|
scrolledTarget.addEventListener("scroll", onContextmMenuRemove); |
|
|
|
node && node.addEventListener('keyup', onKeyPress); |
|
|
|
node && node.addEventListener("mousedown", onRemove); |
|
|
|
document.addEventListener('mousedown', onRemove); |
|
|
|
node && node.addEventListener("keyup", onKeyPress); |
|
|
|
document.addEventListener('keyup', onKeyPress); |
|
|
|
document.addEventListener("mousedown", onRemove); |
|
|
|
|
|
|
|
document.addEventListener("keyup", onKeyPress); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
setDisplay(false); |
|
|
|
setDisplay(false); |
|
|
|
scrolledTarget && scrolledTarget.removeEventListener('scroll', onContextmMenuRemove); |
|
|
|
scrolledTarget && |
|
|
|
node && node.removeEventListener('mousedown', onRemove); |
|
|
|
scrolledTarget.removeEventListener("scroll", onContextmMenuRemove); |
|
|
|
node && node.removeEventListener('keyup', onKeyPress); |
|
|
|
node && node.removeEventListener("mousedown", onRemove); |
|
|
|
document.removeEventListener('mousedown', onRemove); |
|
|
|
node && node.removeEventListener("keyup", onKeyPress); |
|
|
|
document.removeEventListener('keyup', onKeyPress); |
|
|
|
document.removeEventListener("mousedown", onRemove); |
|
|
|
}; |
|
|
|
document.removeEventListener("keyup", onKeyPress); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return () => { |
|
|
|
return () => { |
|
|
|
node && node.removeEventListener('mousedown', onContextmMenuRemove); |
|
|
|
node && node.removeEventListener("mousedown", onContextmMenuRemove); |
|
|
|
node && node.removeEventListener('keyup', onKeyPress); |
|
|
|
node && node.removeEventListener("keyup", onKeyPress); |
|
|
|
document.removeEventListener('keyup', onKeyPress); |
|
|
|
document.removeEventListener("keyup", onKeyPress); |
|
|
|
} |
|
|
|
}; |
|
|
|
}, [ |
|
|
|
}, [viewerRef, active, onRemove, onContextmMenuRemove, onKeyPress]); |
|
|
|
viewerRef,
|
|
|
|
|
|
|
|
active, |
|
|
|
|
|
|
|
onRemove, |
|
|
|
|
|
|
|
onContextmMenuRemove,
|
|
|
|
|
|
|
|
onKeyPress |
|
|
|
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Set modified contextmenu height & whether menu is reverse */ |
|
|
|
/** Set modified contextmenu height & whether menu is reverse */ |
|
|
|
useEffect(() => { |
|
|
|
useEffect(() => { |
|
|
@ -156,7 +160,7 @@ const ContextMenu = ({ |
|
|
|
const { innerHeight } = window; |
|
|
|
const { innerHeight } = window; |
|
|
|
|
|
|
|
|
|
|
|
if (selection.y + defaultHeight > innerHeight) { |
|
|
|
if (selection.y + defaultHeight > innerHeight) { |
|
|
|
y_ = selection.y - selection.height - (defaultHeight); |
|
|
|
y_ = selection.y - selection.height - defaultHeight; |
|
|
|
if (y_ < 0) { |
|
|
|
if (y_ < 0) { |
|
|
|
setHeight(defaultHeight + y_ - 8); |
|
|
|
setHeight(defaultHeight + y_ - 8); |
|
|
|
y_ = 8; |
|
|
|
y_ = 8; |
|
|
@ -170,29 +174,28 @@ const ContextMenu = ({ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
setY(y_); |
|
|
|
setY(y_); |
|
|
|
}, [ |
|
|
|
}, [selection.y, selection.height, ColorList, isEraseBtn, setHeight]); |
|
|
|
selection.y,
|
|
|
|
|
|
|
|
selection.height,
|
|
|
|
|
|
|
|
ColorList,
|
|
|
|
|
|
|
|
isEraseBtn,
|
|
|
|
|
|
|
|
setHeight |
|
|
|
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
return (<> |
|
|
|
<> |
|
|
|
{display && <Wrapper x={selection.x}
|
|
|
|
{display && ( |
|
|
|
|
|
|
|
<Wrapper |
|
|
|
|
|
|
|
x={selection.x} |
|
|
|
y={y} |
|
|
|
y={y} |
|
|
|
width={contextmenuWidth} |
|
|
|
width={contextmenuWidth} |
|
|
|
height={height} |
|
|
|
height={height} |
|
|
|
isReverse={isReverse} |
|
|
|
isReverse={isReverse} |
|
|
|
ref={menuRef}> |
|
|
|
ref={menuRef} |
|
|
|
|
|
|
|
> |
|
|
|
<div> |
|
|
|
<div> |
|
|
|
{ColorList} |
|
|
|
{ColorList} |
|
|
|
{isEraseBtn && <Item text="Remove" onClick={onRemoveHighlight_} />} |
|
|
|
{isEraseBtn && <Item text="حذف" onClick={onRemoveHighlight_} />} |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
</Wrapper>} |
|
|
|
</Wrapper> |
|
|
|
</>); |
|
|
|
)} |
|
|
|
} |
|
|
|
</> |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
interface Props { |
|
|
|
interface Props { |
|
|
|
active: boolean; |
|
|
|
active: boolean; |
|
|
@ -204,4 +207,4 @@ interface Props { |
|
|
|
onContextmMenuRemove: () => void; |
|
|
|
onContextmMenuRemove: () => void; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export default ContextMenu |
|
|
|
export default ContextMenu; |
|
|
|