|
|
@ -1,85 +1,95 @@ |
|
|
|
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, |
|
|
|
viewerRef,
|
|
|
|
viewerRef, |
|
|
|
selection,
|
|
|
|
selection, |
|
|
|
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); |
|
|
|
|
|
|
|
|
|
|
|
const [highlight, setHighlight] = useState<Highlight | null>(null); |
|
|
|
const [highlight, setHighlight] = useState<Highlight | null>(null); |
|
|
|
|
|
|
|
|
|
|
|
const [display, setDisplay] = useState<boolean>(false); |
|
|
|
const [display, setDisplay] = useState<boolean>(false); |
|
|
|
const [isEraseBtn, setIsEraseBtn] = useState<boolean>(false); |
|
|
|
const [isEraseBtn, setIsEraseBtn] = useState<boolean>(false); |
|
|
|
const [isReverse, setIsReverse] = useState<boolean>(false); |
|
|
|
const [isReverse, setIsReverse] = useState<boolean>(false); |
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
name={color.name} |
|
|
|
key={color.code} |
|
|
|
color={color.code} |
|
|
|
name={color.name} |
|
|
|
onClick={selection.update |
|
|
|
color={color.code} |
|
|
|
? () => onUpdateHighlight(highlight, color.code) |
|
|
|
onClick={ |
|
|
|
: () => onAddHighlight(color.code) |
|
|
|
selection.update |
|
|
|
} /> |
|
|
|
? () => onUpdateHighlight(highlight, color.code) |
|
|
|
); |
|
|
|
: () => onAddHighlight(color.code) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/> |
|
|
|
|
|
|
|
)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Remove highlight */ |
|
|
|
/** Remove highlight */ |
|
|
|
const onRemoveHighlight_ = useCallback(() => { |
|
|
|
const onRemoveHighlight_ = useCallback(() => { |
|
|
|
if (!highlight) return; |
|
|
|
if (!highlight) return; |
|
|
|
|
|
|
|
|
|
|
|
onRemoveHighlight(highlight.key, highlight.cfiRange); |
|
|
|
onRemoveHighlight(highlight.key, highlight.cfiRange); |
|
|
|
onContextmMenuRemove(); |
|
|
|
onContextmMenuRemove(); |
|
|
|
|
|
|
|
|
|
|
|
setIsEraseBtn(false); |
|
|
|
setIsEraseBtn(false); |
|
|
|
}, [highlight, onRemoveHighlight, onContextmMenuRemove]); |
|
|
|
}, [highlight, onRemoveHighlight, onContextmMenuRemove]); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Remove contextmenu |
|
|
|
* Remove contextmenu |
|
|
|
* @param e Mouse Event |
|
|
|
* @param e Mouse Event |
|
|
|
* @param e.path |
|
|
|
* @param e.path |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
const onRemove = useCallback(({ path }: any) => { |
|
|
|
const onRemove = useCallback( |
|
|
|
if (!menuRef.current) return; |
|
|
|
({ path }: any) => { |
|
|
|
|
|
|
|
if (!menuRef.current) return; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ([...path].includes(menuRef.current)) return; |
|
|
|
|
|
|
|
onContextmMenuRemove(); |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
[menuRef, onContextmMenuRemove] |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
if ([...path].includes(menuRef.current)) return; |
|
|
|
/** |
|
|
|
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 && key === "ArrowLeft" && onContextmMenuRemove(); |
|
|
|
({ key }: any) => { |
|
|
|
key && key === "ArrowRight" && onContextmMenuRemove(); |
|
|
|
key && key === "ArrowLeft" && onContextmMenuRemove(); |
|
|
|
}, [onContextmMenuRemove]); |
|
|
|
key && key === "ArrowRight" && 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,64 +111,56 @@ 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(() => { |
|
|
|
const menuPadding = 8; |
|
|
|
const menuPadding = 8; |
|
|
|
const itemHeight = 32; |
|
|
|
const itemHeight = 32; |
|
|
|
let itemCnt = ColorList.length; |
|
|
|
let itemCnt = ColorList.length; |
|
|
|
|
|
|
|
|
|
|
|
if (isEraseBtn) itemCnt += 1; |
|
|
|
if (isEraseBtn) itemCnt += 1; |
|
|
|
|
|
|
|
|
|
|
|
const defaultHeight = itemCnt * itemHeight + menuPadding; |
|
|
|
const defaultHeight = itemCnt * itemHeight + menuPadding; |
|
|
|
let y_ = selection.y; |
|
|
|
let y_ = selection.y; |
|
|
|
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,
|
|
|
|
return ( |
|
|
|
ColorList,
|
|
|
|
<> |
|
|
|
isEraseBtn,
|
|
|
|
{display && ( |
|
|
|
setHeight |
|
|
|
<Wrapper |
|
|
|
]); |
|
|
|
x={selection.x} |
|
|
|
|
|
|
|
y={y} |
|
|
|
|
|
|
|
width={contextmenuWidth} |
|
|
|
return (<> |
|
|
|
height={height} |
|
|
|
{display && <Wrapper x={selection.x}
|
|
|
|
isReverse={isReverse} |
|
|
|
y={y}
|
|
|
|
ref={menuRef} |
|
|
|
width={contextmenuWidth} |
|
|
|
> |
|
|
|
height={height} |
|
|
|
<div> |
|
|
|
isReverse={isReverse} |
|
|
|
{ColorList} |
|
|
|
ref={menuRef}> |
|
|
|
{isEraseBtn && <Item text="حذف" onClick={onRemoveHighlight_} />} |
|
|
|
<div> |
|
|
|
</div> |
|
|
|
{ColorList} |
|
|
|
</Wrapper> |
|
|
|
{isEraseBtn && <Item text="Remove" onClick={onRemoveHighlight_} />} |
|
|
|
)} |
|
|
|
</div> |
|
|
|
</> |
|
|
|
</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; |
|
|
|