@annotorious/core 3.0.0-pre-alpha-46 → 3.0.0-pre-alpha-48
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +26 -18
- package/src/lifecycle/Lifecycle.ts +15 -14
- package/src/model/Annotator.ts +51 -0
- package/src/model/FormatAdapter.ts +1 -1
- package/src/model/index.ts +5 -2
- package/src/model/w3c/W3CAnnotation.ts +4 -2
- package/src/state/SvelteStore.ts +23 -7
- package/src/utils/annotationUtils.ts +33 -0
- package/src/utils/index.ts +1 -2
- package/src/model/core/Annotator.ts +0 -47
- package/src/model/core/index.ts +0 -4
- package/src/utils/bodyUtils.ts +0 -23
- package/src/utils/collaboratorUtils.ts +0 -19
- /package/src/model/{core/Annotation.ts → Annotation.ts} +0 -0
- /package/src/model/{core/Formatter.ts → Formatter.ts} +0 -0
- /package/src/model/{core/User.ts → User.ts} +0 -0
package/package.json
CHANGED
|
@@ -1,34 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@annotorious/core",
|
|
3
|
-
"version": "3.0.0-pre-alpha-
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "3.0.0-pre-alpha-48",
|
|
4
|
+
"description": "Annotorious core types and functions",
|
|
5
|
+
"author": "Rainer Simon",
|
|
6
|
+
"license": "BSD-3-Clause",
|
|
7
|
+
"homepage": "https://annotorious.github.io",
|
|
5
8
|
"type": "module",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/annotorious/annotorious.git"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/annotorious/annotorious/issues"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "echo 'Skipping build in @annotorious/core package'",
|
|
18
|
+
"test": "vitest",
|
|
19
|
+
"coverage": "vitest run --coverage"
|
|
20
|
+
},
|
|
6
21
|
"main": "src/index.ts",
|
|
7
22
|
"types": "src/index.ts",
|
|
8
23
|
"files": [
|
|
9
24
|
"src"
|
|
10
25
|
],
|
|
11
|
-
"scripts": {
|
|
12
|
-
"build": "echo 'Skipping build in @annotorious/formats package'",
|
|
13
|
-
"test": "vitest",
|
|
14
|
-
"coverage": "vitest run --coverage"
|
|
15
|
-
},
|
|
16
|
-
"author": "Rainer Simon",
|
|
17
|
-
"license": "BSD-3-Clause",
|
|
18
26
|
"devDependencies": {
|
|
19
27
|
"@tsconfig/svelte": "^3.0.0",
|
|
20
|
-
"@types/deep-equal": "^1.0.
|
|
21
|
-
"@types/uuid": "^9.0.
|
|
22
|
-
"svelte": "^3.
|
|
28
|
+
"@types/deep-equal": "^1.0.2",
|
|
29
|
+
"@types/uuid": "^9.0.4",
|
|
30
|
+
"svelte": "^3.59.2",
|
|
23
31
|
"typescript": "^4.9.5",
|
|
24
|
-
"vite": "^4.
|
|
25
|
-
"vite-plugin-dts": "^
|
|
26
|
-
"vitest": "^0.
|
|
32
|
+
"vite": "^4.4.9",
|
|
33
|
+
"vite-plugin-dts": "^3.6.0",
|
|
34
|
+
"vitest": "^0.34.6"
|
|
27
35
|
},
|
|
28
36
|
"dependencies": {
|
|
29
37
|
"dequal": "^2.0.3",
|
|
30
|
-
"nanoevents": "^
|
|
31
|
-
"nanoid": "^
|
|
32
|
-
"uuid": "^9.0.
|
|
38
|
+
"nanoevents": "^8.0.0",
|
|
39
|
+
"nanoid": "^5.0.1",
|
|
40
|
+
"uuid": "^9.0.1"
|
|
33
41
|
}
|
|
34
42
|
}
|
|
@@ -1,28 +1,29 @@
|
|
|
1
1
|
import { dequal } from 'dequal/lite';
|
|
2
|
-
import type { Annotation, FormatAdapter
|
|
2
|
+
import type { Annotation, FormatAdapter } from '../model';
|
|
3
3
|
import { Origin } from '../state';
|
|
4
4
|
import type { HoverState, SelectionState, Store, ViewportState } from '../state';
|
|
5
5
|
import type { LifecycleEvents } from './LifecycleEvents';
|
|
6
6
|
|
|
7
|
-
export type Lifecycle<
|
|
7
|
+
export type Lifecycle<I extends Annotation, E extends unknown> =
|
|
8
|
+
ReturnType<typeof createLifecyleObserver<I, E>>;
|
|
8
9
|
|
|
9
|
-
export const createLifecyleObserver = <
|
|
10
|
-
store: Store<
|
|
11
|
-
selectionState: SelectionState<
|
|
12
|
-
hoverState: HoverState<
|
|
10
|
+
export const createLifecyleObserver = <I extends Annotation, E extends unknown>(
|
|
11
|
+
store: Store<I>,
|
|
12
|
+
selectionState: SelectionState<I>,
|
|
13
|
+
hoverState: HoverState<I>,
|
|
13
14
|
viewportState?: ViewportState,
|
|
14
|
-
adapter?: FormatAdapter<
|
|
15
|
+
adapter?: FormatAdapter<I, E>
|
|
15
16
|
) => {
|
|
16
|
-
const observers = new Map<string, LifecycleEvents<
|
|
17
|
+
const observers = new Map<string, LifecycleEvents<E>[keyof LifecycleEvents<E>][]>();
|
|
17
18
|
|
|
18
19
|
// The currently selected annotations, in the state when they were selected
|
|
19
|
-
let initialSelection:
|
|
20
|
+
let initialSelection: I[] = [];
|
|
20
21
|
|
|
21
22
|
let currentHover: string | undefined;
|
|
22
23
|
|
|
23
24
|
let idleTimeout: ReturnType<typeof setTimeout>;
|
|
24
25
|
|
|
25
|
-
const on = <
|
|
26
|
+
const on = <T extends keyof LifecycleEvents<E>>(event: T, callback: LifecycleEvents<E>[T]) => {
|
|
26
27
|
if (observers.has(event)) {
|
|
27
28
|
observers.get(event).push(callback);
|
|
28
29
|
} else {
|
|
@@ -30,7 +31,7 @@ export const createLifecyleObserver = <T extends Annotation, A extends unknown =
|
|
|
30
31
|
}
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
const off = <
|
|
34
|
+
const off = <T extends keyof LifecycleEvents<E>>(event: T, callback: LifecycleEvents<E>[T]) => {
|
|
34
35
|
const callbacks = observers.get(event);
|
|
35
36
|
if (callbacks) {
|
|
36
37
|
const idx = callbacks.indexOf(callback);
|
|
@@ -39,7 +40,7 @@ export const createLifecyleObserver = <T extends Annotation, A extends unknown =
|
|
|
39
40
|
}
|
|
40
41
|
}
|
|
41
42
|
|
|
42
|
-
const emit = (event: keyof LifecycleEvents<
|
|
43
|
+
const emit = (event: keyof LifecycleEvents<E>, arg0: I | I[], arg1: I = null) => {
|
|
43
44
|
if (observers.has(event)) {
|
|
44
45
|
setTimeout(() => {
|
|
45
46
|
observers.get(event).forEach(callback => {
|
|
@@ -49,9 +50,9 @@ export const createLifecyleObserver = <T extends Annotation, A extends unknown =
|
|
|
49
50
|
|
|
50
51
|
const serialized1 = arg1 && adapter.serialize(arg1);
|
|
51
52
|
|
|
52
|
-
callback(serialized0 as
|
|
53
|
+
callback(serialized0 as E & E[], serialized1);
|
|
53
54
|
} else {
|
|
54
|
-
callback(arg0 as
|
|
55
|
+
callback(arg0 as E & E[], arg1 as unknown as E);
|
|
55
56
|
}
|
|
56
57
|
});
|
|
57
58
|
}, 1);
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { Annotation } from './Annotation';
|
|
2
|
+
import type { User } from './User';
|
|
3
|
+
import type { PresenceProvider } from '../presence';
|
|
4
|
+
import type { HoverState, SelectionState, Store, ViewportState } from '../state';
|
|
5
|
+
import type { LifecycleEvents } from '../lifecycle';
|
|
6
|
+
import type { Formatter } from './Formatter';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Base annotator interface.
|
|
10
|
+
* I ... internal core data model
|
|
11
|
+
* E ... external adapted representation
|
|
12
|
+
*/
|
|
13
|
+
export interface Annotator<I extends Annotation = Annotation, E extends unknown = Annotation> {
|
|
14
|
+
|
|
15
|
+
addAnnotation(annotation: E): void;
|
|
16
|
+
|
|
17
|
+
getAnnotationById(id: string): E | undefined;
|
|
18
|
+
|
|
19
|
+
getAnnotations(): E[];
|
|
20
|
+
|
|
21
|
+
getUser(): User;
|
|
22
|
+
|
|
23
|
+
loadAnnotations(url: string): Promise<E[]>;
|
|
24
|
+
|
|
25
|
+
setAnnotations(annotations: E[]): void;
|
|
26
|
+
|
|
27
|
+
setFormatter(formatter: Formatter): void;
|
|
28
|
+
|
|
29
|
+
setUser(user: User): void;
|
|
30
|
+
|
|
31
|
+
setPresenceProvider?(provider: PresenceProvider): void;
|
|
32
|
+
|
|
33
|
+
on<T extends keyof LifecycleEvents<E>>(event: T, callback: LifecycleEvents<E>[T]): void;
|
|
34
|
+
|
|
35
|
+
off<T extends keyof LifecycleEvents<E>>(event: T, callback: LifecycleEvents<E>[T]): void;
|
|
36
|
+
|
|
37
|
+
state: AnnotatorState<I>;
|
|
38
|
+
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface AnnotatorState<A extends Annotation> {
|
|
42
|
+
|
|
43
|
+
store: Store<A>;
|
|
44
|
+
|
|
45
|
+
selection: SelectionState<A>;
|
|
46
|
+
|
|
47
|
+
hover: HoverState<A>;
|
|
48
|
+
|
|
49
|
+
viewport: ViewportState;
|
|
50
|
+
|
|
51
|
+
}
|
package/src/model/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { v4 as uuidv4 } from 'uuid';
|
|
2
|
-
import type { AnnotationBody } from '../
|
|
2
|
+
import type { AnnotationBody } from '../Annotation';
|
|
3
3
|
|
|
4
4
|
export interface W3CAnnotation {
|
|
5
5
|
|
|
@@ -67,7 +67,9 @@ export const parseBodies = (
|
|
|
67
67
|
purpose: b.purpose,
|
|
68
68
|
value: b.value,
|
|
69
69
|
created: b.created,
|
|
70
|
-
creator: b.creator ?
|
|
70
|
+
creator: b.creator ?
|
|
71
|
+
typeof b.creator === 'object' ? { ...b.creator }: b.creator :
|
|
72
|
+
undefined
|
|
71
73
|
}));
|
|
72
74
|
|
|
73
75
|
|
package/src/state/SvelteStore.ts
CHANGED
|
@@ -1,8 +1,26 @@
|
|
|
1
|
-
import type { Annotation } from '../model';
|
|
2
|
-
import {
|
|
1
|
+
import type { Annotation, Annotator, AnnotatorState } from '../model';
|
|
2
|
+
import type { Store } from './Store';
|
|
3
3
|
import type { StoreChangeEvent } from './StoreObserver';
|
|
4
4
|
|
|
5
|
-
type Subscriber
|
|
5
|
+
type Subscriber<T extends Annotation> = (annotation: T[]) => void;
|
|
6
|
+
|
|
7
|
+
export interface SvelteStore<T extends Annotation> extends Store<T> {
|
|
8
|
+
|
|
9
|
+
subscribe(onChange: Subscriber<T>): void;
|
|
10
|
+
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface SvelteAnnotatorState<T extends Annotation> extends AnnotatorState<T> {
|
|
14
|
+
|
|
15
|
+
store: SvelteStore<T>
|
|
16
|
+
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface SvelteAnnotator<T extends Annotation> extends Annotator<T> {
|
|
20
|
+
|
|
21
|
+
state: SvelteAnnotatorState<T>
|
|
22
|
+
|
|
23
|
+
}
|
|
6
24
|
|
|
7
25
|
/**
|
|
8
26
|
* A simple wrapper around the event-based store implementation
|
|
@@ -11,11 +29,9 @@ type Subscriber = <T extends Annotation>(annotation: T[]) => void;
|
|
|
11
29
|
* convenient for everyone using Svelte, as well as for the
|
|
12
30
|
* basic (Svelte-based) Annotorious standard implementation.
|
|
13
31
|
*/
|
|
14
|
-
export const
|
|
15
|
-
|
|
16
|
-
const store = createStore<T>();
|
|
32
|
+
export const toSvelteStore = <T extends Annotation>(store: Store<T>): SvelteStore<T> => {
|
|
17
33
|
|
|
18
|
-
const subscribe = (onChange: Subscriber) => {
|
|
34
|
+
const subscribe = (onChange: Subscriber<T>) => {
|
|
19
35
|
|
|
20
36
|
// Register a store observer on behalf of the subscriber
|
|
21
37
|
const shim = (event: StoreChangeEvent<T>) => onChange(event.state);
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
2
|
+
import type { Annotation, AnnotationBody, User } from '../model';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Returns all users listed as creators or updaters in any parts of this
|
|
6
|
+
* annotation.
|
|
7
|
+
*/
|
|
8
|
+
export const getContributors = (annotation: Annotation): User[] => {
|
|
9
|
+
const { creator, updatedBy } = annotation.target;
|
|
10
|
+
|
|
11
|
+
const bodyCollaborators = annotation.bodies.reduce((users, body) => (
|
|
12
|
+
[...users, body.creator, body.updatedBy]
|
|
13
|
+
), [] as User[]);
|
|
14
|
+
|
|
15
|
+
return [
|
|
16
|
+
creator,
|
|
17
|
+
updatedBy,
|
|
18
|
+
...bodyCollaborators
|
|
19
|
+
].filter(u => u); // Remove undefined
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const createBody = (
|
|
23
|
+
annotation: Annotation,
|
|
24
|
+
payload: { value: string, [key: string]: any },
|
|
25
|
+
created?: Date,
|
|
26
|
+
creator?: User
|
|
27
|
+
): AnnotationBody => ({
|
|
28
|
+
id: uuidv4(),
|
|
29
|
+
annotation: annotation.id,
|
|
30
|
+
created: created || new Date(),
|
|
31
|
+
creator,
|
|
32
|
+
...payload
|
|
33
|
+
});
|
package/src/utils/index.ts
CHANGED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import type { Annotation } from './Annotation';
|
|
2
|
-
import type { User } from './User';
|
|
3
|
-
import type { PresenceProvider } from '../../presence';
|
|
4
|
-
import type { HoverState, SelectionState, Store, ViewportState } from '../../state';
|
|
5
|
-
import type { LifecycleEvents } from '../../lifecycle';
|
|
6
|
-
import type { W3CAnnotation } from '../w3c';
|
|
7
|
-
import type { Formatter } from './Formatter';
|
|
8
|
-
|
|
9
|
-
export interface Annotator<A extends Annotation = Annotation, T extends unknown = W3CAnnotation> {
|
|
10
|
-
|
|
11
|
-
addAnnotation(annotation: T): void;
|
|
12
|
-
|
|
13
|
-
getAnnotationById(id: string): T | undefined;
|
|
14
|
-
|
|
15
|
-
getAnnotations(): T[];
|
|
16
|
-
|
|
17
|
-
getUser(): User;
|
|
18
|
-
|
|
19
|
-
loadAnnotations(url: string): Promise<T[]>;
|
|
20
|
-
|
|
21
|
-
setAnnotations(annotations: T[]): void;
|
|
22
|
-
|
|
23
|
-
setFormatter(formatter: Formatter): void;
|
|
24
|
-
|
|
25
|
-
setUser(user: User): void;
|
|
26
|
-
|
|
27
|
-
setPresenceProvider?(provider: PresenceProvider): void;
|
|
28
|
-
|
|
29
|
-
on<E extends keyof LifecycleEvents<T>>(event: E, callback: LifecycleEvents<T>[E]): void;
|
|
30
|
-
|
|
31
|
-
off<E extends keyof LifecycleEvents<T>>(event: E, callback: LifecycleEvents<T>[E]): void;
|
|
32
|
-
|
|
33
|
-
state: AnnotatorState<A>;
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface AnnotatorState<A extends Annotation> {
|
|
38
|
-
|
|
39
|
-
store: Store<A>;
|
|
40
|
-
|
|
41
|
-
selection: SelectionState<A>;
|
|
42
|
-
|
|
43
|
-
hover: HoverState<A>;
|
|
44
|
-
|
|
45
|
-
viewport: ViewportState;
|
|
46
|
-
|
|
47
|
-
}
|
package/src/model/core/index.ts
DELETED
package/src/utils/bodyUtils.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
2
|
-
import type { Annotation, AnnotationBody, User } from '../model';
|
|
3
|
-
|
|
4
|
-
export interface HasValue {
|
|
5
|
-
|
|
6
|
-
value: string;
|
|
7
|
-
|
|
8
|
-
[key:string]: any;
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export const createBody = (
|
|
13
|
-
annotation: Annotation,
|
|
14
|
-
payload: HasValue,
|
|
15
|
-
created?: Date,
|
|
16
|
-
creator?: User
|
|
17
|
-
): AnnotationBody => ({
|
|
18
|
-
id: uuidv4(),
|
|
19
|
-
annotation: annotation.id,
|
|
20
|
-
created: created || new Date(),
|
|
21
|
-
creator,
|
|
22
|
-
...payload
|
|
23
|
-
});
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { Annotation, User } from '../model';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Returns all users listed as creators or updaters in any parts of this
|
|
5
|
-
* annotation.
|
|
6
|
-
*/
|
|
7
|
-
export const getCollaborators = (annotation: Annotation): User[] => {
|
|
8
|
-
const { creator, updatedBy } = annotation.target;
|
|
9
|
-
|
|
10
|
-
const bodyCollaborators = annotation.bodies.reduce((users, body) => (
|
|
11
|
-
[...users, body.creator, body.updatedBy]
|
|
12
|
-
), [] as User[]);
|
|
13
|
-
|
|
14
|
-
return [
|
|
15
|
-
creator,
|
|
16
|
-
updatedBy,
|
|
17
|
-
...bodyCollaborators
|
|
18
|
-
].filter(u => u); // Remove undefined
|
|
19
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|