@annotorious/core 3.0.0-rc.21 → 3.0.0-rc.3
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 +15 -18
- package/{dist/index.d.ts → src/index.ts} +0 -1
- package/src/lifecycle/Lifecycle.ts +197 -0
- package/src/lifecycle/LifecycleEvents.ts +21 -0
- package/src/lifecycle/index.ts +2 -0
- package/src/model/Annotation.ts +73 -0
- package/src/model/Annotator.ts +161 -0
- package/{dist/model/DrawingStyle.d.ts → src/model/DrawingStyle.ts} +14 -8
- package/{dist/model/Filter.d.ts → src/model/Filter.ts} +2 -2
- package/src/model/FormatAdapter.ts +36 -0
- package/src/model/User.ts +19 -0
- package/src/model/W3CAnnotation.ts +118 -0
- package/{dist/model/index.d.ts → src/model/index.ts} +1 -2
- package/src/presence/Appearance.ts +9 -0
- package/src/presence/AppearanceProvider.ts +53 -0
- package/src/presence/ColorPalette.ts +14 -0
- package/src/presence/PresenceEvents.ts +9 -0
- package/src/presence/PresenceProvider.ts +7 -0
- package/src/presence/PresenceState.ts +145 -0
- package/src/presence/PresentUser.ts +10 -0
- package/{dist/presence/index.d.ts → src/presence/index.ts} +1 -2
- package/src/state/Hover.ts +34 -0
- package/src/state/Selection.ts +113 -0
- package/src/state/Store.ts +344 -0
- package/src/state/StoreObserver.ts +194 -0
- package/src/state/SvelteStore.ts +54 -0
- package/src/state/UndoStack.ts +112 -0
- package/src/state/Viewport.ts +14 -0
- package/{dist/state/index.d.ts → src/state/index.ts} +1 -2
- package/src/utils/annotationUtils.ts +33 -0
- package/src/utils/diffAnnotations.ts +39 -0
- package/{dist/utils/index.d.ts → src/utils/index.ts} +1 -1
- package/src/vite-env.d.ts +1 -0
- package/dist/annotorious-core.es.js +0 -776
- package/dist/annotorious-core.es.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/lifecycle/Lifecycle.d.ts +0 -10
- package/dist/lifecycle/Lifecycle.d.ts.map +0 -1
- package/dist/lifecycle/LifecycleEvents.d.ts +0 -12
- package/dist/lifecycle/LifecycleEvents.d.ts.map +0 -1
- package/dist/lifecycle/index.d.ts +0 -3
- package/dist/lifecycle/index.d.ts.map +0 -1
- package/dist/model/Annotation.d.ts +0 -32
- package/dist/model/Annotation.d.ts.map +0 -1
- package/dist/model/Annotator.d.ts +0 -63
- package/dist/model/Annotator.d.ts.map +0 -1
- package/dist/model/DrawingStyle.d.ts.map +0 -1
- package/dist/model/Filter.d.ts.map +0 -1
- package/dist/model/FormatAdapter.d.ts +0 -15
- package/dist/model/FormatAdapter.d.ts.map +0 -1
- package/dist/model/User.d.ts +0 -11
- package/dist/model/User.d.ts.map +0 -1
- package/dist/model/W3CAnnotation.d.ts +0 -41
- package/dist/model/W3CAnnotation.d.ts.map +0 -1
- package/dist/model/index.d.ts.map +0 -1
- package/dist/presence/Appearance.d.ts +0 -6
- package/dist/presence/Appearance.d.ts.map +0 -1
- package/dist/presence/AppearanceProvider.d.ts +0 -16
- package/dist/presence/AppearanceProvider.d.ts.map +0 -1
- package/dist/presence/ColorPalette.d.ts +0 -3
- package/dist/presence/ColorPalette.d.ts.map +0 -1
- package/dist/presence/PresenceEvents.d.ts +0 -6
- package/dist/presence/PresenceEvents.d.ts.map +0 -1
- package/dist/presence/PresenceProvider.d.ts +0 -5
- package/dist/presence/PresenceProvider.d.ts.map +0 -1
- package/dist/presence/PresenceState.d.ts +0 -18
- package/dist/presence/PresenceState.d.ts.map +0 -1
- package/dist/presence/PresentUser.d.ts +0 -7
- package/dist/presence/PresentUser.d.ts.map +0 -1
- package/dist/presence/index.d.ts.map +0 -1
- package/dist/state/Hover.d.ts +0 -10
- package/dist/state/Hover.d.ts.map +0 -1
- package/dist/state/Selection.d.ts +0 -31
- package/dist/state/Selection.d.ts.map +0 -1
- package/dist/state/Store.d.ts +0 -30
- package/dist/state/Store.d.ts.map +0 -1
- package/dist/state/StoreObserver.d.ts +0 -57
- package/dist/state/StoreObserver.d.ts.map +0 -1
- package/dist/state/SvelteStore.d.ts +0 -22
- package/dist/state/SvelteStore.d.ts.map +0 -1
- package/dist/state/UndoStack.d.ts +0 -18
- package/dist/state/UndoStack.d.ts.map +0 -1
- package/dist/state/Viewport.d.ts +0 -7
- package/dist/state/Viewport.d.ts.map +0 -1
- package/dist/state/index.d.ts.map +0 -1
- package/dist/utils/annotationUtils.d.ts +0 -11
- package/dist/utils/annotationUtils.d.ts.map +0 -1
- package/dist/utils/diffAnnotations.d.ts +0 -4
- package/dist/utils/diffAnnotations.d.ts.map +0 -1
- 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.
|
|
3
|
+
"version": "3.0.0-rc.3",
|
|
4
4
|
"description": "Annotorious core types and functions",
|
|
5
5
|
"author": "Rainer Simon",
|
|
6
6
|
"license": "BSD-3-Clause",
|
|
@@ -14,32 +14,29 @@
|
|
|
14
14
|
"url": "https://github.com/annotorious/annotorious/issues"
|
|
15
15
|
},
|
|
16
16
|
"scripts": {
|
|
17
|
-
"build": "
|
|
17
|
+
"build": "echo 'Skipping build in @annotorious/core package'",
|
|
18
18
|
"test": "vitest",
|
|
19
19
|
"coverage": "vitest run --coverage"
|
|
20
20
|
},
|
|
21
|
-
"main": "
|
|
22
|
-
"
|
|
23
|
-
"types": "./dist/index.d.ts",
|
|
21
|
+
"main": "src/index.ts",
|
|
22
|
+
"types": "src/index.ts",
|
|
24
23
|
"files": [
|
|
25
|
-
"
|
|
24
|
+
"src"
|
|
26
25
|
],
|
|
27
26
|
"devDependencies": {
|
|
28
|
-
"@
|
|
29
|
-
"@
|
|
30
|
-
"@types/
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"vite-plugin-dts": "^3.7.3",
|
|
37
|
-
"vitest": "^1.4.0"
|
|
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"
|
|
38
35
|
},
|
|
39
36
|
"dependencies": {
|
|
40
37
|
"dequal": "^2.0.3",
|
|
41
|
-
"nanoevents": "^
|
|
42
|
-
"nanoid": "^5.0.
|
|
38
|
+
"nanoevents": "^8.0.0",
|
|
39
|
+
"nanoid": "^5.0.1",
|
|
43
40
|
"uuid": "^9.0.1"
|
|
44
41
|
}
|
|
45
42
|
}
|
|
@@ -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,73 @@
|
|
|
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?: Purpose | string;
|
|
46
|
+
|
|
47
|
+
value?: string;
|
|
48
|
+
|
|
49
|
+
creator?: User;
|
|
50
|
+
|
|
51
|
+
created?: Date;
|
|
52
|
+
|
|
53
|
+
updatedBy?: User;
|
|
54
|
+
|
|
55
|
+
updated?: Date;
|
|
56
|
+
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Pre-defined purposes from https://www.w3.org/TR/annotation-model/#motivation-and-purpose
|
|
60
|
+
export type Purpose =
|
|
61
|
+
'assessing' |
|
|
62
|
+
'bookmarking' |
|
|
63
|
+
'classifying' |
|
|
64
|
+
'commenting' |
|
|
65
|
+
'describing' |
|
|
66
|
+
'editing' |
|
|
67
|
+
'highlighting' |
|
|
68
|
+
'identifying' |
|
|
69
|
+
'linking' |
|
|
70
|
+
'moderating' |
|
|
71
|
+
'questioning' |
|
|
72
|
+
'replying' |
|
|
73
|
+
'tagging';
|
|
@@ -0,0 +1,161 @@
|
|
|
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 UndoStack, 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
|
+
redo(): void;
|
|
32
|
+
|
|
33
|
+
removeAnnotation(arg: E | string): E;
|
|
34
|
+
|
|
35
|
+
setAnnotations(annotations: E[]): void;
|
|
36
|
+
|
|
37
|
+
setFilter(filter: Filter): void;
|
|
38
|
+
|
|
39
|
+
setPresenceProvider?(provider: PresenceProvider): void;
|
|
40
|
+
|
|
41
|
+
setSelected(arg?: string | string[]): void;
|
|
42
|
+
|
|
43
|
+
setStyle(arg: DrawingStyle | ((annotation: I) => DrawingStyle) | undefined): void;
|
|
44
|
+
|
|
45
|
+
setUser(user: User): void;
|
|
46
|
+
|
|
47
|
+
undo(): void;
|
|
48
|
+
|
|
49
|
+
updateAnnotation(annotation: E): E;
|
|
50
|
+
|
|
51
|
+
on<T extends keyof LifecycleEvents<E>>(event: T, callback: LifecycleEvents<E>[T]): void;
|
|
52
|
+
|
|
53
|
+
off<T extends keyof LifecycleEvents<E>>(event: T, callback: LifecycleEvents<E>[T]): void;
|
|
54
|
+
|
|
55
|
+
state: AnnotatorState<I>;
|
|
56
|
+
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface AnnotatorState<A extends Annotation> {
|
|
60
|
+
|
|
61
|
+
store: Store<A>;
|
|
62
|
+
|
|
63
|
+
selection: SelectionState<A>;
|
|
64
|
+
|
|
65
|
+
hover: HoverState<A>;
|
|
66
|
+
|
|
67
|
+
viewport: ViewportState;
|
|
68
|
+
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export const createBaseAnnotator = <I extends Annotation, E extends unknown>(
|
|
72
|
+
store: Store<I>,
|
|
73
|
+
undoStack: UndoStack,
|
|
74
|
+
adapter?: FormatAdapter<I, E>
|
|
75
|
+
) => {
|
|
76
|
+
|
|
77
|
+
const addAnnotation = (annotation: E) => {
|
|
78
|
+
if (adapter) {
|
|
79
|
+
const { parsed, error } = adapter.parse(annotation);
|
|
80
|
+
if (parsed) {
|
|
81
|
+
store.addAnnotation(parsed, Origin.REMOTE);
|
|
82
|
+
} else {
|
|
83
|
+
console.error(error);
|
|
84
|
+
}
|
|
85
|
+
} else {
|
|
86
|
+
store.addAnnotation(annotation as unknown as I, Origin.REMOTE);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const clearAnnotations = () => store.clear();
|
|
91
|
+
|
|
92
|
+
const getAnnotationById = (id: string): E | undefined => {
|
|
93
|
+
const annotation = store.getAnnotation(id);
|
|
94
|
+
return (adapter && annotation) ?
|
|
95
|
+
adapter.serialize(annotation) as E : annotation as unknown as E;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const getAnnotations = () =>
|
|
99
|
+
(adapter ? store.all().map(adapter.serialize) : store.all()) as E[];
|
|
100
|
+
|
|
101
|
+
const loadAnnotations = (url: string) =>
|
|
102
|
+
fetch(url)
|
|
103
|
+
.then((response) => response.json())
|
|
104
|
+
.then((annotations) => {
|
|
105
|
+
setAnnotations(annotations);
|
|
106
|
+
return annotations;
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const removeAnnotation = (arg: E | string): E => {
|
|
110
|
+
if (typeof arg === 'string') {
|
|
111
|
+
const annotation = store.getAnnotation(arg);
|
|
112
|
+
store.deleteAnnotation(arg);
|
|
113
|
+
|
|
114
|
+
return adapter ? adapter.serialize(annotation) : annotation as unknown as E;
|
|
115
|
+
} else {
|
|
116
|
+
const annotation = adapter ? adapter.parse(arg).parsed : (arg as unknown as I);
|
|
117
|
+
store.deleteAnnotation(annotation);
|
|
118
|
+
return arg;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const setAnnotations = (annotations: E[]) => {
|
|
123
|
+
if (adapter) {
|
|
124
|
+
const { parsed, failed } = parseAll(adapter)(annotations);
|
|
125
|
+
|
|
126
|
+
if (failed.length > 0)
|
|
127
|
+
console.warn(`Discarded ${failed.length} invalid annotations`, failed);
|
|
128
|
+
|
|
129
|
+
store.bulkAddAnnotation(parsed, true, Origin.REMOTE);
|
|
130
|
+
} else {
|
|
131
|
+
store.bulkAddAnnotation(annotations as unknown as I[], true, Origin.REMOTE);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const updateAnnotation = (updated: E): E => {
|
|
136
|
+
if (adapter) {
|
|
137
|
+
const crosswalked = adapter.parse(updated).parsed;
|
|
138
|
+
const previous = adapter.serialize(store.getAnnotation(crosswalked.id));
|
|
139
|
+
store.updateAnnotation(crosswalked);
|
|
140
|
+
return previous;
|
|
141
|
+
} else {
|
|
142
|
+
const previous = store.getAnnotation((updated as unknown as I).id);
|
|
143
|
+
store.updateAnnotation(updated as unknown as I);
|
|
144
|
+
return previous as unknown as E;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
addAnnotation,
|
|
150
|
+
clearAnnotations,
|
|
151
|
+
getAnnotationById,
|
|
152
|
+
getAnnotations,
|
|
153
|
+
loadAnnotations,
|
|
154
|
+
redo: undoStack.redo,
|
|
155
|
+
removeAnnotation,
|
|
156
|
+
setAnnotations,
|
|
157
|
+
undo: undoStack.undo,
|
|
158
|
+
updateAnnotation
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
}
|
|
@@ -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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
3
|
-
|
|
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
|
+
}
|