@annotorious/core 3.0.0-rc.1 → 3.0.0-rc.11

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.
Files changed (89) hide show
  1. package/dist/annotorious-core.es.js +767 -0
  2. package/dist/annotorious-core.es.js.map +1 -0
  3. package/{src/index.ts → dist/index.d.ts} +1 -0
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/lifecycle/Lifecycle.d.ts +10 -0
  6. package/dist/lifecycle/Lifecycle.d.ts.map +1 -0
  7. package/dist/lifecycle/LifecycleEvents.d.ts +12 -0
  8. package/dist/lifecycle/LifecycleEvents.d.ts.map +1 -0
  9. package/dist/lifecycle/index.d.ts +3 -0
  10. package/dist/lifecycle/index.d.ts.map +1 -0
  11. package/dist/model/Annotation.d.ts +32 -0
  12. package/dist/model/Annotation.d.ts.map +1 -0
  13. package/dist/model/Annotator.d.ts +63 -0
  14. package/dist/model/Annotator.d.ts.map +1 -0
  15. package/{src/model/DrawingStyle.ts → dist/model/DrawingStyle.d.ts} +7 -14
  16. package/dist/model/DrawingStyle.d.ts.map +1 -0
  17. package/dist/model/Filter.d.ts +3 -0
  18. package/dist/model/Filter.d.ts.map +1 -0
  19. package/dist/model/FormatAdapter.d.ts +15 -0
  20. package/dist/model/FormatAdapter.d.ts.map +1 -0
  21. package/dist/model/User.d.ts +11 -0
  22. package/dist/model/User.d.ts.map +1 -0
  23. package/dist/model/W3CAnnotation.d.ts +38 -0
  24. package/dist/model/W3CAnnotation.d.ts.map +1 -0
  25. package/{src/model/index.ts → dist/model/index.d.ts} +3 -1
  26. package/dist/model/index.d.ts.map +1 -0
  27. package/dist/presence/Appearance.d.ts +6 -0
  28. package/dist/presence/Appearance.d.ts.map +1 -0
  29. package/dist/presence/AppearanceProvider.d.ts +16 -0
  30. package/dist/presence/AppearanceProvider.d.ts.map +1 -0
  31. package/dist/presence/ColorPalette.d.ts +3 -0
  32. package/dist/presence/ColorPalette.d.ts.map +1 -0
  33. package/dist/presence/PresenceEvents.d.ts +6 -0
  34. package/dist/presence/PresenceEvents.d.ts.map +1 -0
  35. package/dist/presence/PresenceProvider.d.ts +5 -0
  36. package/dist/presence/PresenceProvider.d.ts.map +1 -0
  37. package/dist/presence/PresenceState.d.ts +18 -0
  38. package/dist/presence/PresenceState.d.ts.map +1 -0
  39. package/dist/presence/PresentUser.d.ts +7 -0
  40. package/dist/presence/PresentUser.d.ts.map +1 -0
  41. package/{src/presence/index.ts → dist/presence/index.d.ts} +2 -1
  42. package/dist/presence/index.d.ts.map +1 -0
  43. package/dist/state/Hover.d.ts +34 -0
  44. package/dist/state/Hover.d.ts.map +1 -0
  45. package/dist/state/Selection.d.ts +55 -0
  46. package/dist/state/Selection.d.ts.map +1 -0
  47. package/dist/state/Store.d.ts +30 -0
  48. package/dist/state/Store.d.ts.map +1 -0
  49. package/dist/state/StoreObserver.d.ts +57 -0
  50. package/dist/state/StoreObserver.d.ts.map +1 -0
  51. package/dist/state/SvelteStore.d.ts +48 -0
  52. package/dist/state/SvelteStore.d.ts.map +1 -0
  53. package/dist/state/UndoStack.d.ts +44 -0
  54. package/dist/state/UndoStack.d.ts.map +1 -0
  55. package/dist/state/Viewport.d.ts +6 -0
  56. package/dist/state/Viewport.d.ts.map +1 -0
  57. package/{src/state/index.ts → dist/state/index.d.ts} +3 -1
  58. package/dist/state/index.d.ts.map +1 -0
  59. package/dist/utils/annotationUtils.d.ts +11 -0
  60. package/dist/utils/annotationUtils.d.ts.map +1 -0
  61. package/dist/utils/diffAnnotations.d.ts +4 -0
  62. package/dist/utils/diffAnnotations.d.ts.map +1 -0
  63. package/{src/utils/index.ts → dist/utils/index.d.ts} +1 -1
  64. package/dist/utils/index.d.ts.map +1 -0
  65. package/package.json +6 -5
  66. package/src/lifecycle/Lifecycle.ts +0 -197
  67. package/src/lifecycle/LifecycleEvents.ts +0 -21
  68. package/src/lifecycle/index.ts +0 -2
  69. package/src/model/Annotation.ts +0 -57
  70. package/src/model/Annotator.ts +0 -151
  71. package/src/model/FormatAdapter.ts +0 -36
  72. package/src/model/User.ts +0 -19
  73. package/src/model/W3CAnnotation.ts +0 -118
  74. package/src/presence/Appearance.ts +0 -9
  75. package/src/presence/AppearanceProvider.ts +0 -53
  76. package/src/presence/ColorPalette.ts +0 -14
  77. package/src/presence/PresenceEvents.ts +0 -9
  78. package/src/presence/PresenceProvider.ts +0 -7
  79. package/src/presence/PresenceState.ts +0 -145
  80. package/src/presence/PresentUser.ts +0 -10
  81. package/src/state/Hover.ts +0 -34
  82. package/src/state/Selection.ts +0 -113
  83. package/src/state/Store.ts +0 -327
  84. package/src/state/StoreObserver.ts +0 -149
  85. package/src/state/SvelteStore.ts +0 -54
  86. package/src/state/Viewport.ts +0 -14
  87. package/src/utils/annotationUtils.ts +0 -33
  88. package/src/utils/diffAnnotations.ts +0 -33
  89. package/src/vite-env.d.ts +0 -1
