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
Show 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
{
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
*
* @typedef {Record<string, ConfigDefinition>} ConfigDefinitionMap
*/
*/
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
* 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
(
le
t
key
of
configurationKeys
(
context
))
{
for
(
cons
t
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
(
...
...
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