@jswork/react-questkit 1.0.4 → 1.0.5

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/index.cjs CHANGED
@@ -2614,11 +2614,12 @@ function LinkMatchFeedback({
2614
2614
  function pairKey(left, right) {
2615
2615
  return `${left}-${right}`;
2616
2616
  }
2617
- function inferKind(items) {
2618
- const first = items[0];
2619
- if (!first) return "text";
2620
- if (first.image || first.art) return "image";
2621
- if (!first.text && first.audio) return "audio";
2617
+ function nodeKind(node, legacyKind) {
2618
+ if (legacyKind) return legacyKind;
2619
+ if (!node) return "text";
2620
+ if (node.image || node.art) return "image";
2621
+ if (node.text !== void 0) return "text";
2622
+ if (node.audio) return "audio";
2622
2623
  return "text";
2623
2624
  }
2624
2625
  function nodeLabel(node, kind, nodeIndex) {
@@ -2629,9 +2630,9 @@ function nodeLabel(node, kind, nodeIndex) {
2629
2630
  if (kind === "audio") return `\u64AD\u653E\u97F3\u9891 ${nodeIndex + 1}`;
2630
2631
  return typeof node.text === "string" ? node.text : void 0;
2631
2632
  }
2632
- function boardKind(leftKind, rightKind) {
2633
- if (leftKind === "image" || rightKind === "image") return "image";
2634
- if (leftKind === "audio" || rightKind === "audio") return "audio";
2633
+ function boardKindOf(kinds) {
2634
+ if (kinds.includes("image")) return "image";
2635
+ if (kinds.includes("audio")) return "audio";
2635
2636
  return "text";
2636
2637
  }
2637
2638
  function nodeOk(data, side, nodeIndex) {
@@ -2696,15 +2697,15 @@ function LinkNode({
2696
2697
  ] }) : kind === "audio" ? /* @__PURE__ */ (0, import_jsx_runtime28.jsxs)("span", { className: "rqk-link-node-audio", children: [
2697
2698
  /* @__PURE__ */ (0, import_jsx_runtime28.jsx)("span", { className: "rqk-link-node-audio-icon", "aria-hidden": true, children: /* @__PURE__ */ (0, import_jsx_runtime28.jsx)("span", { className: "rqk-link-node-audio-triangle" }) }),
2698
2699
  /* @__PURE__ */ (0, import_jsx_runtime28.jsx)("span", { className: "rqk-link-node-wave", "aria-hidden": true, children: BARS.map((height, barIndex) => /* @__PURE__ */ (0, import_jsx_runtime28.jsx)("span", { style: { height } }, barIndex)) })
2699
- ] }) : /* @__PURE__ */ (0, import_jsx_runtime28.jsxs)(import_jsx_runtime28.Fragment, { children: [
2700
- /* @__PURE__ */ (0, import_jsx_runtime28.jsx)("span", { className: "rqk-link-node-text", children: node.text }),
2701
- node.phonetic ? /* @__PURE__ */ (0, import_jsx_runtime28.jsx)("span", { className: "rqk-link-node-sub", children: node.phonetic }) : null,
2702
- node.speak || node.audio ? /* @__PURE__ */ (0, import_jsx_runtime28.jsxs)("span", { className: "rqk-link-node-sound", "aria-hidden": true, children: [
2703
- /* @__PURE__ */ (0, import_jsx_runtime28.jsx)("span", {}),
2704
- /* @__PURE__ */ (0, import_jsx_runtime28.jsx)("span", {}),
2705
- /* @__PURE__ */ (0, import_jsx_runtime28.jsx)("span", {})
2706
- ] }) : null
2700
+ ] }) : /* @__PURE__ */ (0, import_jsx_runtime28.jsxs)("span", { className: "rqk-link-node-text", children: [
2701
+ /* @__PURE__ */ (0, import_jsx_runtime28.jsx)("span", { className: "rqk-link-node-word", children: node.text }),
2702
+ node.phonetic ? /* @__PURE__ */ (0, import_jsx_runtime28.jsx)("span", { className: "rqk-link-node-sub", children: node.phonetic }) : null
2707
2703
  ] }),
2704
+ kind !== "audio" && node.audio ? /* @__PURE__ */ (0, import_jsx_runtime28.jsxs)("span", { className: "rqk-link-node-sound", "aria-hidden": true, children: [
2705
+ /* @__PURE__ */ (0, import_jsx_runtime28.jsx)("span", {}),
2706
+ /* @__PURE__ */ (0, import_jsx_runtime28.jsx)("span", {}),
2707
+ /* @__PURE__ */ (0, import_jsx_runtime28.jsx)("span", {})
2708
+ ] }) : null,
2708
2709
  /* @__PURE__ */ (0, import_jsx_runtime28.jsx)("span", { className: "rqk-link-dot", "aria-hidden": true })
2709
2710
  ]
2710
2711
  }
@@ -2715,8 +2716,8 @@ function LinkNode({
2715
2716
  var import_jsx_runtime29 = require("react/jsx-runtime");
2716
2717
  function LinkBoard({
2717
2718
  data,
2718
- leftKind,
2719
- rightKind,
2719
+ leftKinds,
2720
+ rightKinds,
2720
2721
  boardHeightKind,
2721
2722
  boardRef,
2722
2723
  leftRefs,
@@ -2751,18 +2752,16 @@ function LinkBoard({
2751
2752
  path.key
2752
2753
  )) }),
2753
2754
  ["left", "right"].map((side) => {
2754
- const kind = side === "left" ? leftKind : rightKind;
2755
+ const kinds = side === "left" ? leftKinds : rightKinds;
2755
2756
  const nodes = side === "left" ? data.left : data.right;
2756
2757
  const refs = side === "left" ? leftRefs : rightRefs;
2757
2758
  return /* @__PURE__ */ (0, import_jsx_runtime29.jsx)(
2758
2759
  "div",
2759
2760
  {
2760
- className: classNames(
2761
- "rqk-link-column",
2762
- `rqk-link-column-${side}`,
2763
- `rqk-link-column-${kind}`
2764
- ),
2761
+ className: classNames("rqk-link-column", `rqk-link-column-${side}`),
2765
2762
  children: nodes.map((node, nodeIndex) => {
2763
+ var _a;
2764
+ const kind = (_a = kinds[nodeIndex]) != null ? _a : "text";
2766
2765
  const linked = shownPairs.some(
2767
2766
  ([left, right]) => side === "left" ? left === nodeIndex : right === nodeIndex
2768
2767
  );
@@ -2799,52 +2798,46 @@ function LinkBoard({
2799
2798
 
2800
2799
  // src/questions/link-match/use-link-audio.ts
2801
2800
  var import_react15 = require("react");
2801
+ var MIN_PLAYING_MS = 500;
2802
2802
  function useLinkAudio(data) {
2803
- const config = useQuestConfig();
2804
2803
  const [playing, setPlaying] = (0, import_react15.useState)(null);
2805
2804
  const audioRef = (0, import_react15.useRef)(null);
2805
+ const playToken = (0, import_react15.useRef)(0);
2806
2806
  const speakNode = (side, nodeIndex) => {
2807
- var _a, _b, _c;
2807
+ var _a;
2808
2808
  const node = (side === "left" ? data.left : data.right)[nodeIndex];
2809
- if (!node) return;
2809
+ if (!(node == null ? void 0 : node.audio)) return;
2810
+ const token = ++playToken.current;
2810
2811
  (_a = audioRef.current) == null ? void 0 : _a.pause();
2811
2812
  audioRef.current = null;
2812
- if (node.audio) {
2813
- try {
2814
- const audio = new Audio(node.audio);
2815
- audioRef.current = audio;
2816
- const clear = () => {
2813
+ try {
2814
+ const audio = new Audio(node.audio);
2815
+ audioRef.current = audio;
2816
+ const startedAt = Date.now();
2817
+ const clear = (immediate = false) => {
2818
+ if (playToken.current !== token) return;
2819
+ const remaining = immediate ? 0 : Math.max(0, MIN_PLAYING_MS - (Date.now() - startedAt));
2820
+ window.setTimeout(() => {
2821
+ if (playToken.current !== token) return;
2817
2822
  if (audioRef.current === audio) audioRef.current = null;
2818
2823
  setPlaying(
2819
2824
  (current) => (current == null ? void 0 : current.side) === side && current.index === nodeIndex ? null : current
2820
2825
  );
2821
- };
2822
- audio.onended = clear;
2823
- audio.onerror = clear;
2824
- setPlaying({ side, index: nodeIndex });
2825
- const played = audio.play();
2826
- if (played && typeof played.catch === "function") void played.catch(clear);
2827
- } catch (e) {
2828
- setPlaying(null);
2829
- }
2830
- return;
2831
- }
2832
- if (node.speak) {
2833
- const text = (_b = node.spokenText) != null ? _b : typeof node.text === "string" ? node.text : "";
2834
- if (!text) return;
2826
+ }, remaining);
2827
+ };
2828
+ audio.onended = () => clear();
2829
+ audio.onerror = () => clear(true);
2835
2830
  setPlaying({ side, index: nodeIndex });
2836
- try {
2837
- void Promise.resolve(((_c = config.speakText) != null ? _c : speakWithSynthesis)(text)).finally(
2838
- () => setPlaying(null)
2839
- );
2840
- } catch (e) {
2841
- setPlaying(null);
2842
- }
2831
+ const played = audio.play();
2832
+ if (played && typeof played.catch === "function") void played.catch(() => clear(true));
2833
+ } catch (e) {
2834
+ if (playToken.current === token) setPlaying(null);
2843
2835
  }
2844
2836
  };
2845
2837
  (0, import_react15.useEffect)(
2846
2838
  () => () => {
2847
2839
  var _a;
2840
+ playToken.current += 1;
2848
2841
  (_a = audioRef.current) == null ? void 0 : _a.pause();
2849
2842
  audioRef.current = null;
2850
2843
  },
@@ -2928,12 +2921,12 @@ function useLinkPairs({
2928
2921
  onSubmit,
2929
2922
  sound,
2930
2923
  demoAttempt,
2931
- leftKind,
2932
- rightKind,
2933
2924
  speakNode
2934
2925
  }) {
2935
2926
  var _a;
2936
2927
  const readonly = isReadonly(mode);
2928
+ const totalPairs = Math.min(data.left.length, data.right.length);
2929
+ const hasAnswer = data.pairs.length > 0;
2937
2930
  const expected = (0, import_react17.useMemo)(
2938
2931
  () => new Set(data.pairs.map(([left, right]) => pairKey(left, right))),
2939
2932
  [data.pairs]
@@ -2941,19 +2934,17 @@ function useLinkPairs({
2941
2934
  const [pairs, setPairs] = (0, import_react17.useState)([]);
2942
2935
  const [selection, setSelection] = (0, import_react17.useState)(null);
2943
2936
  const judge = useJudge({
2944
- evaluate: () => pairs.length === data.pairs.length && pairs.every(([left, right]) => expected.has(pairKey(left, right))),
2937
+ evaluate: () => pairs.length === totalPairs && (!hasAnswer || pairs.every(([left, right]) => expected.has(pairKey(left, right)))),
2945
2938
  onFirstWrong: () => setPairs((current) => current.filter(([left, right]) => expected.has(pairKey(left, right)))),
2946
2939
  sound,
2947
2940
  demoAttempt
2948
2941
  });
2949
2942
  const frozen = readonly || judge.phase === "correct" || judge.phase === "locked";
2950
2943
  const shownPairs = readonly ? data.pairs : pairs;
2951
- const complete = pairs.length === data.pairs.length;
2944
+ const complete = pairs.length === totalPairs;
2952
2945
  const handleNode = (side, nodeIndex) => {
2953
2946
  const node = (side === "left" ? data.left : data.right)[nodeIndex];
2954
- if (projectKind(side, leftKind, rightKind) === "audio" || (node == null ? void 0 : node.audio)) {
2955
- speakNode(side, nodeIndex);
2956
- }
2947
+ if (node == null ? void 0 : node.audio) speakNode(side, nodeIndex);
2957
2948
  if (frozen) return;
2958
2949
  if (sound !== false) questSfx.tick();
2959
2950
  const existing = pairs.find(
@@ -3004,12 +2995,10 @@ function useLinkPairs({
3004
2995
  readonly,
3005
2996
  selection,
3006
2997
  shownPairs,
3007
- pairs
2998
+ pairs,
2999
+ totalPairs
3008
3000
  };
3009
3001
  }
3010
- function projectKind(side, leftKind, rightKind) {
3011
- return side === "left" ? leftKind : rightKind;
3012
- }
3013
3002
 
3014
3003
  // src/questions/link-match/index.tsx
3015
3004
  var import_jsx_runtime30 = require("react/jsx-runtime");
@@ -3028,11 +3017,10 @@ function LinkMatch({
3028
3017
  demoAttempt,
3029
3018
  className
3030
3019
  }) {
3031
- var _a, _b;
3032
3020
  const labels = useQuestLabels();
3033
- const leftKind = (_a = data.leftKind) != null ? _a : inferKind(data.left);
3034
- const rightKind = (_b = data.rightKind) != null ? _b : inferKind(data.right);
3035
- const boardHeightKind = boardKind(leftKind, rightKind);
3021
+ const leftKinds = data.left.map((node) => nodeKind(node, data.leftKind));
3022
+ const rightKinds = data.right.map((node) => nodeKind(node, data.rightKind));
3023
+ const boardHeightKind = boardKindOf([...leftKinds, ...rightKinds]);
3036
3024
  const audio = useLinkAudio(data);
3037
3025
  const pairs = useLinkPairs({
3038
3026
  data,
@@ -3040,8 +3028,6 @@ function LinkMatch({
3040
3028
  onSubmit,
3041
3029
  sound,
3042
3030
  demoAttempt,
3043
- leftKind,
3044
- rightKind,
3045
3031
  speakNode: audio.speakNode
3046
3032
  });
3047
3033
  const geometry = useLinkGeometry({
@@ -3056,7 +3042,7 @@ function LinkMatch({
3056
3042
  labels,
3057
3043
  complete: pairs.complete,
3058
3044
  pairCount: pairs.pairs.length,
3059
- totalPairs: data.pairs.length,
3045
+ totalPairs: pairs.totalPairs,
3060
3046
  onNext,
3061
3047
  onSubmit: pairs.handleSubmit,
3062
3048
  onRetry: pairs.judge.retry
@@ -3089,8 +3075,8 @@ function LinkMatch({
3089
3075
  LinkBoard,
3090
3076
  {
3091
3077
  data,
3092
- leftKind,
3093
- rightKind,
3078
+ leftKinds,
3079
+ rightKinds,
3094
3080
  boardHeightKind,
3095
3081
  boardRef: geometry.boardRef,
3096
3082
  leftRefs: geometry.leftRefs,
@@ -7021,7 +7007,7 @@ function BlankInput2({
7021
7007
  }
7022
7008
 
7023
7009
  // src/index.tsx
7024
- var version = "1.0.4";
7010
+ var version = "1.0.5";
7025
7011
  var questkit = {
7026
7012
  version,
7027
7013
  QuestShell,