@@ -0,0 +1,6 @@
1
+ export type ViewportState = ReturnType<typeof createViewportState>;
2
+ export declare const createViewportState: () => {
3
+ subscribe: (this: void, run: import("svelte/store").Subscriber<string[]>, invalidate?: (value?: string[]) => void) => import("svelte/store").Unsubscriber;
4
+ set: (this: void, value: string[]) => void;
5
+ };
6
+ //# sourceMappingURL=Viewport.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Viewport.d.ts","sourceRoot":"","sources":["../../src/state/Viewport.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,aAAa,GAAG,UAAU,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAEnE,eAAO,MAAM,mBAAmB;;;CAS/B,CAAA"}
@@ -3,4 +3,6 @@ export * from './Selection';
3
3
  export * from './Store';
4
4
  export * from './StoreObserver';
5
5
  export * from './SvelteStore';
6
- export * from './Viewport';
6
+ export * from './UndoStack';
7
+ export * from './Viewport';
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/state/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC"}
@@ -0,0 +1,11 @@
1
+ import type { Annotation, AnnotationBody } from '../model/Annotation';
2
+ import type { User } from '../model/User';
3
+ /**
4
+ * Returns all users listed as creators or updaters in any parts of this
5
+ * annotation.
6
+ */
7
+ export declare const getContributors: (annotation: Annotation) => User[];
8
+ export declare const createBody: (annotation: Annotation, payload: {
9
+ [key: string]: any;
10
+ }, created?: Date, creator?: User) => AnnotationBody;
11
+ //# sourceMappingURL=annotationUtils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"annotationUtils.d.ts","sourceRoot":"","sources":["../../src/utils/annotationUtils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACtE,OAAO,KAAK,EAAG,IAAI,EAAE,MAAM,eAAe,CAAC;AAC3C;;;GAGG;AACH,eAAO,MAAM,eAAe,eAAgB,UAAU,KAAG,IAAI,EAY5D,CAAA;AAED,eAAO,MAAM,UAAU,eACT,UAAU;;aAEZ,IAAI,YACJ,IAAI,KACb,cAMD,CAAC"}
@@ -0,0 +1,4 @@
1
+ import type { Update } from '../state/StoreObserver';
2
+ import type { Annotation } from '../model/Annotation';
3
+ export declare const diffAnnotations: <T extends Annotation = Annotation>(oldValue: T, newValue: T) => Update<T>;
4
+ //# sourceMappingURL=diffAnnotations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"diffAnnotations.d.ts","sourceRoot":"","sources":["../../src/utils/diffAnnotations.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAuBtD,eAAO,MAAM,eAAe,4EAa3B,CAAA"}
@@ -1,3 +1,3 @@
1
1
  export * from './annotationUtils';
2
2
  export * from './diffAnnotations';
3
-
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@annotorious/core",
3
- "version": "3.0.0-rc.1",
3
+ "version": "3.0.0-rc.11",
4
4
  "description": "Annotorious core types and functions",
