Skip to content

Commit

Permalink
Fix customTextRenderer called more times than there are textContent i…
Browse files Browse the repository at this point in the history
…tems

Related to #1151
  • Loading branch information
wojtekmaj committed Nov 10, 2022
1 parent 5cdd8a0 commit 4962d3b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -405,7 +405,7 @@ Displays a page. Should be placed inside `<Document />`. Alternatively, it can h
| canvasBackground | Canvas background color. Any valid `canvas.fillStyle` can be used. If you set `renderMode` to `"svg"` this prop will be ignored. | n/a | `"transparent"` |
| canvasRef | A prop that behaves like [ref](https://reactjs.org/docs/refs-and-the-dom.html), but it's passed to `<canvas>` rendered by `<PageCanvas>` component. If you set `renderMode` to `"svg"` this prop will be ignored. | n/a | <ul><li>Function:<br />`(ref) => { this.myPage = ref; }`</li><li>Ref created using `React.createRef`:<br />`this.ref = React.createRef();`<br />…<br />`inputRef={this.ref}`</li><li>Ref created using `React.useRef`:<br />`const ref = React.useRef();`<br />…<br />`inputRef={ref}`</li></ul> |
| className | Class name(s) that will be added to rendered element along with the default `react-pdf__Page`. | n/a | <ul><li>String:<br />`"custom-class-name-1 custom-class-name-2"`</li><li>Array of strings:<br />`["custom-class-name-1", "custom-class-name-2"]`</li></ul> |
| customTextRenderer | Function that customizes how a text layer is rendered. | n/a | `` ({ str, itemIndex }) => str && str.replace(/ipsum/g, `<mark>ipsum</mark>`) `` |
| customTextRenderer | Function that customizes how a text layer is rendered. | n/a | `` ({ str, itemIndex }) => str.replace(/ipsum/g, `<mark>ipsum</mark>`) `` |
| error | What the component should display in case of an error. | `"Failed to load the page."` | <ul><li>String:<br />`"An error occurred!"`</li><li>React element:<br />`<div>An error occurred!</div>`</li><li>Function:<br />`this.renderError`</li></ul> |
| height | Page height. If neither `height` nor `width` are defined, page will be rendered at the size defined in PDF. If you define `width` and `height` at the same time, `height` will be ignored. If you define `height` and `scale` at the same time, the height will be multiplied by a given factor. | Page's default height | `300` |
| imageResourcesPath | The path used to prefix the src attributes of annotation SVGs. | n/a (pdf.js will fallback to an empty string) | `"/public/images/"` |
Expand Down
12 changes: 8 additions & 4 deletions src/Page/TextLayer.jsx
Expand Up @@ -141,14 +141,18 @@ export class TextLayerInternal extends PureComponent {
cancellable.promise
.then(() => {
if (customTextRenderer) {
Array.from(this.layerElement.current.children).forEach((element, elementIndex) => {
textContent.items.forEach((item, itemIndex) => {
const child = this.layerElement.current.children[itemIndex];

const content = customTextRenderer({
itemIndex: elementIndex,
...textContent.items[elementIndex],
itemIndex,
...item,
});
element.innerHTML = content;

child.innerHTML = content;
});
}

this.onRenderSuccess();
})
.catch((error) => {
Expand Down
2 changes: 1 addition & 1 deletion test/Test.jsx
Expand Up @@ -183,7 +183,7 @@ export default function Test() {
renderTextLayer,
scale: pageScale,
width: pageWidth,
customTextRenderer: ({ str }) => str && str.replace(/ipsum/g, `<mark>ipsum</mark>`),
customTextRenderer: ({ str }) => str.replace(/ipsum/g, `<mark>ipsum</mark>`),
};
}

Expand Down

0 comments on commit 4962d3b

Please sign in to comment.