@pie-element/match-list 7.1.2-next.1 → 7.1.2-next.10

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,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 (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) if (n.value[i] != null && !a) {
102
+ let e = n.value[i];
103
+ n.value[i] = s, n.value[c] = e;
104
+ } else n.value[i] = s, delete n.value[c];
105
+ r(n);
60
106
  }
61
107
  };
62
108
  onPlaceAnswer = (e) => {
@@ -64,84 +110,101 @@ var S = x(f, "PreviewPrompt") || x(E.PreviewPrompt, "PreviewPrompt"), C = x(d, "
64
110
  let { active: t, over: n } = e;
65
111
  if (!t) return;
66
112
  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
- }
113
+ r && (this.placeAnswer(r, i), this.cancelSelection(), this.lastDragEndAt = Date.now());
114
+ };
115
+ isSameAnswer = (e, t) => !!e && !!t && e.type === t.type && e.id === t.id && e.promptId === t.promptId;
116
+ selectAnswer = (e) => {
117
+ this.setState({ selectedAnswer: e });
118
+ };
119
+ toggleAnswerSelection = (e) => {
120
+ this.setState((t) => ({ selectedAnswer: this.isSameAnswer(t.selectedAnswer, e) ? null : e }));
121
+ };
122
+ cancelSelection = () => {
123
+ this.setState({ selectedAnswer: null });
124
+ };
125
+ endAnyLiveKeyboardDrag = () => {
126
+ document.dispatchEvent(new KeyboardEvent("keydown", {
127
+ code: "Escape",
128
+ bubbles: !0,
129
+ cancelable: !0
130
+ }));
131
+ };
132
+ placeSelectedAnswer = (e) => {
133
+ let { selectedAnswer: t } = this.state;
134
+ t && (this.placeAnswer(t, e), this.cancelSelection(), this.endAnyLiveKeyboardDrag(), this.lastDragEndAt = Date.now());
135
+ };
136
+ isClickSoonAfterDragEnd = () => Date.now() - this.lastDragEndAt < M;
137
+ onChoiceClick = (e) => {
138
+ this.isClickSoonAfterDragEnd() || this.toggleAnswerSelection(e);
139
+ };
140
+ onPlacementClick = (e) => {
141
+ this.isClickSoonAfterDragEnd() || this.placeSelectedAnswer(e);
88
142
  };
89
143
  toggleShowCorrect = () => {
90
144
  this.setState({ showCorrectAnswer: !this.state.showCorrectAnswer });
91
145
  };
92
146
  renderDragOverlay = () => {
93
- let { draggingElement: t } = this.state;
94
- return t ? /* @__PURE__ */ m(e, {
95
- id: t.id,
96
- title: t.value,
147
+ let { draggingElement: e } = this.state;
148
+ return e ? /* @__PURE__ */ y(t, {
149
+ id: e.id,
150
+ title: e.value,
97
151
  disabled: !1,
98
152
  isDragging: !1,
99
- style: t.rect ? {
100
- width: t.rect.width,
101
- height: t.rect.height,
153
+ style: e.rect ? {
154
+ width: e.rect.width,
155
+ height: e.rect.height,
102
156
  boxSizing: "border-box"
103
157
  } : {}
104
158
  }) : null;
105
159
  };
106
160
  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, {
161
+ let { showCorrectAnswer: e } = this.state, { model: t, session: i } = this.props, { config: a, mode: o } = t, { prompt: l, language: u } = a;
162
+ return /* @__PURE__ */ b(s, {
163
+ sensors: N,
164
+ collisionDetection: f,
109
165
  onDragStart: this.onDragStart,
110
166
  onDragEnd: this.onPlaceAnswer,
111
- modifiers: [c],
112
- children: [/* @__PURE__ */ h(D, { children: [
113
- /* @__PURE__ */ m(S, {
167
+ onDragCancel: this.onDragCancel,
168
+ modifiers: [p],
169
+ 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." } },
170
+ children: [/* @__PURE__ */ b(P, { children: [
171
+ /* @__PURE__ */ y(D, {
114
172
  className: "prompt",
115
- prompt: d
173
+ prompt: l
116
174
  }),
117
- /* @__PURE__ */ m(l, {
118
- show: u === "evaluate",
175
+ /* @__PURE__ */ y(m, {
176
+ show: o === "evaluate",
119
177
  toggled: e,
120
178
  onToggle: this.toggleShowCorrect,
121
- language: f
179
+ language: u
122
180
  }),
123
- /* @__PURE__ */ m(t, {
181
+ /* @__PURE__ */ y(F, { children: /* @__PURE__ */ b(I, { children: [/* @__PURE__ */ y(n, {
124
182
  instanceId: this.instanceId,
125
- model: r,
183
+ model: t,
126
184
  session: i,
127
185
  onRemoveAnswer: (e) => this.onRemoveAnswer(e),
128
- disabled: u !== "gather",
129
- showCorrect: e
130
- }),
131
- /* @__PURE__ */ m(n, {
186
+ disabled: o !== "gather",
187
+ showCorrect: e,
188
+ selectedAnswer: this.state.selectedAnswer,
189
+ onChoiceClick: this.onChoiceClick,
190
+ onPlacementClick: this.onPlacementClick
191
+ }), /* @__PURE__ */ y(r, {
132
192
  instanceId: this.instanceId,
133
- model: r,
193
+ model: t,
134
194
  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
195
+ disabled: o !== "gather",
196
+ onRemoveAnswer: (e) => this.onRemoveAnswer(e),
197
+ selectedAnswer: this.state.selectedAnswer,
198
+ onChoiceClick: this.onChoiceClick,
199
+ onPlacementClick: this.onPlacementClick
200
+ })] }) }),
201
+ t.correctness && t.feedback && !e && /* @__PURE__ */ y(O, {
202
+ correctness: t.correctness.correctness,
203
+ feedback: t.feedback
141
204
  })
142
- ] }), /* @__PURE__ */ m(s, { children: this.renderDragOverlay() })]
205
+ ] }), /* @__PURE__ */ y(c, { children: this.renderDragOverlay() })]
143
206
  });
144
207
  }
145
208
  };
146
209
  //#endregion
147
- export { O as default };
210
+ export { L as Main, L as default };