Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom highlight color + on load event for pdf #283

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class App extends Component<{}, State> {
window.addEventListener(
"hashchange",
this.scrollToHighlightFromHash,
false,
false
);
}

Expand All @@ -118,7 +118,7 @@ class App extends Component<{}, State> {
updateHighlight(
highlightId: string,
position: Partial<ScaledPosition>,
content: Partial<Content>,
content: Partial<Content>
) {
console.log("Updating highlight", highlightId, position, content);

Expand Down Expand Up @@ -159,7 +159,15 @@ class App extends Component<{}, State> {
position: "relative",
}}
>
<PdfLoader url={url} beforeLoad={<Spinner />}>
<PdfLoader
url={url}
beforeLoad={<Spinner />}
onLoad={(pdfDocument) =>
console.log(
`PDF document loaded with ${pdfDocument.numPages} pages`
)
}
>
{(pdfDocument) => (
<PdfHighlighter
pdfDocument={pdfDocument}
Expand All @@ -175,7 +183,7 @@ class App extends Component<{}, State> {
position,
content,
hideTipAndSelection,
transformSelection,
transformSelection
) => (
<Tip
onOpen={transformSelection}
Expand All @@ -193,7 +201,7 @@ class App extends Component<{}, State> {
hideTip,
viewportToScaled,
screenshot,
isScrolledTo,
isScrolledTo
) => {
const isTextHighlight = !highlight.content?.image;

Expand All @@ -202,6 +210,10 @@ class App extends Component<{}, State> {
isScrolledTo={isScrolledTo}
position={highlight.position}
comment={highlight.comment}
highlightColor={{
default: "#90EE90",
onScroll: "#FF7F7F",
}}
/>
) : (
<AreaHighlight
Expand All @@ -211,9 +223,13 @@ class App extends Component<{}, State> {
this.updateHighlight(
highlight.id,
{ boundingRect: viewportToScaled(boundingRect) },
{ image: screenshot(boundingRect) },
{ image: screenshot(boundingRect) }
);
}}
highlightColor={{
default: "#90EE90",
onScroll: "#FF7F7F",
}}
/>
);

Expand Down
20 changes: 19 additions & 1 deletion src/components/AreaHighlight.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,38 @@ interface Props {
highlight: ViewportHighlight;
onChange: (rect: LTWHP) => void;
isScrolledTo: boolean;
highlightColor?: { default: string; onScroll: string };
}

export class AreaHighlight extends Component<Props> {
render() {
const { highlight, onChange, isScrolledTo, ...otherProps } = this.props;
const {
highlight,
onChange,
isScrolledTo,
highlightColor = {
default: "rgba(252, 232, 151, 1.0)",
onScroll: "#ff4141",
},
...otherProps
} = this.props;

return (
<div
className={`AreaHighlight ${
isScrolledTo ? "AreaHighlight--scrolledTo" : ""
}`}
style={{
backgroundColor: highlightColor.default,
}}
>
<Rnd
className="AreaHighlight__part"
style={{
background: isScrolledTo
? highlightColor.onScroll
: highlightColor.default,
}}
onDragStop={(_, data) => {
const boundingRect: LTWHP = {
...highlight.position.boundingRect,
Expand Down
12 changes: 11 additions & 1 deletion src/components/Highlight.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface Props {
text: string;
};
isScrolledTo: boolean;
highlightColor?: { default: string; onScroll: string };
}

export class Highlight extends Component<Props> {
Expand All @@ -26,6 +27,10 @@ export class Highlight extends Component<Props> {
onClick,
onMouseOver,
onMouseOut,
highlightColor = {
default: "rgba(252, 232, 151, 1.0)",
onScroll: "#ff4141",
},
comment,
isScrolledTo,
} = this.props;
Expand Down Expand Up @@ -55,7 +60,12 @@ export class Highlight extends Component<Props> {
onClick={onClick}
// biome-ignore lint/suspicious/noArrayIndexKey: We can use position hash at some point in future
key={index}
style={rect}
style={{
...rect,
background: isScrolledTo
? highlightColor.onScroll
: highlightColor.default,
}}
className={"Highlight__part"}
/>
))}
Expand Down
4 changes: 4 additions & 0 deletions src/components/PdfLoader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface Props {
errorMessage?: JSX.Element;
children: (pdfDocument: PDFDocumentProxy) => JSX.Element;
onError?: (error: Error) => void;
onLoad?: (pdfDocument: PDFDocumentProxy) => void;
cMapUrl?: string;
cMapPacked?: boolean;
}
Expand Down Expand Up @@ -85,6 +86,9 @@ export class PdfLoader extends Component<Props, State> {
};

return getDocument(document).promise.then((pdfDocument) => {
if (this.props.onLoad) {
this.props.onLoad(pdfDocument);
}
this.setState({ pdfDocument });
});
})
Expand Down
Loading