@annotorious/core 3.0.0-pre-alpha-47 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@annotorious/core",
3
- "version": "3.0.0-pre-alpha-47",
3
+ "version": "3.0.0-pre-alpha-48",
4
4
  "description": "Annotorious core types and functions",
5
5
  "author": "Rainer Simon",
6
6
  "license": "BSD-3-Clause",
@@ -25,18 +25,18 @@
25
25
  ],
26
26
  "devDependencies": {
27
27
  "@tsconfig/svelte": "^3.0.0",
28
- "@types/deep-equal": "^1.0.1",
29
- "@types/uuid": "^9.0.1",
30
- "svelte": "^3.58.0",
28
+ "@types/deep-equal": "^1.0.2",
29
+ "@types/uuid": "^9.0.4",
30
+ "svelte": "^3.59.2",
31
31
  "typescript": "^4.9.5",
32
- "vite": "^4.2.1",
33
- "vite-plugin-dts": "^2.3.0",
34
- "vitest": "^0.29.3"
32
+ "vite": "^4.4.9",
33
+ "vite-plugin-dts": "^3.6.0",
34
+ "vitest": "^0.34.6"
35
35
  },
36
36
  "dependencies": {
37
37
  "dequal": "^2.0.3",
38
- "nanoevents": "^7.0.1",
39
- "nanoid": "^4.0.1",
40
- "uuid": "^9.0.0"
38
+ "nanoevents": "^8.0.0",
39
+ "nanoid": "^5.0.1",
40
+ "uuid": "^9.0.1"
41
41
  }
42
42
  }
@@ -1,28 +1,29 @@
1
1
  import { dequal } from 'dequal/lite';
2
- import type { Annotation, FormatAdapter, W3CAnnotation } from '../model';
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<T extends Annotation> = ReturnType<typeof createLifecyleObserver<T>>;
7
+ export type Lifecycle<I extends Annotation, E extends unknown> =
8
+ ReturnType<typeof createLifecyleObserver<I, E>>;
8
9
 
9
- export const createLifecyleObserver = <T extends Annotation, A extends unknown = W3CAnnotation>(
10
- store: Store<T>,
11
- selectionState: SelectionState<T>,
12
- hoverState: HoverState<T>,
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<T, A>
15
+ adapter?: FormatAdapter<I, E>
15
16
  ) => {
16
- const observers = new Map<string, LifecycleEvents<A>[keyof LifecycleEvents<T>][]>();
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: T[] = [];
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 = <E extends keyof LifecycleEvents<T>>(event: E, callback: LifecycleEvents<A>[E]) => {
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 = <E extends keyof LifecycleEvents<T>>(event: E, callback: LifecycleEvents<A>[E]) => {
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<T>, arg0: T | T[], arg1: T = null) => {
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 A & A[], serialized1);
53
+ callback(serialized0 as E & E[], serialized1);
53
54
  } else {
54
- callback(arg0 as A & A[], arg1 as unknown as A);
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
+ }
@@ -1,4 +1,4 @@
1
- import type { Annotation } from './core/Annotation';
1
+ import type { Annotation } from './Annotation';
2
2
 
3
3
  export interface FormatAdapter<A extends Annotation, T extends unknown> {
4
4
 
@@ -1,3 +1,6 @@
1
- export * from './core';
2
1
  export * from './w3c';
3
- export * from './FormatAdapter';
2
+ export * from './Annotation';
3
+ export * from './Annotator';
4
+ export * from './FormatAdapter';
5
+ export * from './Formatter';
6
+ export * from './User';
@@ -1,5 +1,5 @@
1
1
  import { v4 as uuidv4 } from 'uuid';
2
- import type { AnnotationBody } from '../core';
2
+ import type { AnnotationBody } from '../Annotation';
3
3
 
4
4
  export interface W3CAnnotation {
5
5
 
@@ -1,8 +1,26 @@
1
- import type { Annotation } from '../model';
2
- import { createStore } from './Store';
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 = <T extends Annotation>(annotation: T[]) => void;
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 createSvelteStore = <T extends Annotation>() => {
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
+ });
@@ -1,4 +1,3 @@
1
- export * from './bodyUtils';
2
- export * from './collaboratorUtils';
1
+ export * from './annotationUtils';
3
2
  export * from './diffAnnotations';
4
3
 
@@ -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
- }
@@ -1,4 +0,0 @@
1
- export * from './Annotation';
2
- export * from './Annotator';
3
- export * from './Formatter';
4
- export * from './User';
@@ -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