@pie-element/match-list 7.1.2-next.3 → 7.1.2-next.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.
@@ -1,42 +1,65 @@
1
- import { Answer as e } from "./answer.js";
2
- import t from "./answer-area.js";
3
- import n from "./choices-list.js";
4
- import r from "react";
5
- import i from "prop-types";
6
- import { swap as a } from "@pie-lib/drag";
7
- import { DndContext as o, DragOverlay as s } from "@dnd-kit/core";
8
- import { restrictToFirstScrollableAncestor as c } from "@dnd-kit/modifiers";
9
- import l from "@pie-lib/correct-answer-toggle";
10
- import * as u from "@pie-lib/render-ui";
11
- import { Feedback as d, PreviewPrompt as f, color as p } from "@pie-lib/render-ui";
12
- import { jsx as m, jsxs as h } from "react/jsx-runtime";
13
- import { styled as g } from "@mui/material/styles";
14
- import { findKey as _, isUndefined as v, uniqueId as y } from "@pie-element/shared-lodash";
1
+ import { closestDroppableKeyboardCoordinates as e } from "./keyboard-coordinates.js";
2
+ import { Answer as t, buildDragId as n } from "./answer.js";
3
+ import { AnswerArea as r } from "./answer-area.js";
4
+ import { ChoicesList as i } from "./choices-list.js";
5
+ import a from "react";
6
+ import o from "prop-types";
7
+ import { swap as s } from "@pie-lib/drag";
8
+ import { DndContext as c, DragOverlay as l, KeyboardCode as u, KeyboardSensor as d, PointerSensor as f, rectIntersection as p } from "@dnd-kit/core";
9
+ import { restrictToFirstScrollableAncestor as m } from "@dnd-kit/modifiers";
10
+ import h from "@pie-lib/correct-answer-toggle";
11
+ import * as g from "@pie-lib/render-ui";
12
+ import { Feedback as _, PreviewPrompt as v, color as y } from "@pie-lib/render-ui";
13
+ import { jsx as b, jsxs as x } from "react/jsx-runtime";
14
+ import { styled as S } from "@mui/material/styles";
15
+ import { findKey as C, isUndefined as w, uniqueId as T } from "@pie-element/shared-lodash";
15
16
  //#region src/delivery/main.tsx