5
5
  "author": "Rainer Simon",
6
6
  "license": "BSD-3-Clause",
@@ -14,14 +14,15 @@
14
14
  "url": "https://github.com/annotorious/annotorious/issues"
15
15
  },
16
16
  "scripts": {
17
- "build": "echo 'Skipping build in @annotorious/core package'",
17
+ "build": "vite build",
18
18
  "test": "vitest",
19
19
  "coverage": "vitest run --coverage"
20
20
  },
21
- "main": "src/index.ts",
22
- "types": "src/index.ts",
21
+ "main": "./dist/annotorious-core.es.js",
22
+ "module": "./dist/annotorious-core.es.js",
23
+ "types": "./dist/index.d.ts",
23
24
  "files": [
24
- "src"
25
+ "dist"
25
26
  ],
26
27
  "devDependencies": {
27
28
  "@tsconfig/svelte": "^3.0.0",
@@ -1,197 +0,0 @@
1
- import { dequal } from 'dequal/lite';
2
- import type { Annotation, FormatAdapter } from '../model';
3
- import { Origin } from '../state';
4
- import type { HoverState, SelectionState, Store, ViewportState } from '../state';
5
- import type { LifecycleEvents } from './LifecycleEvents';
6
-
7
- export type Lifecycle<I extends Annotation, E extends unknown> =
8
- ReturnType<typeof createLifecyleObserver<I, E>>;
9
-
10
- export const createLifecyleObserver = <I extends Annotation, E extends unknown>(
11
- store: Store<I>,
12
- selectionState: SelectionState<I>,
13
- hoverState: HoverState<I>,
14
- viewportState?: ViewportState,
15
- adapter?: FormatAdapter<I, E>,
16
- autoSave?: boolean
17
- ) => {
18
- const observers: Map<keyof LifecycleEvents, Function[]> = new Map();
19
-
20
- // The currently selected annotations, in the state when they were selected
21
- let initialSelection: I[] = [];
22
-
23
- let currentHover: string | undefined;
24
-
25
- let idleTimeout: ReturnType<typeof setTimeout>;
26
-
27
- const on = <T extends keyof LifecycleEvents>(event: T, callback: LifecycleEvents<E>[T]) => {
28
- if (observers.has(event)) {
29
- observers.get(event).push(callback);
30
- } else {
31
- observers.set(event, [callback]);
32
- }
33
- }
34
-
35
- const off = <T extends keyof LifecycleEvents<E>>(event: T, callback: LifecycleEvents<E>[T]) => {
36
- const callbacks = observers.get(event);
37
- if (callbacks) {
38
- const idx = callbacks.indexOf(callback);
39
- if (idx > 0)
40
- callbacks.splice(callbacks.indexOf(callback), 1);
41
- }
42
- }
43
-
44
- const emit = (event: keyof LifecycleEvents<E>, arg0: I | I[], arg1?: I | PointerEvent) => {
45
- if (observers.has(event)) {
46
- setTimeout(() => {
47
- observers.get(event).forEach(callback => {
48
- if (adapter) {
49
- const serialized0 = Array.isArray(arg0) ?
50
- arg0.map(a => adapter.serialize(a)) : adapter.serialize(arg0);
51
-
52
- const serialized1: E | PointerEvent | undefined =
53
- arg1 ? arg1 instanceof PointerEvent ? arg1 : adapter.serialize(arg1) : undefined;
54
-
55
- callback(serialized0 as E & E[], serialized1);
56
- } else {
57
- callback(arg0 as E & E[], arg1 as unknown as E);
58
- }
59
- });
60
- }, 1);
61
- }
62
- }
63
-
64
- const onIdleUpdate = () => {
65
- const { selected } = selectionState;
66
-
67
- // User idle after activity - fire update events for selected
68
- // annotations that changed
69
- const updatedSelected = selected.map(({ id }) => store.getAnnotation(id));
70
-
71
- updatedSelected.forEach(updated => {
72
- const initial = initialSelection.find(a => a.id === updated.id);
73
- if (!initial || !dequal(initial, updated)) {
74
- emit('updateAnnotation', updated, initial);
75
- }
76
- });
77
-
78
- initialSelection = initialSelection.map(initial => {
79
- const updated = updatedSelected.find(({ id }) => id === initial.id);
80
- return updated ? updated : initial
81
- });
82
- }
83
-
84
- selectionState.subscribe(({ selected })=> {
85
- if (initialSelection.length === 0 && selected.length === 0)
86
- return;
87
-
88
- if (initialSelection.length === 0 && selected.length > 0) {
89
- // A new selection was made - store the editable annotation as initial state
90
- initialSelection = selected.map(({ id }) => store.getAnnotation(id));
91
- } else if (initialSelection.length > 0 && selected.length === 0) {
92
- // Deselect!
93
- initialSelection.forEach(initial => {
94
- const updatedState = store.getAnnotation(initial.id);
95
-
96
- if (updatedState && !dequal(updatedState, initial)) {
97
- emit('updateAnnotation', updatedState, initial);
98
- }
99
- });
100
-
101
- initialSelection = [];
102
- } else {
103
- // Changed selection
104
- const initialIds = new Set(initialSelection.map(a => a.id));
105
- const selectedIds = new Set(selected.map(({ id }) => id));
106
-
107
- // Fire update events for deselected annotations that have changed
108
- const deselected = initialSelection.filter(a => !selectedIds.has(a.id));
109
- deselected.forEach(initial => {
110
- const updatedState = store.getAnnotation(initial.id);
111
-
112
- if (updatedState && !dequal(updatedState, initial))
113
- emit('updateAnnotation', updatedState, initial);
114
- });
115
-
116
- initialSelection = [
117
- // Remove annotations that were deselected
118
- ...initialSelection.filter(a => selectedIds.has(a.id)),
119
- // Add editable annotations that were selected
120
- ...selected.filter(({ id }) => !initialIds.has(id))
121
- .map(({ id }) => store.getAnnotation(id))
122
- ];
123
- }
124
-
125
- emit('selectionChanged', initialSelection);
126
- });
127
-
128
- hoverState.subscribe(id => {
129
- if (!currentHover && id) {
130
- emit('mouseEnterAnnotation', store.getAnnotation(id));
131
- } else if (currentHover && !id) {
132
- emit('mouseLeaveAnnotation', store.getAnnotation(currentHover));
133
- } else if (currentHover && id) {
134
- emit('mouseLeaveAnnotation', store.getAnnotation(currentHover));
135
- emit('mouseEnterAnnotation', store.getAnnotation(id));
136
- }
137
-
138
- currentHover = id;
139
- });
140
-
141
- viewportState?.subscribe(ids =>
142
- emit('viewportIntersect', ids.map(store.getAnnotation)));
143
-
144
- store.observe(event => {
145
- // autoSave option triggers update events on idleness
146
- if (autoSave) {
147
- if (idleTimeout)
148
- clearTimeout(idleTimeout);
149
-
150
- idleTimeout = setTimeout(onIdleUpdate, 1000);
151
- }
152
-
153
- // Local CREATE and DELETE events are applied immediately
154
- const { created, deleted } = event.changes;
155
- created.forEach(a => emit('createAnnotation', a));
156
- deleted.forEach(a => emit('deleteAnnotation', a));
157
-
158
- // Updates are only applied immediately if they involve body changes
159
- const updatesWithBody = event.changes.updated.filter(u => [
160
- ...(u.bodiesCreated || []),
161
- ...(u.bodiesDeleted || []),
162
- ...(u.bodiesUpdated || [])
163
- ].length > 0);
164
-
165
- // Emit an update with the new annototation and the stored initial state
166
- updatesWithBody.forEach(({ oldValue, newValue }) => {
167
- const initial = initialSelection.find(a => a.id === oldValue.id) || oldValue;
168
-
169
- // Record the update as the new last known state
170
- initialSelection = initialSelection
171
- .map(a => a.id === oldValue.id ? newValue : a);
172
-
173
- emit('updateAnnotation', newValue, initial);
174
- });
175
- }, { origin: Origin.LOCAL });
176
-
177
- // Track remote changes - these should update the initial state
178
- store.observe(event => {
179
- if (initialSelection) {
180
- const selectedIds = new Set(initialSelection.map(a => a.id));
181
-
182
- const relevantUpdates = event.changes.updated
183
- .filter(({ newValue }) => selectedIds.has(newValue.id))
184
- .map(({ newValue }) => newValue);
185
-
186
- if (relevantUpdates.length > 0) {
187
- initialSelection = initialSelection.map(selected => {
188
- const updated = relevantUpdates.find(updated => updated.id === selected.id);
189
- return updated ? updated : selected;
190
- })
191
- }
192
- }
193
- }, { origin: Origin.REMOTE });
194
-
195
- return { on, off, emit }
196
-
197
- }
@@ -1,21 +0,0 @@
1
- import type { Annotation } from '../model';
2
-
3
- export interface LifecycleEvents<T extends unknown = Annotation> {
4
-
5
- clickAnnotation: (annotation: T, originalEvent: PointerEvent) => void;
6
-
7
- createAnnotation: (annotation: T) => void;
8
-
9
- deleteAnnotation: (annotation: T) => void;
10
-
11
- mouseEnterAnnotation: (annotation: T) => void;
12
-
13
- mouseLeaveAnnotation: (annotation: T) => void;
14
-
15
- selectionChanged: (annotation: T[]) => void;
16
-
17
- updateAnnotation: (annotation: T, previous: T) => void;
18
-
19
- viewportIntersect: (visible: T[]) => void;
20
-
21
- }
@@ -1,2 +0,0 @@
1
- export * from './Lifecycle';
2
- export * from './LifecycleEvents';
@@ -1,57 +0,0 @@
1
- import type { User } from './User';
2
-
3
- export interface Annotation {
4
-
5
- id: string;
6
-
7
- target: AnnotationTarget;
8
-
9
- bodies: AnnotationBody[];
10
-
11
- properties?: {
12
-
13
- [key: string]: any;
14
-
15
- }
16
-
17
- }
18
-
19
- export interface AnnotationTarget {
20
-
21
- annotation: string;
22
-
23
- selector: AbstractSelector;
24
-
25
- creator?: User;
26
-
27
- created?: Date;
28
-
29
- updatedBy?: User;
30
-
31
- updated?: Date;
32
-
33
- }
34
-
35
- export interface AbstractSelector { }
36
-
37
- export interface AnnotationBody {
38
-
39
- id: string;
40
-
41
- annotation: string;
42
-
43
- type?: string;
44
-
45
- purpose?: string;
46
-
47
- value?: string;
48
-
49
- creator?: User;
50
-
51
- created?: Date;
52
-
53
- updatedBy?: User;
54
-
55
- updated?: Date;
56
-
57
- }
@@ -1,151 +0,0 @@
1
- import type { Annotation } from './Annotation';
2
- import type { User } from './User';
3
- import type { PresenceProvider } from '../presence';
4
- import { Origin, type HoverState, type SelectionState, type Store, type ViewportState } from '../state';
5
- import type { LifecycleEvents } from '../lifecycle';
6
- import { parseAll, type FormatAdapter } from './FormatAdapter';
7
- import type { DrawingStyle } from './DrawingStyle';
8
-
9
- /**
10
- * Base annotator interface.
11
- * I ... internal core data model
12
- * E ... external adapted representation
13
- */
14
- export interface Annotator<I extends Annotation = Annotation, E extends unknown = Annotation> {
15
-
16
- style: DrawingStyle | ((annotation: I) => DrawingStyle) | undefined;
17
-
18
- addAnnotation(annotation: E): void;
19
-
20
- clearAnnotations(): void;
21
-
22
- destroy(): void;
23
-
24
- getAnnotationById(id: string): E | undefined;
25
-
26
- getAnnotations(): E[];
27
-
28
- getUser(): User;
29
-
30
- loadAnnotations(url: string): Promise<E[]>;
31
-
32
- removeAnnotation(arg: E | string): E;
33
-
34
- setAnnotations(annotations: E[]): void;
35
-
36
- setPresenceProvider?(provider: PresenceProvider): void;
37
-
38
- setSelected(arg?: string | string[]): void;
39
-
40
- setUser(user: User): void;
41
-
42
- updateAnnotation(annotation: E): E;
43
-
44
- on<T extends keyof LifecycleEvents<E>>(event: T, callback: LifecycleEvents<E>[T]): void;
45
-
46
- off<T extends keyof LifecycleEvents<E>>(event: T, callback: LifecycleEvents<E>[T]): void;
47
-
48
- state: AnnotatorState<I>;
49
-
50
- }
51
-
52
- export interface AnnotatorState<A extends Annotation> {
53
-
54
- store: Store<A>;
55
-
56
- selection: SelectionState<A>;
57
-
58
- hover: HoverState<A>;
59
-
60
- viewport: ViewportState;
61
-
62
- }
63
-
64
- export const createBaseAnnotator = <I extends Annotation, E extends unknown>(
65
- store: Store<I>,
66
- adapter?: FormatAdapter<I, E>
67
- ) => {
68
-
69
- const addAnnotation = (annotation: E) => {
70
- if (adapter) {
71
- const { parsed, error } = adapter.parse(annotation);
72
- if (parsed) {
73
- store.addAnnotation(parsed, Origin.REMOTE);
74
- } else {
75
- console.error(error);
76
- }
77
- } else {
78
- store.addAnnotation(annotation as unknown as I, Origin.REMOTE);
79
- }
80
- }
81
-
82
- const clearAnnotations = () => store.clear();
83
-
84
- const getAnnotationById = (id: string): E | undefined => {
85
- const annotation = store.getAnnotation(id);
86
- return (adapter && annotation) ?
87
- adapter.serialize(annotation) as E : annotation as unknown as E;
88
- }
89
-
90
- const getAnnotations = () =>
91
- (adapter ? store.all().map(adapter.serialize) : store.all()) as E[];
92
-
93
- const loadAnnotations = (url: string) =>
94
- fetch(url)
95
- .then((response) => response.json())
96
- .then((annotations) => {
97
- setAnnotations(annotations);
98
- return annotations;
99
- });
100
-
101
- const removeAnnotation = (arg: E | string): E => {
102
- if (typeof arg === 'string') {
103
- const annotation = store.getAnnotation(arg);
104
- store.deleteAnnotation(arg);
105
-
106
- return adapter ? adapter.serialize(annotation) : annotation as unknown as E;
107
- } else {
108
- const annotation = adapter ? adapter.parse(arg).parsed : (arg as unknown as I);
109
- store.deleteAnnotation(annotation);
110
- return arg;
111
- }
112
- }
113
-
114
- const setAnnotations = (annotations: E[]) => {
115
- if (adapter) {
116
- const { parsed, failed } = parseAll(adapter)(annotations);
117
-
118
- if (failed.length > 0)
119
- console.warn(`Discarded ${failed.length} invalid annotations`, failed);
120
-
121
- store.bulkAddAnnotation(parsed, true, Origin.REMOTE);
122
- } else {
123
- store.bulkAddAnnotation(annotations as unknown as I[], true, Origin.REMOTE);
124
- }
125
- }
126
-
127
- const updateAnnotation = (updated: E): E => {
128
- if (adapter) {
129
- const crosswalked = adapter.parse(updated).parsed;
130
- const previous = adapter.serialize(store.getAnnotation(crosswalked.id));
131
- store.updateAnnotation(crosswalked);
132
- return previous;
133
- } else {
134
- const previous = store.getAnnotation((updated as unknown as I).id);
135
- store.updateAnnotation(updated as unknown as I);
136
- return previous as unknown as E;
137
- }
138
- }
139
-
140
- return {
141
- addAnnotation,
142
- clearAnnotations,
143
- getAnnotationById,
144
- getAnnotations,
145
- loadAnnotations,
146
- removeAnnotation,
147
- setAnnotations,
148
- updateAnnotation
149
- }
150
-
151
- }
@@ -1,36 +0,0 @@
1
- import type { Annotation } from './Annotation';
2
-
3
- export interface FormatAdapter<A extends Annotation, T extends unknown> {
4
-
5
- parse(serialized: T): ParseResult<A>;
6
-
7
- serialize(core: A): T;
8
-
9
- }
10
-
11
- export interface ParseResult<A extends Annotation> {
12
-
13
- parsed?: A;
14
-
15
- error?: Error;
16
-
17
- }
18
-
19
- export const serializeAll =
20
- <A extends Annotation, T extends unknown>(adapter: FormatAdapter<A, T>) =>
21
- (annotations: A[]) => annotations.map(a => adapter.serialize(a));
22
-
23
- export const parseAll =
24
- <A extends Annotation, T extends unknown>(adapter: FormatAdapter<A, T>) =>
25
- (serialized: T[]) => serialized.reduce((result, next) => {
26
- const { parsed, error } = adapter.parse(next);
27
-
28
- return error ? {
29
- parsed: result.parsed,
30
- failed: [...result.failed, next ]
31
- } : {
32
- parsed: [...result.parsed, parsed ],
33
- failed: result.failed
34
- }
35
- }, { parsed: [], failed: [] });
36
-
package/src/model/User.ts DELETED
@@ -1,19 +0,0 @@
1
- import { customAlphabet } from 'nanoid';
2
-
3
- export interface User {
4
-
5
- id: string;
6
-
7
- isGuest?: boolean;
8
-
9
- name?: string;
10
-
11
- avatar?: string;
12
-
13
- }
14
-
15
- export const createAnonymousGuest = () => {
16
- const nanoid = customAlphabet('1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_', 20);
17
-
18
- return { isGuest: true, id: nanoid() }
19
- }