-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(article): support annotation on ebook
- Loading branch information
1 parent
7a4d930
commit d239f37
Showing
14 changed files
with
368 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { HighLightedText } from "@/components/high-lighted-text"; | ||
import { SelectToSearch } from "@/hooks/use-select-to-search"; | ||
import parse, { | ||
type Element, | ||
type Text, | ||
type HTMLReactParserOptions, | ||
} from "html-react-parser"; | ||
import { memo, useMemo } from "react"; | ||
|
||
const EBookRender = memo( | ||
({ | ||
dom, | ||
bookId, | ||
sectionId, | ||
containerRef, | ||
}: { | ||
dom: HTMLElement; | ||
bookId: string; | ||
sectionId: string; | ||
containerRef: React.RefObject<HTMLDivElement | null>; | ||
}) => { | ||
const options: HTMLReactParserOptions = useMemo( | ||
() => ({ | ||
replace: (dom) => { | ||
if ( | ||
(dom as Text).type === "text" && | ||
(dom.parent as Element)?.name === "p" | ||
) { | ||
const paragraphIndex = Number( | ||
(dom.parent as Element).attribs["data-paragraph-index"] || 0, | ||
); | ||
const text = (dom as unknown as Text).data; | ||
return ( | ||
<HighLightedText | ||
articleId={bookId} | ||
chapterId={sectionId} | ||
paragraphIndex={paragraphIndex} | ||
> | ||
{text} | ||
</HighLightedText> | ||
); | ||
} | ||
}, | ||
}), | ||
[bookId, sectionId], | ||
); | ||
|
||
return ( | ||
<SelectToSearch | ||
showAdd | ||
showAnnotate | ||
prompt="sentence" | ||
className="w-full h-full" | ||
> | ||
{/* https://github.com/remarkablemark/html-react-parser/issues/1302 */} | ||
<div className="w-full h-full ebook" ref={containerRef}> | ||
{parse(dom.outerHTML.replaceAll("!important", ""), options)} | ||
</div> | ||
</SelectToSearch> | ||
); | ||
}, | ||
); | ||
|
||
export { EBookRender }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.