Commit 7757e7cf authored by Robert Knight's avatar Robert Knight

Fix typechecking errors in src/annotator/{index, pdf-sidebar}

CoffeeScript imports are marked as expected errors for the moment. They
will be checked once those files are converted.
parent 722ebc1a
/* global process */
/**
* @typedef {import('../types/annotator').HypothesisWindow} HypothesisWindow
*/
import $ from 'jquery';
// Load polyfill for :focus-visible pseudo-class.
......@@ -16,12 +20,20 @@ import iconSet from './icons';
registerIcons(iconSet);
import configFrom from './config/index';
import Guest from './guest';
import PdfSidebar from './pdf-sidebar';
import DocumentPlugin from './plugin/document';
// Modules that are still written in CoffeeScript and need to be converted to
// JS.
// @ts-expect-error
import Guest from './guest';
// @ts-expect-error
import BucketBarPlugin from './plugin/bucket-bar';
// @ts-expect-error
import CrossFramePlugin from './plugin/cross-frame';
import DocumentPlugin from './plugin/document';
// @ts-expect-error
import PDFPlugin from './plugin/pdf';
// @ts-expect-error
import Sidebar from './sidebar';
const pluginClasses = {
......@@ -36,13 +48,20 @@ const pluginClasses = {
CrossFrame: CrossFramePlugin,
};
const appLinkEl = document.querySelector(
const window_ = /** @type {HypothesisWindow} */ (window);
// Look up the URL of the sidebar. This element is added to the page by the
// boot script before the "annotator" bundle loads.
const appLinkEl = /** @type {Element} */ (document.querySelector(
'link[type="application/annotator+html"][rel="sidebar"]'
);
));
const config = configFrom(window);
$.noConflict(true)(function () {
let Klass = window.PDFViewerApplication ? PdfSidebar : Sidebar;
const isPDF = typeof window_.PDFViewerApplication !== 'undefined';
let Klass = isPDF ? PdfSidebar : Sidebar;
if (config.hasOwnProperty('constructor')) {
Klass = config.constructor;
......@@ -51,7 +70,7 @@ $.noConflict(true)(function () {
if (config.subFrameIdentifier) {
// Make sure the PDF plugin is loaded if the subframe contains the PDF.js viewer.
if (typeof window.PDFViewerApplication !== 'undefined') {
if (isPDF) {
config.PDF = {};
}
Klass = Guest;
......@@ -59,14 +78,14 @@ $.noConflict(true)(function () {
// Other modules use this to detect if this
// frame context belongs to hypothesis.
// Needs to be a global property that's set.
window.__hypothesis_frame = true;
window_.__hypothesis_frame = true;
}
config.pluginClasses = pluginClasses;
const annotator = new Klass(document.body, config);
appLinkEl.addEventListener('destroy', function () {
appLinkEl.parentElement.removeChild(appLinkEl);
appLinkEl.remove();
annotator.destroy();
});
});
// @ts-expect-error - './sidebar' needs to be converted from JS.
import Sidebar from './sidebar';
const DEFAULT_CONFIG = {
......
......@@ -32,5 +32,20 @@
* @prop {string} [documentFingerprint]
*/
/**
* Global variables which the Hypothesis client looks for on the `window` object
* when loaded in a frame that influence how it behaves.
*
* @typedef Globals
* @prop {Object} [PDFViewerApplication] -
* PDF.js entry point. If set triggers loading of PDF rather than HTML integration.
* @prop {boolean} [__hypothesis_frame] -
* Flag used to indicate that Hypothesis is loaded in the current frame.
*/
/**
* @typedef {Window & Globals} HypothesisWindow
*/
// Make TypeScript treat this file as a module.
export const unused = {};
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