Commit b8b48606 authored by Lyza Danger Gardner's avatar Lyza Danger Gardner Committed by Lyza Gardner

Update comments in `annotation-counts` and convert to TS

parent 27da5169
const ANNOTATION_COUNT_ATTR = 'data-hypothesis-annotation-count';
/**
* Update the elements in the container element with the count data attribute
* with the new annotation count. See:
* https://h.readthedocs.io/projects/client/en/latest/publishers/host-page-integration/#cmdoption-arg-data-hypothesis-annotation-count
*
* @param {Element} rootEl - The DOM element which contains the elements that
* display annotation count.
* @param {import('../shared/messaging').PortRPC<'publicAnnotationCountChanged', string>} rpc - Channel for host-sidebar communication
*/
export function annotationCounts(rootEl, rpc) {
rpc.on('publicAnnotationCountChanged', updateAnnotationCountElems);
/** @param {number} newCount */
function updateAnnotationCountElems(newCount) {
const elems = rootEl.querySelectorAll('[' + ANNOTATION_COUNT_ATTR + ']');
Array.from(elems).forEach(elem => {
elem.textContent = newCount.toString();
});
}
}
import type { PortRPC } from '../shared/messaging';
const ANNOTATION_COUNT_ATTR = 'data-hypothesis-annotation-count';
/**
* Show the current count of public annotations in designated elements.
*
* Any time the count of public annotations changes, find all elements within
* `rootEl` that have the `data-hypothesis-annotation-count` attribute and
* replace their text content with the current count of public annotations.
*
* This allows publishers to add a count of annotations to their web pages.
*
* See:
* https://h.readthedocs.io/projects/client/en/latest/publishers/host-page-integration.html#cmdoption-arg-data-hypothesis-annotation-count
*
*/
export function annotationCounts(
rootEl: Element,
rpc: PortRPC<'publicAnnotationCountChanged', string>
) {
rpc.on('publicAnnotationCountChanged', updateAnnotationCountElems);
function updateAnnotationCountElems(newCount: number) {
const elems = rootEl.querySelectorAll(`[${ANNOTATION_COUNT_ATTR}]`);
Array.from(elems).forEach(elem => {
elem.textContent = newCount.toString();
});
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment