@astl/player 0.0.0-stage → 0.1.0
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/CHANGELOG.md +48 -0
- package/LICENSE +202 -0
- package/README.md +36 -2
- package/dist/a11y.d.ts +41 -0
- package/dist/a11y.d.ts.map +1 -0
- package/dist/a11y.js +101 -0
- package/dist/a11y.js.map +1 -0
- package/dist/attempt/controller.d.ts +104 -0
- package/dist/attempt/controller.d.ts.map +1 -0
- package/dist/attempt/controller.js +325 -0
- package/dist/attempt/controller.js.map +1 -0
- package/dist/checker/http.d.ts +17 -0
- package/dist/checker/http.d.ts.map +1 -0
- package/dist/checker/http.js +115 -0
- package/dist/checker/http.js.map +1 -0
- package/dist/define.d.ts +35 -0
- package/dist/define.d.ts.map +1 -0
- package/dist/define.js +84 -0
- package/dist/define.js.map +1 -0
- package/dist/element/base.d.ts +83 -0
- package/dist/element/base.d.ts.map +1 -0
- package/dist/element/base.js +239 -0
- package/dist/element/base.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/interaction/index.d.ts +12 -0
- package/dist/interaction/index.d.ts.map +1 -0
- package/dist/interaction/index.js +4 -0
- package/dist/interaction/index.js.map +1 -0
- package/dist/interaction/keys.d.ts +11 -0
- package/dist/interaction/keys.d.ts.map +1 -0
- package/dist/interaction/keys.js +13 -0
- package/dist/interaction/keys.js.map +1 -0
- package/dist/interaction/pick-and-place.d.ts +120 -0
- package/dist/interaction/pick-and-place.d.ts.map +1 -0
- package/dist/interaction/pick-and-place.js +395 -0
- package/dist/interaction/pick-and-place.js.map +1 -0
- package/dist/interaction/roving-focus.d.ts +44 -0
- package/dist/interaction/roving-focus.d.ts.map +1 -0
- package/dist/interaction/roving-focus.js +115 -0
- package/dist/interaction/roving-focus.js.map +1 -0
- package/dist/messages/en.json +53 -0
- package/dist/messages/help/en.json +14 -0
- package/dist/messages/help/pl.json +14 -0
- package/dist/messages/pl.json +53 -0
- package/dist/messages.d.ts +7 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +18 -0
- package/dist/messages.js.map +1 -0
- package/dist/player/book.d.ts +13 -0
- package/dist/player/book.d.ts.map +1 -0
- package/dist/player/book.js +65 -0
- package/dist/player/book.js.map +1 -0
- package/dist/player/help.d.ts +55 -0
- package/dist/player/help.d.ts.map +1 -0
- package/dist/player/help.js +484 -0
- package/dist/player/help.js.map +1 -0
- package/dist/player/player.d.ts +87 -0
- package/dist/player/player.d.ts.map +1 -0
- package/dist/player/player.js +1229 -0
- package/dist/player/player.js.map +1 -0
- package/dist/player/styled.d.ts +10 -0
- package/dist/player/styled.d.ts.map +1 -0
- package/dist/player/styled.js +213 -0
- package/dist/player/styled.js.map +1 -0
- package/dist/player/styles.d.ts +3 -0
- package/dist/player/styles.d.ts.map +1 -0
- package/dist/player/styles.js +355 -0
- package/dist/player/styles.js.map +1 -0
- package/dist/player/templates.d.ts +25 -0
- package/dist/player/templates.d.ts.map +1 -0
- package/dist/player/templates.js +101 -0
- package/dist/player/templates.js.map +1 -0
- package/dist/player/view.d.ts +44 -0
- package/dist/player/view.d.ts.map +1 -0
- package/dist/player/view.js +231 -0
- package/dist/player/view.js.map +1 -0
- package/dist/runtime.d.ts +45 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +48 -0
- package/dist/runtime.js.map +1 -0
- package/package.json +45 -3
- package/src/a11y.ts +134 -0
- package/src/attempt/controller.ts +381 -0
- package/src/checker/http.ts +149 -0
- package/src/define.ts +114 -0
- package/src/element/base.ts +310 -0
- package/src/index.ts +18 -0
- package/src/interaction/index.ts +11 -0
- package/src/interaction/keys.ts +16 -0
- package/src/interaction/pick-and-place.ts +464 -0
- package/src/interaction/roving-focus.ts +133 -0
- package/src/messages/en.json +53 -0
- package/src/messages/help/en.json +14 -0
- package/src/messages/help/pl.json +14 -0
- package/src/messages/pl.json +53 -0
- package/src/messages.ts +21 -0
- package/src/player/book.ts +70 -0
- package/src/player/help.ts +585 -0
- package/src/player/player.ts +1368 -0
- package/src/player/styled.ts +247 -0
- package/src/player/styles.ts +355 -0
- package/src/player/templates.ts +142 -0
- package/src/player/view.ts +294 -0
- package/src/runtime.ts +95 -0
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
import type { ElementContext } from '@astl/core';
|
|
2
|
+
import type { ReactiveController, ReactiveControllerHost } from 'lit';
|
|
3
|
+
import { type Axis, arrowDelta } from './keys.js';
|
|
4
|
+
|
|
5
|
+
/** Pointer travel (px) that turns a press into a drag; anything shorter is a tap. */
|
|
6
|
+
export const DRAG_THRESHOLD = 6;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The moments a student hears: an item is picked, a destination is chosen (arrow keys), the item is put somewhere,
|
|
10
|
+
* or it goes back where it was.
|
|
11
|
+
*/
|
|
12
|
+
export type PickMoment = 'pick' | 'target' | 'put' | 'cancel';
|
|
13
|
+
|
|
14
|
+
/** The element a PickAndPlace works for: a component element (an `AstlElement`) or anything shaped like one. */
|
|
15
|
+
export interface PickAndPlaceHost extends ReactiveControllerHost, HTMLElement {
|
|
16
|
+
readonly disabled: boolean;
|
|
17
|
+
readonly context: Pick<ElementContext, 'announce'>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** What a component tells PickAndPlace. Read on every use, so a getter can follow an attribute. */
|
|
21
|
+
export interface PickAndPlaceOptions<D> {
|
|
22
|
+
/** Where `item` can go, in the order the arrow keys step through. */
|
|
23
|
+
destinations(item: string): readonly D[];
|
|
24
|
+
/** Where `item` is now (its answer, or where it starts). */
|
|
25
|
+
current(item: string): D;
|
|
26
|
+
/** The destination under a pointer at viewport point `x`, `y` while `item` is dragged, or null. */
|
|
27
|
+
at(x: number, y: number, item: string): D | null;
|
|
28
|
+
/** Puts `item` at `to`: the answer changes (report it with `emitChange`). */
|
|
29
|
+
commit(item: string, to: D): void;
|
|
30
|
+
/**
|
|
31
|
+
* The announcement of a moment, in the component's own messages. `to` is where the item is (or would go: `target`)
|
|
32
|
+
* after the moment; `from` is where the student last knew it to be (where it was picked, or the last target).
|
|
33
|
+
*/
|
|
34
|
+
say(moment: PickMoment, item: string, to: D, from: D): string;
|
|
35
|
+
/** After an item is picked, gets a new target, is put or goes back: e.g. to move a roving tab stop along with it. */
|
|
36
|
+
changed?(item: string): void;
|
|
37
|
+
/** The arrow keys that step through the destinations. Default `both`. */
|
|
38
|
+
readonly axis?: Axis;
|
|
39
|
+
/** Past the last destination comes the first, and back. */
|
|
40
|
+
readonly wrap?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* `follow` (default): a dragged item stays where it is until it is put, `over` names the destination under the
|
|
43
|
+
* pointer and the `box` around the pressed element moves with the pointer; a pick from before the drag stays until
|
|
44
|
+
* the drag ends. Putting an item where it is changes nothing: it goes back.
|
|
45
|
+
* `live`: the item is shown where it would go (a list being reordered): a drag picks the item and its target
|
|
46
|
+
* follows the pointer, and putting it down is an answer even where it started.
|
|
47
|
+
*/
|
|
48
|
+
readonly preview?: 'follow' | 'live';
|
|
49
|
+
/** `follow`: a CSS selector for the box, around the pressed element, that moves with the pointer. */
|
|
50
|
+
readonly box?: string;
|
|
51
|
+
/**
|
|
52
|
+
* Focus leaving the host (or a press outside it) with an item picked: `cancel` (default) puts it back without a
|
|
53
|
+
* word; `drop` puts it at its target (announced) when it has one other than where it is, else puts it back.
|
|
54
|
+
*/
|
|
55
|
+
readonly leave?: 'cancel' | 'drop';
|
|
56
|
+
/** A touch drags only after it is held this long (ms) without moving; a touch that moves sooner scrolls the page. */
|
|
57
|
+
readonly hold?: number;
|
|
58
|
+
/** Touches drag only from this part of an item (CSS selector); elsewhere they scroll. Mouse and pen drag anywhere. */
|
|
59
|
+
readonly handle?: string | null;
|
|
60
|
+
/**
|
|
61
|
+
* Elements outside the host that take part, such as destinations elsewhere in the page (the gaps of a word bank):
|
|
62
|
+
* focus moving to them and presses on them keep a pick. Items may sit there too (a gap holding a word).
|
|
63
|
+
*/
|
|
64
|
+
parts?(): Iterable<Element>;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** A pointer pressed on an item: a tap, or a drag once it travels DRAG_THRESHOLD px (a touch: once it is armed). */
|
|
68
|
+
interface Press {
|
|
69
|
+
readonly item: string;
|
|
70
|
+
readonly pointer: number;
|
|
71
|
+
readonly x: number;
|
|
72
|
+
readonly y: number;
|
|
73
|
+
readonly box: HTMLElement | null;
|
|
74
|
+
armed: boolean;
|
|
75
|
+
dragging: boolean;
|
|
76
|
+
timer?: ReturnType<typeof setTimeout>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Pick and place, with every way in (WCAG 2.2: 2.1.1 keyboard, 2.5.7 dragging movements):
|
|
81
|
+
* - keyboard: Space or Enter on an item picks it (a native button click), the arrow keys choose a destination (Home
|
|
82
|
+
* and End: the first and the last), Space or Enter on it again puts it there, Escape puts it back;
|
|
83
|
+
* - one pointer, no dragging: tap an item, then a destination (`put`) or another item (with a `place`: put there;
|
|
84
|
+
* without: pick that one instead);
|
|
85
|
+
* - a drag once the pointer travels DRAG_THRESHOLD px, with pointer capture; the click after a drag is not a tap;
|
|
86
|
+
* a touch drags after a long press (`hold`) or from a handle (`handle`), so other touches scroll the page.
|
|
87
|
+
* Escape and a press outside the host work wherever the focus is; held keys do not repeat a pick; Alt, Ctrl and Meta
|
|
88
|
+
* shortcuts stay with the browser; a disabled host forgets any pick. Announcements go through `context.announce`.
|
|
89
|
+
*
|
|
90
|
+
* Wire each item: `@click=${(e) => pick.click(e, id)}`, `@keydown=${(e) => pick.keydown(e, id)}`,
|
|
91
|
+
* `@pointerdown=${(e) => pick.pointerdown(e, id)}` and, for touches that may scroll first (`hold`),
|
|
92
|
+
* `@touchmove=${pick.touchmove}`. A press's moves, release and menu are heard on the document, also for items outside
|
|
93
|
+
* the host.
|
|
94
|
+
*/
|
|
95
|
+
export class PickAndPlace<D> implements ReactiveController {
|
|
96
|
+
readonly #host: PickAndPlaceHost;
|
|
97
|
+
readonly #options: PickAndPlaceOptions<D>;
|
|
98
|
+
#picked: string | null = null;
|
|
99
|
+
#target: D | null = null;
|
|
100
|
+
#said: D | null = null;
|
|
101
|
+
#dragging: string | null = null;
|
|
102
|
+
#over: D | null = null;
|
|
103
|
+
#press: Press | null = null;
|
|
104
|
+
/** A drag just ended: the click that follows its release is not a tap (keyboard clicks have detail 0). */
|
|
105
|
+
#dragged = false;
|
|
106
|
+
#listening = false;
|
|
107
|
+
|
|
108
|
+
constructor(host: PickAndPlaceHost, options: PickAndPlaceOptions<D>) {
|
|
109
|
+
this.#host = host;
|
|
110
|
+
this.#options = options;
|
|
111
|
+
host.addController(this);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The item picked with the keyboard or a tap, waiting for a destination. */
|
|
115
|
+
get picked(): string | null {
|
|
116
|
+
return this.#picked;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** The destination the arrow keys chose for the picked item; null: where it is. */
|
|
120
|
+
get target(): D | null {
|
|
121
|
+
return this.#target;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** The item being dragged. */
|
|
125
|
+
get dragging(): string | null {
|
|
126
|
+
return this.#dragging;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** The destination under the dragged item. */
|
|
130
|
+
get over(): D | null {
|
|
131
|
+
return this.#over;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* While a touch may still scroll (`hold`), the page must not scroll once it drags. Bound on items, not the host:
|
|
136
|
+
* a non-passive touch listener makes the browser wait for it before scrolling.
|
|
137
|
+
*/
|
|
138
|
+
readonly touchmove: AddEventListenerOptions & EventListenerObject = {
|
|
139
|
+
handleEvent: (event: TouchEvent): void => {
|
|
140
|
+
if (this.#press?.dragging && event.cancelable) event.preventDefault();
|
|
141
|
+
},
|
|
142
|
+
passive: false,
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
hostDisconnected(): void {
|
|
146
|
+
this.reset();
|
|
147
|
+
this.#fresh();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
hostUpdate(): void {
|
|
151
|
+
if (this.#host.disabled && (this.#picked !== null || this.#press !== null)) this.reset();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Picks `item`, announced. */
|
|
155
|
+
pick(item: string): void {
|
|
156
|
+
const at = this.#options.current(item);
|
|
157
|
+
this.#picked = item;
|
|
158
|
+
this.#target = null;
|
|
159
|
+
this.#said = at;
|
|
160
|
+
this.#say('pick', item, at, at);
|
|
161
|
+
this.#changed(item);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Puts the picked item at `to`, announced; where it already is, it goes back (unless the preview is `live`). */
|
|
165
|
+
put(to: D): void {
|
|
166
|
+
const item = this.#picked;
|
|
167
|
+
if (item === null || this.#host.disabled) return;
|
|
168
|
+
if (this.#options.preview !== 'live' && to === this.#options.current(item)) this.cancel();
|
|
169
|
+
else this.#place(item, to);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Puts the picked item back where it is, announced. */
|
|
173
|
+
cancel(): void {
|
|
174
|
+
this.#cancel(true);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Forgets any pick and drag without a word, e.g. when answers are set from outside. */
|
|
178
|
+
reset(): void {
|
|
179
|
+
this.#endPress();
|
|
180
|
+
this.#picked = null;
|
|
181
|
+
this.#target = null;
|
|
182
|
+
this.#sync();
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* A click on `item` (a tap, or Space or Enter on its button) picks it. While an item is picked: with a `place` (the
|
|
187
|
+
* clicked item stands for a destination, like a position in a list) the picked item is put there; without, a click
|
|
188
|
+
* on the picked item puts it at its target (or back), and a click on another item picks that one instead.
|
|
189
|
+
*/
|
|
190
|
+
click(event: MouseEvent, item: string, place?: D): void {
|
|
191
|
+
if (!this.tap(event) || this.#host.disabled) return;
|
|
192
|
+
const picked = this.#picked;
|
|
193
|
+
if (picked === null) this.pick(item);
|
|
194
|
+
else if (place !== undefined) this.put(place);
|
|
195
|
+
else if (picked !== item) this.pick(item);
|
|
196
|
+
else this.put(this.#target ?? this.#options.current(item));
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Whether a click is a tap (or Space or Enter on a button), not the click a drag's release brings, which it uses up:
|
|
201
|
+
* for clicks that do more than `click`.
|
|
202
|
+
*/
|
|
203
|
+
tap(event: MouseEvent): boolean {
|
|
204
|
+
if (!this.#dragged || event.detail === 0) return true;
|
|
205
|
+
this.#fresh();
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** Arrow keys (and Home, End) on the picked `item` choose its target; returns true when the key was used. */
|
|
210
|
+
keydown(event: KeyboardEvent, item: string): boolean {
|
|
211
|
+
if (this.#host.disabled) return false;
|
|
212
|
+
// A held key repeats clicks: it would pick and put items one after another.
|
|
213
|
+
if (event.repeat && (event.key === 'Enter' || event.key === ' ')) {
|
|
214
|
+
event.preventDefault();
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
if (event.altKey || event.ctrlKey || event.metaKey || this.#picked !== item) return false;
|
|
218
|
+
const options = this.#options;
|
|
219
|
+
const list = options.destinations(item);
|
|
220
|
+
const n = list.length;
|
|
221
|
+
const at = Math.max(0, list.indexOf(this.#target ?? options.current(item)));
|
|
222
|
+
const delta = arrowDelta(event, options.axis ?? 'both', this.#host);
|
|
223
|
+
let next: number;
|
|
224
|
+
if (delta !== 0) next = options.wrap ? (at + delta + n) % n : Math.min(Math.max(at + delta, 0), n - 1);
|
|
225
|
+
else if (event.key === 'Home') next = 0;
|
|
226
|
+
else if (event.key === 'End') next = n - 1;
|
|
227
|
+
else return false;
|
|
228
|
+
event.preventDefault();
|
|
229
|
+
const to = list[next];
|
|
230
|
+
if (next !== at && to !== undefined) {
|
|
231
|
+
const from = this.#said ?? to;
|
|
232
|
+
this.#target = to;
|
|
233
|
+
this.#said = to;
|
|
234
|
+
this.#say('target', item, to, from);
|
|
235
|
+
this.#changed(item);
|
|
236
|
+
}
|
|
237
|
+
return true;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** A press on `item` with the primary button, a finger or a pen: a tap or the start of a drag. */
|
|
241
|
+
pointerdown(event: PointerEvent, item: string): void {
|
|
242
|
+
// A press whose release never came must not leave an item hanging.
|
|
243
|
+
this.#endPress();
|
|
244
|
+
this.#fresh();
|
|
245
|
+
const options = this.#options;
|
|
246
|
+
const touch = event.pointerType === 'touch';
|
|
247
|
+
const handle = options.handle;
|
|
248
|
+
if (this.#host.disabled || !event.isPrimary || event.button !== 0) return;
|
|
249
|
+
if (
|
|
250
|
+
touch &&
|
|
251
|
+
handle &&
|
|
252
|
+
!event.composedPath().some((node) => node instanceof Element && node.matches(handle))
|
|
253
|
+
)
|
|
254
|
+
return;
|
|
255
|
+
const target = event.currentTarget as HTMLElement;
|
|
256
|
+
const press: Press = {
|
|
257
|
+
item,
|
|
258
|
+
pointer: event.pointerId,
|
|
259
|
+
x: event.clientX,
|
|
260
|
+
y: event.clientY,
|
|
261
|
+
box: options.box ? target.closest<HTMLElement>(options.box) : null,
|
|
262
|
+
armed: !touch || !options.hold,
|
|
263
|
+
dragging: false,
|
|
264
|
+
};
|
|
265
|
+
this.#press = press;
|
|
266
|
+
this.#hear('addEventListener');
|
|
267
|
+
if (!press.armed) {
|
|
268
|
+
press.timer = setTimeout(() => {
|
|
269
|
+
if (this.#press === press && !this.#host.disabled) this.#start(press);
|
|
270
|
+
}, options.hold);
|
|
271
|
+
} else if (!touch) {
|
|
272
|
+
// Moves and the release outside the item still reach it (touches are captured anyway).
|
|
273
|
+
try {
|
|
274
|
+
target.setPointerCapture(event.pointerId);
|
|
275
|
+
} catch {
|
|
276
|
+
// A synthetic pointer (not active in the browser) cannot be captured; its events reach the document anyway.
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
readonly #move = (event: PointerEvent): void => {
|
|
282
|
+
const press = this.#press;
|
|
283
|
+
if (press === null || event.pointerId !== press.pointer) return;
|
|
284
|
+
if ((event.buttons & 1) === 0) {
|
|
285
|
+
this.#lost(event);
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
const dx = event.clientX - press.x;
|
|
289
|
+
const dy = event.clientY - press.y;
|
|
290
|
+
const far = Math.hypot(dx, dy) >= DRAG_THRESHOLD;
|
|
291
|
+
if (!press.armed) {
|
|
292
|
+
// A touch that moves before the long press scrolls the page: let it go.
|
|
293
|
+
if (far) this.#endPress();
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
if (!press.dragging) {
|
|
297
|
+
if (!far) return;
|
|
298
|
+
this.#start(press);
|
|
299
|
+
}
|
|
300
|
+
event.preventDefault();
|
|
301
|
+
if (press.box !== null) press.box.style.translate = `${dx}px ${dy}px`;
|
|
302
|
+
const over = this.#options.at(event.clientX, event.clientY, press.item);
|
|
303
|
+
if (over !== this.#over) {
|
|
304
|
+
this.#over = over;
|
|
305
|
+
this.#host.requestUpdate();
|
|
306
|
+
}
|
|
307
|
+
if (this.#options.preview === 'live' && over !== null && over !== this.#target) {
|
|
308
|
+
this.#target = over;
|
|
309
|
+
this.#changed(press.item);
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
readonly #up = (event: PointerEvent): void => {
|
|
314
|
+
const press = this.#press;
|
|
315
|
+
if (press === null || event.pointerId !== press.pointer) return;
|
|
316
|
+
const over = this.#over;
|
|
317
|
+
this.#endPress();
|
|
318
|
+
// Not dragged: a tap, and the click that follows picks or puts.
|
|
319
|
+
if (!press.dragging) return;
|
|
320
|
+
const item = press.item;
|
|
321
|
+
const at = this.#options.current(item);
|
|
322
|
+
if (this.#options.preview === 'live') this.#place(item, this.#target ?? at);
|
|
323
|
+
else if (over !== null && over !== at) this.#place(item, over);
|
|
324
|
+
// A drag is a move of its own: a pick from before it goes back.
|
|
325
|
+
else this.#cancel(false);
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
/** The pointer was cancelled, lost its capture, or its release was missed. */
|
|
329
|
+
readonly #lost = (event: PointerEvent): void => {
|
|
330
|
+
const press = this.#press;
|
|
331
|
+
if (press === null || event.pointerId !== press.pointer) return;
|
|
332
|
+
this.#endPress();
|
|
333
|
+
if (press.dragging && this.#options.preview === 'live') this.#cancel(true);
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
/** A long press must not open a menu over the item it is about to drag. */
|
|
337
|
+
readonly #menu = (event: Event): void => {
|
|
338
|
+
if (this.#press !== null) event.preventDefault();
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Focus moving to anything outside the host and its parts. A move to nothing (a tap on something that takes no
|
|
343
|
+
* focus, like a zone) is not leaving: presses outside are handled on their own.
|
|
344
|
+
*/
|
|
345
|
+
readonly #focusin = (event: FocusEvent): void => {
|
|
346
|
+
if (!this.#press?.dragging && !this.#owns(event.composedPath())) this.#leave();
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
/** The next press anywhere starts afresh: a touch drag's release brings no click that would use the flag up. */
|
|
350
|
+
readonly #fresh = (): void => {
|
|
351
|
+
this.#dragged = false;
|
|
352
|
+
document.removeEventListener('pointerdown', this.#fresh, true);
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
readonly #outside = (event: PointerEvent): void => {
|
|
356
|
+
if (!this.#owns(event.composedPath())) this.#leave();
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
/** Escape cancels a pick or a drag wherever the focus is (a destination button, or nowhere after a tap). */
|
|
360
|
+
readonly #escape = (event: KeyboardEvent): void => {
|
|
361
|
+
if (event.key !== 'Escape') return;
|
|
362
|
+
event.preventDefault();
|
|
363
|
+
event.stopPropagation();
|
|
364
|
+
this.#endPress();
|
|
365
|
+
this.#cancel(true);
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
#start(press: Press): void {
|
|
369
|
+
press.armed = true;
|
|
370
|
+
press.dragging = true;
|
|
371
|
+
this.#dragging = press.item;
|
|
372
|
+
if (this.#options.preview === 'live' && this.#picked !== press.item) {
|
|
373
|
+
this.#cancel(false);
|
|
374
|
+
this.#picked = press.item;
|
|
375
|
+
this.#target = null;
|
|
376
|
+
this.#said = this.#options.current(press.item);
|
|
377
|
+
}
|
|
378
|
+
this.#changed(press.item);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
#place(item: string, to: D): void {
|
|
382
|
+
const from = (item === this.#picked ? this.#said : null) ?? this.#options.current(item);
|
|
383
|
+
this.#picked = null;
|
|
384
|
+
this.#target = null;
|
|
385
|
+
this.#options.commit(item, to);
|
|
386
|
+
this.#say('put', item, to, from);
|
|
387
|
+
this.#changed(item);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
#cancel(announce: boolean): void {
|
|
391
|
+
const item = this.#picked;
|
|
392
|
+
if (item === null) return;
|
|
393
|
+
const at = this.#options.current(item);
|
|
394
|
+
const from = this.#said ?? at;
|
|
395
|
+
this.#picked = null;
|
|
396
|
+
this.#target = null;
|
|
397
|
+
if (announce) this.#say('cancel', item, at, from);
|
|
398
|
+
this.#changed(item);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
#leave(): void {
|
|
402
|
+
const item = this.#picked;
|
|
403
|
+
const to = this.#target;
|
|
404
|
+
if (item === null) return;
|
|
405
|
+
if (this.#options.leave === 'drop' && to !== null && to !== this.#options.current(item))
|
|
406
|
+
this.#place(item, to);
|
|
407
|
+
else this.#cancel(false);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
#endPress(): void {
|
|
411
|
+
const press = this.#press;
|
|
412
|
+
if (press === null) return;
|
|
413
|
+
clearTimeout(press.timer);
|
|
414
|
+
press.box?.style.removeProperty('translate');
|
|
415
|
+
this.#hear('removeEventListener');
|
|
416
|
+
if (press.dragging) {
|
|
417
|
+
this.#dragged = true;
|
|
418
|
+
document.addEventListener('pointerdown', this.#fresh, true);
|
|
419
|
+
}
|
|
420
|
+
this.#press = null;
|
|
421
|
+
this.#dragging = null;
|
|
422
|
+
this.#over = null;
|
|
423
|
+
this.#sync();
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
#say(moment: PickMoment, item: string, to: D, from: D): void {
|
|
427
|
+
this.#host.context.announce(this.#options.say(moment, item, to, from));
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
#changed(item: string): void {
|
|
431
|
+
this.#options.changed?.(item);
|
|
432
|
+
this.#sync();
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** Requests a render; Escape, presses and focus outside are heard on the document only while something is picked. */
|
|
436
|
+
#sync(): void {
|
|
437
|
+
this.#listen();
|
|
438
|
+
this.#host.requestUpdate();
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
#listen(): void {
|
|
442
|
+
const on = this.#host.isConnected && (this.#picked !== null || this.#dragging !== null);
|
|
443
|
+
if (on === this.#listening) return;
|
|
444
|
+
this.#listening = on;
|
|
445
|
+
const method = on ? 'addEventListener' : 'removeEventListener';
|
|
446
|
+
document[method]('pointerdown', this.#outside as EventListener, true);
|
|
447
|
+
document[method]('keydown', this.#escape as EventListener, true);
|
|
448
|
+
document[method]('focusin', this.#focusin as EventListener, true);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/** The moves, release and menu of a press, heard on the document while it lasts (the item may be anywhere). */
|
|
452
|
+
#hear(method: 'addEventListener' | 'removeEventListener'): void {
|
|
453
|
+
document[method]('pointermove', this.#move as EventListener);
|
|
454
|
+
document[method]('pointerup', this.#up as EventListener);
|
|
455
|
+
document[method]('pointercancel', this.#lost as EventListener);
|
|
456
|
+
document[method]('lostpointercapture', this.#lost as EventListener);
|
|
457
|
+
document[method]('contextmenu', this.#menu);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/** Whether an event's composed path runs through the host or one of its parts. */
|
|
461
|
+
#owns(path: readonly EventTarget[]): boolean {
|
|
462
|
+
return [this.#host, ...(this.#options.parts?.() ?? [])].some((el) => path.includes(el));
|
|
463
|
+
}
|
|
464
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import type { ReactiveController, ReactiveControllerHost } from 'lit';
|
|
2
|
+
import { type Axis, arrowDelta } from './keys.js';
|
|
3
|
+
|
|
4
|
+
export interface RovingFocusOptions {
|
|
5
|
+
/** How many items share the one tab stop, now. */
|
|
6
|
+
count(): number;
|
|
7
|
+
/** The rendered element of item `index` (focused after a render), or null. */
|
|
8
|
+
item(index: number): HTMLElement | null;
|
|
9
|
+
/** A list along an axis (see `Axis`), or `grid`: rows of `columns` items in reading order. */
|
|
10
|
+
readonly axis: Axis | 'grid';
|
|
11
|
+
/** Items per row of a grid. */
|
|
12
|
+
readonly columns?: number;
|
|
13
|
+
/** Past the last item comes the first (in a grid: the next row, or the top of the column), and back. */
|
|
14
|
+
readonly wrap?: boolean;
|
|
15
|
+
/** Rows that PageUp and PageDown move in a grid. Default 5. */
|
|
16
|
+
readonly page?: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Roving tabindex (WAI-ARIA APG): many items, one tab stop. The arrow keys move it (a grid: Left and Right along the
|
|
21
|
+
* row, Up and Down along the column), Home and End go to the ends (a grid: of the row; with Ctrl, of the grid),
|
|
22
|
+
* PageUp and PageDown move a few rows of a grid. Render each item with `tabindex=${roving.tabIndex(i)}`; after a
|
|
23
|
+
* render the active item gets the focus back, but only while the focus is in the host (never pulled from elsewhere).
|
|
24
|
+
* An item the focus moves to by key comes all into view (its scroll margin too), scrolled as little as that needs; one
|
|
25
|
+
* a pointer presses stays where it is.
|
|
26
|
+
*/
|
|
27
|
+
export class RovingFocus implements ReactiveController {
|
|
28
|
+
readonly #host: ReactiveControllerHost & HTMLElement;
|
|
29
|
+
readonly #options: RovingFocusOptions;
|
|
30
|
+
#active = 0;
|
|
31
|
+
/** Focus the active item after the next render: the focus was in the host when the move was asked for. */
|
|
32
|
+
#pending = false;
|
|
33
|
+
/** The last input in the host was a pointer's press, not a key. */
|
|
34
|
+
#pressed = false;
|
|
35
|
+
|
|
36
|
+
constructor(host: ReactiveControllerHost & HTMLElement, options: RovingFocusOptions) {
|
|
37
|
+
this.#host = host;
|
|
38
|
+
this.#options = options;
|
|
39
|
+
host.addController(this);
|
|
40
|
+
// Captured: known before the host's own handlers ask for a move.
|
|
41
|
+
host.addEventListener(
|
|
42
|
+
'pointerdown',
|
|
43
|
+
() => {
|
|
44
|
+
this.#pressed = true;
|
|
45
|
+
},
|
|
46
|
+
{ capture: true },
|
|
47
|
+
);
|
|
48
|
+
host.addEventListener(
|
|
49
|
+
'keydown',
|
|
50
|
+
() => {
|
|
51
|
+
this.#pressed = false;
|
|
52
|
+
},
|
|
53
|
+
{ capture: true },
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The item with the tab stop. */
|
|
58
|
+
get active(): number {
|
|
59
|
+
return this.#active;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
tabIndex(index: number): 0 | -1 {
|
|
63
|
+
return index === this.#active ? 0 : -1;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Makes item `index` the tab stop and, when the focus is in the host now, moves it there after the next render
|
|
68
|
+
* (also when the render replaces the focused element).
|
|
69
|
+
*/
|
|
70
|
+
focus(index: number): void {
|
|
71
|
+
const host = this.#host;
|
|
72
|
+
const root = host.shadowRoot;
|
|
73
|
+
this.#active = index;
|
|
74
|
+
this.#pending = root ? root.activeElement !== null : host.contains(document.activeElement);
|
|
75
|
+
host.requestUpdate();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Moves the tab stop from item `from` (default: the active one) for a navigation key and returns true (the key is
|
|
80
|
+
* used); false for other keys and for Alt and Meta shortcuts (also Ctrl, except Ctrl+Home and Ctrl+End in a grid).
|
|
81
|
+
*/
|
|
82
|
+
keydown(event: KeyboardEvent, from: number = this.#active): boolean {
|
|
83
|
+
const options = this.#options;
|
|
84
|
+
const n = options.count();
|
|
85
|
+
const key = event.key;
|
|
86
|
+
const ends = options.axis === 'grid' && (key === 'Home' || key === 'End');
|
|
87
|
+
if (n === 0 || event.altKey || event.metaKey || (event.ctrlKey && !ends)) return false;
|
|
88
|
+
let to: number | null = null;
|
|
89
|
+
if (options.axis !== 'grid') {
|
|
90
|
+
const delta = arrowDelta(event, options.axis, this.#host);
|
|
91
|
+
if (delta !== 0)
|
|
92
|
+
to = options.wrap ? (from + delta + n) % n : Math.min(Math.max(from + delta, 0), n - 1);
|
|
93
|
+
else if (key === 'Home') to = 0;
|
|
94
|
+
else if (key === 'End') to = n - 1;
|
|
95
|
+
} else {
|
|
96
|
+
const columns = Math.max(1, options.columns ?? 1);
|
|
97
|
+
const column = from % columns;
|
|
98
|
+
const start = from - column;
|
|
99
|
+
const end = Math.min(start + columns, n) - 1;
|
|
100
|
+
const bottom = column + Math.floor((n - 1 - column) / columns) * columns;
|
|
101
|
+
const across = arrowDelta(event, 'inline', this.#host);
|
|
102
|
+
const down = arrowDelta(event, 'block', this.#host);
|
|
103
|
+
const rows = (key === 'PageDown' ? 1 : key === 'PageUp' ? -1 : 0) * (options.page ?? 5);
|
|
104
|
+
if (across !== 0) {
|
|
105
|
+
to = options.wrap ? (from + across + n) % n : Math.min(Math.max(from + across, start), end);
|
|
106
|
+
} else if (down !== 0) {
|
|
107
|
+
const next = from + down * columns;
|
|
108
|
+
to = next >= 0 && next < n ? next : !options.wrap ? from : down > 0 ? column : bottom;
|
|
109
|
+
} else if (rows !== 0) to = Math.min(Math.max(from + rows * columns, column), bottom);
|
|
110
|
+
else if (key === 'Home') to = event.ctrlKey ? 0 : start;
|
|
111
|
+
else if (key === 'End') to = event.ctrlKey ? n - 1 : end;
|
|
112
|
+
}
|
|
113
|
+
if (to === null) return false;
|
|
114
|
+
event.preventDefault();
|
|
115
|
+
this.focus(to);
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
hostUpdate(): void {
|
|
120
|
+
this.#active = Math.max(0, Math.min(this.#active, this.#options.count() - 1));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
hostUpdated(): void {
|
|
124
|
+
if (!this.#pending) return;
|
|
125
|
+
this.#pending = false;
|
|
126
|
+
const target = this.#options.item(this.#active);
|
|
127
|
+
if (target === null || (this.#host.shadowRoot ?? document).activeElement === target) return;
|
|
128
|
+
target.focus();
|
|
129
|
+
// focus() centres an item it finds hidden, but one partly hidden sideways stays cut (Chromium). After a press,
|
|
130
|
+
// nothing more: scrolling would move what is under the pointer (a word search reads a drag from where letters are).
|
|
131
|
+
if (!this.#pressed) target.scrollIntoView({ block: 'nearest', inline: 'nearest' });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"loading": "Loading the exercise…",
|
|
3
|
+
"page.heading": "Page {n}",
|
|
4
|
+
"page.counter": "Page {n} of {total}",
|
|
5
|
+
"page.counter.spread": "Pages {from}-{to} of {total}",
|
|
6
|
+
"progress.label": "Pages",
|
|
7
|
+
"progress.item": "Page {n}, {state, select, done {answered} partial {partly answered} other {not answered}}",
|
|
8
|
+
"nav.previous": "Back",
|
|
9
|
+
"nav.next": "Next",
|
|
10
|
+
"action.check": "Check",
|
|
11
|
+
"action.reset": "Start over",
|
|
12
|
+
"action.submit": "Hand in",
|
|
13
|
+
"confirm.empty": "{count, plural, one {You have # empty answer.} other {You have # empty answers.}} Hand in anyway?",
|
|
14
|
+
"confirm.submit": "Hand in anyway",
|
|
15
|
+
"confirm.cancel": "Back to the answers",
|
|
16
|
+
"checks.left": "{count, plural, =0 {No checks left} one {# check left} other {# checks left}}",
|
|
17
|
+
"result.title": "Result",
|
|
18
|
+
"result.score": "{score} of {max, plural, one {# point} other {# points}}",
|
|
19
|
+
"result.count": "{verdict}: {count}",
|
|
20
|
+
"result.passed": "Passed",
|
|
21
|
+
"result.notPassed": "Not passed yet",
|
|
22
|
+
"result.submitted": "Your answers have been handed in.",
|
|
23
|
+
"result.later": "You will see your results later.",
|
|
24
|
+
"result.teacher.sent": "Your answers were sent to your teacher.",
|
|
25
|
+
"result.teacher.count": "{count, plural, =0 {No answer goes to your teacher to mark.} one {# answer goes to your teacher to mark.} other {# answers go to your teacher to mark.}}",
|
|
26
|
+
"result.teacher.unchecked": "Answers for your teacher are not checked here.",
|
|
27
|
+
"result.practice": "Practice only: nothing here is scored.",
|
|
28
|
+
"result.announce": "Result: {score}.",
|
|
29
|
+
"result.countAnnounce": "{verdict}: {count}.",
|
|
30
|
+
"verdict.correct": "Correct",
|
|
31
|
+
"verdict.partial": "Partly correct",
|
|
32
|
+
"verdict.incorrect": "Not yet",
|
|
33
|
+
"verdict.empty": "No answer",
|
|
34
|
+
"timer.label": "Time left",
|
|
35
|
+
"timer.minutes": "{minutes, plural, one {# minute left} other {# minutes left}}",
|
|
36
|
+
"timer.expired": "Time is up. Your answers are being handed in.",
|
|
37
|
+
"unsupported": "This part of the exercise does not work in this player.",
|
|
38
|
+
"error.invalid_request": "Your answers could not be sent. Try again.",
|
|
39
|
+
"error.unauthorized": "We could not confirm who you are. Sign in and try again.",
|
|
40
|
+
"error.check_not_allowed": "Checking is turned off in this exercise.",
|
|
41
|
+
"error.checks_exhausted": "You have no checks left.",
|
|
42
|
+
"error.not_found": "This exercise could not be found.",
|
|
43
|
+
"error.method_not_allowed": "The exercise server turned the request down.",
|
|
44
|
+
"error.attempt_closed": "These answers have already been handed in.",
|
|
45
|
+
"error.payload_too_large": "Your answers are too long to send.",
|
|
46
|
+
"error.internal_error": "We could not reach the exercise server. Try again in a moment.",
|
|
47
|
+
"error.no_checker": "This exercise cannot be checked here.",
|
|
48
|
+
"error.load_failed": "The exercise could not be loaded.",
|
|
49
|
+
"error.view_invalid": "The exercise file is damaged.",
|
|
50
|
+
"error.integrity_mismatch": "The exercise file did not pass verification, so it was not opened.",
|
|
51
|
+
"error.source_needs_compiler": "This exercise needs the full Astrolabe script to run.",
|
|
52
|
+
"error.source_invalid": "This exercise is not written correctly, so it cannot be shown."
|
|
53
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"help.label": "Help with this question",
|
|
3
|
+
"help.labelField": "Help with field {n}",
|
|
4
|
+
"help.labelPage": "Help with this page",
|
|
5
|
+
"help.hint": "Hint ({n}/{total})",
|
|
6
|
+
"help.hintCost": "Costs {percent}% of the points",
|
|
7
|
+
"help.hintItem": "Hint {n}",
|
|
8
|
+
"help.hintAnnounce": "Hint {n}: {text}",
|
|
9
|
+
"help.check": "this question",
|
|
10
|
+
"help.checkField": "field {n}",
|
|
11
|
+
"help.why": "Why?",
|
|
12
|
+
"error.hints_not_allowed": "Hints are not available in this exercise.",
|
|
13
|
+
"error.hints_exhausted": "There are no more hints."
|
|
14
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"help.label": "Pomoc do tego pytania",
|
|
3
|
+
"help.labelField": "Pomoc do pola {n}",
|
|
4
|
+
"help.labelPage": "Pomoc do tej strony",
|
|
5
|
+
"help.hint": "Podpowiedź ({n}/{total})",
|
|
6
|
+
"help.hintCost": "Kosztuje {percent}% punktów",
|
|
7
|
+
"help.hintItem": "Podpowiedź {n}",
|
|
8
|
+
"help.hintAnnounce": "Podpowiedź {n}: {text}",
|
|
9
|
+
"help.check": "to pytanie",
|
|
10
|
+
"help.checkField": "pole {n}",
|
|
11
|
+
"help.why": "Dlaczego?",
|
|
12
|
+
"error.hints_not_allowed": "W tym zadaniu nie ma podpowiedzi.",
|
|
13
|
+
"error.hints_exhausted": "Nie ma już więcej podpowiedzi."
|
|
14
|
+
}
|