@annotorious/core 3.0.0-rc.3 → 3.0.0-rc.31
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/dist/annotorious-core.es.js +799 -0
- package/dist/annotorious-core.es.js.map +1 -0
- package/{src/index.ts → dist/index.d.ts} +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/lifecycle/Lifecycle.d.ts +11 -0
- package/dist/lifecycle/Lifecycle.d.ts.map +1 -0
- package/dist/lifecycle/LifecycleEvents.d.ts +13 -0
- package/dist/lifecycle/LifecycleEvents.d.ts.map +1 -0
- package/dist/lifecycle/index.d.ts +3 -0
- package/dist/lifecycle/index.d.ts.map +1 -0
- package/dist/model/Annotation.d.ts +34 -0
- package/dist/model/Annotation.d.ts.map +1 -0
- package/dist/model/AnnotationState.d.ts +8 -0
- package/dist/model/AnnotationState.d.ts.map +1 -0
- package/dist/model/Annotator.d.ts +67 -0
- package/dist/model/Annotator.d.ts.map +1 -0
- package/dist/model/DrawingStyle.d.ts +17 -0
- package/dist/model/DrawingStyle.d.ts.map +1 -0
- package/dist/model/Filter.d.ts +4 -0
- package/dist/model/Filter.d.ts.map +1 -0
- package/dist/model/FormatAdapter.d.ts +16 -0
- package/dist/model/FormatAdapter.d.ts.map +1 -0
- package/dist/model/User.d.ts +11 -0
- package/dist/model/User.d.ts.map +1 -0
- package/dist/model/W3CAnnotation.d.ts +43 -0
- package/dist/model/W3CAnnotation.d.ts.map +1 -0
- package/{src/model/index.ts → dist/model/index.d.ts} +3 -1
- package/dist/model/index.d.ts.map +1 -0
- package/dist/presence/Appearance.d.ts +6 -0
- package/dist/presence/Appearance.d.ts.map +1 -0
- package/dist/presence/AppearanceProvider.d.ts +17 -0
- package/dist/presence/AppearanceProvider.d.ts.map +1 -0
- package/dist/presence/ColorPalette.d.ts +3 -0
- package/dist/presence/ColorPalette.d.ts.map +1 -0
- package/dist/presence/PresenceEvents.d.ts +7 -0
- package/dist/presence/PresenceEvents.d.ts.map +1 -0
- package/dist/presence/PresenceProvider.d.ts +6 -0
- package/dist/presence/PresenceProvider.d.ts.map +1 -0
- package/dist/presence/PresenceState.d.ts +19 -0
- package/dist/presence/PresenceState.d.ts.map +1 -0
- package/dist/presence/PresentUser.d.ts +8 -0
- package/dist/presence/PresentUser.d.ts.map +1 -0
- package/{src/presence/index.ts → dist/presence/index.d.ts} +2 -1
- package/dist/presence/index.d.ts.map +1 -0
- package/dist/state/Hover.d.ts +10 -0
- package/dist/state/Hover.d.ts.map +1 -0
- package/dist/state/Selection.d.ts +34 -0
- package/dist/state/Selection.d.ts.map +1 -0
- package/dist/state/Store.d.ts +35 -0
- package/dist/state/Store.d.ts.map +1 -0
- package/dist/state/StoreObserver.d.ts +58 -0
- package/dist/state/StoreObserver.d.ts.map +1 -0
- package/dist/state/SvelteStore.d.ts +23 -0
- package/dist/state/SvelteStore.d.ts.map +1 -0
- package/dist/state/UndoStack.d.ts +19 -0
- package/dist/state/UndoStack.d.ts.map +1 -0
- package/dist/state/Viewport.d.ts +6 -0
- package/dist/state/Viewport.d.ts.map +1 -0
- package/{src/state/index.ts → dist/state/index.d.ts} +2 -1
- package/dist/state/index.d.ts.map +1 -0
- package/dist/utils/annotationUtils.d.ts +12 -0
- package/dist/utils/annotationUtils.d.ts.map +1 -0
- package/dist/utils/diffAnnotations.d.ts +5 -0
- package/dist/utils/diffAnnotations.d.ts.map +1 -0
- package/{src/utils/index.ts → dist/utils/index.d.ts} +1 -1
- package/dist/utils/index.d.ts.map +1 -0
- package/package.json +20 -17
- package/src/lifecycle/Lifecycle.ts +0 -197
- package/src/lifecycle/LifecycleEvents.ts +0 -21
- package/src/lifecycle/index.ts +0 -2
- package/src/model/Annotation.ts +0 -73
- package/src/model/Annotator.ts +0 -161
- package/src/model/DrawingStyle.ts +0 -19
- package/src/model/Filter.ts +0 -3
- package/src/model/FormatAdapter.ts +0 -36
- package/src/model/User.ts +0 -19
- package/src/model/W3CAnnotation.ts +0 -118
- package/src/presence/Appearance.ts +0 -9
- package/src/presence/AppearanceProvider.ts +0 -53
- package/src/presence/ColorPalette.ts +0 -14
- package/src/presence/PresenceEvents.ts +0 -9
- package/src/presence/PresenceProvider.ts +0 -7
- package/src/presence/PresenceState.ts +0 -145
- package/src/presence/PresentUser.ts +0 -10
- package/src/state/Hover.ts +0 -34
- package/src/state/Selection.ts +0 -113
- package/src/state/Store.ts +0 -344
- package/src/state/StoreObserver.ts +0 -194
- package/src/state/SvelteStore.ts +0 -54
- package/src/state/UndoStack.ts +0 -112
- package/src/state/Viewport.ts +0 -14
- package/src/utils/annotationUtils.ts +0 -33
- package/src/utils/diffAnnotations.ts +0 -39
- package/src/vite-env.d.ts +0 -1
|
@@ -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
|
-
}
|
package/src/lifecycle/index.ts
DELETED
package/src/model/Annotation.ts
DELETED
|
@@ -1,73 +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?: 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';
|
package/src/model/Annotator.ts
DELETED
|
@@ -1,161 +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 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,19 +0,0 @@
|
|
|
1
|
-
type RGB = `rgb(${number}, ${number}, ${number})`;
|
|
2
|
-
|
|
3
|
-
type RGBA = `rgba(${number}, ${number}, ${number}, ${number})`;
|
|
4
|
-
|
|
5
|
-
type HEX = `#${string}`;
|
|
6
|
-
|
|
7
|
-
export type Color = RGB | RGBA | HEX;
|
|
8
|
-
|
|
9
|
-
export interface DrawingStyle {
|
|
10
|
-
|
|
11
|
-
fill?: Color;
|
|
12
|
-
|
|
13
|
-
fillOpacity?: number;
|
|
14
|
-
|
|
15
|
-
stroke?: Color;
|
|
16
|
-
|
|
17
|
-
strokeOpacity?: number;
|
|
18
|
-
|
|
19
|
-
}
|
package/src/model/Filter.ts
DELETED
|
@@ -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
|
-
}
|
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import type { AnnotationBody } from './Annotation';
|
|
2
|
-
|
|
3
|
-
export interface W3CAnnotation {
|
|
4
|
-
|
|
5
|
-
'@context': 'http://www.w3.org/ns/anno.jsonld';
|
|
6
|
-
|
|
7
|
-
type: 'Annotation';
|
|
8
|
-
|
|
9
|
-
id: string;
|
|
10
|
-
|
|
11
|
-
body: W3CAnnotationBody | W3CAnnotationBody[]
|
|
12
|
-
|
|
13
|
-
target: W3CAnnotationTarget | W3CAnnotationTarget[];
|
|
14
|
-
|
|
15
|
-
[key: string]: any;
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface W3CAnnotationBody {
|
|
20
|
-
|
|
21
|
-
id?: string;
|
|
22
|
-
|
|
23
|
-
type?: string;
|
|
24
|
-
|
|
25
|
-
purpose?: string;
|
|
26
|
-
|
|
27
|
-
value?: string;
|
|
28
|
-
|
|
29
|
-
source?: string;
|
|
30
|
-
|
|
31
|
-
created?: Date;
|
|
32
|
-
|
|
33
|
-
creator?: {
|
|
34
|
-
|
|
35
|
-
type?: string;
|
|
36
|
-
|
|
37
|
-
id: string;
|
|
38
|
-
|
|
39
|
-
name?: string;
|
|
40
|
-
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export interface W3CAnnotationTarget {
|
|
46
|
-
|
|
47
|
-
source: string;
|
|
48
|
-
|
|
49
|
-
selector?: W3CSelector | W3CSelector[];
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export interface W3CSelector {
|
|
54
|
-
|
|
55
|
-
type: string;
|
|
56
|
-
|
|
57
|
-
conformsTo?: string;
|
|
58
|
-
|
|
59
|
-
value: string;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// https://stackoverflow.com/questions/6122571/simple-non-secure-hash-function-for-javascript
|
|
63
|
-
const hashCode = (obj: Object): string => {
|
|
64
|
-
const str = JSON.stringify(obj);
|
|
65
|
-
|
|
66
|
-
let hash = 0;
|
|
67
|
-
|
|
68
|
-
for (let i = 0, len = str.length; i < len; i++) {
|
|
69
|
-
let chr = str.charCodeAt(i);
|
|
70
|
-
hash = (hash << 5) - hash + chr;
|
|
71
|
-
hash |= 0; // Convert to 32bit integer
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return `${hash}`;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Helper to crosswalk the W3C annotation body to a list of core AnnotationBody objects.
|
|
79
|
-
*/
|
|
80
|
-
export const parseW3CBodies = (
|
|
81
|
-
body: W3CAnnotationBody | W3CAnnotationBody[],
|
|
82
|
-
annotationId: string
|
|
83
|
-
): AnnotationBody[] => (Array.isArray(body) ? body : [body]).map(body => {
|
|
84
|
-
|
|
85
|
-
// Exctract properties that conform to the internal model, but keep custom props
|
|
86
|
-
const { id, type, purpose, value, created, creator, ...rest } = body;
|
|
87
|
-
|
|
88
|
-
// The internal model strictly requires IDs. (Because multi-user scenarios
|
|
89
|
-
// will have problems without them.) In the W3C model, bodys *may* have IDs.
|
|
90
|
-
// We'll create ad-hoc IDs for bodies without IDs, but want to make sure that
|
|
91
|
-
// generating the ID is idempotent: the same body should always get the same ID.
|
|
92
|
-
// This will avoid unexpected results when checking for equality.
|
|
93
|
-
return {
|
|
94
|
-
id: id || `temp-${hashCode(body)}`,
|
|
95
|
-
annotation: annotationId,
|
|
96
|
-
type,
|
|
97
|
-
purpose,
|
|
98
|
-
value,
|
|
99
|
-
created,
|
|
100
|
-
creator: creator ?
|
|
101
|
-
typeof creator === 'object' ? { ...creator }: creator :
|
|
102
|
-
undefined,
|
|
103
|
-
...rest
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
/** Serialization helper to remove core-specific fields from the annotation body **/
|
|
109
|
-
export const serializeW3CBodies = (bodies: AnnotationBody[]): W3CAnnotationBody[] =>
|
|
110
|
-
bodies.map(b => {
|
|
111
|
-
const w3c = { ...b };
|
|
112
|
-
delete w3c.annotation;
|
|
113
|
-
|
|
114
|
-
if (w3c.id?.startsWith('temp-'))
|
|
115
|
-
delete w3c.id;
|
|
116
|
-
|
|
117
|
-
return w3c;
|
|
118
|
-
});
|