Commit 77867094 authored by Robert Knight's avatar Robert Knight

Convert src/annotator/util to ES modules

parent cbdaaf0f
......@@ -4,18 +4,18 @@
* @param {Element} container
* @return {HTMLIFrameElement[]}
*/
function findFrames(container) {
export function findFrames(container) {
const frames = Array.from(container.getElementsByTagName('iframe'));
return frames.filter(shouldEnableAnnotation);
}
// Check if the iframe has already been injected
function hasHypothesis(iframe) {
export function hasHypothesis(iframe) {
return iframe.contentWindow.__hypothesis_frame === true;
}
// Inject embed.js into the iframe
function injectHypothesis(iframe, scriptUrl, config) {
export function injectHypothesis(iframe, scriptUrl, config) {
const configElement = document.createElement('script');
configElement.className = 'js-hypothesis-config';
configElement.type = 'application/json';
......@@ -32,7 +32,7 @@ function injectHypothesis(iframe, scriptUrl, config) {
}
// Check if we can access this iframe's document
function isAccessible(iframe) {
export function isAccessible(iframe) {
try {
return !!iframe.contentDocument;
} catch (e) {
......@@ -63,7 +63,7 @@ function shouldEnableAnnotation(iframe) {
return isNotClientFrame && enabled;
}
function isDocumentReady(iframe, callback) {
export function isDocumentReady(iframe, callback) {
if (iframe.contentDocument.readyState === 'loading') {
iframe.contentDocument.addEventListener('DOMContentLoaded', function() {
callback();
......@@ -73,7 +73,7 @@ function isDocumentReady(iframe, callback) {
}
}
function isLoaded(iframe, callback) {
export function isLoaded(iframe, callback) {
if (iframe.contentDocument.readyState !== 'complete') {
iframe.addEventListener('load', function() {
callback();
......@@ -82,12 +82,3 @@ function isLoaded(iframe, callback) {
callback();
}
}
module.exports = {
findFrames: findFrames,
hasHypothesis: hasHypothesis,
injectHypothesis: injectHypothesis,
isAccessible: isAccessible,
isLoaded: isLoaded,
isDocumentReady: isDocumentReady,
};
......@@ -3,7 +3,7 @@
* values using the Observable API.
*/
const Observable = require('zen-observable');
import Observable from 'zen-observable';
/**
* Returns an observable of events emitted by a DOM event source
......@@ -12,7 +12,7 @@ const Observable = require('zen-observable');
* @param {EventTarget} src - The event source.
* @param {Array<string>} eventNames - List of events to subscribe to
*/
function listen(src, eventNames) {
export function listen(src, eventNames) {
return new Observable(function(observer) {
const onNext = function(event) {
observer.next(event);
......@@ -33,7 +33,7 @@ function listen(src, eventNames) {
/**
* Delay events from a source Observable by `delay` ms.
*/
function delay(delay, src) {
export function delay(delay, src) {
return new Observable(function(obs) {
let timeouts = [];
const sub = src.subscribe({
......@@ -62,7 +62,7 @@ function delay(delay, src) {
* @param {Observable<T>} src
* @return {Observable<T>}
*/
function buffer(delay, src) {
export function buffer(delay, src) {
return new Observable(function(obs) {
let lastValue;
let timeout;
......@@ -92,7 +92,7 @@ function buffer(delay, src) {
* @param {Array<Observable>} sources
* @return Observable
*/
function merge(sources) {
export function merge(sources) {
return new Observable(function(obs) {
const subs = sources.map(function(src) {
return src.subscribe({
......@@ -111,7 +111,7 @@ function merge(sources) {
}
/** Drop the first `n` events from the `src` Observable. */
function drop(src, n) {
export function drop(src, n) {
let count = 0;
return src.filter(function() {
++count;
......@@ -119,11 +119,4 @@ function drop(src, n) {
});
}
module.exports = {
buffer: buffer,
delay: delay,
drop: drop,
listen: listen,
merge: merge,
Observable: Observable,
};
export { Observable };
const baseURI = require('document-base-uri');
import baseURI from 'document-base-uri';
/**
* Return a normalized version of a URI.
......@@ -9,7 +9,7 @@ const baseURI = require('document-base-uri');
* @param {string} [base] - Base URL to resolve relative to. Defaults to
* the document's base URL.
*/
function normalizeURI(uri, base = baseURI) {
export function normalizeURI(uri, base = baseURI) {
const absUrl = new URL(uri, base).href;
// Remove the fragment identifier.
......@@ -18,7 +18,3 @@ function normalizeURI(uri, base = baseURI) {
// See https://github.com/hypothesis/h/issues/3471#issuecomment-226713750
return absUrl.toString().replace(/#.*/, '');
}
module.exports = {
normalizeURI,
};
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