@pie-element/match-list 7.1.2-next.5 → 7.1.2-next.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.
@@ -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 } from "./answer.js";
3
+ import { AnswerArea as n } from "./answer-area.js";
4
+ import { ChoicesList as r } from "./choices-list.js";
5
+ import i from "react";
6
+ import a from "prop-types";
7
+ import { swap as o } from "@pie-lib/drag";
8
+ import { DndContext as s, DragOverlay as c, KeyboardCode as l, KeyboardSensor as u, PointerSensor as d, rectIntersection as f } from "@dnd-kit/core";
9
+ import { restrictToFirstScrollableAncestor as p } from "@dnd-kit/modifiers";
10
+ import m from "@pie-lib/correct-answer-toggle";
11
+ import * as h from "@pie-lib/render-ui";
12
+ import { Feedback as g, PreviewPrompt as _, color as v } from "@pie-lib/render-ui";
13
+ import { jsx as y, jsxs as b } from "react/jsx-runtime";
14
+ import { styled as x } from "@mui/material/styles";
15
+ import { findKey as S, isUndefined as C, uniqueId as w } from "@pie-element/shared-lodash";
15
16
  //#region src/delivery/main.tsx
16
- function b(e) {
17
+ function T(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 E(e, t) {
21
+ return !e || T(e) ? e : T(e.default) ? e.default : t && T(e[t]) ? e[t] : t && T(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 D = E(_, "PreviewPrompt") || E(j.PreviewPrompt, "PreviewPrompt"), O = E(g, "Feedback") || E(j.Feedback, "Feedback"), k = h, A = k.default, j = A && typeof A == "object" ? A : k, M = 250, N = [{
24
+ sensor: d,
25
+ options: { activationConstraint: { distance: 8 } }
26
+ }, {
27
+ sensor: u,
28
+ options: {
29
+ coordinateGetter: e,
30
+ keyboardCodes: {
31
+ start: [l.Space, l.Enter],
32
+ cancel: [l.Esc],
33
+ end: [l.Space, l.Enter]
34
+ }
35
+ }
36
+ }], P = x("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: v.text(),
41
+ backgroundColor: v.background()
42
+ }), F = x("div")({
43
+ width: "100%",
44
+ overflowX: "auto",
45
+ overflowY: "hidden"
46
+ }), I = x("div")({
47
+ display: "flex",
48
+ flexDirection: "column",
49
+ minWidth: "min-content"
50
+ }), L = class extends i.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: a.object.isRequired,
53
+ onSessionChange: a.func,
54
+ model: a.object.isRequired,
55
+ prompt: a.string
34
56
  };
35
57
  constructor(e) {
36
- super(e), this.instanceId = y(), this.state = {
58
+ super(e), this.instanceId = w(), 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,33 @@ 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 (C(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 s = 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 = S(n.value, (e) => e === s);
100
+ e && !a ? n.value = o(n.value, e, i) : n.value[i] = s;
101
+ } else if (e.type === "target" && c != null && c !== i) {
102
+ let e = n.value[i] != null;
103
+ if (e && !a) {
104
+ let e = n.value[i];
105
+ n.value[i] = s, n.value[c] = e;
106
+ } else e || (n.value[i] = s, delete n.value[c]);
107
+ }
108
+ r(n);
60
109
  }
61
110
  };
62
111
  onPlaceAnswer = (e) => {
@@ -64,84 +113,101 @@ var S = x(f, "PreviewPrompt") || x(E.PreviewPrompt, "PreviewPrompt"), C = x(d, "
64
113
  let { active: t, over: n } = e;
65
114
  if (!t) return;
66
115
  let r = t.data.current, i = n?.data.current;
67
- 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
- }
116
+ r && (this.placeAnswer(r, i), this.cancelSelection(), this.lastDragEndAt = Date.now());
117
+ };
118
+ isSameAnswer = (e, t) => !!e && !!t && e.type === t.type && e.id === t.id && e.promptId === t.promptId;
119
+ selectAnswer = (e) => {
120
+ this.setState({ selectedAnswer: e });
121
+ };
122
+ toggleAnswerSelection = (e) => {
123
+ this.setState((t) => ({ selectedAnswer: this.isSameAnswer(t.selectedAnswer, e) ? null : e }));
124
+ };
125
+ cancelSelection = () => {
126
+ this.setState({ selectedAnswer: null });
127
+ };
128
+ endAnyLiveKeyboardDrag = () => {
129
+ document.dispatchEvent(new KeyboardEvent("keydown", {
130
+ code: "Escape",
131
+ bubbles: !0,
132
+ cancelable: !0
133
+ }));
134
+ };
135
+ placeSelectedAnswer = (e) => {
136
+ let { selectedAnswer: t } = this.state;
137
+ t && (this.placeAnswer(t, e), this.cancelSelection(), this.endAnyLiveKeyboardDrag(), this.lastDragEndAt = Date.now());
138
+ };
139
+ isClickSoonAfterDragEnd = () => Date.now() - this.lastDragEndAt < M;
140
+ onChoiceClick = (e) => {
141
+ this.isClickSoonAfterDragEnd() || this.toggleAnswerSelection(e);
142
+ };
143
+ onPlacementClick = (e) => {
144
+ this.isClickSoonAfterDragEnd() || this.placeSelectedAnswer(e);
88
145
  };
89
146
  toggleShowCorrect = () => {
90
147
  this.setState({ showCorrectAnswer: !this.state.showCorrectAnswer });
91
148
  };
92
149
  renderDragOverlay = () => {
93
- let { draggingElement: t } = this.state;
94
- return t ? /* @__PURE__ */ m(e, {
95
- id: t.id,
96
- title: t.value,
150
+ let { draggingElement: e } = this.state;
151
+ return e ? /* @__PURE__ */ y(t, {
152
+ id: e.id,
153
+ title: e.value,
97
154
  disabled: !1,
98
155
  isDragging: !1,
99
- style: t.rect ? {
100
- width: t.rect.width,
101
- height: t.rect.height,
156
+ style: e.rect ? {
157
+ width: e.rect.width,
158
+ height: e.rect.height,
102
159
  boxSizing: "border-box"
103
160
  } : {}
104
161
  }) : null;
105
162
  };
106
163
  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, {
164
+ let { showCorrectAnswer: e } = this.state, { model: t, session: i } = this.props, { config: a, mode: o } = t, { prompt: l, language: u } = a;
165
+ return /* @__PURE__ */ b(s, {
166
+ sensors: N,
167
+ collisionDetection: f,
109
168
  onDragStart: this.onDragStart,
110
169
  onDragEnd: this.onPlaceAnswer,
111
- modifiers: [c],
112
- children: [/* @__PURE__ */ h(D, { children: [
113
- /* @__PURE__ */ m(S, {
170
+ onDragCancel: this.onDragCancel,
171
+ modifiers: [p],
172
+ 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." } },
173
+ children: [/* @__PURE__ */ b(P, { children: [
174
+ /* @__PURE__ */ y(D, {
114
175
  className: "prompt",
115
- prompt: d
176
+ prompt: l
116
177
  }),
117
- /* @__PURE__ */ m(l, {
118
- show: u === "evaluate",
178
+ /* @__PURE__ */ y(m, {
179
+ show: o === "evaluate",
119
180
  toggled: e,
120
181
  onToggle: this.toggleShowCorrect,
121
- language: f
182
+ language: u
122
183
  }),
123
- /* @__PURE__ */ m(t, {
184
+ /* @__PURE__ */ y(F, { children: /* @__PURE__ */ b(I, { children: [/* @__PURE__ */ y(n, {
124
185
  instanceId: this.instanceId,
125
- model: r,
186
+ model: t,
126
187
  session: i,
127
188
  onRemoveAnswer: (e) => this.onRemoveAnswer(e),
128
- disabled: u !== "gather",
129
- showCorrect: e
130
- }),
131
- /* @__PURE__ */ m(n, {
189
+ disabled: o !== "gather",
190
+ showCorrect: e,
191
+ selectedAnswer: this.state.selectedAnswer,
192
+ onChoiceClick: this.onChoiceClick,
193
+ onPlacementClick: this.onPlacementClick
194
+ }), /* @__PURE__ */ y(r, {
132
195
  instanceId: this.instanceId,
133
- model: r,
196
+ model: t,
134
197
  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
198
+ disabled: o !== "gather",
199
+ onRemoveAnswer: (e) => this.onRemoveAnswer(e),
200
+ selectedAnswer: this.state.selectedAnswer,
201
+ onChoiceClick: this.onChoiceClick,
202
+ onPlacementClick: this.onPlacementClick
203
+ })] }) }),
204
+ t.correctness && t.feedback && !e && /* @__PURE__ */ y(O, {
205
+ correctness: t.correctness.correctness,
206
+ feedback: t.feedback
141
207
  })
142
- ] }), /* @__PURE__ */ m(s, { children: this.renderDragOverlay() })]
208
+ ] }), /* @__PURE__ */ y(c, { children: this.renderDragOverlay() })]
143
209
  });
144
210
  }
145
211
  };
146
212
  //#endregion
147
- export { O as default };
213
+ export { L as Main, L as default };