Commit cb86891a authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Migrate annotator config index to TypeScript

parent c70c9412
import { toBoolean } from '../../shared/type-coercions'; import { toBoolean } from '../../shared/type-coercions';
import { isBrowserExtension } from './is-browser-extension'; import { isBrowserExtension } from './is-browser-extension';
import { settingsFrom } from './settings'; import { settingsFrom } from './settings';
import type { SettingsGetters } from './settings';
import { urlFromLinkTag } from './url-from-link-tag'; import { urlFromLinkTag } from './url-from-link-tag';
/** type ValueGetter = (settings: SettingsGetters, name: string) => any;
* @typedef {import('./settings').SettingsGetters} SettingsGetters
* @typedef {(settings: SettingsGetters, name: string) => any} ValueGetter type ConfigDefinition = {
* /** Method to retrieve the value from the incoming source */
* @typedef ConfigDefinition getValue: ValueGetter;
* @prop {ValueGetter} getValue - Method to retrieve the value from the incoming source
* @prop {boolean} allowInBrowserExt - /**
* Allow this setting to be read in the browser extension. If this is false * Allow this setting to be read in the browser extension. If this is false
* and browser extension context is true, use `defaultValue` if provided otherwise * and browser extension context is true, use `defaultValue` if provided
* ignore the config key * otherwise ignore the config key
* @prop {any} [defaultValue] - Sets a default if `getValue` returns undefined */
* @prop {(value: any) => any} [coerce] - Transform a value's type, value or both allowInBrowserExt: boolean;
*
* @typedef {Record<string, ConfigDefinition>} ConfigDefinitionMap /** Sets a default if `getValue` returns undefined */
*/ defaultValue?: any;
/** Transform a value's type, value or both */
coerce?: (value: any) => any;
};
type ConfigDefinitionMap = Record<string, ConfigDefinition>;
/** /**
* Named subset of the Hypothesis client configuration that is relevant in * Named subset of the Hypothesis client configuration that is relevant in
* a particular context. * a particular context.
*
* @typedef {'sidebar'|'notebook'|'profile'|'annotator'|'all'} Context
*/ */
type Context = 'sidebar' | 'notebook' | 'profile' | 'annotator' | 'all';
/** /**
* Returns the configuration keys that are relevant to a particular context. * Returns the configuration keys that are relevant to a particular context.
*
* @param {Context} context
*/ */
function configurationKeys(context) { function configurationKeys(context: Context): string[] {
const contexts = { const contexts = {
annotator: ['clientUrl', 'contentInfoBanner', 'subFrameIdentifier'], annotator: ['clientUrl', 'contentInfoBanner', 'subFrameIdentifier'],
sidebar: [ sidebar: [
...@@ -81,17 +84,13 @@ function configurationKeys(context) { ...@@ -81,17 +84,13 @@ function configurationKeys(context) {
} }
} }
/** @type {ValueGetter} */ const getHostPageSetting: ValueGetter = (settings, name) =>
function getHostPageSetting(settings, name) { settings.hostPageSetting(name);
return settings.hostPageSetting(name);
}
/** /**
* Definitions of configuration keys * Definitions of configuration keys
*
* @type {ConfigDefinitionMap}
*/ */
const configDefinitions = { const configDefinitions: ConfigDefinitionMap = {
annotations: { annotations: {
allowInBrowserExt: true, allowInBrowserExt: true,
defaultValue: null, defaultValue: null,
...@@ -214,18 +213,14 @@ const configDefinitions = { ...@@ -214,18 +213,14 @@ const configDefinitions = {
* the embedder, the boot script also passes some additional configuration * the embedder, the boot script also passes some additional configuration
* to the annotator, such as URLs of the various sub-applications and the * to the annotator, such as URLs of the various sub-applications and the
* boot script itself. * boot script itself.
*
* @param {Context} context
*/ */
export function getConfig(context, window_ = window) { export function getConfig(context: Context, window_: Window = window) {
const settings = settingsFrom(window_); const settings = settingsFrom(window_);
const config: Record<string, unknown> = {};
/** @type {Record<string, unknown>} */
const config = {};
// Filter the config based on the application context as some config values // Filter the config based on the application context as some config values
// may be inappropriate or erroneous for some applications. // may be inappropriate or erroneous for some applications.
for (let key of configurationKeys(context)) { for (const key of configurationKeys(context)) {
const configDef = configDefinitions[key]; const configDef = configDefinitions[key];
const hasDefault = configDef.defaultValue !== undefined; // A default could be null const hasDefault = configDef.defaultValue !== undefined; // A default could be null
const isURLFromBrowserExtension = isBrowserExtension( const isURLFromBrowserExtension = isBrowserExtension(
......
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