16
- function b(e) {
17
+ function E(e) {
17
18
  return typeof e == "function" || typeof e == "object" && !!e && typeof e.$$typeof == "symbol";
18
19
  }
19
- function x(e, t) {
20
- return !e || b(e) ? e : b(e.default) ? e.default : t && b(e[t]) ? e[t] : t && b(e[t]?.default) ? e[t].default : e;
20
+ function D(e, t) {
21
+ return !e || E(e) ? e : E(e.default) ? e.default : t && E(e[t]) ? e[t] : t && E(e[t]?.default) ? e[t].default : e;
21
22
  }
22
- var S = x(f, "PreviewPrompt") || x(E.PreviewPrompt, "PreviewPrompt"), C = x(d, "Feedback") || x(E.Feedback, "Feedback"), w = u, T = w.default, E = T && typeof T == "object" ? T : w, D = g("div")({
23
+ var O = D(v, "PreviewPrompt") || D(M.PreviewPrompt, "PreviewPrompt"), k = D(_, "Feedback") || D(M.Feedback, "Feedback"), A = g, j = A.default, M = j && typeof j == "object" ? j : A, N = 250, P = [{
24
+ sensor: f,
25
+ options: { activationConstraint: { distance: 8 } }
26
+ }, {
27
+ sensor: d,
28
+ options: {
29
+ coordinateGetter: e,
30
+ keyboardCodes: {
31
+ start: [u.Space, u.Enter],
32
+ cancel: [u.Esc],
33
+ end: [u.Space, u.Enter]
34
+ }
35
+ }
36
+ }], F = S("div")({
23
37
  display: "flex",
24
38
  flexDirection: "column",
25
39
  justifyContent: "center",
26
- color: p.text(),
27
- backgroundColor: p.background()
28
- }), O = class extends r.Component {
40
+ color: y.text(),
41
+ backgroundColor: y.background()
42
+ }), I = S("div")({
43
+ width: "100%",
44
+ overflowX: "auto",
45
+ overflowY: "hidden"
46
+ }), L = S("div")({
47
+ display: "flex",
48
+ flexDirection: "column",
49
+ minWidth: "min-content"
50
+ }), R = class extends a.Component {
29
51
  static propTypes = {
30
- session: i.object.isRequired,
31
- onSessionChange: i.func,
32
- model: i.object.isRequired,
33
- prompt: i.string
52
+ session: o.object.isRequired,
53
+ onSessionChange: o.func,
54
+ model: o.object.isRequired,
55
+ prompt: o.string
34
56
  };
35
57
  constructor(e) {
36
- super(e), this.instanceId = y(), this.state = {
58
+ super(e), this.instanceId = T(), this.state = {
37
59
  showCorrectAnswer: !1,
38
- draggingElement: null
39
- };
60
+ draggingElement: null,
61
+ selectedAnswer: null
62
+ }, this.lastDragEndAt = 0;
40
63
  }
41
64
  onRemoveAnswer(e) {
42
65
  let { session: t, onSessionChange: n } = this.props;
@@ -56,7 +79,30 @@ var S = x(f, "PreviewPrompt") || x(E.PreviewPrompt, "PreviewPrompt"), C = x(d, "
56
79
  this.setState({ draggingElement: {
57
80
  ...t.data.current,
58
81
  rect: e
59
- } });
82
+ } }), this.selectAnswer(t.data.current);
83
+ }
84
+ };
85
+ onDragCancel = () => {
86
+ this.setState({ draggingElement: null }), this.cancelSelection(), this.lastDragEndAt = Date.now();
87
+ };
88
+ placeAnswer = (e, t) => {
89
+ if (!e || !t) return;
90
+ let { session: n, onSessionChange: r, model: i } = this.props, { config: { duplicates: a } } = i;
91
+ if (w(n.value) && (n.value = {}), t.type === "choices-pool" && e.promptId !== void 0) {
92
+ n.value[e.promptId] = void 0, r(n);
93
+ return;
94
+ }
95
+ let o = e.id, c = e.promptId;
96
+ if (t.type === "drop-zone" && t.promptId != null) {
97
+ let i = t.promptId;
98
+ if (e.type === "choice" && i !== void 0) {
99
+ let e = C(n.value, (e) => e === o);
100
+ e && !a ? n.value = s(n.value, e, i) : n.value[i] = o;
101
+ } else if (e.type === "target" && c != null && c !== i) if (n.value[i] != null && !a) {
102
+ let e = n.value[i];
103
+ n.value[i] = o, n.value[c] = e;
104
+ } else n.value[i] = o, delete n.value[c];
105
+ r(n);
60
106
  }
61
107
  };
62
108
  onPlaceAnswer = (e) => {
@@ -65,83 +111,120 @@ var S = x(f, "PreviewPrompt") || x(E.PreviewPrompt, "PreviewPrompt"), C = x(d, "
65
111
  if (!t) return;
66
112
  let r = t.data.current, i = n?.data.current;
67
113
  if (!r) return;
68
- let { session: o, onSessionChange: s, model: c } = this.props, { config: { duplicates: l } } = c;
69
- if (v(o.value) && (o.value = {}), i.type === "choices-pool" && r.promptId !== void 0) {
70
- o.value[r.promptId] = void 0, s(o);
71
- return;
72
- }
73
- let u = r.id, d = r.promptId;
74
- if (i && i.type === "drop-zone" && i.promptId != null) {
75
- let e = i.promptId;
76
- if (r.type === "choice" && i.type === "drop-zone" && e !== void 0) {
77
- let t = _(o.value, (e) => e === u);
78
- t && !l ? o.value = a(o.value, t, e) : o.value[e] = u;
79
- } else if (r.type === "target" && d != null && d !== e) {
80
- let t = o.value[e] != null;
81
- if (t && !l) {
82
- let t = o.value[e];
83
- o.value[e] = u, o.value[d] = t;
84
- } else t || (o.value[e] = u, delete o.value[d]);
85
- }
86
- s(o);
87
- }
114
+ let a = this.getDestinationDragId(r, i);
115
+ this.placeAnswer(r, i), this.cancelSelection(), this.lastDragEndAt = Date.now(), this.focusTile(a);
116
+ };
117
+ getDestinationDragId = (e, t) => !e || !t ? null : t.type === "choices-pool" && e.promptId !== void 0 ? n({
118
+ type: "choice",
119
+ id: e.id
120
+ }) : t.type === "drop-zone" && t.promptId != null ? n({
121
+ type: "target",
122
+ promptId: t.promptId
123
+ }) : null;
124
+ focusTile = (e) => {
125
+ if (!e) return;
126
+ let t = `[data-tile-id="${this.instanceId}:${e}"]`;
127
+ requestAnimationFrame(() => {
128
+ requestAnimationFrame(() => {
129
+ document.querySelector(t)?.focus();
130
+ });
131
+ });
132
+ };
133
+ isSameAnswer = (e, t) => !!e && !!t && e.type === t.type && e.id === t.id && e.promptId === t.promptId;
134
+ selectAnswer = (e) => {
135
+ this.setState({ selectedAnswer: e });
136
+ };
137
+ toggleAnswerSelection = (e) => {
138
+ this.setState((t) => ({ selectedAnswer: this.isSameAnswer(t.selectedAnswer, e) ? null : e }));
139
+ };
140
+ cancelSelection = () => {
141
+ this.setState({ selectedAnswer: null });
142
+ };
143
+ endAnyLiveKeyboardDrag = () => {
144
+ document.dispatchEvent(new KeyboardEvent("keydown", {
145
+ code: "Escape",
146
+ bubbles: !0,
147
+ cancelable: !0
148
+ }));
149
+ };
150
+ placeSelectedAnswer = (e) => {
151
+ let { selectedAnswer: t } = this.state;
152
+ if (!t) return;
153
+ let n = this.getDestinationDragId(t, e);
154
+ this.placeAnswer(t, e), this.cancelSelection(), this.endAnyLiveKeyboardDrag(), this.lastDragEndAt = Date.now(), this.focusTile(n);
155
+ };
156
+ isClickSoonAfterDragEnd = () => Date.now() - this.lastDragEndAt < N;
157
+ onChoiceClick = (e) => {
158
+ this.isClickSoonAfterDragEnd() || this.toggleAnswerSelection(e);
159
+ };
160
+ onPlacementClick = (e) => {
161
+ this.isClickSoonAfterDragEnd() || this.placeSelectedAnswer(e);
88
162
  };
89
163
  toggleShowCorrect = () => {
90
164
  this.setState({ showCorrectAnswer: !this.state.showCorrectAnswer });
91
165
  };
92
166
  renderDragOverlay = () => {
93
- let { draggingElement: t } = this.state;
94
- return t ? /* @__PURE__ */ m(e, {
95
- id: t.id,
96
- title: t.value,
167
+ let { draggingElement: e } = this.state;
168
+ return e ? /* @__PURE__ */ b(t, {
169
+ id: e.id,
170
+ title: e.value,
97
171
  disabled: !1,
98
172
  isDragging: !1,
99
- style: t.rect ? {
100
- width: t.rect.width,
101
- height: t.rect.height,
173
+ style: e.rect ? {
174
+ width: e.rect.width,
175
+ height: e.rect.height,
102
176
  boxSizing: "border-box"
103
177
  } : {}
104
178
  }) : null;
105
179
  };
106
180
  render() {
107
- let { showCorrectAnswer: e } = this.state, { model: r, session: i } = this.props, { config: a, mode: u } = r, { prompt: d, language: f } = a;
108
- return /* @__PURE__ */ h(o, {
181
+ let { showCorrectAnswer: e } = this.state, { model: t, session: n } = this.props, { config: a, mode: o } = t, { prompt: s, language: u } = a;
182
+ return /* @__PURE__ */ x(c, {
183
+ sensors: P,
184
+ collisionDetection: p,
109
185
  onDragStart: this.onDragStart,
110
186
  onDragEnd: this.onPlaceAnswer,
111
- modifiers: [c],
112
- children: [/* @__PURE__ */ h(D, { children: [
113
- /* @__PURE__ */ m(S, {
187
+ onDragCancel: this.onDragCancel,
188
+ modifiers: [m],
189
+ accessibility: { screenReaderInstructions: { draggable: "Press Space or Enter to pick up this answer choice. Once picked up, use Tab or Shift+Tab to cycle through response areas, or use arrow keys to move it freely. Press Space or Enter to drop, or Escape to cancel. You can also click an answer choice to select it, then click a response area to place it there." } },
190
+ children: [/* @__PURE__ */ x(F, { children: [
191
+ /* @__PURE__ */ b(O, {
114
192
  className: "prompt",
115
- prompt: d
193
+ prompt: s
116
194
  }),
117
- /* @__PURE__ */ m(l, {
118
- show: u === "evaluate",
195
+ /* @__PURE__ */ b(h, {
196
+ show: o === "evaluate",
119
197
  toggled: e,
120
198
  onToggle: this.toggleShowCorrect,
121
- language: f
199
+ language: u
122
200
  }),
123
- /* @__PURE__ */ m(t, {
201
+ /* @__PURE__ */ b(I, { children: /* @__PURE__ */ x(L, { children: [/* @__PURE__ */ b(r, {
124
202
  instanceId: this.instanceId,
125
- model: r,
126
- session: i,
203
+ model: t,
204
+ session: n,
127
205
  onRemoveAnswer: (e) => this.onRemoveAnswer(e),
128
- disabled: u !== "gather",
129
- showCorrect: e
130
- }),
131
- /* @__PURE__ */ m(n, {
206
+ disabled: o !== "gather",
207
+ showCorrect: e,
208
+ selectedAnswer: this.state.selectedAnswer,
209
+ onChoiceClick: this.onChoiceClick,
210
+ onPlacementClick: this.onPlacementClick
211
+ }), /* @__PURE__ */ b(i, {
132
212
  instanceId: this.instanceId,
133
- model: r,
134
- session: i,
135
- disabled: u !== "gather",
136
- onRemoveAnswer: (e) => this.onRemoveAnswer(e)
137
- }),
138
- r.correctness && r.feedback && !e && /* @__PURE__ */ m(C, {
139
- correctness: r.correctness.correctness,
140
- feedback: r.feedback
213
+ model: t,
214
+ session: n,
215
+ disabled: o !== "gather",
216
+ onRemoveAnswer: (e) => this.onRemoveAnswer(e),
217
+ selectedAnswer: this.state.selectedAnswer,
218
+ onChoiceClick: this.onChoiceClick,
219
+ onPlacementClick: this.onPlacementClick
220
+ })] }) }),
221
+ t.correctness && t.feedback && !e && /* @__PURE__ */ b(k, {
222
+ correctness: t.correctness.correctness,
223
+ feedback: t.feedback
141
224
  })
142
- ] }), /* @__PURE__ */ m(s, { children: this.renderDragOverlay() })]
225
+ ] }), /* @__PURE__ */ b(l, { children: this.renderDragOverlay() })]
143
226
  });
144
227
  }
145
228
  };
146
229
  //#endregion
147
- export { O as default };
230
+ export { R as Main, R as default };