@annotorious/core 3.0.0-rc.19 → 3.0.0-rc.2

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/package.json +15 -16
  2. package/{dist/index.d.ts → src/index.ts} +0 -1
  3. package/src/lifecycle/Lifecycle.ts +197 -0
  4. package/src/lifecycle/LifecycleEvents.ts +21 -0
  5. package/src/lifecycle/index.ts +2 -0
  6. package/src/model/Annotation.ts +57 -0
  7. package/src/model/Annotator.ts +154 -0
  8. package/{dist/model/DrawingStyle.d.ts → src/model/DrawingStyle.ts} +14 -8
  9. package/{dist/model/Filter.d.ts → src/model/Filter.ts} +2 -2
  10. package/src/model/FormatAdapter.ts +36 -0
  11. package/src/model/User.ts +19 -0
  12. package/src/model/W3CAnnotation.ts +118 -0
  13. package/{dist/model/index.d.ts → src/model/index.ts} +1 -2
  14. package/src/presence/Appearance.ts +9 -0
  15. package/src/presence/AppearanceProvider.ts +53 -0
  16. package/src/presence/ColorPalette.ts +14 -0
  17. package/src/presence/PresenceEvents.ts +9 -0
  18. package/src/presence/PresenceProvider.ts +7 -0
  19. package/src/presence/PresenceState.ts +145 -0
  20. package/src/presence/PresentUser.ts +10 -0
  21. package/{dist/presence/index.d.ts → src/presence/index.ts} +1 -2
  22. package/src/state/Hover.ts +34 -0
  23. package/src/state/Selection.ts +113 -0
  24. package/src/state/Store.ts +327 -0
  25. package/src/state/StoreObserver.ts +149 -0
  26. package/src/state/SvelteStore.ts +54 -0
  27. package/src/state/Viewport.ts +14 -0
  28. package/{dist/state/index.d.ts → src/state/index.ts} +1 -3
  29. package/src/utils/annotationUtils.ts +33 -0
  30. package/src/utils/diffAnnotations.ts +33 -0
  31. package/{dist/utils/index.d.ts → src/utils/index.ts} +1 -1
  32. package/src/vite-env.d.ts +1 -0
  33. package/dist/annotorious-core.es.js +0 -771
  34. package/dist/annotorious-core.es.js.map +0 -1
  35. package/dist/index.d.ts.map +0 -1
  36. package/dist/lifecycle/Lifecycle.d.ts +0 -10
  37. package/dist/lifecycle/Lifecycle.d.ts.map +0 -1
  38. package/dist/lifecycle/LifecycleEvents.d.ts +0 -12
  39. package/dist/lifecycle/LifecycleEvents.d.ts.map +0 -1
  40. package/dist/lifecycle/index.d.ts +0 -3
  41. package/dist/lifecycle/index.d.ts.map +0 -1
  42. package/dist/model/Annotation.d.ts +0 -32
  43. package/dist/model/Annotation.d.ts.map +0 -1
  44. package/dist/model/Annotator.d.ts +0 -63
  45. package/dist/model/Annotator.d.ts.map +0 -1
  46. package/dist/model/DrawingStyle.d.ts.map +0 -1
  47. package/dist/model/Filter.d.ts.map +0 -1
  48. package/dist/model/FormatAdapter.d.ts +0 -15
  49. package/dist/model/FormatAdapter.d.ts.map +0 -1
  50. package/dist/model/User.d.ts +0 -11
  51. package/dist/model/User.d.ts.map +0 -1
  52. package/dist/model/W3CAnnotation.d.ts +0 -40
  53. package/dist/model/W3CAnnotation.d.ts.map +0 -1
  54. package/dist/model/index.d.ts.map +0 -1
  55. package/dist/presence/Appearance.d.ts +0 -6
  56. package/dist/presence/Appearance.d.ts.map +0 -1
  57. package/dist/presence/AppearanceProvider.d.ts +0 -16
  58. package/dist/presence/AppearanceProvider.d.ts.map +0 -1
  59. package/dist/presence/ColorPalette.d.ts +0 -3
  60. package/dist/presence/ColorPalette.d.ts.map +0 -1
  61. package/dist/presence/PresenceEvents.d.ts +0 -6
  62. package/dist/presence/PresenceEvents.d.ts.map +0 -1
  63. package/dist/presence/PresenceProvider.d.ts +0 -5
  64. package/dist/presence/PresenceProvider.d.ts.map +0 -1
  65. package/dist/presence/PresenceState.d.ts +0 -18
  66. package/dist/presence/PresenceState.d.ts.map +0 -1
  67. package/dist/presence/PresentUser.d.ts +0 -7
  68. package/dist/presence/PresentUser.d.ts.map +0 -1
  69. package/dist/presence/index.d.ts.map +0 -1
  70. package/dist/state/Hover.d.ts +0 -10
  71. package/dist/state/Hover.d.ts.map +0 -1
  72. package/dist/state/Selection.d.ts +0 -31
  73. package/dist/state/Selection.d.ts.map +0 -1
  74. package/dist/state/Store.d.ts +0 -30
  75. package/dist/state/Store.d.ts.map +0 -1
  76. package/dist/state/StoreObserver.d.ts +0 -57
  77. package/dist/state/StoreObserver.d.ts.map +0 -1
  78. package/dist/state/SvelteStore.d.ts +0 -22
  79. package/dist/state/SvelteStore.d.ts.map +0 -1
  80. package/dist/state/UndoStack.d.ts +0 -18
  81. package/dist/state/UndoStack.d.ts.map +0 -1
  82. package/dist/state/Viewport.d.ts +0 -7
  83. package/dist/state/Viewport.d.ts.map +0 -1
  84. package/dist/state/index.d.ts.map +0 -1
  85. package/dist/utils/annotationUtils.d.ts +0 -11
  86. package/dist/utils/annotationUtils.d.ts.map +0 -1
  87. package/dist/utils/diffAnnotations.d.ts +0 -4
  88. package/dist/utils/diffAnnotations.d.ts.map +0 -1
  89. package/dist/utils/index.d.ts.map +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@annotorious/core",
