Commit 2b09fdc9 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Migrate annotator config settings to TypeScript

parent 06a0a5bb
...@@ -4,45 +4,36 @@ import { toBoolean } from '../../shared/type-coercions'; ...@@ -4,45 +4,36 @@ import { toBoolean } from '../../shared/type-coercions';
import { configFuncSettingsFrom } from './config-func-settings-from'; import { configFuncSettingsFrom } from './config-func-settings-from';
import { urlFromLinkTag } from './url-from-link-tag'; import { urlFromLinkTag } from './url-from-link-tag';
/** export type SettingsGetters = {
* @typedef SettingsGetters annotations: string | null;
* @prop {string|null} annotations query: string | null;
* @prop {string|null} query group: string | null;
* @prop {string|null} group showHighlights: string;
* @prop {string} showHighlights clientUrl: string;
* @prop {string} clientUrl sidebarAppUrl: string;
* @prop {string} sidebarAppUrl notebookAppUrl: string;
* @prop {string} notebookAppUrl profileAppUrl: string;
* @prop {string} profileAppUrl hostPageSetting: (name: string) => unknown;
* @prop {(name: string) => unknown} hostPageSetting };
*/
/** /**
* Discard a setting if it is not a string. * Discard a setting if it is not a string.
*
* @param {unknown} value
*/ */
function checkIfString(value) { function checkIfString(value: unknown): string | null {
return typeof value === 'string' ? value : null; return typeof value === 'string' ? value : null;
} }
/** export function settingsFrom(window_: Window): SettingsGetters {
* @param {Window} window_
* @return {SettingsGetters}
*/
export function settingsFrom(window_) {
// Prioritize the `window.hypothesisConfig` function over the JSON format // Prioritize the `window.hypothesisConfig` function over the JSON format
// Via uses `window.hypothesisConfig` and makes it non-configurable and non-writable. // Via uses `window.hypothesisConfig` and makes it non-configurable and non-writable.
// In addition, Via sets the `ignoreOtherConfiguration` option to prevent configuration merging. // In addition, Via sets the `ignoreOtherConfiguration` option to prevent configuration merging.
const configFuncSettings = configFuncSettingsFrom(window_); const configFuncSettings = configFuncSettingsFrom(window_);
/** @type {Record<string, unknown>} */ const jsonConfigs: Record<string, unknown> = toBoolean(
let jsonConfigs; configFuncSettings.ignoreOtherConfiguration
if (toBoolean(configFuncSettings.ignoreOtherConfiguration)) { )
jsonConfigs = {}; ? {}
} else { : parseJsonConfig(window_.document);
jsonConfigs = parseJsonConfig(window_.document);
}
/** /**
* Return the `#annotations:*` ID from the given URL's fragment. * Return the `#annotations:*` ID from the given URL's fragment.
...@@ -50,9 +41,9 @@ export function settingsFrom(window_) { ...@@ -50,9 +41,9 @@ export function settingsFrom(window_) {
* If the URL contains a `#annotations:<ANNOTATION_ID>` fragment then return * If the URL contains a `#annotations:<ANNOTATION_ID>` fragment then return
* the annotation ID extracted from the fragment. Otherwise return `null`. * the annotation ID extracted from the fragment. Otherwise return `null`.
* *
* @return {string|null} - The extracted ID, or null. * @return The extracted ID, or null.
*/ */
function annotations() { function annotations(): string | null {
/** Return the annotations from the URL, or null. */ /** Return the annotations from the URL, or null. */
function annotationsFromURL() { function annotationsFromURL() {
// Annotation IDs are url-safe-base64 identifiers // Annotation IDs are url-safe-base64 identifiers
...@@ -75,9 +66,9 @@ export function settingsFrom(window_) { ...@@ -75,9 +66,9 @@ export function settingsFrom(window_) {
* If the URL contains a `#annotations:group:<GROUP_ID>` fragment then return * If the URL contains a `#annotations:group:<GROUP_ID>` fragment then return
* the group ID extracted from the fragment. Otherwise return `null`. * the group ID extracted from the fragment. Otherwise return `null`.
* *
* @return {string|null} - The extracted ID, or null. * @return The extracted ID, or null.
*/ */
function group() { function group(): string | null {
function groupFromURL() { function groupFromURL() {
const groupFragmentMatch = window_.location.href.match( const groupFragmentMatch = window_.location.href.match(
/#annotations:group:([A-Za-z0-9_-]+)$/ /#annotations:group:([A-Za-z0-9_-]+)$/
...@@ -119,9 +110,9 @@ export function settingsFrom(window_) { ...@@ -119,9 +110,9 @@ export function settingsFrom(window_) {
* *
* Otherwise return null. * Otherwise return null.
* *
* @return {string|null} - The config.query setting, or null. * @return The config.query setting, or null.
*/ */
function query() { function query(): string | null {
/** Return the query from the URL, or null. */ /** Return the query from the URL, or null. */
function queryFromURL() { function queryFromURL() {
const queryFragmentMatch = window_.location.href.match( const queryFragmentMatch = window_.location.href.match(
...@@ -148,9 +139,9 @@ export function settingsFrom(window_) { ...@@ -148,9 +139,9 @@ export function settingsFrom(window_) {
* *
* If the setting is not found in either source, then return undefined. * If the setting is not found in either source, then return undefined.
* *
* @param {string} name - Unique name of the setting * @param name - Unique name of the setting
*/ */
function hostPageSetting(name) { function hostPageSetting(name: string) {
if (hasOwn(configFuncSettings, name)) { if (hasOwn(configFuncSettings, name)) {
return configFuncSettings[name]; return configFuncSettings[name];
} }
......
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