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 */ /* global process */
/**
* @typedef {import('../types/annotator').HypothesisWindow} HypothesisWindow
*/
import $ from 'jquery'; import $ from 'jquery';
// Load polyfill for :focus-visible pseudo-class. // Load polyfill for :focus-visible pseudo-class.
...@@ -16,12 +20,20 @@ import iconSet from './icons'; ...@@ -16,12 +20,20 @@ import iconSet from './icons';
registerIcons(iconSet); registerIcons(iconSet);
import configFrom from './config/index'; import configFrom from './config/index';
import Guest from './guest';
import PdfSidebar from './pdf-sidebar'; 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'; import BucketBarPlugin from './plugin/bucket-bar';
// @ts-expect-error
import CrossFramePlugin from './plugin/cross-frame'; import CrossFramePlugin from './plugin/cross-frame';
import DocumentPlugin from './plugin/document'; // @ts-expect-error
import PDFPlugin from './plugin/pdf'; import PDFPlugin from './plugin/pdf';
// @ts-expect-error
import Sidebar from './sidebar'; import Sidebar from './sidebar';
const pluginClasses = { const pluginClasses = {
...@@ -36,13 +48,20 @@ const pluginClasses = { ...@@ -36,13 +48,20 @@ const pluginClasses = {
CrossFrame: CrossFramePlugin, 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"]' 'link[type="application/annotator+html"][rel="sidebar"]'
); ));
const config = configFrom(window); const config = configFrom(window);
$.noConflict(true)(function () { $.noConflict(true)(function () {
let Klass = window.PDFViewerApplication ? PdfSidebar : Sidebar; const isPDF = typeof window_.PDFViewerApplication !== 'undefined';
let Klass = isPDF ? PdfSidebar : Sidebar;
if (config.hasOwnProperty('constructor')) { if (config.hasOwnProperty('constructor')) {
Klass = config.constructor; Klass = config.constructor;
...@@ -51,7 +70,7 @@ $.noConflict(true)(function () { ...@@ -51,7 +70,7 @@ $.noConflict(true)(function () {
if (config.subFrameIdentifier) { if (config.subFrameIdentifier) {
// Make sure the PDF plugin is loaded if the subframe contains the PDF.js viewer. // Make sure the PDF plugin is loaded if the subframe contains the PDF.js viewer.
if (typeof window.PDFViewerApplication !== 'undefined') { if (isPDF) {
config.PDF = {}; config.PDF = {};
} }
Klass = Guest; Klass = Guest;
...@@ -59,14 +78,14 @@ $.noConflict(true)(function () { ...@@ -59,14 +78,14 @@ $.noConflict(true)(function () {
// Other modules use this to detect if this // Other modules use this to detect if this
// frame context belongs to hypothesis. // frame context belongs to hypothesis.
// Needs to be a global property that's set. // Needs to be a global property that's set.
window.__hypothesis_frame = true; window_.__hypothesis_frame = true;
} }
config.pluginClasses = pluginClasses; config.pluginClasses = pluginClasses;
const annotator = new Klass(document.body, config); const annotator = new Klass(document.body, config);
appLinkEl.addEventListener('destroy', function () { appLinkEl.addEventListener('destroy', function () {
appLinkEl.parentElement.removeChild(appLinkEl); appLinkEl.remove();
annotator.destroy(); annotator.destroy();
}); });
}); });
// @ts-expect-error - './sidebar' needs to be converted from JS.
import Sidebar from './sidebar'; import Sidebar from './sidebar';
const DEFAULT_CONFIG = { const DEFAULT_CONFIG = {
......
...@@ -32,5 +32,20 @@ ...@@ -32,5 +32,20 @@
* @prop {string} [documentFingerprint] * @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. // Make TypeScript treat this file as a module.
export const unused = {}; 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