3
- "version": "3.0.0-rc.19",
3
+ "version": "3.0.0-rc.2",
4
4
  "description": "Annotorious core types and functions",
5
5
  "author": "Rainer Simon",
6
6
  "license": "BSD-3-Clause",
@@ -14,30 +14,29 @@
14
14
  "url": "https://github.com/annotorious/annotorious/issues"
15
15
  },
16
16
  "scripts": {
17
- "build": "vite build",
17
+ "build": "echo 'Skipping build in @annotorious/core package'",
18
18
  "test": "vitest",
19
19
  "coverage": "vitest run --coverage"
20
20
  },
21
- "main": "./dist/annotorious-core.es.js",
22
- "module": "./dist/annotorious-core.es.js",
23
- "types": "./dist/index.d.ts",
21
+ "main": "src/index.ts",
22
+ "types": "src/index.ts",
24
23
  "files": [
25
- "dist"
24
+ "src"
26
25
  ],
27
26
  "devDependencies": {
28
- "@tsconfig/svelte": "^5.0.2",
29
- "@types/deep-equal": "^1.0.4",
30
- "@types/uuid": "^9.0.7",
31
- "svelte": "^4.2.9",
32
- "typescript": "^5.3.3",
33
- "vite": "^5.0.12",
34
- "vite-plugin-dts": "^3.7.2",
35
- "vitest": "^1.2.1"
27
+ "@tsconfig/svelte": "^3.0.0",
28
+ "@types/deep-equal": "^1.0.2",
29
+ "@types/uuid": "^9.0.4",
30
+ "svelte": "^3.59.2",
31
+ "typescript": "^4.9.5",
32
+ "vite": "^4.4.9",
33
+ "vite-plugin-dts": "^3.6.0",
34
+ "vitest": "^0.34.6"
36
35
  },
37
36
  "dependencies": {
38
37
  "dequal": "^2.0.3",
39
- "nanoevents": "^9.0.0",
40
- "nanoid": "^5.0.4",
38
+ "nanoevents": "^8.0.0",
39
+ "nanoid": "^5.0.1",
41
40
  "uuid": "^9.0.1"
42
41
  }
43
42
  }
@@ -3,4 +3,3 @@ export * from './model';
3
3
  export * from './presence';
4
4
  export * from './state';
5
5
  export * from './utils';
