Skip to content

Commit

Permalink
Merge pull request #143 from oleksandr-danylchenko/#142-span-ordered-…
Browse files Browse the repository at this point in the history
…highlights-rendering

#142 [`SPANS`] Added highlights sorting for the renderer
  • Loading branch information
rsimon authored Sep 2, 2024
2 parents 2823297 + a719784 commit dc4e1dc
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/text-annotator/src/highlight/span/spansRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,20 @@ const createRenderer = (container: HTMLElement): RendererImplementation => {
if (shouldRedraw)
highlightLayer.innerHTML = '';

highlights.forEach(highlight => {
/**
* Highlights rendering in the span highlight layer is an order-sensitive operation.
* The later the highlight is rendered, the higher it will be in the visual stack.
*
* By default, we should expect that the newer highlight
* will be rendered over the older one
*/
const highlightsByCreation = [...highlights].sort((highlightA, highlightB) => {
const { annotation: { target: { created: createdA } } } = highlightA;
const { annotation: { target: { created: createdB } } } = highlightB;
return createdA.getTime() - createdB.getTime();
});

highlightsByCreation.forEach(highlight => {
highlight.rects.map(rect => {
const zIndex = computeZIndex(rect, highlights);
const style = paint(highlight, viewportBounds, currentStyle, painter, zIndex);
Expand Down

0 comments on commit dc4e1dc

Please sign in to comment.