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
2b8b9411
Commit
2b8b9411
authored
May 18, 2023
by
Alejandro Celaya
Committed by
Alejandro Celaya
May 18, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate TagsService to TS
parent
797e231e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
20 deletions
+21
-20
tags.ts
src/sidebar/services/tags.ts
+21
-20
No files found.
src/sidebar/services/tags.
j
s
→
src/sidebar/services/tags.
t
s
View file @
2b8b9411
import
escapeStringRegexp
from
'escape-string-regexp'
;
/**
* @typedef Tag
* @prop {string} text - The label of the tag
* @prop {number} count - The number of times this tag has been used.
* @prop {number} updated - The timestamp when this tag was last used.
*/
import
type
{
LocalStorageService
}
from
'./local-storage'
;
type
Tag
=
{
/** The label of the tag */
text
:
string
;
/** The number of times this tag has been used. */
count
:
number
;
/** The timestamp when this tag was last used. */
updated
:
number
;
};
const
TAGS_LIST_KEY
=
'hypothesis.user.tags.list'
;
const
TAGS_MAP_KEY
=
'hypothesis.user.tags.map'
;
...
...
@@ -19,24 +23,23 @@ const TAGS_MAP_KEY = 'hypothesis.user.tags.map';
*/
// @inject
export
class
TagsService
{
private
_storage
:
LocalStorageService
;
/**
* @param {import('./local-storage').LocalStorageService} localStorage -
* Storage used to persist the tags
* @param localStorage - Storage used to persist the tags
*/
constructor
(
localStorage
)
{
constructor
(
localStorage
:
LocalStorageService
)
{
this
.
_storage
=
localStorage
;
}
/**
* Return a list of tag suggestions matching `query`.
*
* @param {string} query
* @param {number|null} limit - Optional limit of the results.
* @return {string[]} List of matching tags
* @param limit - Optional limit of the results.
* @return List of matching tags
*/
filter
(
query
,
limit
=
null
)
{
/** @type {string[]} */
const
savedTags
=
this
.
_storage
.
getObject
(
TAGS_LIST_KEY
)
||
[];
filter
(
query
:
string
,
limit
:
number
|
null
=
null
):
string
[]
{
const
savedTags
=
this
.
_storage
.
getObject
<
string
[]
>
(
TAGS_LIST_KEY
)
||
[];
let
resultCount
=
0
;
// Match any tag where the query is a prefix of the tag or a word within the tag.
return
savedTags
.
filter
(
tag
=>
{
...
...
@@ -62,13 +65,11 @@ export class TagsService {
/**
* Update the list of stored tag suggestions based on the tags that a user has
* entered for a given annotation.
*
* @param {string[]} tags - List of tags.
*/
store
(
tags
)
{
store
(
tags
:
string
[]
)
{
// Update the stored (tag, frequency) map.
/** @type Record<string, { text: string; count: number; updated: number }> */
const
savedTags
=
this
.
_storage
.
getObject
(
TAGS_MAP_KEY
)
||
{};
const
savedTags
:
Record
<
string
,
Tag
>
=
this
.
_storage
.
getObject
(
TAGS_MAP_KEY
)
||
{};
tags
.
forEach
(
tag
=>
{
if
(
savedTags
[
tag
])
{
savedTags
[
tag
].
count
+=
1
;
...
...
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