Commit 3deaed68 authored by Robert Knight's avatar Robert Knight

Convert annotator entry point to TypeScript

parent 146898d6
...@@ -101,7 +101,7 @@ function bundleConfig({ name, entry, format = 'es' }) { ...@@ -101,7 +101,7 @@ function bundleConfig({ name, entry, format = 'es' }) {
export default [ export default [
bundleConfig({ bundleConfig({
name: 'annotator', name: 'annotator',
entry: 'src/annotator/index.js', entry: 'src/annotator/index.ts',
// The annotator bundle is created as a non-module script because module // The annotator bundle is created as a non-module script because module
// scripts are not supported in Safari in XHTML documents. // scripts are not supported in Safari in XHTML documents.
......
...@@ -13,34 +13,27 @@ import { ...@@ -13,34 +13,27 @@ import {
PortProvider, PortProvider,
installPortCloseWorkaroundForSafari, installPortCloseWorkaroundForSafari,
} from '../shared/messaging'; } from '../shared/messaging';
import type { Destroyable } from '../types/annotator';
import { getConfig } from './config/index'; import { getConfig } from './config/index';
import type { NotebookConfig } from './components/NotebookModal';
import { Guest } from './guest'; import { Guest } from './guest';
import type { GuestConfig } from './guest';
import { HypothesisInjector } from './hypothesis-injector'; import { HypothesisInjector } from './hypothesis-injector';
import type { InjectConfig } from './hypothesis-injector';
import { import {
VitalSourceInjector, VitalSourceInjector,
vitalSourceFrameRole, vitalSourceFrameRole,
} from './integrations/vitalsource'; } from './integrations/vitalsource';
import { Notebook } from './notebook'; import { Notebook } from './notebook';
import { Sidebar } from './sidebar'; import { Sidebar } from './sidebar';
import type { SidebarConfig } from './sidebar';
import { EventBus } from './util/emitter'; import { EventBus } from './util/emitter';
/** @typedef {import('../types/annotator').Destroyable} Destroyable */
// Look up the URL of the sidebar. This element is added to the page by the // Look up the URL of the sidebar. This element is added to the page by the
// boot script before the "annotator" bundle loads. // boot script before the "annotator" bundle loads.
const sidebarLinkElement = /** @type {HTMLLinkElement} */ ( const sidebarLinkElement = document.querySelector(
document.querySelector(
'link[type="application/annotator+html"][rel="sidebar"]' 'link[type="application/annotator+html"][rel="sidebar"]'
) ) as HTMLLinkElement;
);
/**
* @typedef {import('./components/NotebookModal').NotebookConfig} NotebookConfig
* @typedef {import('./guest').GuestConfig} GuestConfig
* @typedef {import('./hypothesis-injector').InjectConfig} InjectConfig
* @typedef {import('./sidebar').SidebarConfig} SidebarConfig
* @typedef {import('./sidebar').SidebarContainerConfig} SidebarContainerConfig
*/
/** /**
* Entry point for the part of the Hypothesis client that runs in the page being * Entry point for the part of the Hypothesis client that runs in the page being
...@@ -55,21 +48,18 @@ const sidebarLinkElement = /** @type {HTMLLinkElement} */ ( ...@@ -55,21 +48,18 @@ const sidebarLinkElement = /** @type {HTMLLinkElement} */ (
* client is initially loaded, is also the only guest frame. * client is initially loaded, is also the only guest frame.
*/ */
function init() { function init() {
const annotatorConfig = /** @type {GuestConfig & InjectConfig} */ ( const annotatorConfig = getConfig('annotator') as GuestConfig & InjectConfig;
getConfig('annotator')
);
const hostFrame = annotatorConfig.subFrameIdentifier ? window.parent : window; const hostFrame = annotatorConfig.subFrameIdentifier ? window.parent : window;
/** @type {Destroyable[]} */ const destroyables = [] as Destroyable[];
const destroyables = [];
if (hostFrame === window) { if (hostFrame === window) {
// Ensure port "close" notifications from eg. guest frames are delivered properly. // Ensure port "close" notifications from eg. guest frames are delivered properly.
const removeWorkaround = installPortCloseWorkaroundForSafari(); const removeWorkaround = installPortCloseWorkaroundForSafari();
destroyables.push({ destroy: removeWorkaround }); destroyables.push({ destroy: removeWorkaround });
const sidebarConfig = /** @type {SidebarConfig} */ (getConfig('sidebar')); const sidebarConfig = getConfig('sidebar') as SidebarConfig;
const hypothesisAppsOrigin = new URL(sidebarConfig.sidebarAppUrl).origin; const hypothesisAppsOrigin = new URL(sidebarConfig.sidebarAppUrl).origin;
const portProvider = new PortProvider(hypothesisAppsOrigin); const portProvider = new PortProvider(hypothesisAppsOrigin);
...@@ -79,7 +69,7 @@ function init() { ...@@ -79,7 +69,7 @@ function init() {
const notebook = new Notebook( const notebook = new Notebook(
document.body, document.body,
eventBus, eventBus,
/** @type {NotebookConfig} */ (getConfig('notebook')) getConfig('notebook') as NotebookConfig
); );
portProvider.on('frameConnected', (source, port) => portProvider.on('frameConnected', (source, port) =>
...@@ -116,10 +106,8 @@ function init() { ...@@ -116,10 +106,8 @@ function init() {
/** /**
* Returns a Promise that resolves when the document has loaded (but subresources * Returns a Promise that resolves when the document has loaded (but subresources
* may still be loading). * may still be loading).
*
* @return {Promise<void>}
*/ */
function documentReady() { function documentReady(): Promise<void> {
return new Promise(resolve => { return new Promise(resolve => {
if (document.readyState !== 'loading') { if (document.readyState !== 'loading') {
resolve(); resolve();
......
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