Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
cb86891a
Commit
cb86891a
authored
Mar 31, 2023
by
Alejandro Celaya
Committed by
Alejandro Celaya
Apr 10, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate annotator config index to TypeScript
parent
c70c9412
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
34 deletions
+29
-34
index.ts
src/annotator/config/index.ts
+29
-34
No files found.
src/annotator/config/index.
j
s
→
src/annotator/config/index.
t
s
View file @
cb86891a
import
{
toBoolean
}
from
'../../shared/type-coercions'
;
import
{
isBrowserExtension
}
from
'./is-browser-extension'
;
import
{
settingsFrom
}
from
'./settings'
;
import
type
{
SettingsGetters
}
from
'./settings'
;
import
{
urlFromLinkTag
}
from
'./url-from-link-tag'
;
/**
* @typedef {import('./settings').SettingsGetters} SettingsGetters
* @typedef {(settings: SettingsGetters, name: string) => any} ValueGetter
*
* @typedef ConfigDefinition
* @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
* and browser extension context is true, use `defaultValue` if provided 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
*
* @typedef {Record<string, ConfigDefinition>} ConfigDefinitionMap
*/
type
ValueGetter
=
(
settings
:
SettingsGetters
,
name
:
string
)
=>
any
;
type
ConfigDefinition
=
{
/** Method to retrieve the value from the incoming source */
getValue
:
ValueGetter
;
/**
* 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 ignore the config key
*/
allowInBrowserExt
:
boolean
;
/** 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
* 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.
*
* @param {Context} context
*/
function
configurationKeys
(
context
)
{
function
configurationKeys
(
context
:
Context
):
string
[]
{
const
contexts
=
{
annotator
:
[
'clientUrl'
,
'contentInfoBanner'
,
'subFrameIdentifier'
],
sidebar
:
[
...
...
@@ -81,17 +84,13 @@ function configurationKeys(context) {
}
}
/** @type {ValueGetter} */
function
getHostPageSetting
(
settings
,
name
)
{
return
settings
.
hostPageSetting
(
name
);
}
const
getHostPageSetting
:
ValueGetter
=
(
settings
,
name
)
=>
settings
.
hostPageSetting
(
name
);
/**
* Definitions of configuration keys
*
* @type {ConfigDefinitionMap}
*/
const
configDefinitions
=
{
const
configDefinitions
:
ConfigDefinitionMap
=
{
annotations
:
{
allowInBrowserExt
:
true
,
defaultValue
:
null
,
...
...
@@ -214,18 +213,14 @@ const configDefinitions = {
* the embedder, the boot script also passes some additional configuration
* to the annotator, such as URLs of the various sub-applications and the
* boot script itself.
*
* @param {Context} context
*/
export
function
getConfig
(
context
,
window_
=
window
)
{
export
function
getConfig
(
context
:
Context
,
window_
:
Window
=
window
)
{
const
settings
=
settingsFrom
(
window_
);
/** @type {Record<string, unknown>} */
const
config
=
{};
const
config
:
Record
<
string
,
unknown
>
=
{};
// Filter the config based on the application context as some config values
// may be inappropriate or erroneous for some applications.
for
(
le
t
key
of
configurationKeys
(
context
))
{
for
(
cons
t
key
of
configurationKeys
(
context
))
{
const
configDef
=
configDefinitions
[
key
];
const
hasDefault
=
configDef
.
defaultValue
!==
undefined
;
// A default could be null
const
isURLFromBrowserExtension
=
isBrowserExtension
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment