Commit 24b65649 authored by Robert Knight's avatar Robert Knight

Add missing parameter and variable types in src/annotator/

Add missing types found while trying to typecheck src/annotator with
`noImplicitAny` enabled.
parent 6d50b1b5
...@@ -31,6 +31,7 @@ export const ARROW_POINTING_UP = 2; ...@@ -31,6 +31,7 @@ export const ARROW_POINTING_UP = 2;
* @prop {ArrowDirection} arrowDirection - Direction of the adder's arrow. * @prop {ArrowDirection} arrowDirection - Direction of the adder's arrow.
*/ */
/** @param {number} pixels */
function toPx(pixels) { function toPx(pixels) {
return pixels.toString() + 'px'; return pixels.toString() + 'px';
} }
...@@ -325,6 +326,7 @@ export class Adder { ...@@ -325,6 +326,7 @@ export class Adder {
} }
_render() { _render() {
/** @param {import('./components/AdderToolbar').Command} command */
const handleCommand = command => { const handleCommand = command => {
switch (command) { switch (command) {
case 'annotate': case 'annotate':
......
...@@ -30,6 +30,7 @@ function getNodePosition(node) { ...@@ -30,6 +30,7 @@ function getNodePosition(node) {
return pos; return pos;
} }
/** @param {Node} node */
function getPathSegment(node) { function getPathSegment(node) {
const name = getNodeName(node); const name = getNodeName(node);
const pos = getNodePosition(node); const pos = getNodePosition(node);
......
...@@ -11,7 +11,7 @@ import classnames from 'classnames'; ...@@ -11,7 +11,7 @@ import classnames from 'classnames';
* the sidebar. * the sidebar.
* *
* @param {object} props * @param {object} props
* @prop {Children} props.children * @param {Children} props.children
*/ */
function BucketList({ children }) { function BucketList({ children }) {
return ( return (
...@@ -38,8 +38,8 @@ function BucketList({ children }) { ...@@ -38,8 +38,8 @@ function BucketList({ children }) {
* Render a vertically-positioned bucket-list item. * Render a vertically-positioned bucket-list item.
* *
* @param {object} props * @param {object} props
* @prop {Children} props.children * @param {Children} props.children
* @prop {number} props.topPosition - The vertical top position, in pixels, * @param {number} props.topPosition - The vertical top position, in pixels,
* for this bucket item relative to the top of the containing BucketList * for this bucket item relative to the top of the containing BucketList
*/ */
function BucketItem({ children, topPosition }) { function BucketItem({ children, topPosition }) {
......
...@@ -18,6 +18,7 @@ import { urlFromLinkTag } from './url-from-link-tag'; ...@@ -18,6 +18,7 @@ import { urlFromLinkTag } from './url-from-link-tag';
*/ */
/** /**
* @param {Window} window_
* @return {SettingsGetters} * @return {SettingsGetters}
*/ */
export function settingsFrom(window_) { export function settingsFrom(window_) {
......
import { warnOnce } from '../shared/warn-once'; import { warnOnce } from '../shared/warn-once';
/** @type {Record<string, boolean>} */
let _features = {}; let _features = {};
/** @param {Record<string, boolean>} features */
const _set = features => { const _set = features => {
_features = features || {}; _features = features || {};
}; };
...@@ -18,6 +20,7 @@ export const features = { ...@@ -18,6 +20,7 @@ export const features = {
_set({}); _set({});
}, },
/** @param {string} flag */
flagEnabled: function (flag) { flagEnabled: function (flag) {
if (!(flag in _features)) { if (!(flag in _features)) {
warnOnce('looked up unknown feature', flag); warnOnce('looked up unknown feature', flag);
......
...@@ -226,8 +226,8 @@ export function highlightRange(range, cssClass = 'hypothesis-highlight') { ...@@ -226,8 +226,8 @@ export function highlightRange(range, cssClass = 'hypothesis-highlight') {
// Group text nodes into spans of adjacent nodes. If a group of text nodes are // Group text nodes into spans of adjacent nodes. If a group of text nodes are
// adjacent, we only need to create one highlight element for the group. // adjacent, we only need to create one highlight element for the group.
let textNodeSpans = []; let textNodeSpans = /** @type {Text[][]} */ ([]);
let prevNode = null; let prevNode = /** @type {Node|null} */ (null);
let currentSpan = null; let currentSpan = null;
textNodes.forEach(node => { textNodes.forEach(node => {
...@@ -246,11 +246,11 @@ export function highlightRange(range, cssClass = 'hypothesis-highlight') { ...@@ -246,11 +246,11 @@ export function highlightRange(range, cssClass = 'hypothesis-highlight') {
const whitespace = /^\s*$/; const whitespace = /^\s*$/;
textNodeSpans = textNodeSpans.filter(span => textNodeSpans = textNodeSpans.filter(span =>
// Check for at least one text node with non-space content. // Check for at least one text node with non-space content.
span.some(node => !whitespace.test(node.nodeValue)) span.some(node => !whitespace.test(node.data))
); );
// Wrap each text node span with a `<hypothesis-highlight>` element. // Wrap each text node span with a `<hypothesis-highlight>` element.
const highlights = []; const highlights = /** @type {HighlightElement[]} */ ([]);
textNodeSpans.forEach(nodes => { textNodeSpans.forEach(nodes => {
// A custom element name is used here rather than `<span>` to reduce the // A custom element name is used here rather than `<span>` to reduce the
// likelihood of highlights being hidden by page styling. // likelihood of highlights being hidden by page styling.
...@@ -259,7 +259,8 @@ export function highlightRange(range, cssClass = 'hypothesis-highlight') { ...@@ -259,7 +259,8 @@ export function highlightRange(range, cssClass = 'hypothesis-highlight') {
const highlightEl = document.createElement('hypothesis-highlight'); const highlightEl = document.createElement('hypothesis-highlight');
highlightEl.className = cssClass; highlightEl.className = cssClass;
nodes[0].parentNode.replaceChild(highlightEl, nodes[0]); const parent = /** @type {Node} */ (nodes[0].parentNode);
parent.replaceChild(highlightEl, nodes[0]);
nodes.forEach(node => highlightEl.appendChild(node)); nodes.forEach(node => highlightEl.appendChild(node));
highlights.push(highlightEl); highlights.push(highlightEl);
......
...@@ -272,6 +272,8 @@ export class HTMLMetadata { ...@@ -272,6 +272,8 @@ export class HTMLMetadata {
/** /**
* Convert a possibly relative URI to an absolute one. This will throw an * Convert a possibly relative URI to an absolute one. This will throw an
* exception if the URL cannot be parsed. * exception if the URL cannot be parsed.
*
* @param {string} url
*/ */
_absoluteUrl(url) { _absoluteUrl(url) {
return normalizeURI(url, this.document.baseURI); return normalizeURI(url, this.document.baseURI);
......
...@@ -261,7 +261,7 @@ export class PDFIntegration { ...@@ -261,7 +261,7 @@ export class PDFIntegration {
// This method (re-)anchors annotations when pages are rendered and destroyed. // This method (re-)anchors annotations when pages are rendered and destroyed.
_update() { _update() {
// A list of annotations that need to be refreshed. // A list of annotations that need to be refreshed.
const refreshAnnotations = []; const refreshAnnotations = /** @type {AnnotationData[]} */ ([]);
const pageCount = this.pdfViewer.pagesCount; const pageCount = this.pdfViewer.pagesCount;
for (let pageIndex = 0; pageIndex < pageCount; pageIndex++) { for (let pageIndex = 0; pageIndex < pageCount; pageIndex++) {
......
...@@ -66,16 +66,17 @@ export function forEachNodeInRange(range, callback) { ...@@ -66,16 +66,17 @@ export function forEachNodeInRange(range, callback) {
*/ */
export function getTextBoundingBoxes(range) { export function getTextBoundingBoxes(range) {
const whitespaceOnly = /^\s*$/; const whitespaceOnly = /^\s*$/;
const textNodes = []; const textNodes = /** @type {Text[]} */ ([]);
forEachNodeInRange(range, node => { forEachNodeInRange(range, node => {
if ( if (
node.nodeType === Node.TEXT_NODE && node.nodeType === Node.TEXT_NODE &&
!(/** @type {string} */ (node.textContent).match(whitespaceOnly)) !(/** @type {string} */ (node.textContent).match(whitespaceOnly))
) { ) {
textNodes.push(node); textNodes.push(/** @type {Text} */ (node));
} }
}); });
/** @type {DOMRect[]} */
let rects = []; let rects = [];
textNodes.forEach(node => { textNodes.forEach(node => {
const nodeRange = node.ownerDocument.createRange(); const nodeRange = node.ownerDocument.createRange();
......
...@@ -5,9 +5,8 @@ const SIDEBAR_TRIGGER_BTN_ATTR = 'data-hypothesis-trigger'; ...@@ -5,9 +5,8 @@ const SIDEBAR_TRIGGER_BTN_ATTR = 'data-hypothesis-trigger';
* trigger data attribute. * trigger data attribute.
* *
* @param {Element} rootEl - The DOM element which contains the trigger elements. * @param {Element} rootEl - The DOM element which contains the trigger elements.
* @param {object} showFn - Function which shows the sidebar. * @param {() => void} showFn - Function which shows the sidebar.
*/ */
export function sidebarTrigger(rootEl, showFn) { export function sidebarTrigger(rootEl, showFn) {
const triggerElems = rootEl.querySelectorAll( const triggerElems = rootEl.querySelectorAll(
'[' + SIDEBAR_TRIGGER_BTN_ATTR + ']' '[' + SIDEBAR_TRIGGER_BTN_ATTR + ']'
......
/** /**
* Load stylesheets for annotator UI components into the shadow DOM root. * Load stylesheets for annotator UI components into the shadow DOM root.
*
* @param {ShadowRoot} shadowRoot
*/ */
function loadStyles(shadowRoot) { function loadStyles(shadowRoot) {
// Find the preloaded stylesheet added by the boot script. // Find the preloaded stylesheet added by the boot script.
......
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