6
- //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,197 @@
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
+ }
@@ -0,0 +1,21 @@
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
+ }
@@ -0,0 +1,2 @@
1
+ export * from './Lifecycle';
2
+ export * from './LifecycleEvents';
@@ -0,0 +1,57 @@
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
+ }
@@ -0,0 +1,154 @@
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
+ import type { Filter } from './Filter';
9
+
10
+ /**
11
+ * Base annotator interface.
12
+ * I ... internal core data model
13
+ * E ... external adapted representation
14
+ */
15
+ export interface Annotator<I extends Annotation = Annotation, E extends unknown = Annotation> {
16
+
17
+ addAnnotation(annotation: E): void;
18
+
19
+ clearAnnotations(): void;
20
+
21
+ destroy(): void;
22
+
23
+ getAnnotationById(id: string): E | undefined;
24
+
25
+ getAnnotations(): E[];
26
+
27
+ getUser(): User;
28
+
29
+ loadAnnotations(url: string): Promise<E[]>;
30
+
31
+ removeAnnotation(arg: E | string): E;
32
+
33
+ setAnnotations(annotations: E[]): void;
34
+
35
+ setFilter(filter: Filter): void;
36
+
37
+ setPresenceProvider?(provider: PresenceProvider): void;
38
+
39
+ setSelected(arg?: string | string[]): void;
40
+
41
+ setStyle(arg: DrawingStyle | ((annotation: I) => DrawingStyle) | undefined): void;
42
+
43
+ setUser(user: User): void;
44
+
45
+ updateAnnotation(annotation: E): E;
46
+
47
+ on<T extends keyof LifecycleEvents<E>>(event: T, callback: LifecycleEvents<E>[T]): void;
48
+
49
+ off<T extends keyof LifecycleEvents<E>>(event: T, callback: LifecycleEvents<E>[T]): void;
50
+
51
+ state: AnnotatorState<I>;
52
+
53
+ }
54
+
55
+ export interface AnnotatorState<A extends Annotation> {
56
+
57
+ store: Store<A>;
58
+
59
+ selection: SelectionState<A>;
60
+
61
+ hover: HoverState<A>;
62
+
63
+ viewport: ViewportState;
64
+
65
+ }
66
+
67
+ export const createBaseAnnotator = <I extends Annotation, E extends unknown>(
68
+ store: Store<I>,
69
+ adapter?: FormatAdapter<I, E>
70
+ ) => {
71
+
72
+ const addAnnotation = (annotation: E) => {
73
+ if (adapter) {
74
+ const { parsed, error } = adapter.parse(annotation);
75
+ if (parsed) {
76
+ store.addAnnotation(parsed, Origin.REMOTE);
77
+ } else {
78
+ console.error(error);
79
+ }
80
+ } else {
81
+ store.addAnnotation(annotation as unknown as I, Origin.REMOTE);
82
+ }
83
+ }
84
+
85
+ const clearAnnotations = () => store.clear();
86
+
87
+ const getAnnotationById = (id: string): E | undefined => {
88
+ const annotation = store.getAnnotation(id);
89
+ return (adapter && annotation) ?
90
+ adapter.serialize(annotation) as E : annotation as unknown as E;
91
+ }
92
+
93
+ const getAnnotations = () =>
94
+ (adapter ? store.all().map(adapter.serialize) : store.all()) as E[];
95
+
96
+ const loadAnnotations = (url: string) =>
97
+ fetch(url)
98
+ .then((response) => response.json())
99
+ .then((annotations) => {
100
+ setAnnotations(annotations);
101
+ return annotations;
102
+ });
103
+
104
+ const removeAnnotation = (arg: E | string): E => {
105
+ if (typeof arg === 'string') {
106
+ const annotation = store.getAnnotation(arg);
107
+ store.deleteAnnotation(arg);
108
+
109
+ return adapter ? adapter.serialize(annotation) : annotation as unknown as E;
110
+ } else {
111
+ const annotation = adapter ? adapter.parse(arg).parsed : (arg as unknown as I);
112
+ store.deleteAnnotation(annotation);
113
+ return arg;
114
+ }
115
+ }
116
+
117
+ const setAnnotations = (annotations: E[]) => {
118
+ if (adapter) {
119
+ const { parsed, failed } = parseAll(adapter)(annotations);
120
+
121
+ if (failed.length > 0)
122
+ console.warn(`Discarded ${failed.length} invalid annotations`, failed);
123
+
124
+ store.bulkAddAnnotation(parsed, true, Origin.REMOTE);
125
+ } else {
126
+ store.bulkAddAnnotation(annotations as unknown as I[], true, Origin.REMOTE);
127
+ }
128
+ }
129
+
130
+ const updateAnnotation = (updated: E): E => {
131
+ if (adapter) {
132
+ const crosswalked = adapter.parse(updated).parsed;
133
+ const previous = adapter.serialize(store.getAnnotation(crosswalked.id));
134
+ store.updateAnnotation(crosswalked);
135
+ return previous;
136
+ } else {
137
+ const previous = store.getAnnotation((updated as unknown as I).id);
138
+ store.updateAnnotation(updated as unknown as I);
139
+ return previous as unknown as E;
140
+ }
141
+ }
142
+
143
+ return {
144
+ addAnnotation,
145
+ clearAnnotations,
146
+ getAnnotationById,
147
+ getAnnotations,
148
+ loadAnnotations,
149
+ removeAnnotation,
150
+ setAnnotations,
151
+ updateAnnotation
152
+ }
153
+
154
+ }
@@ -1,13 +1,19 @@
1
1
  type RGB = `rgb(${number}, ${number}, ${number})`;
2
+
2
3
  type RGBA = `rgba(${number}, ${number}, ${number}, ${number})`;
4
+
3
5
  type HEX = `#${string}`;
6
+
4
7
  export type Color = RGB | RGBA | HEX;
8
+
5
9
  export interface DrawingStyle {
6
- fill?: Color;
7
- fillOpacity?: number;
8
- stroke?: Color;
9
- strokeOpacity?: number;
10
- strokeWidth?: number;
11
- }
12
- export {};
13
- //# sourceMappingURL=DrawingStyle.d.ts.map
10
+
11
+ fill?: Color;
12
+
13
+ fillOpacity?: number;
14
+
15
+ stroke?: Color;
16
+
17
+ strokeOpacity?: number;
18
+
19
+ }
@@ -1,3 +1,3 @@
1
1
  import type { Annotation } from './Annotation';
2
- export type Filter<T extends Annotation = Annotation> = (annotation: T) => boolean;
3
- //# sourceMappingURL=Filter.d.ts.map
2
+
3
+ export type Filter<T extends Annotation = Annotation> = (annotation: T) => boolean;
@@ -0,0 +1,36 @@
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
+
@@ -0,0 +1,19 @@
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
+ }