@annotorious/core 3.0.0-rc.5 → 3.0.0-rc.7
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.map +1 -1
- package/dist/model/Annotator.d.ts +2 -2
- package/dist/model/Annotator.d.ts.map +1 -1
- package/dist/presence/AppearanceProvider.d.ts +1 -1
- package/dist/presence/AppearanceProvider.d.ts.map +1 -1
- package/dist/presence/PresenceState.d.ts +1 -1
- package/dist/presence/PresenceState.d.ts.map +1 -1
- package/dist/presence/PresentUser.d.ts +1 -1
- package/dist/presence/PresentUser.d.ts.map +1 -1
- package/dist/state/StoreObserver.d.ts +1 -1
- package/dist/state/StoreObserver.d.ts.map +1 -1
- package/dist/utils/annotationUtils.d.ts +2 -1
- package/dist/utils/annotationUtils.d.ts.map +1 -1
- package/dist/utils/diffAnnotations.d.ts +2 -2
- package/dist/utils/diffAnnotations.d.ts.map +1 -1
- package/package.json +3 -15
- package/src/index.ts +0 -5
- package/src/lifecycle/Lifecycle.ts +0 -211
- 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 -199
- 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/model/index.ts +0 -7
- 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/presence/index.ts +0 -6
- 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 -143
- package/src/state/Viewport.ts +0 -14
- package/src/state/index.ts +0 -7
- package/src/utils/annotationUtils.ts +0 -33
- package/src/utils/diffAnnotations.ts +0 -39
- package/src/utils/index.ts +0 -3
- package/src/vite-env.d.ts +0 -1
|
@@ -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
|
-
});
|
package/src/model/index.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import type { User } from '../model';
|
|
2
|
-
import type { Appearance } from './Appearance';
|
|
3
|
-
import type { PresentUser } from './PresentUser';
|
|
4
|
-
import { DEFAULT_PALETTE } from './ColorPalette';
|
|
5
|
-
|
|
6
|
-
export interface AppearanceProvider {
|
|
7
|
-
|
|
8
|
-
addUser(presenceKey: string, user: User): Appearance;
|
|
9
|
-
|
|
10
|
-
removeUser(user: PresentUser): void;
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export const defaultColorProvider = () => {
|
|
15
|
-
|
|
16
|
-
const unassignedColors = [...DEFAULT_PALETTE];
|
|
17
|
-
|
|
18
|
-
const assignRandomColor = () => {
|
|
19
|
-
const rnd = Math.floor(Math.random() * unassignedColors.length);
|
|
20
|
-
const color = unassignedColors[rnd];
|
|
21
|
-
|
|
22
|
-
unassignedColors.splice(rnd, 1);
|
|
23
|
-
|
|
24
|
-
return color;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const releaseColor = (color: string) =>
|
|
28
|
-
unassignedColors.push(color);
|
|
29
|
-
|
|
30
|
-
return { assignRandomColor, releaseColor };
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export const createDefaultAppearenceProvider = () => {
|
|
35
|
-
|
|
36
|
-
const colorProvider = defaultColorProvider();
|
|
37
|
-
|
|
38
|
-
const addUser = (presenceKey: string, user: User): Appearance => {
|
|
39
|
-
const color = colorProvider.assignRandomColor();
|
|
40
|
-
|
|
41
|
-
return {
|
|
42
|
-
label: user.name || user.id,
|
|
43
|
-
avatar: user.avatar,
|
|
44
|
-
color
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const removeUser = (user: PresentUser) =>
|
|
49
|
-
colorProvider.releaseColor(user.appearance.color);
|
|
50
|
-
|
|
51
|
-
return { addUser, removeUser }
|
|
52
|
-
|
|
53
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
// SEABORN_BRIGHT
|
|
2
|
-
export const DEFAULT_PALETTE: Palette = [
|
|
3
|
-
'#ff7c00', // orange
|
|
4
|
-
'#1ac938', // green
|
|
5
|
-
'#e8000b', // red
|
|
6
|
-
'#8b2be2', // purple
|
|
7
|
-
'#9f4800', // brown
|
|
8
|
-
'#f14cc1', // pink
|
|
9
|
-
'#ffc400', // khaki
|
|
10
|
-
'#00d7ff', // cyan
|
|
11
|
-
'#023eff' // blue
|
|
12
|
-
];
|
|
13
|
-
|
|
14
|
-
export type Palette = string[];
|
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
import { nanoid } from 'nanoid';
|
|
2
|
-
import { createNanoEvents, type Unsubscribe } from 'nanoevents';
|
|
3
|
-
import type { User } from '../model';
|
|
4
|
-
import type { PresentUser } from './PresentUser';
|
|
5
|
-
import type { PresenceEvents } from './PresenceEvents';
|
|
6
|
-
import { createDefaultAppearenceProvider } from './AppearanceProvider';
|
|
7
|
-
import type { AppearanceProvider } from './AppearanceProvider';
|
|
8
|
-
|
|
9
|
-
export interface PresenceState {
|
|
10
|
-
|
|
11
|
-
// Get users currently present to this room
|
|
12
|
-
getPresentUsers(): PresentUser[];
|
|
13
|
-
|
|
14
|
-
// Notify of a given present user's activity on the given annotations
|
|
15
|
-
notifyActivity(presenceKey: string, annotationIds: string[]): void;
|
|
16
|
-
|
|
17
|
-
// Add a listener for the given presence event
|
|
18
|
-
on<E extends keyof PresenceEvents>(event: E, callback: PresenceEvents[E]): Unsubscribe;
|
|
19
|
-
|
|
20
|
-
// Initial sync - which users are present under which keys
|
|
21
|
-
syncUsers(state: { presenceKey: string, user: User }[]): void;
|
|
22
|
-
|
|
23
|
-
// Update the selection state for the given prresent user
|
|
24
|
-
updateSelection(presenceKey: string, selection: string[] | null): void;
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const isListEqual = (listA: any[], listB: any[]) =>
|
|
29
|
-
listA.every(a => listA.includes(a)) && listB.every(b => listA.includes(b));
|
|
30
|
-
|
|
31
|
-
// This client's presence key
|
|
32
|
-
export const PRESENCE_KEY = nanoid();
|
|
33
|
-
|
|
34
|
-
export const createPresenceState = (
|
|
35
|
-
appearanceProvider: AppearanceProvider = createDefaultAppearenceProvider()
|
|
36
|
-
): PresenceState => {
|
|
37
|
-
|
|
38
|
-
const emitter = createNanoEvents<PresenceEvents>();
|
|
39
|
-
|
|
40
|
-
const presentUsers = new Map<string, PresentUser>();
|
|
41
|
-
|
|
42
|
-
const selectionStates = new Map<string, string[]>();
|
|
43
|
-
|
|
44
|
-
const addUser = (presenceKey: string, user: User) => {
|
|
45
|
-
if (presentUsers.has(presenceKey)) {
|
|
46
|
-
console.warn('Attempt to add user that is already present', presenceKey, user);
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const appearance = appearanceProvider.addUser(presenceKey, user);
|
|
51
|
-
|
|
52
|
-
presentUsers.set(presenceKey, {
|
|
53
|
-
...user,
|
|
54
|
-
presenceKey,
|
|
55
|
-
appearance
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const removeUser = (presenceKey: string) => {
|
|
60
|
-
const user = presentUsers.get(presenceKey);
|
|
61
|
-
if (!user) {
|
|
62
|
-
console.warn('Attempt to remove user that is not present', presenceKey);
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
appearanceProvider.removeUser(user);
|
|
67
|
-
|
|
68
|
-
presentUsers.delete(presenceKey);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const syncUsers = (state: { presenceKey: string, user: User }[]) => {
|
|
72
|
-
// const keys = new Set(others.map(s => s.presenceKey));
|
|
73
|
-
const keys = new Set(state.map(s => s.presenceKey));
|
|
74
|
-
|
|
75
|
-
// These users need to be added to the presentUsers list
|
|
76
|
-
// const toAdd = others.filter(({ presenceKey }) => !presentUsers.has(presenceKey));
|
|
77
|
-
const toAdd = state.filter(({ presenceKey }) => !presentUsers.has(presenceKey));
|
|
78
|
-
|
|
79
|
-
// These users need to be dropped from the list
|
|
80
|
-
const toRemove = Array.from(presentUsers.values()).filter(presentUser =>
|
|
81
|
-
!keys.has(presentUser.presenceKey));
|
|
82
|
-
|
|
83
|
-
toAdd.forEach(({ presenceKey, user }) => addUser(presenceKey, user));
|
|
84
|
-
|
|
85
|
-
toRemove.forEach(user => {
|
|
86
|
-
const { presenceKey } = user;
|
|
87
|
-
|
|
88
|
-
// If this user has a selection, fire deselect event
|
|
89
|
-
if (selectionStates.has(presenceKey))
|
|
90
|
-
emitter.emit('selectionChange', user, null);
|
|
91
|
-
|
|
92
|
-
removeUser(presenceKey)
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
if (toAdd.length > 0 || toRemove.length > 0)
|
|
96
|
-
emitter.emit('presence', getPresentUsers());
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const notifyActivity = (presenceKey: string, annotationIds: string[]) => {
|
|
100
|
-
const user = presentUsers.get(presenceKey);
|
|
101
|
-
|
|
102
|
-
if (!user) {
|
|
103
|
-
console.warn('Activity notification from user that is not present');
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const currentSelection = selectionStates.get(presenceKey);
|
|
108
|
-
|
|
109
|
-
// Was there a selection change we might have missed?
|
|
110
|
-
if (!currentSelection || !isListEqual(currentSelection, annotationIds)) {
|
|
111
|
-
selectionStates.set(presenceKey, annotationIds);
|
|
112
|
-
emitter.emit('selectionChange', user, annotationIds);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const updateSelection = (presenceKey: string, selection: string[] | null) => {
|
|
117
|
-
const from = presentUsers.get(presenceKey);
|
|
118
|
-
if (!from) {
|
|
119
|
-
console.warn('Selection change for user that is not present', presenceKey);
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (selection)
|
|
124
|
-
selectionStates.set(presenceKey, selection);
|
|
125
|
-
else
|
|
126
|
-
selectionStates.delete(presenceKey);
|
|
127
|
-
|
|
128
|
-
emitter.emit('selectionChange', from, selection);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const getPresentUsers = () =>
|
|
132
|
-
[...Array.from(presentUsers.values())];
|
|
133
|
-
|
|
134
|
-
const on = <E extends keyof PresenceEvents>(event: E, callback: PresenceEvents[E]) =>
|
|
135
|
-
emitter.on(event, callback);
|
|
136
|
-
|
|
137
|
-
return {
|
|
138
|
-
getPresentUsers,
|
|
139
|
-
notifyActivity,
|
|
140
|
-
on,
|
|
141
|
-
syncUsers,
|
|
142
|
-
updateSelection
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
}
|
package/src/presence/index.ts
DELETED
package/src/state/Hover.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { writable } from 'svelte/store';
|
|
2
|
-
import type { Annotation } from '../model';
|
|
3
|
-
import type { Store } from './Store';
|
|
4
|
-
|
|
5
|
-
export type HoverState<T extends Annotation> = ReturnType<typeof createHoverState<T>>;
|
|
6
|
-
|
|
7
|
-
export const createHoverState = <T extends Annotation>(store: Store<T>) => {
|
|
8
|
-
|
|
9
|
-
const { subscribe, set } = writable<string>(null);
|
|
10
|
-
|
|
11
|
-
let currentHover: string = null;
|
|
12
|
-
|
|
13
|
-
subscribe(updated => currentHover = updated);
|
|
14
|
-
|
|
15
|
-
// Track store delete and update events
|
|
16
|
-
store.observe(( { changes }) => {
|
|
17
|
-
if (currentHover) {
|
|
18
|
-
const isDeleted = changes.deleted.some(a => a.id === currentHover);
|
|
19
|
-
if (isDeleted)
|
|
20
|
-
set(null);
|
|
21
|
-
|
|
22
|
-
const updated = changes.updated.find(({ oldValue }) => oldValue.id === currentHover);
|
|
23
|
-
if (updated)
|
|
24
|
-
set(updated.newValue.id);
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
return {
|
|
29
|
-
get current() { return currentHover },
|
|
30
|
-
subscribe,
|
|
31
|
-
set
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
}
|
package/src/state/Selection.ts
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { writable } from 'svelte/store';
|
|
2
|
-
import type { Annotation } from '../model';
|
|
3
|
-
import type { Store } from './Store';
|
|
4
|
-
|
|
5
|
-
export type Selection = {
|
|
6
|
-
|
|
7
|
-
selected: { id: string, editable?: boolean }[],
|
|
8
|
-
|
|
9
|
-
pointerEvent?: PointerEvent;
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export type SelectionState<T extends Annotation> = ReturnType<typeof createSelectionState<T>>;
|
|
14
|
-
|
|
15
|
-
export enum PointerSelectAction {
|
|
16
|
-
|
|
17
|
-
EDIT = 'EDIT', // Make annotation target(s) editable on pointer select
|
|
18
|
-
|
|
19
|
-
SELECT = 'SELECT', // Just select, but don't make editable
|
|
20
|
-
|
|
21
|
-
NONE = 'NONE' // Click won't select - annotation is completely inert
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const EMPTY: Selection = { selected: [] };
|
|
26
|
-
|
|
27
|
-
export const createSelectionState = <T extends Annotation>(
|
|
28
|
-
store: Store<T>,
|
|
29
|
-
selectAction: PointerSelectAction | ((a: Annotation) => PointerSelectAction) = PointerSelectAction.EDIT
|
|
30
|
-
) => {
|
|
31
|
-
const { subscribe, set } = writable<Selection>(EMPTY);
|
|
32
|
-
|
|
33
|
-
let currentSelection: Selection = EMPTY;
|
|
34
|
-
|
|
35
|
-
subscribe(updated => currentSelection = updated);
|
|
36
|
-
|
|
37
|
-
const clear = () => set(EMPTY);
|
|
38
|
-
|
|
39
|
-
const isEmpty = () => currentSelection.selected?.length === 0;
|
|
40
|
-
|
|
41
|
-
const isSelected = (annotationOrId: T | string) => {
|
|
42
|
-
if (currentSelection.selected.length === 0)
|
|
43
|
-
return false;
|
|
44
|
-
|
|
45
|
-
const id = typeof annotationOrId === 'string' ? annotationOrId : annotationOrId.id;
|
|
46
|
-
return currentSelection.selected.some(i => i.id === id);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// TODO enable CTRL select
|
|
50
|
-
const clickSelect = (id: string, pointerEvent: PointerEvent) => {
|
|
51
|
-
const annotation = store.getAnnotation(id);
|
|
52
|
-
if (annotation) {
|
|
53
|
-
const action = onPointerSelect(annotation, selectAction);
|
|
54
|
-
if (action === PointerSelectAction.EDIT)
|
|
55
|
-
set({ selected: [{ id, editable: true }], pointerEvent });
|
|
56
|
-
else if (action === PointerSelectAction.SELECT)
|
|
57
|
-
set({ selected: [{ id }], pointerEvent });
|
|
58
|
-
else
|
|
59
|
-
set({ selected: [], pointerEvent });
|
|
60
|
-
} else {
|
|
61
|
-
console.warn('Invalid selection: ' + id);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const setSelected = (idOrIds: string | string[], editable: boolean = true) => {
|
|
66
|
-
const ids = Array.isArray(idOrIds) ? idOrIds : [idOrIds];
|
|
67
|
-
|
|
68
|
-
// Remove invalid
|
|
69
|
-
const annotations =
|
|
70
|
-
ids.map(id => store.getAnnotation(id)).filter(a => a);
|
|
71
|
-
|
|
72
|
-
set({ selected: annotations.map(({ id }) => ({ id, editable })) });
|
|
73
|
-
|
|
74
|
-
if (annotations.length !== ids.length)
|
|
75
|
-
console.warn('Invalid selection', idOrIds);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const removeFromSelection = (ids: string[]) => {
|
|
79
|
-
if (currentSelection.selected.length === 0)
|
|
80
|
-
return false;
|
|
81
|
-
|
|
82
|
-
const { selected } = currentSelection;
|
|
83
|
-
|
|
84
|
-
// Checks which of the given annotations are actually in the selection
|
|
85
|
-
const toRemove = selected.filter(({ id }) => ids.includes(id))
|
|
86
|
-
|
|
87
|
-
if (toRemove.length > 0)
|
|
88
|
-
set({ selected: selected.filter(({ id }) => !ids.includes(id)) });
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// Track store delete and update events
|
|
92
|
-
store.observe(({ changes }) =>
|
|
93
|
-
removeFromSelection(changes.deleted.map(a => a.id)));
|
|
94
|
-
|
|
95
|
-
return {
|
|
96
|
-
clear,
|
|
97
|
-
clickSelect,
|
|
98
|
-
get selected() { return currentSelection ? [...currentSelection.selected ] : null},
|
|
99
|
-
get pointerEvent() { return currentSelection ? currentSelection.pointerEvent : null },
|
|
100
|
-
isEmpty,
|
|
101
|
-
isSelected,
|
|
102
|
-
setSelected,
|
|
103
|
-
subscribe
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export const onPointerSelect = (
|
|
109
|
-
annotation: Annotation,
|
|
110
|
-
action?: PointerSelectAction | ((a: Annotation) => PointerSelectAction)
|
|
111
|
-
): PointerSelectAction => (typeof action === 'function') ?
|
|
112
|
-
(action(annotation) || PointerSelectAction.EDIT) :
|
|
113
|
-
(action || PointerSelectAction.EDIT);
|